fix(sns): match String.Array attributes per element and honour $or in filter policies (#3746)

A subscription filter policy compared a String.Array message attribute against its
serialized JSON array as a single opaque string, so a policy rule of "Name" never
matched an attribute of ["Name","stringName"]. Any subscription filtering on a
multi-valued header silently matched nothing. AWS treats each element as a separate
string: a positive rule matches when any element matches, while anything-but matches
only when no element is listed, and exists asks about the attribute rather than its
elements.

$or was not recognised at all. The key was looked up as a message attribute literally
named "$or", which never exists, so the subscription went silent with no warning. It
is now honoured in both MessageAttributes and MessageBody scope under the conditions
AWS requires -- an array of at least two objects, none using a reserved operator name
as a field name -- and otherwise falls back to being an ordinary key, as AWS does.
EventBridge already had $or; SNS never got it.

Parsing the array value uses FAIL_ON_TRAILING_TOKENS, so a value like "[\"a\"] junk"
is treated as malformed and matched whole rather than element-matching as ["a"].
This commit is contained in:
Martin Acosta authored and GitHub committed 2026-09-16 15:42:13 -04:00
1 parent 934d0b829f
commit 1ae5c22f83
2 files changed
+380 -25

No files matched your search

@@ -22,6 +22,7 @@ import io.github.hectorvent.floci.services.sns.model.Topic;
import io.github.hectorvent.floci.services.sqs.SqsService;
import io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
@@ -45,6 +46,7 @@ import java.util.Base64;
import java.util.Collections;
import java.util.Deque;
import java.util.HexFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -52,6 +54,7 @@ import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.function.Predicate;
@ApplicationScoped
public class SnsService implements Resettable, ResourceProvider {
@@ -70,6 +73,14 @@ public class SnsService implements Resettable, ResourceProvider {
private static final java.util.regex.Pattern PLATFORM_APP_NAME_PATTERN =
java.util.regex.Pattern.compile("[a-zA-Z0-9_.\\-]{1,256}");
/**
* Operator names that may not appear as a field name inside a {@code $or} array; their
* presence is one of the conditions AWS uses to decide whether {@code $or} is an operator.
*/
private static final Set<String> RESERVED_POLICY_KEYWORDS = Set.of(
"$or", "anything-but", "exists", "prefix", "suffix", "numeric", "cidr",
"equals-ignore-case", "wildcard");
private final StorageBackend<String, Topic> topicStore;
private final StorageBackend<String, Subscription> subscriptionStore;
private final StorageBackend<String, PlatformApplication> platformAppStore;
@@ -1016,19 +1027,8 @@ public class SnsService implements Resettable, ResourceProvider {
}
return matchesBodyPolicy(filterPolicy, parsedBody);
}
Map<String, MessageAttributeValue> attrs = messageAttributes != null ? messageAttributes : Map.of();
var fields = filterPolicy.fields();
while (fields.hasNext()) {
var entry = fields.next();
String key = entry.getKey();
JsonNode rules = entry.getValue();
MessageAttributeValue attr = attrs.get(key);
String actualValue = attr != null ? attr.getStringValue() : null;
if (!matchesAttributeRules(actualValue, rules)) {
return false;
}
}
return true;
return matchesAttributePolicy(filterPolicy,
messageAttributes != null ? messageAttributes : Map.of());
} catch (Exception e) {
LOG.warnv("Failed to parse filter policy for {0}: {1}", sub.getSubscriptionArn(), e.getMessage());
return false;
@@ -1049,6 +1049,12 @@ public class SnsService implements Resettable, ResourceProvider {
var entry = fields.next();
String key = entry.getKey();
JsonNode ruleOrNested = entry.getValue();
if (isOrOperator(key, ruleOrNested)) {
if (!anyClauseMatches(ruleOrNested, clause -> matchesBodyPolicy(clause, body))) {
return false;
}
continue;
}
JsonNode bodyValue = (body != null && body.isObject()) ? body.get(key) : null;
if (ruleOrNested.isArray()) {
if (!matchesBodyRules(bodyValue, ruleOrNested)) {
@@ -1166,6 +1172,114 @@ public class SnsService implements Resettable, ResourceProvider {
return false;
}
/**
* Evaluates a filter policy against the message attribute map. Every key must match (AND),
* except {@code $or}, whose clauses are alternatives (OR) that still AND with their siblings.
*/
private boolean matchesAttributePolicy(JsonNode policy, Map<String, MessageAttributeValue> attrs) {
if (!policy.isObject()) {
return false;
}
Iterator<Map.Entry<String, JsonNode>> fields = policy.fields();
while (fields.hasNext()) {
Map.Entry<String, JsonNode> entry = fields.next();
String key = entry.getKey();
JsonNode rules = entry.getValue();
if (isOrOperator(key, rules)) {
if (!anyClauseMatches(rules, clause -> matchesAttributePolicy(clause, attrs))) {
return false;
}
continue;
}
if (!matchesAttribute(attrs.get(key), rules)) {
return false;
}
}
return true;
}
/**
* Applies a rule array to one message attribute. A {@code String.Array} attribute carries a
* JSON array in its StringValue and AWS matches each element separately, so a positive rule
* passes when any element matches. {@code anything-but} inverts that: it passes only when no
* element is listed. {@code exists} asks about the attribute itself, not about its elements.
*/
private boolean matchesAttribute(MessageAttributeValue attr, JsonNode rules) {
if (!rules.isArray()) {
return false;
}
List<String> elements = stringArrayElements(attr);
if (elements == null) {
return matchesAttributeRules(attr != null ? attr.getStringValue() : null, rules);
}
for (JsonNode rule : rules) {
if (rule.isObject() && rule.has("exists")) {
if (rule.get("exists").asBoolean()) {
return true;
}
continue;
}
if (rule.isObject() && rule.has("anything-but")) {
if (noElementIsListed(rule, elements)) {
return true;
}
continue;
}
for (String element : elements) {
if (matchesSingleAttributeRule(element, rule)) {
return true;
}
}
}
return false;
}
/**
* True when no element of a {@code String.Array} is named by an {@code anything-but} rule,
* which is what AWS requires for that rule to match. A JSON null element names nothing, so
* it cannot veto the match. Note {@link #matchesObjectRule} returns true for a value that is
* NOT listed, so a false from it is the element that vetoes.
*/
private boolean noElementIsListed(JsonNode rule, List<String> elements) {
for (String element : elements) {
if (element != null && !matchesObjectRule(rule, element)) {
return false;
}
}
return true;
}
/**
* Returns the elements of a {@code String.Array} attribute, or {@code null} when the attribute
* is not one -- including when its StringValue does not hold a JSON array. AWS rejects that at
* publish time; here the value falls back to being matched whole.
*/
private List<String> stringArrayElements(MessageAttributeValue attr) {
if (attr == null || !"String.Array".equals(attr.getDataType())
|| attr.getStringValue() == null) {
return null;
}
try {
// FAIL_ON_TRAILING_TOKENS matters here: without it a value of "[\"a\"] junk" parses as
// ["a"] and element-matches, instead of falling back to being matched whole.
JsonNode parsed = objectMapper.reader()
.with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
.readTree(attr.getStringValue());
if (!parsed.isArray()) {
return null;
}
List<String> elements = new ArrayList<>(parsed.size());
for (JsonNode element : parsed) {
elements.add(element.isNull() ? null : element.asText());
}
return elements;
} catch (Exception e) {
LOG.debugv("String.Array attribute is not a JSON array, matching it whole: {0}",
attr.getStringValue());
return null;
}
}
/**
* Checks if an attribute value matches a single filter policy rule set.
* Rules must be a JSON array where ANY element matching means the rule passes (OR logic).
@@ -1176,24 +1290,65 @@ public class SnsService implements Resettable, ResourceProvider {
return false;
}
for (JsonNode rule : rules) {
if (rule.isTextual() && rule.asText().equals(actualValue)) {
return true;
}
if (rule.isNumber() && actualValue != null) {
try {
if (new BigDecimal(actualValue).compareTo(rule.decimalValue()) == 0) {
return true;
}
} catch (NumberFormatException ignored) {
}
}
if (rule.isObject() && matchesObjectRule(rule, actualValue)) {
if (matchesSingleAttributeRule(actualValue, rule)) {
return true;
}
}
return false;
}
/** Evaluates one rule out of a rule array against a single string value. */
private boolean matchesSingleAttributeRule(String actualValue, JsonNode rule) {
if (rule.isTextual()) {
return rule.asText().equals(actualValue);
}
if (rule.isNumber() && actualValue != null) {
try {
return new BigDecimal(actualValue).compareTo(rule.decimalValue()) == 0;
} catch (NumberFormatException ignored) {
// A non-numeric attribute simply does not match a numeric rule.
return false;
}
}
if (rule.isObject()) {
return matchesObjectRule(rule, actualValue);
}
return false;
}
/** True when at least one clause of a {@code $or} array matches. */
private boolean anyClauseMatches(JsonNode clauses, Predicate<JsonNode> matches) {
for (JsonNode clause : clauses) {
if (matches.test(clause)) {
return true;
}
}
return false;
}
/**
* AWS reads {@code $or} as an operator only when its value is an array holding at least two
* objects, none of which use a reserved operator name as a field name. Anything else is an
* ordinary key named {@code $or} and is matched as one.
*/
private static boolean isOrOperator(String key, JsonNode value) {
if (!"$or".equals(key) || value == null || !value.isArray() || value.size() < 2) {
return false;
}
for (JsonNode clause : value) {
if (!clause.isObject()) {
return false;
}
Iterator<String> names = clause.fieldNames();
while (names.hasNext()) {
if (RESERVED_POLICY_KEYWORDS.contains(names.next())) {
return false;
}
}
}
return true;
}
/**
* Evaluates a single object-type filter rule (exists, prefix, anything-but, numeric)
* against the actual attribute value.
@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.sns.model.Subscription;
import io.github.hectorvent.floci.services.sns.model.Topic;
import io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -521,4 +522,203 @@ class SnsServiceTest {
assertEquals("order-processor", SnsService.extractFunctionName("order-processor"));
assertNull(SnsService.extractFunctionName(null));
}
private static Map<String, MessageAttributeValue> attr(String name, String value, String dataType) {
return Map.of(name, new MessageAttributeValue(value, dataType));
}
private static Map<String, MessageAttributeValue> stringArray(String name, String json) {
return attr(name, json, "String.Array");
}
/**
* A String.Array attribute carries a JSON array in its StringValue, and AWS matches a rule
* against each element separately. Comparing the rule against the serialized array as one
* opaque string makes every such subscription match nothing at all.
*/
@Test
void filterPolicy_stringArrayAttribute_matchesAnyElement() {
Subscription sub = subscriptionWithPolicy("{\"updatedFields\":[\"Name\"]}", null);
assertTrue(snsService.matchesFilterPolicy(sub, null,
stringArray("updatedFields", "[\"Name\",\"stringName\"]")));
assertTrue(snsService.matchesFilterPolicy(sub, null,
stringArray("updatedFields", "[\"Name\"]")));
assertFalse(snsService.matchesFilterPolicy(sub, null,
stringArray("updatedFields", "[\"Description\",\"stringName\"]")));
assertFalse(snsService.matchesFilterPolicy(sub, null,
stringArray("updatedFields", "[]")));
}
/** Operator rules apply per element too. */
@Test
void filterPolicy_stringArrayAttribute_appliesOperatorsPerElement() {
Subscription prefix = subscriptionWithPolicy(
"{\"updatedFields\":[{\"prefix\":\"string\"}]}", null);
assertTrue(snsService.matchesFilterPolicy(prefix, null,
stringArray("updatedFields", "[\"Name\",\"stringName\"]")));
assertFalse(snsService.matchesFilterPolicy(prefix, null,
stringArray("updatedFields", "[\"Name\",\"Description\"]")));
Subscription numeric = subscriptionWithPolicy(
"{\"codes\":[{\"numeric\":[\">=\",100]}]}", null);
assertTrue(snsService.matchesFilterPolicy(numeric, null,
stringArray("codes", "[\"12\",\"150\"]")));
assertFalse(snsService.matchesFilterPolicy(numeric, null,
stringArray("codes", "[\"12\",\"99\"]")));
}
/**
* anything-but does not follow the any-element rule: AWS matches a String.Array only when
* NONE of its elements are listed, so a single listed element vetoes the whole attribute.
*/
@Test
void filterPolicy_stringArrayAttribute_anythingButRequiresNoElementListed() {
Subscription sub = subscriptionWithPolicy(
"{\"updatedFields\":[{\"anything-but\":[\"Name\"]}]}", null);
assertTrue(snsService.matchesFilterPolicy(sub, null,
stringArray("updatedFields", "[\"Description\",\"stringName\"]")));
assertFalse(snsService.matchesFilterPolicy(sub, null,
stringArray("updatedFields", "[\"Description\",\"Name\"]")));
}
/** exists asks about the attribute, not its elements. */
@Test
void filterPolicy_stringArrayAttribute_existsIsAboutTheAttribute() {
Subscription present = subscriptionWithPolicy("{\"updatedFields\":[{\"exists\":true}]}", null);
assertTrue(snsService.matchesFilterPolicy(present, null,
stringArray("updatedFields", "[\"Name\"]")));
assertTrue(snsService.matchesFilterPolicy(present, null, stringArray("updatedFields", "[]")));
assertFalse(snsService.matchesFilterPolicy(present, null, Map.of()));
Subscription absent = subscriptionWithPolicy("{\"updatedFields\":[{\"exists\":false}]}", null);
assertFalse(snsService.matchesFilterPolicy(absent, null,
stringArray("updatedFields", "[\"Name\"]")));
assertTrue(snsService.matchesFilterPolicy(absent, null, Map.of()));
}
/** A String attribute keeps whole-string semantics even when its value looks like an array. */
@Test
void filterPolicy_plainStringAttribute_isNotSplit() {
Subscription sub = subscriptionWithPolicy("{\"updatedFields\":[\"Name\"]}", null);
assertFalse(snsService.matchesFilterPolicy(sub, null,
attr("updatedFields", "[\"Name\",\"stringName\"]", "String")));
assertTrue(snsService.matchesFilterPolicy(sub, null,
attr("updatedFields", "Name", "String")));
}
/** A String.Array whose value is not a JSON array falls back to whole-string matching. */
@Test
void filterPolicy_stringArrayAttribute_malformedValueFallsBackToWholeString() {
Subscription sub = subscriptionWithPolicy("{\"updatedFields\":[\"Name\"]}", null);
assertTrue(snsService.matchesFilterPolicy(sub, null, stringArray("updatedFields", "Name")));
assertFalse(snsService.matchesFilterPolicy(sub, null,
stringArray("updatedFields", "[\"Name\",")));
// trailing tokens make the whole value malformed, not a one-element array
assertFalse(snsService.matchesFilterPolicy(sub, null,
stringArray("updatedFields", "[\"Name\"] junk")));
}
/**
* AWS ignores Binary message attributes when applying a filter policy, so a Binary attribute
* counts as absent however the policy asks about it. Binary is also the only data type whose
* StringValue is null, which is why no rule here ever sees a value.
*/
@Test
void filterPolicy_binaryAttribute_countsAsAbsent() {
Map<String, MessageAttributeValue> binary = Map.of(
"updatedFields", new MessageAttributeValue(new byte[] {1, 2, 3}, "Binary"));
Subscription value = subscriptionWithPolicy("{\"updatedFields\":[\"Name\"]}", null);
assertFalse(snsService.matchesFilterPolicy(value, null, binary));
Subscription present = subscriptionWithPolicy("{\"updatedFields\":[{\"exists\":true}]}", null);
assertFalse(snsService.matchesFilterPolicy(present, null, binary));
Subscription absent = subscriptionWithPolicy("{\"updatedFields\":[{\"exists\":false}]}", null);
assertTrue(snsService.matchesFilterPolicy(absent, null, binary));
}
/**
* $or lets one subscription watch two unrelated collections. Without it the key is looked up
* as an attribute literally named "$or", which never exists, so the subscription goes silent.
*/
@Test
void filterPolicy_orOperator_messageAttributes() {
Subscription sub = subscriptionWithPolicy("{\"$or\":["
+ "{\"tableName\":[\"Asset\"],\"updatedFields\":[\"Name\"]},"
+ "{\"tableName\":[\"Location\"],\"updatedFields\":[\"stringName\"]}]}", null);
assertTrue(snsService.matchesFilterPolicy(sub, null, Map.of(
"tableName", new MessageAttributeValue("Asset", "String"),
"updatedFields", new MessageAttributeValue("[\"Name\"]", "String.Array"))));
assertTrue(snsService.matchesFilterPolicy(sub, null, Map.of(
"tableName", new MessageAttributeValue("Location", "String"),
"updatedFields", new MessageAttributeValue("[\"id\",\"stringName\"]", "String.Array"))));
// right table, wrong field
assertFalse(snsService.matchesFilterPolicy(sub, null, Map.of(
"tableName", new MessageAttributeValue("Asset", "String"),
"updatedFields", new MessageAttributeValue("[\"stringName\"]", "String.Array"))));
// clauses must not cross-pollinate: Location + Name matches neither branch
assertFalse(snsService.matchesFilterPolicy(sub, null, Map.of(
"tableName", new MessageAttributeValue("Location", "String"),
"updatedFields", new MessageAttributeValue("[\"Name\"]", "String.Array"))));
}
/** Keys beside $or still have to match, and they AND with the $or result. */
@Test
void filterPolicy_orOperator_andsWithSiblingKeys() {
Subscription sub = subscriptionWithPolicy("{\"env\":[\"prod\"],\"$or\":["
+ "{\"tableName\":[\"Asset\"]},{\"tableName\":[\"Location\"]}]}", null);
assertTrue(snsService.matchesFilterPolicy(sub, null, Map.of(
"env", new MessageAttributeValue("prod", "String"),
"tableName", new MessageAttributeValue("Asset", "String"))));
assertFalse(snsService.matchesFilterPolicy(sub, null, Map.of(
"env", new MessageAttributeValue("dev", "String"),
"tableName", new MessageAttributeValue("Asset", "String"))));
assertFalse(snsService.matchesFilterPolicy(sub, null, Map.of(
"env", new MessageAttributeValue("prod", "String"),
"tableName", new MessageAttributeValue("WorkOrder", "String"))));
}
/**
* AWS only reads $or as an operator when it holds at least two objects and none of them use a
* reserved operator name as a field; otherwise it is an ordinary attribute name.
*/
@Test
void filterPolicy_orOperator_invalidShapesAreOrdinaryAttributeNames() {
Subscription single = subscriptionWithPolicy(
"{\"$or\":[{\"tableName\":[\"Asset\"]}]}", null);
assertFalse(snsService.matchesFilterPolicy(single, null,
Map.of("tableName", new MessageAttributeValue("Asset", "String"))));
Subscription reserved = subscriptionWithPolicy(
"{\"$or\":[{\"prefix\":\"a\"},{\"tableName\":[\"Asset\"]}]}", null);
assertFalse(snsService.matchesFilterPolicy(reserved, null,
Map.of("tableName", new MessageAttributeValue("Asset", "String"))));
// a literal attribute named "$or" is matched as one
Subscription literal = subscriptionWithPolicy("{\"$or\":[\"yes\"]}", null);
assertTrue(snsService.matchesFilterPolicy(literal, null,
Map.of("$or", new MessageAttributeValue("yes", "String"))));
}
/** $or works the same way against a JSON body, including nested inside a key. */
@Test
void filterPolicy_orOperator_messageBody() {
Subscription sub = subscriptionWithPolicy("{\"$or\":["
+ "{\"tableName\":[\"Asset\"],\"updatedFields\":[\"Name\"]},"
+ "{\"tableName\":[\"Location\"],\"updatedFields\":[\"stringName\"]}]}", "MessageBody");
assertTrue(snsService.matchesFilterPolicy(sub,
body("{\"tableName\":\"Asset\",\"updatedFields\":[\"id\",\"Name\"]}"), null));
assertTrue(snsService.matchesFilterPolicy(sub,
body("{\"tableName\":\"Location\",\"updatedFields\":[\"stringName\"]}"), null));
assertFalse(snsService.matchesFilterPolicy(sub,
body("{\"tableName\":\"Location\",\"updatedFields\":[\"Name\"]}"), null));
Subscription nested = subscriptionWithPolicy(
"{\"detail\":{\"$or\":[{\"a\":[\"1\"]},{\"b\":[\"2\"]}]}}", "MessageBody");
assertTrue(snsService.matchesFilterPolicy(nested, body("{\"detail\":{\"b\":\"2\"}}"), null));
assertFalse(snsService.matchesFilterPolicy(nested, body("{\"detail\":{\"b\":\"3\"}}"), null));
}
}