fix(local-apigw): treat authorizer resource ARNs as literals, not regex - #9155
fix(local-apigw): treat authorizer resource ARNs as literals, not regex#9155devteamaegis wants to merge 1 commit into
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 <devteam.aegis@gmail.com>
|
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 |
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.