Skip to content

Commit 7ecae1e

Browse files
authored
fix(apigw): route regression for non-word chars 1st take (#556)
1 parent a768b68 commit 7ecae1e

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
logger = logging.getLogger(__name__)
2222

2323
_DYNAMIC_ROUTE_PATTERN = r"(<\w+>)"
24-
_NAMED_GROUP_BOUNDARY_PATTERN = r"(?P\1\\w+\\b)"
24+
_SAFE_URI = "-._~()'!*:@,;" # https://www.ietf.org/rfc/rfc3986.txt
25+
# API GW/ALB decode non-safe URI chars; we must support them too
26+
_UNSAFE_URI = "%<>\[\]{}|^" # noqa: W605
27+
28+
_NAMED_GROUP_BOUNDARY_PATTERN = fr"(?P\1[{_SAFE_URI}{_UNSAFE_URI}\\w]+)"
2529

2630

2731
class ProxyEventType(Enum):

tests/functional/event_handler/test_api_gateway.py

+27
Original file line numberDiff line numberDiff line change
@@ -701,3 +701,30 @@ def get_network_account(account_id: str, network_id: str):
701701
event["resource"] = "/accounts/{account_id}/source_networks/{network_id}"
702702
event["path"] = "/accounts/nested_account/source_networks/network"
703703
app.resolve(event, {})
704+
705+
706+
@pytest.mark.parametrize(
707+
"req",
708+
[
709+
pytest.param(123456789, id="num"),
710+
pytest.param("[email protected]", id="email"),
711+
pytest.param("-._~'!*:@,;()", id="safe-rfc3986"),
712+
pytest.param("%<>[]{}|^", id="unsafe-rfc3986"),
713+
],
714+
)
715+
def test_non_word_chars_route(req):
716+
# GIVEN
717+
app = ApiGatewayResolver()
718+
event = deepcopy(LOAD_GW_EVENT)
719+
720+
# WHEN
721+
@app.get("/accounts/<account_id>")
722+
def get_account(account_id: str):
723+
assert account_id == f"{req}"
724+
725+
# THEN
726+
event["resource"] = "/accounts/{account_id}"
727+
event["path"] = f"/accounts/{req}"
728+
729+
ret = app.resolve(event, None)
730+
assert ret["statusCode"] == 200

0 commit comments

Comments
 (0)