fix(eventbridge): enforce \$or negation in event pattern matching (#1068)

\$or was silently ignored — the key was never read from the parsed
pattern, so rules using \$or routed every source/detail-type-matched
event regardless of the OR conditions.

Add \$or handling to both matchesPattern (top-level) and
matchesDetailNode (detail-level):
- iterate each sub-pattern; return false if none match
- skip the \$or key in the regular field-by-field AND loop

Top-level \$or sub-patterns are full event patterns, so each is
evaluated via recursive matchesPattern call. Detail-level sub-patterns
are field-match objects, evaluated via recursive matchesDetailNode on
the same actual node.
This commit is contained in:
Jos Huiting authored and GitHub committed 2026-05-29 00:58:55 -05:00
1 parent 76edd1f78b
commit 34084fef87
2 files changed
+95

No files matched your search

@@ -540,6 +540,72 @@ class EventBridgeServiceTest {
"{\"detail\":{\"a\":{\"b\":{\"c\":[\"wrong\"]}}}}"));
}
@Test
void matchesPatternTopLevelOr_firstBranchMatches() {
Map<String, Object> event = Map.of("Source", "aws.ec2", "DetailType", "Other");
assertTrue(service.matchesPattern(event,
"{\"$or\":[{\"source\":[\"aws.ec2\"]},{\"source\":[\"aws.s3\"]}]}"));
}
@Test
void matchesPatternTopLevelOr_secondBranchMatches() {
Map<String, Object> event = Map.of("Source", "aws.s3", "DetailType", "Other");
assertTrue(service.matchesPattern(event,
"{\"$or\":[{\"source\":[\"aws.ec2\"]},{\"source\":[\"aws.s3\"]}]}"));
}
@Test
void matchesPatternTopLevelOr_noBranchMatches() {
Map<String, Object> event = Map.of("Source", "aws.rds", "DetailType", "Other");
assertFalse(service.matchesPattern(event,
"{\"$or\":[{\"source\":[\"aws.ec2\"]},{\"source\":[\"aws.s3\"]}]}"));
}
@Test
void matchesPatternTopLevelOrCombinedWithOtherField() {
Map<String, Object> event = Map.of("Source", "aws.ec2", "DetailType", "EC2 Instance State-change Notification");
// source must match AND one of the detail-types must match
assertTrue(service.matchesPattern(event,
"{\"source\":[\"aws.ec2\"],\"$or\":[{\"detail-type\":[\"EC2 Instance State-change Notification\"]},{\"detail-type\":[\"EC2 Spot Instance Interruption Warning\"]}]}"));
// source matches but neither detail-type matches
assertFalse(service.matchesPattern(event,
"{\"source\":[\"aws.ec2\"],\"$or\":[{\"detail-type\":[\"Something Else\"]},{\"detail-type\":[\"Also Wrong\"]}]}"));
}
@Test
void matchesPatternDetailLevelOr_matches() {
Map<String, Object> event = Map.of(
"Source", "my.app",
"Detail", "{\"status\":\"CONFIRMED\"}"
);
assertTrue(service.matchesPattern(event,
"{\"detail\":{\"$or\":[{\"status\":[\"CONFIRMED\"]},{\"status\":[\"PENDING\"]}]}}"));
}
@Test
void matchesPatternDetailLevelOr_noMatch() {
Map<String, Object> event = Map.of(
"Source", "my.app",
"Detail", "{\"status\":\"CANCELLED\"}"
);
assertFalse(service.matchesPattern(event,
"{\"detail\":{\"$or\":[{\"status\":[\"CONFIRMED\"]},{\"status\":[\"PENDING\"]}]}}"));
}
@Test
void matchesPatternDetailLevelOrCombinedWithOtherField() {
Map<String, Object> event = Map.of(
"Source", "my.app",
"Detail", "{\"status\":\"CONFIRMED\",\"region\":\"us-east-1\"}"
);
// region matches AND one of the statuses matches
assertTrue(service.matchesPattern(event,
"{\"detail\":{\"region\":[\"us-east-1\"],\"$or\":[{\"status\":[\"CONFIRMED\"]},{\"status\":[\"PENDING\"]}]}}"));
// region matches but status doesn't
assertFalse(service.matchesPattern(event,
"{\"detail\":{\"region\":[\"us-east-1\"],\"$or\":[{\"status\":[\"CANCELLED\"]},{\"status\":[\"FAILED\"]}]}}"));
}
@Test
void matchesPatternCombinesAccountRegionAndDetail() {
Map<String, Object> event = Map.of(