Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(sns): match nested message-body policies in arrays (#4234)

Aniket Shuklade5085927347
diff --git a/docs/services/sns.md b/docs/services/sns.md
index e3160bbeca..1be719a670 100644
--- a/docs/services/sns.md
+++ b/docs/services/sns.md
@@ -90,6 +90,9 @@ Supported subscription protocols:
- `http` / `https`: posts to an HTTP endpoint
- `application`: fans out to a mobile push platform endpoint (see [Mobile push](#mobile-push-mock))
+For `FilterPolicyScope=MessageBody`, nested policy objects descend into JSON objects and arrays.
+An object inside an array matches when one array element satisfies the complete nested policy.
+
## Message size
`MaximumMessageSize` is the per-topic limit, in bytes, on a published payload. It accepts `1024`
diff --git a/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java b/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java
index db3c5270e4..5cdd0bb6f4 100644
--- a/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java
@@ -1236,6 +1236,14 @@ public class SnsService implements Resettable, ResourceProvider {
if (!policy.isObject()) {
return false;
}
+ if (body != null && body.isArray()) {
+ for (JsonNode element : body) {
+ if (matchesBodyPolicy(policy, element)) {
+ return true;
+ }
+ }
+ return false;
+ }
var fields = policy.fields();
while (fields.hasNext()) {
var entry = fields.next();
diff --git a/src/test/java/io/github/hectorvent/floci/services/sns/SnsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/sns/SnsIntegrationTest.java
index 0af2386348..6daadef0b6 100644
--- a/src/test/java/io/github/hectorvent/floci/services/sns/SnsIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/sns/SnsIntegrationTest.java
@@ -681,6 +681,66 @@ class SnsIntegrationTest {
.when().post("/");
}
+ @Test
+ @Order(19)
+ void filterPolicy_messageBodyMatchesS3RecordsArray() {
+ String queueUrl = given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "CreateQueue")
+ .formParam("QueueName", "sns-records-filter-" + UUID.randomUUID())
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .extract().xmlPath().getString("CreateQueueResponse.CreateQueueResult.QueueUrl");
+
+ String subscription = given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "Subscribe")
+ .formParam("TopicArn", topicArn)
+ .formParam("Protocol", "sqs")
+ .formParam("Endpoint", queueUrl)
+ .formParam("Attributes.entry.1.key", "FilterPolicyScope")
+ .formParam("Attributes.entry.1.value", "MessageBody")
+ .formParam("Attributes.entry.2.key", "FilterPolicy")
+ .formParam("Attributes.entry.2.value",
+ "{\"Records\":{\"s3\":{\"bucket\":{\"name\":[\"mybucket\"]}}}}")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .extract().xmlPath().getString("SubscribeResponse.SubscribeResult.SubscriptionArn");
+
+ try {
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "Publish")
+ .formParam("TopicArn", topicArn)
+ .formParam("Message", "{\"Records\":[{\"s3\":{\"bucket\":{\"name\":\"mybucket\"}}}]}")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "ReceiveMessage")
+ .formParam("QueueUrl", queueUrl)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString("mybucket"));
+ } finally {
+ given().contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "Unsubscribe").formParam("SubscriptionArn", subscription)
+ .when().post("/");
+ given().contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DeleteQueue").formParam("QueueUrl", queueUrl)
+ .when().post("/");
+ }
+ }
+
@Test
@Order(50)
void rawDelivery_createQueuesAndSubscribe() {
diff --git a/src/test/java/io/github/hectorvent/floci/services/sns/SnsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/sns/SnsServiceTest.java
index 91bb792398..97641d2723 100644
--- a/src/test/java/io/github/hectorvent/floci/services/sns/SnsServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/sns/SnsServiceTest.java
@@ -469,6 +469,33 @@ class SnsServiceTest {
body("{\"store\":{\"region\":\"west\"}}"), null));
}
+ @Test
+ void filterPolicy_messageBody_nestedObjectsInsideArray() {
+ Subscription sub = subscriptionWithPolicy(
+ "{\"Records\":{\"s3\":{\"bucket\":{\"name\":[\"mybucket\"]}}}}", "MessageBody");
+ assertTrue(snsService.matchesFilterPolicy(sub, body("""
+ {"Records":[{"s3":{"bucket":{"name":"other"}}},
+ {"s3":{"bucket":{"name":"mybucket"}}}]}
+ """), null));
+ assertFalse(snsService.matchesFilterPolicy(sub, body("""
+ {"Records":[{"s3":{"bucket":{"name":"other"}}}]}
+ """), null));
+ assertFalse(snsService.matchesFilterPolicy(sub, body("{\"Records\":[]}"), null));
+ }
+
+ @Test
+ void filterPolicy_messageBody_nestedArrayRequiresOneElementToMatchWholePolicy() {
+ Subscription sub = subscriptionWithPolicy(
+ "{\"Records\":{\"name\":[\"mybucket\"],\"region\":[\"us-east-1\"]}}", "MessageBody");
+ assertFalse(snsService.matchesFilterPolicy(sub, body("""
+ {"Records":[{"name":"mybucket","region":"us-west-2"},
+ {"name":"other","region":"us-east-1"}]}
+ """), null));
+ assertTrue(snsService.matchesFilterPolicy(sub, body("""
+ {"Records":[{"name":"mybucket","region":"us-east-1"}]}
+ """), null));
+ }
+
@Test
void filterPolicy_messageBody_numericRule() {
Subscription sub = subscriptionWithPolicy(