Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Crash when using an invalid method in open api #2001

Merged
merged 4 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions samtranslator/swagger/swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,17 @@ def set_path_default_authorizer(
if add_default_auth_to_preflight or normalized_method_name != "options":
normalized_method_name = self._normalize_method_name(method_name)
# It is possible that the method could have two definitions in a Fn::If block.

# check for valid methods
if normalized_method_name.upper() not in self._ALL_HTTP_METHODS:
raise InvalidDocumentException(
[
InvalidTemplateException(
"Path '{}' contains method '{}' which is not a supported method {}".format(path, method_name, self._ALL_HTTP_METHODS)
)
]
)

for method_definition in self.get_method_contents(self.get_path(path)[normalized_method_name]):

# If no integration given, then we don't need to process this definition (could be AWS::NoValue)
Expand Down
47 changes: 47 additions & 0 deletions tests/translator/input/error_api_with_invalid_path_object.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
Globals:
Api:
Name: "some api"
Variables:
SomeVar: Value
Auth:
DefaultAuthorizer: MyCognitoAuth
Authorizers:
MyCognitoAuth:
UserPoolArn: !GetAtt MyUserPool.Arn

Resources:
ImplicitApiFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: s3://sam-demo-bucket/member_portal.zip
Handler: index.gethtml
Runtime: nodejs12.x

ExplicitApi:
Type: AWS::Serverless::Api
Properties:
StageName: SomeStage
DefinitionBody:
swagger: 2.0
paths:
"/a":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the httpMethod and paths dont line up with the test itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I swear this worked locally. Good catch.

SomeInvalidKey:
x-amazon-apigateway-integration:
httpMethod: POST
type: aws_proxy
uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ImplicitApiFunction.Arn}/invocations
responses: {}

MyUserPool:
Type: AWS::Cognito::UserPool
Properties:
UserPoolName: UserPoolName
Policies:
PasswordPolicy:
MinimumLength: 8
UsernameAttributes:
- email
Schema:
- AttributeDataType: String
Name: email
Required: false
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Resources:
description: Application domain
type: string
required: true
tags:
options:
- InvalidMethodDefinition
get:
x-amazon-apigateway-integration:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Path '/command' contains method 'Auth' which is not a supported method ['OPTIONS', 'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'PATCH']"
}