From bbcc58f03b85fd51cb9fd86bcc80cdc73d3ea988 Mon Sep 17 00:00:00 2001 From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> Date: Sat, 14 Nov 2026 15:18:10 -0400 Subject: [PATCH] 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 , a and a . 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 *. --- docs/services/iam.md | 10 +- .../aws/services/iam/IamPolicyEvaluator.java | 63 ++++++++-- .../iam/IamStatementPatternValuesTest.java | 116 ++++++++++++++++++ 3 files changed, 177 insertions(+), 12 deletions(-) create mode 100644 src/test/java/io/floci/aws/services/iam/IamStatementPatternValuesTest.java diff --git a/docs/services/iam.md b/docs/services/iam.md index ddedda7b8a..6ba31b40d1 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -1052,7 +1052,10 @@ on the one action a level allows leaves none either. What a namespace-wide readi right: an SCP denying `s3:DeleteBucket` alone keeps S3 on the list, because every other S3 action is still reachable, and a `Deny` that carries a `Condition` or names particular resources closes nothing at all, since the region-restricting deny on `*` that most organizations carry, and a deny scoped to -one bucket, both leave the service reachable by every request they do not cover. The survivors are +one bucket, both leave the service reachable by every request they do not cover. A `Deny` that names no +resource closes nothing either: a statement reaches through whichever resource element it names, so +one naming neither, because the element is absent or carries nothing but a value that is no string, +reaches no request and takes no service off the list. The survivors are found by probing the chain with the actions the policies themselves name plus one stand-in action per wildcard, which is exact for the patterns policies are written with; a wildcard that is not a trailing one, as in `s3:*Object`, cannot be probed that way and leaves the service on the list rather @@ -2847,7 +2850,10 @@ account key carries no identity policies of its own. - **Session policies**: inline policies passed during `sts:AssumeRole`. - **Permission boundaries**: managed policies used to cap maximum permissions. - **Action/Resource patterns**: literal matches, wildcards (`*`, `?`), and `NotAction`/`NotResource` blocks. - Action names match without regard to case, resource ARNs match case-sensitively, as on AWS. + Action names match without regard to case, resource ARNs match case-sensitively, as on AWS. Each + value is written as a string, so one that is not, in a list as much as on its own, names nothing: + a statement whose only `NotResource` entry is a number excludes nothing and matches nothing, + rather than excluding nothing and so covering everything. - **Conditions**: support for `Condition` blocks with multiple operators. - **Policy variables**: `${...}` placeholders in a `Resource` pattern or a `Condition` value, resolved from the request context. diff --git a/src/main/java/io/floci/aws/services/iam/IamPolicyEvaluator.java b/src/main/java/io/floci/aws/services/iam/IamPolicyEvaluator.java index d9a09aafb3..10bddb0e79 100644 --- a/src/main/java/io/floci/aws/services/iam/IamPolicyEvaluator.java +++ b/src/main/java/io/floci/aws/services/iam/IamPolicyEvaluator.java @@ -791,7 +791,14 @@ public class IamPolicyEvaluator { return false; } - /** Whether a {@code Deny} applies to every request, rather than to some of them. */ + /** + * Whether a {@code Deny} applies to every request, rather than to some of them. A statement + * reaches through whichever resource element it names, so one that names neither reaches no + * request at all: {@link #matchesResource} answers no for it, and this answers the same way + * rather than reading the absence as every resource. Taking an entry that is no string for no + * value is what leaves that shape, so the two readings have to agree, or a report would drop a + * service over a {@code Deny} that the decision it reports never applies. + */ private static boolean deniesEveryRequest(PolicyStatement stmt) { if (stmt.getConditions() != null && !stmt.getConditions().isEmpty()) { return false; @@ -800,7 +807,7 @@ public class IamPolicyEvaluator { return false; } List resources = stmt.getResources(); - return resources == null || resources.isEmpty() || resources.contains("*"); + return resources != null && resources.contains("*"); } /** Appends the key name inside every {@code ${key}} policy variable found in {@code values}. */ @@ -1883,10 +1890,10 @@ public class IamPolicyEvaluator { Map> notPrincipals = parsePrincipals(stmt.get("NotPrincipal")); // Action names are case-insensitive on AWS, so they are lowercased once here instead of // on every request. Resource ARNs are case-sensitive on AWS and are kept as written. - List actions = lowercaseAll(nodeToList(stmt.get("Action"))); - List notActions = lowercaseAll(nodeToList(stmt.get("NotAction"))); - List resources = nodeToList(stmt.get("Resource")); - List notResources= nodeToList(stmt.get("NotResource")); + List actions = lowercaseAll(patternValues(stmt.get("Action"))); + List notActions = lowercaseAll(patternValues(stmt.get("NotAction"))); + List resources = patternValues(stmt.get("Resource")); + List notResources= patternValues(stmt.get("NotResource")); Map>> conditions = parseConditions(stmt.get("Condition")); return new PolicyStatement( effect, @@ -1911,12 +1918,12 @@ public class IamPolicyEvaluator { return result; } if (node.isArray()) { - result.put("*", nodeToList(node)); + result.put("*", patternValues(node)); return result; } if (node.isObject()) { node.fields().forEachRemaining(entry -> { - List values = nodeToList(entry.getValue()); + List values = patternValues(entry.getValue()); if (!values.isEmpty()) { result.put(entry.getKey(), values); } @@ -1936,7 +1943,7 @@ public class IamPolicyEvaluator { opEntry.getValue().fields().forEachRemaining(kvEntry -> { JsonNode value = kvEntry.getValue(); kvMap.put(kvEntry.getKey(), value.isBoolean() || value.isNumber() - ? List.of(value.asText()) : nodeToList(value)); + ? List.of(value.asText()) : conditionValues(value)); }); result.put(opEntry.getKey(), kvMap); }); @@ -1951,7 +1958,43 @@ public class IamPolicyEvaluator { return lowered; } - private List nodeToList(JsonNode node) { + /** + * The values a pattern element names. The grammar writes each of them as a string: an action is + * an {@code }, a resource a {@code } and a principal a + * {@code }, so an entry that is no string names no value and is left out. + * A bare non-string element was read that way already, while a list carrying one took its text + * for a pattern, so {@code "Resource": 123} named no resource and {@code "Resource": [123]} + * named the pattern {@code 123}. The list is read like the bare element now. + * + *

It tells most where a pattern carves an exception out, since the element the statement + * matches on is whichever of the two it names: a {@code NotAction} or {@code NotResource} whose + * entries name nothing excludes nothing, and a statement excluding nothing covers every action + * or every resource there is. Naming no value leaves the statement matching neither instead. + */ + private List patternValues(JsonNode node) { + List values = new ArrayList<>(); + if (node == null) { + return values; + } + if (node.isTextual()) { + values.add(node.asText()); + } else if (node.isArray()) { + for (JsonNode item : node) { + if (item.isTextual()) { + values.add(item.asText()); + } + } + } + return values; + } + + /** + * The values a condition element names, where a number and a boolean are values of their own: + * the grammar's JSON rules say quotation marks "are optional for numeric and Boolean values", + * and an operator reads what it is given as the text of the value either way. A lone number or + * boolean is read by the caller, so what is left here is the list a key may carry. + */ + private List conditionValues(JsonNode node) { List list = new ArrayList<>(); if (node == null) { return list; diff --git a/src/test/java/io/floci/aws/services/iam/IamStatementPatternValuesTest.java b/src/test/java/io/floci/aws/services/iam/IamStatementPatternValuesTest.java new file mode 100644 index 0000000000..b609ab57af --- /dev/null +++ b/src/test/java/io/floci/aws/services/iam/IamStatementPatternValuesTest.java @@ -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")))); + } +}