Skip to content

fix(event-handler): body to empty string in CORS preflight (ALB non-compliant) #1249

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aws_lambda_powertools/event_handler/api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ def _not_found(self, method: str) -> ResponseBuilder:
if method == "OPTIONS":
logger.debug("Pre-flight request detected. Returning CORS with null response")
headers["Access-Control-Allow-Methods"] = ",".join(sorted(self._cors_methods))
return ResponseBuilder(Response(status_code=204, content_type=None, headers=headers, body=None))
return ResponseBuilder(Response(status_code=204, content_type=None, headers=headers, body=""))

handler = self._lookup_exception_handler(NotFoundError)
if handler:
Expand Down
15 changes: 14 additions & 1 deletion tests/functional/event_handler/test_api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,19 @@ def handler(event, context):
assert "Access-Control-Allow-Origin" not in result["headers"]


def test_cors_preflight_body_is_empty_not_null():
# GIVEN CORS is configured
app = ALBResolver(cors=CORSConfig())

event = {"path": "/my/request", "httpMethod": "OPTIONS"}

# WHEN calling the event handler
result = app(event, {})

# THEN there body should be empty strings
assert result["body"] == ""


def test_compress():
# GIVEN a function that has compress=True
# AND an event with a "Accept-Encoding" that include gzip
Expand Down Expand Up @@ -485,7 +498,7 @@ def post_no_cors():
# THEN return no content
# AND include Access-Control-Allow-Methods of the cors methods used
assert result["statusCode"] == 204
assert result["body"] is None
assert result["body"] == ""
headers = result["headers"]
assert "Content-Type" not in headers
assert "Access-Control-Allow-Origin" in result["headers"]
Expand Down