fix(iam): read a policy value that is no string as naming nothing (#8313)

An action, a resource and a principal are written as strings: the policy grammar
spells each one out, as an <action_string>, a <resource_string> and a
<principal_id_string>. The evaluator read an element that was no string as
naming no value when it stood on its own and as the text of that value when it
sat in a list, so "Resource": 123 named no resource while "Resource": [123]
named the pattern 123.

A list is read like the bare element now, and an entry that is no string names
nothing. It tells where the element carves an exception out, since a statement
matches on whichever of the two elements it names: a NotAction or a NotResource
whose only entry was a number excluded that one text and so covered every action
or every resource there is, which made an Allow a grant over everything and a
Deny a refusal of everything. Naming no value leaves the statement matching
neither.

Condition values keep the reading they had. The grammar's JSON rules say
quotation marks "are optional for numeric and Boolean values", a lone number or
boolean was read as the text of the value already, and a list of them is read
that way still, so the two readings sit under names that say which is which.

Naming no value is a shape the access report had to learn as well.
deniesEveryRequest read a Deny that names no resource as one that bites on
every request, where matchesResource reads the same statement as matching
nothing, so an allow-everything beside a Deny whose Resource carries only a
number took the service off the Organizations access report although every
request still reached it. The report reads a statement the way the decision
does now: a Deny covers every request only when its Resource names *.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-14 15:18:10 -04:00
1 parent 66bec3b89b
commit bbcc58f03b
3 files changed
+177 -12

No files matched your search

@@ -0,0 +1,116 @@
package io.floci.aws.services.iam;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.services.iam.IamPolicyEvaluator.Decision;
import io.floci.aws.services.iam.model.CallerContext;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* An action, a resource and a principal are written as strings, so a list entry that is no string
* names no value, which is what a bare non-string element already came to. It tells where the
* element carves an exception out: a {@code NotAction} or {@code NotResource} naming nothing
* excludes nothing, and a statement that excludes nothing would otherwise cover everything.
*/
class IamStatementPatternValuesTest {
private static final String RESOURCE = "arn:aws:s3:::b/k";
private final IamPolicyEvaluator evaluator = new IamPolicyEvaluator(new ObjectMapper());
private Decision decide(String document) {
return evaluator.evaluate(
CallerContext.of(List.of(document)), null, "s3:GetObject", RESOURCE, null);
}
@ParameterizedTest
@ValueSource(strings = {"123", "true", "null", "{}", "[\"arn:aws:s3:::b/k\"]"})
void aNotResourceEntryThatIsNoStringExcludesNothingAndCoversNothing(String entry) {
String document = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
+ "\"Action\":\"s3:*\",\"NotResource\":[" + entry + "]}]}";
assertEquals(Decision.DENY, decide(document));
}
@ParameterizedTest
@ValueSource(strings = {"123", "true", "null", "{}", "[\"s3:GetObject\"]"})
void aNotActionEntryThatIsNoStringLeavesTheStatementNamingNoAction(String entry) {
String document = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
+ "\"NotAction\":[" + entry + "],\"Resource\":\"*\"}]}";
assertEquals(Decision.DENY, decide(document));
}
@Test
void aDenyWhoseNotResourceNamesNothingDeniesNothing() {
String document = "{\"Version\":\"2012-10-17\",\"Statement\":["
+ "{\"Effect\":\"Allow\",\"Action\":\"s3:*\",\"Resource\":\"*\"},"
+ "{\"Effect\":\"Deny\",\"Action\":\"s3:*\",\"NotResource\":[123]}]}";
assertEquals(Decision.ALLOW, decide(document));
}
/**
* The report and the decision read one statement one way. A {@code Deny} naming no resource
* reaches no request, so the service it names stays reachable; reading the absence as every
* resource would take a service off the report that every request can still reach.
*/
@Test
void aDenyWhoseResourceNamesNothingTakesNoServiceOffTheReport() {
String document = "{\"Version\":\"2012-10-17\",\"Statement\":["
+ "{\"Effect\":\"Allow\",\"Action\":\"*\",\"Resource\":\"*\"},"
+ "{\"Effect\":\"Deny\",\"Action\":\"s3:*\",\"Resource\":[123]}]}";
assertEquals(Decision.ALLOW, decide(document));
assertTrue(evaluator.servicesAllowedByScpLevels(List.of(List.of(document))).contains("s3"));
}
@Test
void aDenyWhoseNotResourceNamesNothingTakesNoServiceOffTheReport() {
String document = "{\"Version\":\"2012-10-17\",\"Statement\":["
+ "{\"Effect\":\"Allow\",\"Action\":\"*\",\"Resource\":\"*\"},"
+ "{\"Effect\":\"Deny\",\"Action\":\"s3:*\",\"NotResource\":[123]}]}";
assertEquals(Decision.ALLOW, decide(document));
assertTrue(evaluator.servicesAllowedByScpLevels(List.of(List.of(document))).contains("s3"));
}
@Test
void aBareNonStringElementIsReadTheSameWayAsAListCarryingOne() {
String bare = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
+ "\"Action\":\"s3:*\",\"NotResource\":123}]}";
String listed = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
+ "\"Action\":\"s3:*\",\"NotResource\":[123]}]}";
assertEquals(decide(bare), decide(listed));
}
@Test
void theStringsBesideItAreStillNamed() {
String document = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
+ "\"Action\":[\"s3:GetObject\",17],\"Resource\":[\"" + RESOURCE + "\",17]}]}";
assertEquals(Decision.ALLOW, decide(document));
assertEquals(Decision.DENY, evaluator.evaluate(CallerContext.of(List.of(document)), null,
"s3:GetObject", "arn:aws:s3:::other/k", null));
}
@Test
void aConditionValueListStillReadsANumberAsTheTextOfTheValue() {
String document = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
+ "\"Action\":\"s3:*\",\"Resource\":\"*\",\"Condition\":{\"NumericLessThan\":"
+ "{\"aws:MultiFactorAuthAge\":[3600]}}}]}";
assertEquals(Decision.ALLOW, evaluator.simulateCustomPolicy(List.of(document),
"s3:GetObject", RESOURCE, Map.of("aws:MultiFactorAuthAge", List.of("1000"))));
assertEquals(Decision.DENY, evaluator.simulateCustomPolicy(List.of(document),
"s3:GetObject", RESOURCE, Map.of("aws:MultiFactorAuthAge", List.of("7200"))));
}
}