Skip to content

Commit a659b21

Browse files
fix(parser): raise ValidationError when SNS->SQS keys are intentionally missing (aws-powertools#1299)
Co-authored-by: Heitor Lessa <[email protected]>
1 parent c912034 commit a659b21

File tree

2 files changed

+32
-2
lines changed
  • aws_lambda_powertools/utilities/parser/models
  • tests/functional/parser

2 files changed

+32
-2
lines changed

aws_lambda_powertools/utilities/parser/models/sns.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ class SnsNotificationModel(BaseModel):
3131
def check_sqs_protocol(cls, values):
3232
sqs_rewritten_keys = ("UnsubscribeURL", "SigningCertURL")
3333
if any(key in sqs_rewritten_keys for key in values):
34-
values["UnsubscribeUrl"] = values.pop("UnsubscribeURL")
35-
values["SigningCertUrl"] = values.pop("SigningCertURL")
34+
# The sentinel value 'None' forces the validator to fail with
35+
# ValidatorError instead of KeyError when the key is missing from
36+
# the SQS payload
37+
values["UnsubscribeUrl"] = values.pop("UnsubscribeURL", None)
38+
values["SigningCertUrl"] = values.pop("SigningCertURL", None)
3639
return values
3740

3841

tests/functional/parser/test_sns.py

+27
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
from typing import Any, List
23

34
import pytest
@@ -103,3 +104,29 @@ def handle_sns_sqs_json_body(event: List[MySnsBusiness], _: LambdaContext):
103104
def test_handle_sns_sqs_trigger_event_json_body(): # noqa: F811
104105
event_dict = load_event("snsSqsEvent.json")
105106
handle_sns_sqs_json_body(event_dict, LambdaContext())
107+
108+
109+
def test_handle_sns_sqs_trigger_event_json_body_missing_signing_cert_url():
110+
# GIVEN an event is tampered with a missing SigningCertURL
111+
event_dict = load_event("snsSqsEvent.json")
112+
payload = json.loads(event_dict["Records"][0]["body"])
113+
payload.pop("SigningCertURL")
114+
event_dict["Records"][0]["body"] = json.dumps(payload)
115+
116+
# WHEN parsing the payload
117+
# THEN raise a ValidationError error
118+
with pytest.raises(ValidationError):
119+
handle_sns_sqs_json_body(event_dict, LambdaContext())
120+
121+
122+
def test_handle_sns_sqs_trigger_event_json_body_missing_unsubscribe_url():
123+
# GIVEN an event is tampered with a missing UnsubscribeURL
124+
event_dict = load_event("snsSqsEvent.json")
125+
payload = json.loads(event_dict["Records"][0]["body"])
126+
payload.pop("UnsubscribeURL")
127+
event_dict["Records"][0]["body"] = json.dumps(payload)
128+
129+
# WHEN parsing the payload
130+
# THEN raise a ValidationError error
131+
with pytest.raises(ValidationError):
132+
handle_sns_sqs_json_body(event_dict, LambdaContext())

0 commit comments

Comments
 (0)