Skip to content

Commit be743c3

Browse files
committed
fix: addressed comments
1 parent 1f59251 commit be743c3

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def with_cors():
8484
8585
cors_config = CORSConfig(
8686
allow_origin="https://wwww.example.com/",
87-
extra_origins=["https://www1.example.com/"],
87+
extra_origins=["https://dev.example.com/"],
8888
expose_headers=["x-exposed-response-header"],
8989
allow_headers=["x-custom-request-header"],
9090
max_age=100,
@@ -132,9 +132,9 @@ def __init__(
132132
allow_credentials: bool
133133
A boolean value that sets the value of `Access-Control-Allow-Credentials`
134134
"""
135-
self.allowed_origins = [allow_origin]
135+
self._allowed_origins = [allow_origin]
136136
if extra_origins:
137-
self.allowed_origins.extend(extra_origins)
137+
self._allowed_origins.extend(extra_origins)
138138
self.allow_headers = set(self._REQUIRED_HEADERS + (allow_headers or []))
139139
self.expose_headers = expose_headers or []
140140
self.max_age = max_age
@@ -149,7 +149,7 @@ def to_dict(self, origin: Optional[str]) -> Dict[str, str]:
149149

150150
# If the origin doesn't match any of the allowed origins, and we don't allow all origins ("*"),
151151
# don't add any CORS headers
152-
if origin not in self.allowed_origins and "*" not in self.allowed_origins:
152+
if origin not in self._allowed_origins and "*" not in self._allowed_origins:
153153
return {}
154154

155155
# The origin matched an allowed origin, so return the CORS headers

docs/core/event_handler/api_gateway.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,13 @@ For convenience, these are the default values when using `CORSConfig` to enable
311311
???+ warning
312312
Always configure `allow_origin` when using in production.
313313

314-
???+ tip "Multiple allowed origins?"
315-
If you require multiple allowed origins, pass the additional origins using the `extra_origins` key.
314+
???+ tip "Multiple origins?"
315+
If you need to allow multiple origins, pass the additional origins using the `extra_origins` key.
316316

317317
| Key | Value | Note |
318-
| -------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
318+
|----------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
319319
| **[allow_origin](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin){target="_blank"}**: `str` | `*` | Only use the default value for development. **Never use `*` for production** unless your use case requires it |
320+
| **[extra_origins](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin){target="_blank"}**: `List[str]` | `[]` | Additional origins to be allowed, in addition to the one specified in `allow_origin` |
320321
| **[allow_headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers){target="_blank"}**: `List[str]` | `[Authorization, Content-Type, X-Amz-Date, X-Api-Key, X-Amz-Security-Token]` | Additional headers will be appended to the default list for your convenience |
321322
| **[expose_headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Expose-Headers){target="_blank"}**: `List[str]` | `[]` | Any additional header beyond the [safe listed by CORS specification](https://developer.mozilla.org/en-US/docs/Glossary/CORS-safelisted_response_header){target="_blank"}. |
322323
| **[max_age](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Max-Age){target="_blank"}**: `int` | `` | Only for pre-flight requests if you choose to have your function to handle it instead of API Gateway |

tests/functional/event_handler/test_api_gateway.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,6 @@ def get_with_cors():
547547
result = app(event, None)
548548

549549
# THEN routes by default return the custom cors headers
550-
assert "multiValueHeaders" in result
551550
headers = result["multiValueHeaders"]
552551
assert headers["Content-Type"] == [content_types.APPLICATION_JSON]
553552
assert headers["Access-Control-Allow-Origin"] == ["https://origin3"]
@@ -557,7 +556,6 @@ def get_with_cors():
557556
result = app(event, None)
558557

559558
# THEN routes by default return the custom cors headers
560-
assert "multiValueHeaders" in result
561559
headers = result["multiValueHeaders"]
562560
assert headers["Content-Type"] == [content_types.APPLICATION_JSON]
563561
assert "Access-Control-Allow-Origin" not in headers
@@ -591,7 +589,7 @@ def another_one():
591589
assert "multiValueHeaders" in result
592590
headers = result["multiValueHeaders"]
593591
assert headers["Content-Type"] == [content_types.APPLICATION_JSON]
594-
assert headers["Access-Control-Allow-Origin"] == [cors_config.allowed_origins[0]]
592+
assert headers["Access-Control-Allow-Origin"] == ["https://foo1"]
595593
expected_allows_headers = [",".join(sorted(set(allow_header + cors_config._REQUIRED_HEADERS)))]
596594
assert headers["Access-Control-Allow-Headers"] == expected_allows_headers
597595
assert headers["Access-Control-Expose-Headers"] == [",".join(cors_config.expose_headers)]
@@ -689,8 +687,11 @@ def custom_preflight():
689687
def custom_method():
690688
...
691689

690+
# AND the request includes an origin
691+
headers = {"Origin": "https://example.org"}
692+
692693
# WHEN calling the handler
693-
result = app({"path": "/some-call", "httpMethod": "OPTIONS", "headers": {"Origin": "https://example.org"}}, None)
694+
result = app({"path": "/some-call", "httpMethod": "OPTIONS", "headers": headers}, None)
694695

695696
# THEN return the custom preflight response
696697
assert result["statusCode"] == 200

0 commit comments

Comments
 (0)