fix(iam): share case-sensitive condition matching (#4725)

* fix(iam): share case-sensitive condition matching

* fix(iam): use iterative glob matching for conditions

* fix(iam): match wildcard before literal asterisk
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-09-30 14:59:42 -04:00
1 parent 0b53580969
commit 055c411b9a
8 files changed
+97 -116

No files matched your search

+2 -1
View File
@@ -640,7 +640,8 @@ account key carries no identity policies of its own.
- `ArnEquals`, `ArnLike`, `ArnNotEquals`, `ArnNotLike`: case-sensitive glob matching
of each of the six ARN components independently. Wildcards cannot cross the first five
colon separators; colons within the resource component are retained. `ArnEquals` and
`ArnLike` behave identically, as do their negated forms.
`ArnLike` behave identically, as do their negated forms. Service-principal trust policies
use the same component-by-component ARN matching for `aws:SourceArn`.
- `NumericEquals`, `NumericNotEquals`, `NumericLessThan`, `NumericGreaterThan` (and Equals variants)
- `DateEquals`, `DateNotEquals`, `DateLessThan`, `DateGreaterThan` (and Equals variants)
- `Bool`, `IpAddress`, `NotIpAddress`, `Null`
@@ -262,36 +262,12 @@ public class AssumeRolePolicyEvaluator {
if (!values.isTextual()) {
return false;
}
if ("StringEquals".equals(operator)) {
return values.asText().equals(actual);
}
return globMatchesCaseSensitive(values.asText(), actual);
}
private boolean globMatchesCaseSensitive(String pattern, String value) {
int patternIndex = 0;
int valueIndex = 0;
int starIndex = -1;
int starValueIndex = -1;
while (valueIndex < value.length()) {
if (patternIndex < pattern.length()
&& (pattern.charAt(patternIndex) == '?' || pattern.charAt(patternIndex) == value.charAt(valueIndex))) {
patternIndex++;
valueIndex++;
} else if (patternIndex < pattern.length() && pattern.charAt(patternIndex) == '*') {
starIndex = patternIndex++;
starValueIndex = valueIndex;
} else if (starIndex >= 0) {
patternIndex = starIndex + 1;
valueIndex = ++starValueIndex;
} else {
return false;
}
}
while (patternIndex < pattern.length() && pattern.charAt(patternIndex) == '*') {
patternIndex++;
}
return patternIndex == pattern.length();
return switch (operator) {
case "StringEquals" -> values.asText().equals(actual);
case "StringLike" -> IamPolicyEvaluator.caseSensitiveGlobMatches(values.asText(), actual);
case "ArnEquals", "ArnLike" -> IamPolicyEvaluator.matchesArnCondition(values.asText(), actual);
default -> false;
};
}
private boolean matchesServicePrincipal(JsonNode principalNode, String servicePrincipal) {
@@ -806,7 +806,7 @@ public class IamPolicyEvaluator {
return false;
}
for (String pattern : patterns) {
if (globMatchesHelper(pattern, value, 0, 0)) {
if (globMatchesHelper(pattern, value)) {
return true;
}
}
@@ -1050,14 +1050,14 @@ public class IamPolicyEvaluator {
if (pattern == null || value == null) {
return false;
}
return globMatchesHelper(pattern.toLowerCase(), value.toLowerCase(), 0, 0);
return globMatchesHelper(pattern.toLowerCase(), value.toLowerCase());
}
private static boolean caseSensitiveGlobMatches(String pattern, String value) {
return pattern != null && value != null && globMatchesHelper(pattern, value, 0, 0);
public static boolean caseSensitiveGlobMatches(String pattern, String value) {
return pattern != null && value != null && globMatchesHelper(pattern, value);
}
private static boolean matchesArnCondition(String pattern, String value) {
public static boolean matchesArnCondition(String pattern, String value) {
if (pattern == null || value == null) {
return false;
}
@@ -1075,33 +1075,31 @@ public class IamPolicyEvaluator {
return true;
}
private static boolean globMatchesHelper(String pat, String val, int pi, int vi) {
while (pi < pat.length() && vi < val.length()) {
char p = pat.charAt(pi);
if (p == '*') {
while (pi < pat.length() && pat.charAt(pi) == '*') {
pi++;
}
if (pi == pat.length()) {
return true;
}
for (int i = vi; i <= val.length(); i++) {
if (globMatchesHelper(pat, val, pi, i)) {
return true;
}
}
return false;
} else if (p == '?' || p == val.charAt(vi)) {
pi++;
vi++;
private static boolean globMatchesHelper(String pattern, String value) {
int patternIndex = 0;
int valueIndex = 0;
int starIndex = -1;
int starValueIndex = -1;
while (valueIndex < value.length()) {
if (patternIndex < pattern.length() && pattern.charAt(patternIndex) == '*') {
starIndex = patternIndex++;
starValueIndex = valueIndex;
} else if (patternIndex < pattern.length()
&& (pattern.charAt(patternIndex) == '?' || pattern.charAt(patternIndex) == value.charAt(valueIndex))) {
patternIndex++;
valueIndex++;
} else if (starIndex >= 0) {
patternIndex = starIndex + 1;
valueIndex = ++starValueIndex;
} else {
return false;
}
}
while (pi < pat.length() && pat.charAt(pi) == '*') {
pi++;
while (patternIndex < pattern.length() && pattern.charAt(patternIndex) == '*') {
patternIndex++;
}
return pi == pat.length() && vi == val.length();
return patternIndex == pattern.length();
}
// -----------------------------------------------------------------------
@@ -88,14 +88,14 @@ public class SAMLTrustPolicyEvaluator {
}
if ("StringLike".equals(operator)) {
return actual.stream().anyMatch(value -> expected.stream().anyMatch(pattern ->
WebIdentityTrustPolicyEvaluator.globMatchesCaseSensitive(pattern, value)));
IamPolicyEvaluator.caseSensitiveGlobMatches(pattern, value)));
}
if ("StringNotEquals".equals(operator)) {
return actual.stream().allMatch(value -> expected.stream().noneMatch(value::equals));
}
if ("StringNotLike".equals(operator)) {
return actual.stream().allMatch(value -> expected.stream().noneMatch(pattern ->
WebIdentityTrustPolicyEvaluator.globMatchesCaseSensitive(pattern, value)));
IamPolicyEvaluator.caseSensitiveGlobMatches(pattern, value)));
}
return false;
}
@@ -206,9 +206,9 @@ public class WebIdentityTrustPolicyEvaluator {
case "StringEquals" -> actual.stream().anyMatch(expectedValues::contains);
case "StringNotEquals" -> actual.stream().noneMatch(expectedValues::contains);
case "StringLike" -> actual.stream().anyMatch(
a -> expectedValues.stream().anyMatch(e -> globMatchesCaseSensitive(e, a)));
a -> expectedValues.stream().anyMatch(e -> IamPolicyEvaluator.caseSensitiveGlobMatches(e, a)));
case "StringNotLike" -> actual.stream().noneMatch(
a -> expectedValues.stream().anyMatch(e -> globMatchesCaseSensitive(e, a)));
a -> expectedValues.stream().anyMatch(e -> IamPolicyEvaluator.caseSensitiveGlobMatches(e, a)));
default -> {
LOG.debugv("Unsupported web-identity trust policy condition operator: {0}", operator);
yield false;
@@ -216,49 +216,6 @@ public class WebIdentityTrustPolicyEvaluator {
};
}
/**
* Glob matching over {@code *} and {@code ?} that preserves case, unlike
* {@link IamPolicyEvaluator#globMatches} which lowercases both operands. A {@code StringLike}
* constraint on {@code oidc:sub} must not admit a service account differing only in case, so it
* needs the same case sensitivity as the {@code StringEquals} branch above.
*/
static boolean globMatchesCaseSensitive(String pattern, String value) {
if (pattern == null || value == null) {
return false;
}
return globMatchesHelper(pattern, value, 0, 0);
}
private static boolean globMatchesHelper(String pattern, String value, int pi, int vi) {
while (pi < pattern.length()) {
char p = pattern.charAt(pi);
if (p == '*') {
// Collapse consecutive stars, then try every split point for the remainder.
while (pi + 1 < pattern.length() && pattern.charAt(pi + 1) == '*') {
pi++;
}
if (pi == pattern.length() - 1) {
return true;
}
for (int i = vi; i <= value.length(); i++) {
if (globMatchesHelper(pattern, value, pi + 1, i)) {
return true;
}
}
return false;
}
if (vi >= value.length()) {
return false;
}
if (p != '?' && p != value.charAt(vi)) {
return false;
}
pi++;
vi++;
}
return vi == value.length();
}
/**
* Resolves a policy condition key such as
* {@code oidc.eks.us-east-1.amazonaws.com/id/ABC123:sub} to the token's claim values. The key
@@ -275,6 +275,17 @@ class AssumeRolePolicyEvaluatorTest {
"arn:aws:appsync:us-east-1:000000000000:apis/example", "000000000000"));
}
@Test
void serviceSourceArnWildcardCannotCrossArnComponents() {
String doc = """
{"Statement":{"Effect":"Allow","Principal":{"Service":"appsync.amazonaws.com"},
"Action":"sts:AssumeRole",
"Condition":{"ArnLike":{"aws:SourceArn":"arn:aws:lambda:us-east-1:*:worker"}}}}
""";
assertFalse(evaluator.allowsService(doc, "appsync.amazonaws.com",
"arn:aws:lambda:us-east-1:111122223333:function:worker", "111122223333"));
}
@Test
void deniesBlankOrMalformedDocument() {
assertFalse(evaluator.allows(null, CALLER_ARN, CALLER_ACCOUNT));
@@ -7,10 +7,14 @@ import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.ResourcePolicy
import io.github.hectorvent.floci.services.iam.model.CallerContext;
import org.junit.jupiter.api.Test;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
@@ -38,6 +42,40 @@ class IamPolicyEvaluatorTest {
private final IamPolicyEvaluator evaluator = new IamPolicyEvaluator(new ObjectMapper());
@Test
void globMatchingWithRepeatedWildcardsDoesNotBacktrackExponentially() {
String pattern = "*a".repeat(10) + "*b";
String value = "a".repeat(40);
assertTimeoutPreemptively(Duration.ofSeconds(2), () -> {
assertFalse(IamPolicyEvaluator.caseSensitiveGlobMatches(pattern, value));
assertFalse(IamPolicyEvaluator.globMatches(pattern, value));
assertTrue(IamPolicyEvaluator.caseSensitiveGlobMatches("*a?*b", "zzacccb"));
});
}
@Test
void wildcardMatchesLiteralAsteriskInValue() {
assertTrue(IamPolicyEvaluator.caseSensitiveGlobMatches("a*", "a*b"));
assertTrue(IamPolicyEvaluator.globMatches("A*", "a*b"));
}
@Test
void resourceDenyMatchesS3KeyContainingLiteralAsterisk() {
String policy = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"s3:GetObject","Resource":"*"},
{"Effect":"Deny","Action":"s3:GetObject",
"Resource":"arn:aws:s3:::bucket/private/*"}
]}""";
CallerContext caller = CallerContext.of(List.of(policy));
assertEquals(Decision.DENY,
evaluator.evaluate(caller, null, "s3:GetObject", "arn:aws:s3:::bucket/private/*x", null));
assertEquals(Decision.ALLOW,
evaluator.evaluate(caller, null, "s3:GetObject", "arn:aws:s3:::bucket/public/*x", null));
}
private static CallerContext adminWithScps(List<List<String>> scpLevels) {
return CallerContext.of(List.of(ALLOW_ALL)).withScpLevels(scpLevels);
}
@@ -186,17 +186,17 @@ class WebIdentityTrustPolicyEvaluatorTest {
@Test
void caseSensitiveGlobHandlesWildcardsAndQuestionMarks() {
assertTrue(WebIdentityTrustPolicyEvaluator.globMatchesCaseSensitive("abc", "abc"));
assertFalse(WebIdentityTrustPolicyEvaluator.globMatchesCaseSensitive("abc", "ABC"));
assertTrue(WebIdentityTrustPolicyEvaluator.globMatchesCaseSensitive("a*", "abcdef"));
assertTrue(WebIdentityTrustPolicyEvaluator.globMatchesCaseSensitive("*f", "abcdef"));
assertTrue(WebIdentityTrustPolicyEvaluator.globMatchesCaseSensitive("a*e?", "abcdef"));
assertTrue(WebIdentityTrustPolicyEvaluator.globMatchesCaseSensitive("**abc**", "xxabcyy"));
assertTrue(WebIdentityTrustPolicyEvaluator.globMatchesCaseSensitive("*", ""));
assertFalse(WebIdentityTrustPolicyEvaluator.globMatchesCaseSensitive("a?c", "ac"));
assertFalse(WebIdentityTrustPolicyEvaluator.globMatchesCaseSensitive("abc", "abcd"));
assertFalse(WebIdentityTrustPolicyEvaluator.globMatchesCaseSensitive(null, "abc"));
assertFalse(WebIdentityTrustPolicyEvaluator.globMatchesCaseSensitive("abc", null));
assertTrue(IamPolicyEvaluator.caseSensitiveGlobMatches("abc", "abc"));
assertFalse(IamPolicyEvaluator.caseSensitiveGlobMatches("abc", "ABC"));
assertTrue(IamPolicyEvaluator.caseSensitiveGlobMatches("a*", "abcdef"));
assertTrue(IamPolicyEvaluator.caseSensitiveGlobMatches("*f", "abcdef"));
assertTrue(IamPolicyEvaluator.caseSensitiveGlobMatches("a*e?", "abcdef"));
assertTrue(IamPolicyEvaluator.caseSensitiveGlobMatches("**abc**", "xxabcyy"));
assertTrue(IamPolicyEvaluator.caseSensitiveGlobMatches("*", ""));
assertFalse(IamPolicyEvaluator.caseSensitiveGlobMatches("a?c", "ac"));
assertFalse(IamPolicyEvaluator.caseSensitiveGlobMatches("abc", "abcd"));
assertFalse(IamPolicyEvaluator.caseSensitiveGlobMatches(null, "abc"));
assertFalse(IamPolicyEvaluator.caseSensitiveGlobMatches("abc", null));
}
@Test