Skip to content

fix(parser): loose validation on SNS fields to support FIFO #1606

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions aws_lambda_powertools/utilities/parser/models/sns.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ class SnsNotificationModel(BaseModel):
MessageAttributes: Optional[Dict[str, SnsMsgAttributeModel]]
Message: Union[str, TypingType[BaseModel]]
MessageId: str
SigningCertUrl: HttpUrl
Signature: str
SigningCertUrl: Optional[HttpUrl] # NOTE: FIFO opt-in removes attribute
Signature: Optional[str] # NOTE: FIFO opt-in removes attribute
Timestamp: datetime
SignatureVersion: str
SignatureVersion: Optional[str] # NOTE: FIFO opt-in removes attribute

@root_validator(pre=True, allow_reuse=True)
def check_sqs_protocol(cls, values):
Expand Down
23 changes: 23 additions & 0 deletions tests/events/snsSqsFifoEvent.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"Records": [
{
"messageId": "69bc4bbd-ed69-4325-a434-85c3b428ceab",
"receiptHandle": "AQEBbfAqjhrgIdW3HGWYPz57mdDatG/dT9LZhRPAsNQ1pJmw495w4esDc8ZSbOwMZuPBol7wtiNWug8U25GpSQDDLY1qv//8/lfmdzXOiprG6xRVeiXSHj0j731rJQ3xo+GPdGjOzjIxI09CrE3HtZ4lpXY9NjjHzP8hdxkCLlbttumc8hDBUR365/Tk+GfV2nNP9qvZtLGEbKCdTm/GYdTSoAr+ML9HnnGrS9T25Md71ASiZMI4DZqptN6g7CYYojFPs1LVM9o1258ferA72zbNoQ==",
"body": "{\n \"Type\" : \"Notification\",\n \"MessageId\" : \"a7c9d2fa-77fa-5184-9de9-89391027cc7d\",\n \"SequenceNumber\" : \"10000000000000004000\",\n \"TopicArn\" : \"arn:aws:sns:eu-west-1:231436140809:Test.fifo\",\n \"Message\" : \"{\\\"message\\\": \\\"hello world\\\", \\\"username\\\": \\\"lessa\\\"}\",\n \"Timestamp\" : \"2022-10-14T13:35:25.419Z\",\n \"UnsubscribeURL\" : \"https://sns.eu-west-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-west-1:231436140809:Test.fifo:bb81d3de-a0f9-46e4-b619-d3152a4d545f\"\n}",
"attributes": {
"ApproximateReceiveCount": "1",
"SentTimestamp": "1665754525442",
"SequenceNumber": "18873177232222703872",
"MessageGroupId": "powertools-test",
"SenderId": "AIDAWYJAWPFU7SUQGUJC6",
"MessageDeduplicationId": "4e0a0f61eed277a4b9e4c01d5722b07b0725e42fe782102abee5711adfac701f",
"ApproximateFirstReceiveTimestamp": "1665754525442"
},
"messageAttributes": {},
"md5OfBody": "f3c788e623445e3feb263e80c1bffc0b",
"eventSource": "aws:sqs",
"eventSourceARN": "arn:aws:sqs:eu-west-1:231436140809:Test.fifo",
"awsRegion": "eu-west-1"
}
]
}
18 changes: 5 additions & 13 deletions tests/functional/parser/test_sns.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,6 @@ def test_handle_sns_sqs_trigger_event_json_body(): # noqa: F811
handle_sns_sqs_json_body(event_dict, LambdaContext())


def test_handle_sns_sqs_trigger_event_json_body_missing_signing_cert_url():
# GIVEN an event is tampered with a missing SigningCertURL
event_dict = load_event("snsSqsEvent.json")
payload = json.loads(event_dict["Records"][0]["body"])
payload.pop("SigningCertURL")
event_dict["Records"][0]["body"] = json.dumps(payload)

# WHEN parsing the payload
# THEN raise a ValidationError error
with pytest.raises(ValidationError):
handle_sns_sqs_json_body(event_dict, LambdaContext())


def test_handle_sns_sqs_trigger_event_json_body_missing_unsubscribe_url():
# GIVEN an event is tampered with a missing UnsubscribeURL
event_dict = load_event("snsSqsEvent.json")
Expand All @@ -134,3 +121,8 @@ def test_handle_sns_sqs_trigger_event_json_body_missing_unsubscribe_url():
# THEN raise a ValidationError error
with pytest.raises(ValidationError):
handle_sns_sqs_json_body(event_dict, LambdaContext())


def test_handle_sns_sqs_fifo_trigger_event_json_body():
event_dict = load_event("snsSqsFifoEvent.json")
handle_sns_sqs_json_body(event_dict, LambdaContext())