fix(iam): match condition values case-sensitively (#4672)

* fix(iam): match condition values case-sensitively

* chore(ci): retry transient registry pull failure
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-09-29 11:05:26 -04:00
1 parent 970bdf2e54
commit 33617e340d
4 files changed
+189 -6

No files matched your search

@@ -0,0 +1,71 @@
package com.floci.test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import software.amazon.awssdk.services.iam.IamClient;
import software.amazon.awssdk.services.iam.model.ContextEntry;
import software.amazon.awssdk.services.iam.model.ContextKeyTypeEnum;
import software.amazon.awssdk.services.iam.model.PolicyEvaluationDecisionType;
import software.amazon.awssdk.services.iam.model.SimulateCustomPolicyRequest;
import software.amazon.awssdk.services.iam.model.SimulateCustomPolicyResponse;
import static org.assertj.core.api.Assertions.assertThat;
@DisplayName("IAM condition matching through the policy simulator")
class IamConditionMatchingTest {
private static IamClient iam;
@BeforeAll
static void setup() {
iam = TestFixtures.iamClient();
}
@AfterAll
static void cleanup() {
if (iam != null) {
iam.close();
}
}
@ParameterizedTest
@CsvSource({
"StringLike, home/Alice/*, home/Alice/notes, ALLOWED",
"StringLike, home/Alice/*, home/alice/notes, IMPLICIT_DENY",
"StringNotLike, home/Alice/*, home/Alice/notes, IMPLICIT_DENY",
"StringNotLike, home/Alice/*, home/alice/notes, ALLOWED",
"ArnEquals, arn:aws:sns:*:*:Alerts, arn:aws:sns:us-east-1:111122223333:Alerts, ALLOWED",
"ArnEquals, arn:aws:sns:*:*:Alerts, arn:aws:sns:us-east-1:111122223333:alerts, IMPLICIT_DENY",
"ArnLike, arn:aws:sns:*:*:Alerts, arn:aws:sns:us-east-1:111122223333:alerts, IMPLICIT_DENY",
"ArnNotEquals, arn:aws:sns:*:*:Alerts, arn:aws:sns:us-east-1:111122223333:alerts, ALLOWED",
"ArnNotLike, arn:aws:sns:*:*:Alerts, arn:aws:sns:us-east-1:111122223333:alerts, ALLOWED",
"ArnLike, arn:aws:lambda:us-east-1:*:worker, arn:aws:lambda:us-east-1:111122223333:function:worker, IMPLICIT_DENY",
"ArnNotLike, arn:aws:lambda:us-east-1:*:worker, arn:aws:lambda:us-east-1:111122223333:function:worker, ALLOWED",
"ArnLike, arn:aws:lambda:us-east-1:*:function:worker:*, arn:aws:lambda:us-east-1:111122223333:function:worker:live, ALLOWED",
"ArnLike, arn:aws:s3:::Reports/*, arn:aws:s3:::Reports/quarter:a, ALLOWED"
})
void simulatorPreservesCaseAndArnComponentBoundaries(String operator, String pattern,
String value, PolicyEvaluationDecisionType expected) {
String policy = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"sqs:SendMessage","Resource":"*",
"Condition":{"%s":{"aws:SourceArn":"%s"}}}
]}
""".formatted(operator, pattern);
SimulateCustomPolicyResponse response = iam.simulateCustomPolicy(SimulateCustomPolicyRequest.builder()
.policyInputList(policy)
.actionNames("sqs:SendMessage")
.contextEntries(ContextEntry.builder()
.contextKeyName("aws:SourceArn")
.contextKeyValues(value)
.contextKeyType(ContextKeyTypeEnum.STRING)
.build())
.build());
assertThat(response.evaluationResults()).hasSize(1);
assertThat(response.evaluationResults().get(0).evalDecision()).isEqualTo(expected);
}
}
+5 -2
View File
@@ -636,8 +636,11 @@ account key carries no identity policies of its own.
#### Supported Condition Operators:
- `StringEquals`, `StringNotEquals`, `StringEqualsIgnoreCase`, `StringNotEqualsIgnoreCase`
- `StringLike`, `StringNotLike`
- `ArnEquals`, `ArnLike`, `ArnNotEquals`, `ArnNotLike`
- `StringLike`, `StringNotLike`: case-sensitive glob matching with `*` and `?`.
- `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.
- `NumericEquals`, `NumericNotEquals`, `NumericLessThan`, `NumericGreaterThan` (and Equals variants)
- `DateEquals`, `DateNotEquals`, `DateLessThan`, `DateGreaterThan` (and Equals variants)
- `Bool`, `IpAddress`, `NotIpAddress`, `Null`
@@ -968,10 +968,10 @@ public class IamPolicyEvaluator {
case "StringNotEquals" -> !ctxValue.equals(condValue);
case "StringEqualsIgnoreCase" -> ctxValue.equalsIgnoreCase(condValue);
case "StringNotEqualsIgnoreCase" -> !ctxValue.equalsIgnoreCase(condValue);
case "StringLike" -> globMatches(condValue, ctxValue);
case "StringNotLike" -> !globMatches(condValue, ctxValue);
case "ArnEquals", "ArnLike" -> globMatches(condValue, ctxValue);
case "ArnNotEquals", "ArnNotLike"-> !globMatches(condValue, ctxValue);
case "StringLike" -> caseSensitiveGlobMatches(condValue, ctxValue);
case "StringNotLike" -> !caseSensitiveGlobMatches(condValue, ctxValue);
case "ArnEquals", "ArnLike" -> matchesArnCondition(condValue, ctxValue);
case "ArnNotEquals", "ArnNotLike"-> !matchesArnCondition(condValue, ctxValue);
case "Bool" -> Boolean.parseBoolean(condValue) == Boolean.parseBoolean(ctxValue);
case "NumericEquals" -> compareNumeric(ctxValue, condValue) == 0;
case "NumericNotEquals" -> compareNumeric(ctxValue, condValue) != 0;
@@ -1053,6 +1053,28 @@ public class IamPolicyEvaluator {
return globMatchesHelper(pattern.toLowerCase(), value.toLowerCase(), 0, 0);
}
private static boolean caseSensitiveGlobMatches(String pattern, String value) {
return pattern != null && value != null && globMatchesHelper(pattern, value, 0, 0);
}
private static boolean matchesArnCondition(String pattern, String value) {
if (pattern == null || value == null) {
return false;
}
// The resource component may itself contain colons (for example Lambda qualifiers).
String[] patternComponents = pattern.split(":", 6);
String[] valueComponents = value.split(":", 6);
if (patternComponents.length != 6 || valueComponents.length != 6) {
return false;
}
for (int i = 0; i < patternComponents.length; i++) {
if (!caseSensitiveGlobMatches(patternComponents[i], valueComponents[i])) {
return false;
}
}
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);
@@ -0,0 +1,87 @@
package io.github.hectorvent.floci.services.iam;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.Decision;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class IamPolicyConditionMatchingTest {
private final IamPolicyEvaluator evaluator = new IamPolicyEvaluator(new ObjectMapper());
@ParameterizedTest
@CsvSource({
"StringLike, home/Alice/*, home/Alice/notes, ALLOW",
"StringLike, home/Alice/*, home/alice/notes, DENY",
"StringNotLike, home/Alice/*, home/Alice/notes, DENY",
"StringNotLike, home/Alice/*, home/alice/notes, ALLOW",
"StringLike, home/Alice/?.txt, home/Alice/a.txt, ALLOW",
"StringLike, home/Alice/?.txt, home/Alice/ab.txt, DENY",
"StringLike, home/Alice/*, home/Alice/a:b/notes, ALLOW",
"StringEqualsIgnoreCase, Alice, alice, ALLOW",
"StringNotEqualsIgnoreCase, Alice, alice, DENY",
"ArnEquals, arn:aws:sns:us-east-1:111122223333:Alerts, arn:aws:sns:us-east-1:111122223333:Alerts, ALLOW",
"ArnEquals, arn:aws:sns:us-east-1:111122223333:Alerts, arn:aws:sns:us-east-1:111122223333:alerts, DENY",
"ArnLike, arn:aws:sns:us-east-1:111122223333:Alerts, arn:aws:sns:us-east-1:111122223333:alerts, DENY",
"ArnNotEquals, arn:aws:sns:us-east-1:111122223333:Alerts, arn:aws:sns:us-east-1:111122223333:alerts, ALLOW",
"ArnNotLike, arn:aws:sns:us-east-1:111122223333:Alerts, arn:aws:sns:us-east-1:111122223333:alerts, ALLOW",
"ArnNotLike, arn:aws:sns:us-east-1:111122223333:Alerts, arn:aws:sns:us-east-1:111122223333:Alerts, DENY",
"ArnEquals, arn:aws:sns:*:111122223333:Alert?, arn:aws:sns:us-east-1:111122223333:Alerts, ALLOW",
"ArnLike, arn:aws:lambda:us-east-1:*:worker, arn:aws:lambda:us-east-1:111122223333:function:worker, DENY",
"ArnNotLike, arn:aws:lambda:us-east-1:*:worker, arn:aws:lambda:us-east-1:111122223333:function:worker, ALLOW",
"ArnLike, arn:aws:lambda:us-east-1:*:function:worker:*, arn:aws:lambda:us-east-1:111122223333:function:worker:live, ALLOW",
"ArnLike, arn:aws-*:sns:*:*:Alert?, arn:aws-us-gov:sns:us-gov-west-1:111122223333:Alerts, ALLOW",
"ArnLike, arn:aws:sns:*:*:Alert?, arn:aws:sns:us-east-1:111122223333:Alert, DENY"
})
void conditionMatchingPreservesCaseAndArnComponents(String operator, String pattern,
String value, Decision expected) {
assertEquals(expected, decision(operator, List.of(pattern), List.of(value)));
}
@Test
void setOperatorsRemainCaseSensitive() {
assertEquals(Decision.DENY, decision("ForAllValues:StringLike",
List.of("home/Alice/*"), List.of("home/Alice/notes", "home/alice/notes")));
assertEquals(Decision.ALLOW, decision("ForAnyValue:StringLike",
List.of("home/Alice/*"), List.of("home/Alice/notes", "home/alice/notes")));
assertEquals(Decision.DENY, decision("ForAnyValue:ArnLike",
List.of("arn:aws:sns:*:*:Alerts"), List.of("arn:aws:sns:us-east-1:111122223333:alerts")));
}
@Test
void negatedOperatorsRequireMismatchAgainstEveryPolicyValue() {
assertEquals(Decision.DENY, decision("ArnNotLike",
List.of("arn:aws:sns:*:*:Alerts", "arn:aws:sns:*:*:alerts"),
List.of("arn:aws:sns:us-east-1:111122223333:alerts")));
assertEquals(Decision.ALLOW, decision("ArnNotLike",
List.of("arn:aws:sns:*:*:Alerts", "arn:aws:sns:*:*:Warnings"),
List.of("arn:aws:sns:us-east-1:111122223333:alerts")));
}
@Test
void publicPrincipalGlobRemainsCaseInsensitive() {
assertTrue(IamPolicyEvaluator.globMatches("arn:aws:iam::*:user/Alice", "arn:aws:iam::111122223333:user/alice"));
}
private Decision decision(String operator, List<String> patterns, List<String> values) {
String policy;
try {
policy = new ObjectMapper().writeValueAsString(Map.of(
"Version", "2012-10-17",
"Statement", List.of(Map.of("Effect", "Allow", "Action", "s3:GetObject", "Resource", "*",
"Condition", Map.of(operator, Map.of("aws:SourceArn", patterns))))));
} catch (JsonProcessingException e) {
throw new IllegalStateException(e);
}
return evaluator.simulateCustomPolicy(List.of(policy), "s3:GetObject", "*",
Map.of("aws:SourceArn", values));
}
}