Repository navigation
fix(local-apigw): treat authorizer resource ARNs as literals, not regex - #9155
Conversation
_is_resource_authorized built a regular expression straight from the
Lambda authorizer's Resource ARN, escaping nothing but the wildcards.
Any regex metacharacter in the ARN was therefore interpreted as syntax.
The common case is the HTTP API '$default' stage: '$' is an end-of-string
anchor, so an Allow statement for
'arn:aws:execute-api:...:api/$default/*' never matched the method ARN and
sam local start-api returned 403 for a request that succeeds when
deployed - even when the authorizer echoed back the exact methodArn it
was handed. A '[' or '(' in the path raised re.PatternError outright.
Escape the ARN first, then translate the '*' and '?' wildcards.
Signed-off-by: devteamaegis <[email protected]>
|
Thanks for this, and sorry for the long silence — 30 days is too long for a fix this small. I reviewed it on a local worktree of The escaping covers every ARN comparison site.
Both failure directions are real. With
The Wildcards still translate. Tests run (worktree of
Non-blocking notes, none of which I would hold the merge for:
This looks good to me as it stands. Note that only 3 checks have reported so far; the remaining fork workflows need a maintainer approval to run before it can go in. |
|
Thanks for the thorough review @roger-zhangg, and for verifying it on a worktree rather than taking it on faith — that write-up is more careful than the fix deserved. Agreed on leaving the scope as-is; the On note 1: since I'm already touching that line, I'm happy to switch the trailing |
|
@roger-zhangg gentle ping on this one: I've updated the branch with the latest base. Is there anything else you'd like changed before it can be approved and the fork workflows run? |
🧪 Hands-on test plan (not run: external contributor)Commit Summary: Runs the apigw unit suite, then calls the authorizer resource matcher directly on ARNs with regex metacharacters ($, ., [, (, +, {}, backslash, spaces, unicode) and on existing IAM wildcard patterns (*, ?, prod/*/*), so before/after results can be compared. Then runs sam local start-api on an HttpApi with StageName $default and an IAM-policy Lambda authorizer to confirm end to end that echoed method ARNs and $default wildcards now return 200, that '[' and '(' paths no longer crash, and that '.' and other-stage policies are still denied. Why: The fix changes how sam local start-api matches Lambda authorizer IAM policy resources against the method ARN. This is runtime behavior a user hits locally: an HttpApi with StageName $default plus an authorizer that echoes methodArn/routeArn currently gets 403, and '[' or '(' in the path crashes the matcher. Both are easy to reproduce with a small SAM app and Docker, and comparing against the merge base shows the fix. The diff is two files (a one-line logic change plus unit tests) with no new dependencies, hooks, network code or agent-directed text, so running it is low risk. Needs Docker: yes Needs cloud resources (would not be run):
Risk notes:
Setup: Scenarios (5)S1: Check that the PR's new unit test and the existing apigw unit tests pass, so the existing wildcard cases (*, ?, prod/*/* on the root URL) still work after re.escape. Expected: Exit code 0. All tests pass, including the 6 parameterized cases of test_is_resource_authorized_treats_arn_as_literal and all cases of test_validate_is_resource_authorized. The PR description says 286 passed; small count differences are fine as long as nothing fails or errors. S2: Call LambdaAuthorizer._is_resource_authorized directly on ARNs with regex metacharacters and on existing IAM wildcard patterns, to show before/after behavior and catch compatibility breaks (?, multiple *, anchoring, unicode, spaces, +, {}, backslash). (compared with the base branch) Expected: Exit code 0, one RESULT line per case. With the PR: dollar_wild True, dollar_echo True, bracket_echo True, paren_echo True, dot_literal False, dot_echo True, other_stage False, star_root True, qmark True, qmark_short False, multi_star True, star_only True, prefix_only False, plus_brace_echo True, u… S3: Reproduce the PR's main claim end to end: on an HttpApi with StageName $default, an authorizer that echoes the routeArn it receives, or allows .../$default/*, should get 200 from sam local start-api instead of 403. (compared with the base branch) Expected: The sam log shows AUTHZ_ARN lines like arn:aws:execute-api:us-east-1:123456789012:1234567890/$default/GET/hello, which confirms '$default' is in the method ARN. With the PR, all four cases print HTTP 200 with a body containing "ok": true. On the merge base, all four print HTTP 403 with a body like … S4: Error path: a request path containing '[' or '(' that the authorizer echoes back should no longer crash the authorizer check; the request should be allowed. (compared with the base branch) Expected: With the PR: bracket and paren print HTTP 200 with "ok": true; bracket_header prints HTTP 403 (the literal resource '.../GET/ok[' does not match '.../GET/ok'); plain_after prints HTTP 200 (the server keeps working). The sam log has no Traceback, PatternError or re.error lines. On the merge base: br… S5: Check that policies that should deny still deny: '.' is literal, other stages and other methods are denied, a prefix without a wildcard does not match, and a normal '*' wildcard still allows. (compared with the base branch) Expected: With the PR: dot_overmatch 403, dot_exact 200, other_stage 403, other_method 403, prefix_only 403, global_star 200. On the merge base: dot_overmatch returns 200 (the '.' over-match the PR removes), other_method/prefix_only/global_star are the same as with the PR, and the $default-based dot_exact re… 🤖 Automated hands-on test — not a review approval · model claude-opus-5.5 · commit History (2)
|
|
Thanks @reedham-aws! The failing |
|
@reedham-aws thanks again for the approval. Is there anyone else who needs to review before this can merge? Happy to make any changes. |
|
Yes, I'll get another maintainer and then we can merge. Thanks for the contribution! |
|
Thanks @reedham-aws for the review and for getting a second maintainer on it. This was investigated and written with an AI coding agent (Claude) working on my behalf. |
What's broken
sam local start-apireturns 403 for requests a Lambda authorizer explicitly allows, whenever the resource ARN contains a regex metacharacter. The everyday case is the HTTP API$defaultstage:It fails even when the authorizer echoes back the exact
methodArnit was handed — the most common example in AWS's own docs. Deployed API Gateway allows the same request, so this is a pure local/deployed divergence.A
[or(in the path is worse — the request dies withre.PatternError: unterminated character set.Why it happens
_is_resource_authorizedbuilds a regex directly from the ARN, escaping nothing but the wildcards:So
$becomes an end-of-string anchor,.matches any character, and[/(are unbalanced syntax.The fix
re.escape()the ARN first, then translate the two IAM wildcards:*and?keep working; everything else in the ARN is matched literally. This also stops.from over-matching —.../GET/a.cno longer authorizes.../GET/abc.The test
test_is_resource_authorized_treats_arn_as_literalintests/unit/local/apigw/test_lambda_authorizer.py— six cases covering$default, an echoed method ARN,[,(, literal., and a stage that should still be denied. Five of six fail ondevelop:black --checkclean on both files.