fix(iam): compare numeric and boolean condition values (#5086)

* fix(iam): compare numeric and boolean condition values

A condition value written as a JSON number, such as
{"NumericLessThan":{"s3:max-keys":10}}, or as a boolean under an operator
other than Bool, parsed to an empty value list, so the condition never
matched. Compare both as strings, as IAM does.

* fix(iam): never match an unparsable numeric or date condition value

compareNumeric and compareDates returned 0 when a value failed to parse, so
NumericEquals and DateEquals matched a non-numeric or non-date value and an
Allow could grant access. Numeric values were also compared as doubles, so
integers beyond 2^53 collapsed together. Compare numbers as BigDecimal, and
treat a value that does not parse as not satisfying the operator.

* fix(iam): never match a null numeric or date condition value

* fix(iam): accept epoch seconds and W3C short date forms in date conditions

* fix(iam): read lowercase date-time separators in date conditions

* chore(iam): keep the policy-variable comment next to its pattern

* test(iam): cover Null conditions with unquoted boolean values
This commit is contained in:
Abhigya authored and GitHub committed 2026-10-05 11:51:17 -04:00
1 parent d508a4e197
commit 7d9e38cf56
3 files changed
+134 -22

No files matched your search

@@ -7,6 +7,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -81,6 +82,43 @@ class IamPolicyConditionMatchingTest {
assertEquals(negated, decision("NotIpAddress", List.of(range), List.of(sourceIp)));
}
@ParameterizedTest
@CsvSource({
"NumericEquals, 10, invalid, DENY",
"NumericNotEquals, 10, invalid, DENY",
"NumericLessThanEquals, 10, invalid, DENY",
"NumericEquals, 10, 10.0, ALLOW",
"NumericLessThanEquals, 9007199254740992, 9007199254740993, DENY",
"NumericGreaterThan, 9007199254740992, 9007199254740993, ALLOW",
"DateEquals, 2026-01-01T00:00:00Z, invalid, DENY",
"DateNotEquals, 2026-01-01T00:00:00Z, invalid, DENY",
"DateLessThan, 2026-01-01T00:00:00Z, 2025-12-31T23:59:59Z, ALLOW",
"DateEquals, 1893456000, 2030-01-01T00:00:00Z, ALLOW",
"DateEquals, 2030-01-01, 2030-01-01T00:00:00Z, ALLOW",
"DateEquals, 2030-01, 2030-01-01T00:00:00Z, ALLOW",
"DateEquals, 2030-01-01T00:00Z, 2030-01-01T00:00:00Z, ALLOW",
"DateEquals, 2030-01-01T05:30+05:30, 2030-01-01T00:00:00Z, ALLOW",
"DateEquals, 2030-01-01T00:00:00.000Z, 2030-01-01T00:00:00Z, ALLOW",
"DateGreaterThanEquals, 2020-01-01, 2026-10-05T04:58:46Z, ALLOW",
"DateLessThan, 2020-01-01, 1577836800, DENY",
"DateLessThan, 2020-01-01, 1577836799, ALLOW",
"DateEquals, 2030-01-01t00:00:00z, 2030-01-01T00:00:00Z, ALLOW",
"DateEquals, 2030-01-01T00:00, 2030-01-01T00:00:00Z, DENY",
"DateEquals, 2030-13-01, 2030-01-01T00:00:00Z, DENY",
"DateEquals, 99999999999999999999, 2030-01-01T00:00:00Z, DENY"
})
void numericAndDateConditionsCompareExactlyAndRejectUnparsableValues(String operator, String policyValue,
String requestValue, Decision expected) {
assertEquals(expected, decision(operator, List.of(policyValue), List.of(requestValue)));
}
@ParameterizedTest
@CsvSource({"NumericEquals, 10", "NumericNotEquals, 10", "DateEquals, 2026-01-01T00:00:00Z",
"DateNotEquals, 2026-01-01T00:00:00Z"})
void numericAndDateConditionsNeverMatchANullRequestValue(String operator, String policyValue) {
assertEquals(Decision.DENY, decision(operator, List.of(policyValue), Collections.singletonList(null)));
}
@Test
void setOperatorsRemainCaseSensitive() {
assertEquals(Decision.DENY, decision("ForAllValues:StringLike",
@@ -64,6 +64,38 @@ class IamPolicyEvaluatorTest {
assertFalse(IamPolicyEvaluator.isSupportedConditionOperator("forallvalues:StringEquals"));
}
@Test
void numericAndBooleanScalarConditionValuesAreCompared() {
String policy = """
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"s3:ListBucket",
"Resource":"*","Condition":{"NumericLessThanEquals":{"s3:max-keys":10},
"StringEquals":{"aws:MultiFactorAuthPresent":true}}}]}""";
Map<String, List<String>> within = Map.of("s3:max-keys", List.of("10"),
"aws:MultiFactorAuthPresent", List.of("true"));
Map<String, List<String>> over = Map.of("s3:max-keys", List.of("11"),
"aws:MultiFactorAuthPresent", List.of("true"));
assertEquals(Decision.ALLOW, evaluator.simulateCustomPolicy(List.of(policy), "s3:ListBucket", "*", within));
assertEquals(Decision.DENY, evaluator.simulateCustomPolicy(List.of(policy), "s3:ListBucket", "*", over));
}
@Test
void nullConditionReadsUnquotedBooleanValues() {
String requireAbsent = """
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"s3:ListBucket",
"Resource":"*","Condition":{"Null":{"aws:TokenIssueTime":true}}}]}""";
String requirePresent = """
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"s3:ListBucket",
"Resource":"*","Condition":{"Null":{"aws:TokenIssueTime":false}}}]}""";
Map<String, List<String>> absent = Map.of();
Map<String, List<String>> present = Map.of("aws:TokenIssueTime", List.of("2030-01-01T00:00:00Z"));
assertEquals(Decision.ALLOW, evaluator.simulateCustomPolicy(List.of(requireAbsent), "s3:ListBucket", "*", absent));
assertEquals(Decision.DENY, evaluator.simulateCustomPolicy(List.of(requireAbsent), "s3:ListBucket", "*", present));
assertEquals(Decision.DENY, evaluator.simulateCustomPolicy(List.of(requirePresent), "s3:ListBucket", "*", absent));
assertEquals(Decision.ALLOW, evaluator.simulateCustomPolicy(List.of(requirePresent), "s3:ListBucket", "*", present));
}
@Test
void wildcardMatchesLiteralAsteriskInValue() {
assertTrue(IamPolicyEvaluator.caseSensitiveGlobMatches("a*", "a*b"));