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); + } +}