Skip to content

Commit 1438fdb

Browse files
committed
[EventsBridge] Add validation and parity test for malformed JSON details field
1 parent 78fdf45 commit 1438fdb

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

localstack-core/localstack/services/events/provider.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,18 @@ def validate_event(event: PutEventsRequestEntry) -> None | PutEventsResultEntry:
197197
"ErrorCode": "InvalidArgument",
198198
"ErrorMessage": "Parameter Detail is not valid. Reason: Detail is a required argument.",
199199
}
200+
else:
201+
try:
202+
json_detail = json.loads(event.get("Detail"))
203+
if isinstance(json_detail, dict):
204+
return
205+
except json.JSONDecodeError:
206+
pass
207+
208+
return {
209+
"ErrorCode": "MalformedDetail",
210+
"ErrorMessage": "Detail is malformed.",
211+
}
200212

201213

202214
def check_unique_tags(tags: TagsList) -> None:

tests/aws/services/events/test_events.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@
5050
"detail": {"command": ["update-account"]},
5151
}
5252

53+
TEST_EVENT_PATTERN_MALFORMED_DETAIL = {
54+
"source": ["core.update-account-command"],
55+
"detail-type": ["core.update-account-command"],
56+
}
57+
5358
API_DESTINATION_AUTHS = [
5459
{
5560
"type": "BASIC",
@@ -134,6 +139,27 @@ def test_put_event_without_detail_type(self, snapshot, aws_client):
134139
response = aws_client.events.put_events(Entries=entries)
135140
snapshot.match("put-events", response)
136141

142+
@markers.aws.validated
143+
@pytest.mark.skipif(
144+
is_old_provider(),
145+
reason="V1 provider does not support this feature",
146+
)
147+
@pytest.mark.parametrize(
148+
"detail",
149+
["NotJSON", "[]", "{{}", json.dumps("NotJSON")],
150+
ids=["STRING", "ARRAY", "MALFORMED_JSON", "SERIALIZED_STRING"],
151+
)
152+
def test_put_event_malformed_detail(self, snapshot, aws_client, detail):
153+
entries = [
154+
{
155+
"Source": TEST_EVENT_PATTERN["source"][0],
156+
"DetailType": TEST_EVENT_PATTERN["detail-type"][0],
157+
"Detail": detail,
158+
},
159+
]
160+
response = aws_client.events.put_events(Entries=entries)
161+
snapshot.match("put-events", response)
162+
137163
@markers.aws.validated
138164
def test_put_events_time(self, put_events_with_filter_to_sqs, snapshot):
139165
entries1 = [

tests/aws/services/events/test_events.snapshot.json

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2732,5 +2732,77 @@
27322732
}
27332733
]
27342734
}
2735+
},
2736+
"tests/aws/services/events/test_events.py::TestEvents::test_put_event_malformed_detail[STRING]": {
2737+
"recorded-date": "05-12-2024, 14:33:58",
2738+
"recorded-content": {
2739+
"put-events": {
2740+
"Entries": [
2741+
{
2742+
"ErrorCode": "MalformedDetail",
2743+
"ErrorMessage": "Detail is malformed."
2744+
}
2745+
],
2746+
"FailedEntryCount": 1,
2747+
"ResponseMetadata": {
2748+
"HTTPHeaders": {},
2749+
"HTTPStatusCode": 200
2750+
}
2751+
}
2752+
}
2753+
},
2754+
"tests/aws/services/events/test_events.py::TestEvents::test_put_event_malformed_detail[ARRAY]": {
2755+
"recorded-date": "05-12-2024, 14:33:58",
2756+
"recorded-content": {
2757+
"put-events": {
2758+
"Entries": [
2759+
{
2760+
"ErrorCode": "MalformedDetail",
2761+
"ErrorMessage": "Detail is malformed."
2762+
}
2763+
],
2764+
"FailedEntryCount": 1,
2765+
"ResponseMetadata": {
2766+
"HTTPHeaders": {},
2767+
"HTTPStatusCode": 200
2768+
}
2769+
}
2770+
}
2771+
},
2772+
"tests/aws/services/events/test_events.py::TestEvents::test_put_event_malformed_detail[MALFORMED_JSON]": {
2773+
"recorded-date": "05-12-2024, 14:33:58",
2774+
"recorded-content": {
2775+
"put-events": {
2776+
"Entries": [
2777+
{
2778+
"ErrorCode": "MalformedDetail",
2779+
"ErrorMessage": "Detail is malformed."
2780+
}
2781+
],
2782+
"FailedEntryCount": 1,
2783+
"ResponseMetadata": {
2784+
"HTTPHeaders": {},
2785+
"HTTPStatusCode": 200
2786+
}
2787+
}
2788+
}
2789+
},
2790+
"tests/aws/services/events/test_events.py::TestEvents::test_put_event_malformed_detail[SERIALIZED_STRING]": {
2791+
"recorded-date": "05-12-2024, 14:33:58",
2792+
"recorded-content": {
2793+
"put-events": {
2794+
"Entries": [
2795+
{
2796+
"ErrorCode": "MalformedDetail",
2797+
"ErrorMessage": "Detail is malformed."
2798+
}
2799+
],
2800+
"FailedEntryCount": 1,
2801+
"ResponseMetadata": {
2802+
"HTTPHeaders": {},
2803+
"HTTPStatusCode": 200
2804+
}
2805+
}
2806+
}
27352807
}
27362808
}

tests/aws/services/events/test_events.validation.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,18 @@
164164
"tests/aws/services/events/test_events.py::TestEvents::test_create_connection_validations": {
165165
"last_validated_date": "2024-11-14T20:29:49+00:00"
166166
},
167+
"tests/aws/services/events/test_events.py::TestEvents::test_put_event_malformed_detail[ARRAY]": {
168+
"last_validated_date": "2024-12-05T14:33:58+00:00"
169+
},
170+
"tests/aws/services/events/test_events.py::TestEvents::test_put_event_malformed_detail[MALFORMED_JSON]": {
171+
"last_validated_date": "2024-12-05T14:33:58+00:00"
172+
},
173+
"tests/aws/services/events/test_events.py::TestEvents::test_put_event_malformed_detail[SERIALIZED_STRING]": {
174+
"last_validated_date": "2024-12-05T14:33:58+00:00"
175+
},
176+
"tests/aws/services/events/test_events.py::TestEvents::test_put_event_malformed_detail[STRING]": {
177+
"last_validated_date": "2024-12-05T14:33:58+00:00"
178+
},
167179
"tests/aws/services/events/test_events.py::TestEvents::test_put_event_without_detail": {
168180
"last_validated_date": "2024-06-19T10:40:51+00:00"
169181
},

0 commit comments

Comments
 (0)