-
Notifications
You must be signed in to change notification settings - Fork 61
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
Adds BFF support for CORS #710
Conversation
Signed-off-by: Alex Creasy <alex@creasy.dev>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alex great PR and I loved the explanation in the description! Just did two small suggestions that we can do in a FUP.
// TODO(ederign) deal with preflight requests | ||
w.Header().Set("Access-Control-Allow-Origin", "*") | ||
func (app *App) EnableCORS(next http.Handler) http.Handler { | ||
allowedOrigins, ok := ParseOriginList(app.config.AllowedOrigins) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a nitpick and can be done in a FUP PR.
As we are not planning to change AllowedOrigins values in runtime, we can parse them once during initialization and avoid dois this call on every request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that's a really good point actually - I'll put a follow up in today just to fix these couple
|
||
next.ServeHTTP(w, r) | ||
if !ok { | ||
return next |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a nitpick and can be done in a FUP PR. Can we log a warning (error) for invalid entries instead of silently failing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think maybe my naming isn't so good here - "not ok" in this instance means that the allowlist is empty - as such we don't need a CORS middleware since not having CORS headers is the default - I'll make this more explicit in the follow up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this is the "normal" use case (yeah, "not ok" being the normal case is weird naming!!) we'd be getting a WARN on probably 99% of deployed use cases.
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: ederign The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
@alexcreasy what do you prefer? Merge this and do a FUP? |
/lgtm |
just talked with @alexcreasy and he will send a FUP PR. |
NOTE: This PR is stacked on top of my logging PR, that will need to be merged first.
Description
Adds W3C standards compliant support for CORS for the BFF
How Has This Been Tested?
This can be tested by running the BFF in full mock mode as the headers are generated by a middleware that will run in all scenarios. The tests involve merely querying the health check endpoint with a different combination of headers.
This to note:
Scenario 1: Testing with CORS disabled
Start the server:
Run a curl against the health check endpoint:
Expected outcome: The endpoint should function as normal and there should be no CORS headers in the response.
Scenario 2: Test with a single allowed origin
Start the server:
2.1 Make a request with an allowed origin header
Expected outcome: Successful request with the following CORS specific headers:
2.2 Make a request with a disallowed origin header
Expected outcome: The request should be successful, but the only CORS header in the response should be:
If there are any other Access-Control-* headers present the test has failed!
2.3 Simulate a CORS pre-flight request for an allowed origin:
Expected outcome: A response like below - note the CORS headers.
2.4 Simulate a CORS pre-flight request for a disallowed origin
Expected outcome: A response like below - note the lack of CORS headers aside from the
Vary
headerAdditional tests:
You can repeat the above tests using an allow list of multiple origins, simply add comma separated ones to the make command e.g.
NOTE: Only a single origin is allowed in the
Access-Control-Allow-Origin
header, so the BFF will return only a single origin if theOrigin
request header matches the allow list.Also you can test that allowing all origins (wildcard) works as expected:
Merge criteria:
DCO
check)If you have UI changes