Skip to content

Commit d1b8e83

Browse files
authored
fix(event-handler): body to empty string in CORS preflight (ALB non-compliant) (#1249)
1 parent 2bd8722 commit d1b8e83

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

Diff for: aws_lambda_powertools/event_handler/api_gateway.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ def _not_found(self, method: str) -> ResponseBuilder:
595595
if method == "OPTIONS":
596596
logger.debug("Pre-flight request detected. Returning CORS with null response")
597597
headers["Access-Control-Allow-Methods"] = ",".join(sorted(self._cors_methods))
598-
return ResponseBuilder(Response(status_code=204, content_type=None, headers=headers, body=None))
598+
return ResponseBuilder(Response(status_code=204, content_type=None, headers=headers, body=""))
599599

600600
handler = self._lookup_exception_handler(NotFoundError)
601601
if handler:

Diff for: tests/functional/event_handler/test_api_gateway.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,19 @@ def handler(event, context):
236236
assert "Access-Control-Allow-Origin" not in result["headers"]
237237

238238

239+
def test_cors_preflight_body_is_empty_not_null():
240+
# GIVEN CORS is configured
241+
app = ALBResolver(cors=CORSConfig())
242+
243+
event = {"path": "/my/request", "httpMethod": "OPTIONS"}
244+
245+
# WHEN calling the event handler
246+
result = app(event, {})
247+
248+
# THEN there body should be empty strings
249+
assert result["body"] == ""
250+
251+
239252
def test_compress():
240253
# GIVEN a function that has compress=True
241254
# AND an event with a "Accept-Encoding" that include gzip
@@ -485,7 +498,7 @@ def post_no_cors():
485498
# THEN return no content
486499
# AND include Access-Control-Allow-Methods of the cors methods used
487500
assert result["statusCode"] == 204
488-
assert result["body"] is None
501+
assert result["body"] == ""
489502
headers = result["headers"]
490503
assert "Content-Type" not in headers
491504
assert "Access-Control-Allow-Origin" in result["headers"]

0 commit comments

Comments
 (0)