mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
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.
This commit is contained in:
1 parent
b658720afe
commit
cb03bc373b
4 files changed
+116
-4
No files matched your search
@@ -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
|
||||
|
||||
|
||||
@@ -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<String, List<String>> principals = parsePrincipals(stmt.get("Principal"));
|
||||
Map<String, List<String>> notPrincipals = parsePrincipals(stmt.get("NotPrincipal"));
|
||||
// Action names are case-insensitive on AWS, so they are lowercased once here instead of
|
||||
|
||||
@@ -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<String, List<String>> principals; // Principal patterns by type; null when not set
|
||||
private final Map<String, List<String>> notPrincipals; // NotPrincipal patterns by type; null when not set
|
||||
private final List<String> actions; // IAM action patterns; null when notActions is set
|
||||
|
||||
@@ -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<String> reachable =
|
||||
evaluator.servicesAllowedByScpLevels(List.of(List.of(identityStatement(""))));
|
||||
|
||||
assertTrue(reachable.isEmpty(), () -> "reached " + reachable);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anScpStatementThatAllowsIsStillRead() {
|
||||
List<String> reachable = evaluator.servicesAllowedByScpLevels(
|
||||
List.of(List.of(identityStatement("\"Effect\":\"Allow\","))));
|
||||
|
||||
assertEquals(List.of("s3"), reachable);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user