Skip to content

Commit 6fbad2f

Browse files
fix(event_handler): CORS Origin for ALBResolver multi-headers (#4385)
1 parent e360005 commit 6fbad2f

File tree

10 files changed

+48
-8
lines changed

10 files changed

+48
-8
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
validation_error_definition,
4444
validation_error_response_definition,
4545
)
46-
from aws_lambda_powertools.event_handler.util import _FrozenDict
46+
from aws_lambda_powertools.event_handler.util import _FrozenDict, extract_origin_header
4747
from aws_lambda_powertools.shared.cookies import Cookie
4848
from aws_lambda_powertools.shared.functions import powertools_dev_is_set
4949
from aws_lambda_powertools.shared.json_encoder import Encoder
@@ -782,7 +782,8 @@ def __init__(
782782

783783
def _add_cors(self, event: ResponseEventT, cors: CORSConfig):
784784
"""Update headers to include the configured Access-Control headers"""
785-
self.response.headers.update(cors.to_dict(event.get_header_value("Origin")))
785+
extracted_origin_header = extract_origin_header(event.resolved_headers_field)
786+
self.response.headers.update(cors.to_dict(extracted_origin_header))
786787

787788
def _add_cache_control(self, cache_control: str):
788789
"""Set the specified cache control headers for 200 http responses. For non-200 `no-cache` is used."""
@@ -2129,7 +2130,8 @@ def _not_found(self, method: str) -> ResponseBuilder:
21292130
headers = {}
21302131
if self._cors:
21312132
logger.debug("CORS is enabled, updating headers.")
2132-
headers.update(self._cors.to_dict(self.current_event.get_header_value("Origin")))
2133+
extracted_origin_header = extract_origin_header(self.current_event.resolved_headers_field)
2134+
headers.update(self._cors.to_dict(extracted_origin_header))
21332135

21342136
if method == "OPTIONS":
21352137
logger.debug("Pre-flight request detected. Returning CORS with null response")

aws_lambda_powertools/event_handler/util.py

+29
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
from typing import Any, Dict
2+
3+
from aws_lambda_powertools.utilities.data_classes.shared_functions import get_header_value
4+
5+
16
class _FrozenDict(dict):
27
"""
38
A dictionary that can be used as a key in another dictionary.
@@ -11,3 +16,27 @@ class _FrozenDict(dict):
1116

1217
def __hash__(self):
1318
return hash(frozenset(self.keys()))
19+
20+
21+
def extract_origin_header(resolver_headers: Dict[str, Any]):
22+
"""
23+
Extracts the 'origin' or 'Origin' header from the provided resolver headers.
24+
25+
The 'origin' or 'Origin' header can be either a single header or a multi-header.
26+
27+
Args:
28+
resolver_headers (Dict): A dictionary containing the headers.
29+
30+
Returns:
31+
Optional[str]: The value(s) of the origin header or None.
32+
"""
33+
resolved_header = get_header_value(
34+
headers=resolver_headers,
35+
name="origin",
36+
default_value=None,
37+
case_sensitive=False,
38+
)
39+
if isinstance(resolved_header, list):
40+
return resolved_header[0]
41+
42+
return resolved_header

docs/index.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ You can install Powertools for AWS Lambda (Python) using your favorite dependenc
8989
=== "CDK"
9090

9191
```python hl_lines="13 19"
92-
--8<-- "examples/homepage/install/x86_64/cdk.py"
92+
--8<-- "examples/homepage/install/x86_64/cdk_x86.py"
9393
```
9494

9595
=== "Terraform"
@@ -101,7 +101,7 @@ You can install Powertools for AWS Lambda (Python) using your favorite dependenc
101101
=== "Pulumi"
102102

103103
```python hl_lines="21-27"
104-
--8<-- "examples/homepage/install/x86_64/pulumi.py"
104+
--8<-- "examples/homepage/install/x86_64/pulumi_x86.py"
105105
```
106106

107107
=== "Amplify"
@@ -127,7 +127,7 @@ You can install Powertools for AWS Lambda (Python) using your favorite dependenc
127127
=== "CDK"
128128

129129
```python hl_lines="13 19"
130-
--8<-- "examples/homepage/install/arm64/cdk.py"
130+
--8<-- "examples/homepage/install/arm64/cdk_arm64.py"
131131
```
132132

133133
=== "Terraform"
@@ -139,7 +139,7 @@ You can install Powertools for AWS Lambda (Python) using your favorite dependenc
139139
=== "Pulumi"
140140

141141
```python hl_lines="21-27"
142-
--8<-- "examples/homepage/install/arm64/pulumi.py"
142+
--8<-- "examples/homepage/install/arm64/pulumi_arm64.py"
143143
```
144144

145145
=== "Amplify"
@@ -275,7 +275,7 @@ Compared with the [public Layer ARN](#lambda-layer) option, SAR allows you to ch
275275
=== "CDK"
276276

277277
```python hl_lines="7 16-20 23-27"
278-
--8<-- "examples/homepage/install/sar/cdk.py"
278+
--8<-- "examples/homepage/install/sar/cdk_sar.py"
279279
```
280280

281281
=== "Terraform"

mypy.ini

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ disable_error_code = annotation-unchecked
1212
[mypy-jmespath]
1313
ignore_missing_imports=True
1414

15+
[mypy-pulumi.*]
16+
ignore_missing_imports=True
17+
18+
[mypy-pulumi_aws.*]
19+
ignore_missing_imports=True
20+
1521
[mypy-aws_encryption_sdk.*]
1622
ignore_missing_imports=True
1723

tests/events/apiGatewayProxyEvent.json

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
"Header1": [
1313
"value1"
1414
],
15+
"Origin": [
16+
"https://aws.amazon.com"
17+
],
1518
"Header2": [
1619
"value1",
1720
"value2"

0 commit comments

Comments
 (0)