fix: HttpApi explicit AuthorizationScopes: [] is not overridden by authorizer default - #3980
Open
Adityaj0 wants to merge 1 commit into
Open
Conversation
…thorizer default OpenApiEditor.add_auth_to_method defaulted an unset AuthorizationScopes to [], then _set_method_authorizer checked it with `if authorization_scopes:`. Since [] is falsy, this couldn't distinguish "not set" from an explicit empty-list override, so a method-level `AuthorizationScopes: []` (meant to require no scopes, overriding the authorizer's default) was silently ignored and the authorizer's default AuthorizationScopes were enforced instead. SwaggerEditor's equivalent REST API code path (swagger.py) already gets this right by using None as the "not set" sentinel and an `is not None` check. This applies the same fix to OpenApiEditor so HTTP APIs behave consistently with REST APIs, and also aligns the authorizer-presence check (`authorizers.get(authorizer_name) is not None`) with swagger.py's pattern. Fixes aws#3979
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #3979.
For
AWS::Serverless::HttpApi, an event/function-levelAuth.AuthorizationScopes: []is meant to override the named authorizer's defaultAuthorizationScopes, requiring no scopes for that method — this already works correctly forAWS::Serverless::Api(REST APIs).OpenApiEditor.add_auth_to_method(samtranslator/open_api/open_api.py) defaulted an unsetAuthorizationScopesto[]:_set_method_authorizerthen checked it withif authorization_scopes:— since[]is falsy, this is indistinguishable from "not set," so the explicit override was silently dropped and the authorizer's default scopes were enforced instead. This means requests the template author intended to allow without those scopes get rejected by API Gateway.SwaggerEditor's equivalent REST API code path (samtranslator/swagger/swagger.py) already handles this correctly — it usesNoneas the "not set" sentinel (auth.get("AuthorizationScopes"), no default) and checksis not None. This PR applies the identical fix toOpenApiEditor, and also aligns the authorizer-presence check (authorizers.get(authorizer_name) is not None) withswagger.py's pattern, replacing an unguardedauthorizers[authorizer_name]dict-subscript.Test plan
tests/translator/input/http_api_with_auth_with_default_scopes.yaml, a direct HttpApi port of the existing REST API regression testtests/translator/input/api_with_auth_with_default_scopes.yaml(default authorizer, explicit authorizer, scope overwrite, and bothAuthorizationScopes: []cases), with expected output for all three partitions (aws,aws-cn,aws-us-gov).AuthorizationScopes: []cases produced the authorizer's default scopes instead of[]— and passes with the fix, with output matching the semantics already proven correct for REST APIs.python -m pytest tests/translator tests/plugins tests/parser tests/openapi tests/swagger tests/model— 3241 passed (same 5 pre-existing, unrelated SAR-timing/region failures as ondevelop).ruff check/black --checkclean on the changed file.