Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty/incorrect CORS headers #1669

Merged
merged 1 commit into from
Nov 20, 2020

Conversation

ulasakdeniz
Copy link
Contributor

Hi, this PR attempts to fix #1668.

- Fix empty Access-Control-Allow-Origin
- Set CORS headers only if request Origin is existing and allowed
- Increase middleware test coverage
@codecov
Copy link

codecov bot commented Nov 6, 2020

Codecov Report

Merging #1669 into master will increase coverage by 0.27%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1669      +/-   ##
==========================================
+ Coverage   84.06%   84.33%   +0.27%     
==========================================
  Files          28       28              
  Lines        1901     1909       +8     
==========================================
+ Hits         1598     1610      +12     
+ Misses        191      189       -2     
+ Partials      112      110       -2     
Impacted Files Coverage Δ
middleware/cors.go 85.52% <100.00%> (+7.58%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ceffc10...871ed9c. Read the comment docs.

@ulasakdeniz
Copy link
Contributor Author

Hi @lammel, can you have a look at this PR? It's a very small one that solves an issue I linked in the description.

Copy link
Contributor

@lammel lammel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to see some extra tests. Looking good.

The additional checks for empty origin and allowedOrigin seem to not be breaking change.
Do you expect any breaking behaviour here?

@@ -102,6 +102,17 @@ func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
origin := req.Header.Get(echo.HeaderOrigin)
allowOrigin := ""

preflight := req.Method == http.MethodOptions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we add OPTIONS also to DefaultCORSConfig.AllowedMethods?

Copy link
Contributor Author

@ulasakdeniz ulasakdeniz Nov 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is needed. Because OPTIONS is used for preflight requests whereas AllowedMethods defines allowed methods for simple requests.

Note that other frameworks I tested (play and gin) do not provide any Access-Control-Allow-Methods headers by default.

@ulasakdeniz
Copy link
Contributor Author

Nice to see some extra tests. Looking good.

The additional checks for empty origin and allowedOrigin seem to not be breaking change.
Do you expect any breaking behaviour here?

Thank you for the review. As you said it only checks for Origin, so I don't expect any breaking change.

@ulasakdeniz
Copy link
Contributor Author

Hi @lammel, I replied your comments. Can we merge if you don't want any changes?

@lammel lammel merged commit 90bef88 into labstack:master Nov 20, 2020
@lammel
Copy link
Contributor

lammel commented Nov 20, 2020

All questions answered.
Thanks @ulasakdeniz !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CORS middleware sends empty and/or unnecessary headers
2 participants