-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Bug: CORS Support in start-api #4991
Changes from all commits
e02f4f2
3bb9360
d250973
2cadadf
0117c32
81b5f57
af3abe6
40674dd
912b2bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -640,6 +640,7 @@ def _request_handler(self, **kwargs): | |
|
||
route: Route = self._get_current_route(request) | ||
cors_headers = Cors.cors_to_headers(self.api.cors) | ||
cors_headers = self._response_cors_headers(request, cors_headers) | ||
lambda_authorizer = route.authorizer_object | ||
|
||
# payloadFormatVersion can only support 2 values: "1.0" and "2.0" | ||
|
@@ -800,6 +801,31 @@ def _get_current_route(self, flask_request): | |
|
||
return route | ||
|
||
@staticmethod | ||
def _response_cors_headers(flask_request, cors_headers): | ||
if "Access-Control-Allow-Origin" not in cors_headers: | ||
return cors_headers | ||
|
||
cors_origins = cors_headers["Access-Control-Allow-Origin"] | ||
# unset this header due to restrictive manner | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why unset this? It looks like it should be removed in the case that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @jfuss , thank you for taking your time to review this we unset and set again if a match is found (restrictive manner) if multiple domains are allowed, we only send back 1 allowed domain in our response header we do not return this header (implying a deny) if no matches is found There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hi @jfuss please also refer to my below comment: |
||
del cors_headers["Access-Control-Allow-Origin"] | ||
|
||
incoming_origin = flask_request.headers.get("Origin") | ||
# Restrictive manner: do not allow any origin by default | ||
response_allowed_origin = None | ||
if incoming_origin: | ||
if cors_origins == "*" and cors_headers.get("Access-Control-Allow-Credentials") is True: | ||
response_allowed_origin = incoming_origin | ||
else: | ||
cors_origins_arr = cors_origins.split(",") | ||
if incoming_origin in cors_origins_arr: | ||
response_allowed_origin = incoming_origin | ||
|
||
if response_allowed_origin: | ||
cors_headers["Access-Control-Allow-Origin"] = response_allowed_origin | ||
|
||
return cors_headers | ||
|
||
@staticmethod | ||
def get_request_methods_endpoints(flask_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.
Thanks for creating this PR! Did we need to restrict this behaviour to only run for HTTP APIs? From the docs and the linked issue, this seems to be something that the Lambda function deals with on for REST, but we would need to manage for HTTP.
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.
hi @lucashuy
During the implementation, I referred to MDN web docs for reference:
I also referred to the implementation of middy cors middleware
below piece of code will basically parse and return a single origin if matched:
it is explained if looking deeper into the
getOrigin()
function:Hope these help explain the reasoning behind my code.
Thanks,