From cb03bc373be57070450c1f06c4436660d342d179 Mon Sep 17 00:00:00 2001 From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> Date: Sat, 14 Nov 2026 01:07:34 -0400 Subject: [PATCH] fix(iam): read a statement that names no effect as stating nothing (#8268) The policy evaluator defaulted a statement's Effect to Allow, so a statement that named none granted what it described. The IAM guide says the element "is required and specifies whether the statement results in an allow or an explicit deny" and that "valid values for Effect are Allow and Deny", so a statement naming neither describes no permission, and a document that describes no permission is not a document that grants one. A resource policy whose statement named a principal and an action but no effect allowed that call, and so did a session policy, a service control policy and an identity policy: a statement is held to naming an action and a resource when a document is stored, not to naming an effect, so such a document is kept as written and was then read as a grant. A statement names one of the two effects the policy language defines or it names none, and one that names none states nothing. An Effect that is absent, that is not a string, or that is some other word is read as neither now: the statement grants nothing, denies nothing, and the request is decided by the rest of the document. That is already this package's reading of a role trust policy, where a statement whose Effect names neither effect matches nothing. The service list behind GetOrganizationsAccessReport read the same default the other way round. Walking the service control policy levels, every statement that was not a Deny was counted as an allow, so a statement naming no effect, or naming some word that is neither, left a service reachable through a statement that permits nothing. Only an Allow counts there. No document is held to a shape here: what is stored is read exactly as it was, and this changes only what reading it comes to. --- docs/services/iam.md | 4 +- .../aws/services/iam/IamPolicyEvaluator.java | 9 +- .../services/iam/model/PolicyStatement.java | 2 +- .../services/iam/IamStatementEffectTest.java | 105 ++++++++++++++++++ 4 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 src/test/java/io/floci/aws/services/iam/IamStatementEffectTest.java diff --git a/docs/services/iam.md b/docs/services/iam.md index 9d00ae93c2..1d564baf35 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -2806,7 +2806,9 @@ account key carries no identity policies of its own. - **Conditions**: support for `Condition` blocks with multiple operators. - **Policy variables**: `${...}` placeholders in a `Resource` pattern or a `Condition` value, resolved from the request context. -- **Effects**: `Allow` and `Deny`. +- **Effects**: `Allow` and `Deny`. A statement whose `Effect` names neither, because it is absent, + is not a string, or is some other word, states nothing: it grants nothing, denies nothing, and + leaves the request to the rest of the document. #### Policy variables 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 4427cda2ca..d9a09aafb3 100644 --- a/src/main/java/io/floci/aws/services/iam/IamPolicyEvaluator.java +++ b/src/main/java/io/floci/aws/services/iam/IamPolicyEvaluator.java @@ -664,7 +664,7 @@ public class IamPolicyEvaluator { for (PolicyStatement stmt : parsed.statements()) { if (stmt.isDeny()) { denials.add(stmt); - } else { + } else if (stmt.isAllow()) { allows.add(stmt); } } @@ -1873,7 +1873,12 @@ public class IamPolicyEvaluator { } private PolicyStatement parseStatement(JsonNode stmt, String version) { - String effect = stmt.path("Effect").asText("Allow"); + // A statement states one of the two effects the policy language defines or it states + // nothing. An Effect that is absent, is not a string, or names neither is read as neither: + // the statement grants nothing, denies nothing, and leaves the request to the rest of the + // document. Reading an absent Effect as an Allow would grant on a statement that describes + // no permission. + String effect = stmt.path("Effect").isTextual() ? stmt.get("Effect").asText() : ""; Map> principals = parsePrincipals(stmt.get("Principal")); Map> notPrincipals = parsePrincipals(stmt.get("NotPrincipal")); // Action names are case-insensitive on AWS, so they are lowercased once here instead of diff --git a/src/main/java/io/floci/aws/services/iam/model/PolicyStatement.java b/src/main/java/io/floci/aws/services/iam/model/PolicyStatement.java index 01191c2972..b96cf133a8 100644 --- a/src/main/java/io/floci/aws/services/iam/model/PolicyStatement.java +++ b/src/main/java/io/floci/aws/services/iam/model/PolicyStatement.java @@ -10,7 +10,7 @@ import java.util.Map; */ public class PolicyStatement { - private final String effect; // "Allow" or "Deny" + private final String effect; // "Allow", "Deny", or neither, which states nothing private final Map> principals; // Principal patterns by type; null when not set private final Map> notPrincipals; // NotPrincipal patterns by type; null when not set private final List actions; // IAM action patterns; null when notActions is set diff --git a/src/test/java/io/floci/aws/services/iam/IamStatementEffectTest.java b/src/test/java/io/floci/aws/services/iam/IamStatementEffectTest.java new file mode 100644 index 0000000000..22ab1db174 --- /dev/null +++ b/src/test/java/io/floci/aws/services/iam/IamStatementEffectTest.java @@ -0,0 +1,105 @@ +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.IamPolicyEvaluator.ResourcePolicyDecision; +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 static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * A statement names one of the two effects the policy language defines or it names none, and one + * that names none states nothing: it grants nothing and denies nothing, whichever family of policy + * it was written in. A document that describes no permission must not be read as granting one. + */ +class IamStatementEffectTest { + + private final IamPolicyEvaluator evaluator = new IamPolicyEvaluator(new ObjectMapper()); + + private static String identityStatement(String effectElement) { + return "{\"Version\":\"2012-10-17\",\"Statement\":[{" + effectElement + + "\"Action\":\"s3:*\",\"Resource\":\"*\"}]}"; + } + + @Test + void aStatementCarryingNoEffectGrantsNothing() { + String document = identityStatement(""); + + assertEquals(Decision.DENY, evaluator.evaluate( + CallerContext.of(List.of(document)), null, "s3:GetObject", "arn:aws:s3:::b/k", null)); + } + + @ParameterizedTest + @ValueSource(strings = { + "\"Effect\":[\"Allow\"],", + "\"Effect\":{\"Allow\":true},", + "\"Effect\":1,", + "\"Effect\":true,", + "\"Effect\":null,", + }) + void anEffectThatIsNoStringNamesNeitherOfThem(String effectElement) { + String document = identityStatement(effectElement); + + assertEquals(Decision.DENY, evaluator.evaluate( + CallerContext.of(List.of(document)), null, "s3:GetObject", "arn:aws:s3:::b/k", null)); + } + + @ParameterizedTest + @ValueSource(strings = {"Permit", "Grant", "ALLOWED", "", " Allow"}) + void aWordThatIsNeitherEffectGrantsNothing(String effect) { + String document = identityStatement("\"Effect\":\"" + effect + "\","); + + assertEquals(Decision.DENY, evaluator.evaluate( + CallerContext.of(List.of(document)), null, "s3:GetObject", "arn:aws:s3:::b/k", null)); + } + + @Test + void aStatementThatStatesNothingLeavesTheRestOfTheDocumentReadable() { + String document = "{\"Version\":\"2012-10-17\",\"Statement\":[" + + "{\"Action\":\"*\",\"Resource\":\"*\"}," + + "{\"Effect\":\"Allow\",\"Action\":\"s3:GetObject\",\"Resource\":\"*\"}]}"; + + assertEquals(Decision.ALLOW, evaluator.evaluate( + CallerContext.of(List.of(document)), null, "s3:GetObject", "arn:aws:s3:::b/k", null)); + assertEquals(Decision.DENY, evaluator.evaluate( + CallerContext.of(List.of(document)), null, "s3:PutObject", "arn:aws:s3:::b/k", null)); + } + + @Test + void aResourcePolicyStatementCarryingNoEffectGrantsNothing() { + String bucketPolicy = "{\"Version\":\"2012-10-17\",\"Statement\":[{" + + "\"Principal\":\"*\",\"Action\":\"s3:*\",\"Resource\":\"*\"}]}"; + + assertEquals(ResourcePolicyDecision.NEUTRAL, evaluator.evaluateResourcePolicy( + List.of(bucketPolicy), "arn:aws:iam::123456789012:user/alice", "s3:GetObject", + "arn:aws:s3:::b/k", null)); + } + + @Test + void aSimulatedStatementCarryingNoEffectGrantsNothing() { + assertEquals(Decision.DENY, evaluator.simulateCustomPolicy( + List.of(identityStatement("")), "s3:GetObject", "arn:aws:s3:::b/k", null)); + } + + @Test + void anScpStatementCarryingNoEffectReachesNoService() { + List reachable = + evaluator.servicesAllowedByScpLevels(List.of(List.of(identityStatement("")))); + + assertTrue(reachable.isEmpty(), () -> "reached " + reachable); + } + + @Test + void anScpStatementThatAllowsIsStillRead() { + List reachable = evaluator.servicesAllowedByScpLevels( + List.of(List.of(identityStatement("\"Effect\":\"Allow\",")))); + + assertEquals(List.of("s3"), reachable); + } +}