Repository navigation
fix(event_handler): strip the HTTP API stage only when rawPath has it - #8605
leandrodamascena merged 2 commits into
Conversation
APIGatewayProxyEventV2.path removed len("/" + stage) characters from
rawPath for any named stage. Behind a custom domain API mapping or in
SAM local the stage is not part of rawPath, so the path was truncated
and every route returned 404.
Strip the stage only when rawPath equals "/{stage}" or starts with
"/{stage}/", and return "/" for the stage root.
|
Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need. |
leandrodamascena
left a comment
There was a problem hiding this comment.
Thanks @Thanatos9404, really appreciate this one! I ran the suites and also deployed it behind a real HTTP API with $default, dev and stage-1 stages, plus direct invokes with the custom-domain event shape. Everything that worked before still works, and the cases that change were all returning garbage before. I even found one more on develop: stage dev with rawPath /api/users was routed to /users by accident because the stage length matched the segment. Your fix handles it.
I'm approving now, but this will go out in 3.38.0, so it will sit here for a bit before I merge. You don't need to rebase or touch anything in the meantime, I'll take care of that, and you'll get the credit in the release notes. Thank you so much!
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #8605 +/- ##
========================================
Coverage 96.93% 96.94%
========================================
Files 318 318
Lines 16045 16051 +6
Branches 1425 1427 +2
========================================
+ Hits 15554 15560 +6
Misses 349 349
Partials 142 142 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
9c6ca41
into
aws-powertools:develop
|
Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience! |
|
Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience! |



Issue number: closes #8577
Summary
Changes
APIGatewayProxyEventV2.pathremovedlen("/" + stage)characters fromrawPathfor every named stage. Through the execute-api endpoint the stage is part ofrawPath, so that worked. Behind a custom domain API mapping, or in SAM local,rawPathhas no stage, so the path got cut (/my/pathbecamepathfor stagedev) andAPIGatewayHttpResolverreturned 404 for every route.pathnow strips the stage only whenrawPathis/{stage}or starts with/{stage}/. Otherwise it returnsrawPathas is.$defaultis unchanged.rawPathequal to/{stage}(the root of a named stage) now gives/instead of an empty string, so it matches a/route like/{stage}/already did. The empty string didn't match any route./devicesfor stagedev, is no longer cut (it used to becomeices).LambdaFunctionUrlEventinheritspath, but its stage is always$default, so nothing changes there.APIGatewayProxyEvent(REST),ALBEventand the VPC Lattice events don't strip a stage inpath, so they don't have this bug.Tests
tests/functional/event_handler/required_dependencies/test_api_gateway.py:test_api_gateway_v2_stage_prefix_in_raw_pathsends a GET throughAPIGatewayHttpResolverfor 7 shapes: named stage with and without the stage inrawPath,$default,rawPathequal to/devand to/dev/,/deviceson stagedev, and/dev/devices. It checks the status code andapp.current_event.path.tests/unit/data_classes/required_dependencies/test_api_gateway_proxy_event.py: the same shapes againstAPIGatewayProxyEventV2.path, plus/dev/devto show the stage is stripped once.6 of the 14 new cases fail on
develop(no stage inrawPath,rawPathequal to the stage, and/devices, in both files). All 14 pass with this change.Checks
tests/functional/event_handler,tests/functional/data_classes,tests/unit/event_handlerandtests/unit/data_classes: 1385 passed, 1 skipped. Three tests fail on my Windows machine and fail the same way ondevelop(test_openapi_encode_purepath,test_load_resolver_file_not_found,test_code_pipeline_get_artifact; path separators and temp file locking).ruff checkandruff format --checkon the changed files: clean.mypyandty checkonapi_gateway_proxy_event.py: clean.User experience
Before, with the snippet from the issue (stage
dev, custom domain,rawPath/my/path):After:
200, andapp.current_event.path == "/my/path". Requests through the execute-api URL (rawPath/dev/my/path) still resolve to/my/path.This also fixes
strip_prefixesfor an API mapping path on a named stage. WithAPIGatewayHttpResolver(strip_prefixes=["/payment"]), stagedevandrawPath/payment/my/path,pathused to bement/my/path, so the prefix never matched and the request got a 404. Now it routes to/my/path.Risk: code that reads
event.pathfor a named stage behind a custom domain gets the real path instead of a truncated one. If a custom domain request's own path starts with/{stage}/(a/dev/itemsroute on stagedev), the stage is still stripped, becauserawPathalone can't tell the two cases apart. That part behaves as it did before.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.