Repository navigation
fix(event_handler): match exact routes before catch-all regex routes - #8881
annastudios wants to merge 2 commits into
Conversation
A regex rule without named groups, such as /proxy/.+, was stored with the exact rules and matched in registration order, so it took requests for /proxy/health when registered first. The "Catch-all routes" docs say exact matches take precedence over regex patterns. Exact static rules are now placed before regex static rules when the route is registered, so matching order no longer depends on registration order. Request-time matching is unchanged. closes aws-powertools#8879
|
Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need. |
| _NAMED_GROUP_BOUNDARY_PATTERN = rf"(?P\1[{_SAFE_URI}{_UNSAFE_URI}\\w]+)" | ||
| _ROUTE_REGEX = "^{}$" | ||
| # A static rule with any of these is a regex (/proxy/.+), not an exact path (/proxy/health) | ||
| _REGEX_METACHARACTERS = frozenset(".^$*+?{}[]\\|()") |
There was a problem hiding this comment.
Exact paths that contain a dot still lose to a catch-all registered first, because . is in this set, so they go to the regex bucket and are appended after it:
app.get("/.well-known/.+")(lambda: {"route": "catch"})
app.get("/.well-known/jwks.json")(lambda: {"route": "jwks"})
# GET /.well-known/jwks.json -> {"route": "catch"}Same for /files/.+ + /files/readme.txt and /v1.0/.+ + /v1.0/health. File names and versioned prefixes are common in exact routes.
Leaving . out fixes these: a rule whose only metacharacter is . still matches one fixed-length path shape, so it can safely go before rules with quantifiers or groups.
| _REGEX_METACHARACTERS = frozenset(".^$*+?{}[]\\|()") | |
| _REGEX_METACHARACTERS = frozenset("^$*+?{}[]\\|()") |
With that change all three cases above pick the exact route, /proxy/health still works, and tests/functional/event_handler gives the same results as on this branch.
There was a problem hiding this comment.
Good catch, thanks! Applied your suggestion in e4de9f8 and added a parametrized test covering /.well-known/jwks.json, /files/readme.txt and /v1.0/health (each fails without the change). tests/unit/event_handler + tests/functional/event_handler: 1291 passed; ruff and mypy clean.
A rule whose only metacharacter is "." (/.well-known/jwks.json, /files/readme.txt, /v1.0/health) matches one fixed-length path shape, so it now goes before catch-all regex rules too. Suggested in review.
|



Issue number:
closes #8879
Summary
Changes
A regex rule without named groups, such as
/proxy/.+, hasrule.groups == 0, soroute()stored it in_static_routesnext to exact rules, and_resolvematched them in registration order. A catch-all registered before/proxy/healthtook its requests.route()now inserts exact static rules (no regex metacharacters) before regex static rules, so the order no longer depends on registration. This happens at registration; matching at request time is unchanged, and dynamic routes keep coming after static ones. Routes included from aRoutergo throughroute()too, so they are covered.Thanks @bardrop for the detailed report and repro.
User experience
Before: with
@app.get("/proxy/.+")registered before@app.get("/proxy/health"),GET /proxy/healthreturns{"route":"catch-all"}.After: it returns
{"route":"health"}, as the "Route Matching Priority" box in the Catch-all routes docs says.GET /proxy/otherstill goes to the catch-all.Verification
tests/functional/event_handler/required_dependencies/test_api_gateway.py, through the public API: both registration orders, and an exact route included later from a prefixedRouter. Two of the three fail ondevelop(the "exact registered first" case already worked) and all pass with the fix.pytest tests/unit/event_handler tests/functional/event_handler: 1288 passed, 1 skipped. Two failures,test_load_resolver_file_not_foundandtest_openapi_encode_purepath, also fail ondevelop: they are Windows path-separator issues on my machine, unrelated to this change.ruff format --check,ruff checkandmypyclean on the changed files.{"route":"health"}for/proxy/healthand{"route":"catch-all"}for/proxy/other.AI-assisted; I reproduced the bug, reviewed the change and ran the checks above myself.
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.