diff --git a/docs/services/sts.md b/docs/services/sts.md index 00d39ef95a..9e0b485eeb 100644 --- a/docs/services/sts.md +++ b/docs/services/sts.md @@ -304,6 +304,11 @@ when its condition matches. The target role's tags do not supply the caller's principal tags. A chained request reads the current caller role's tags; tags on the user that originally assumed it are not inherited. +A trust policy with a statement whose `Effect` is not exactly `Allow` or `Deny`, or whose +`Condition` is not an object of non-empty operator objects, lets no one assume the role, the same +on `AssumeRole`, `AssumeRoleWithWebIdentity` and `AssumeRoleWithSAML`. IAM refuses such a document +when it is written; Floci stores it as given, so the whole document is refused when it is read. + ### Known limitations - **Session tags and source identity are not modelled.** `aws:RequestTag/*`, `aws:TagKeys`, diff --git a/src/main/java/io/floci/aws/services/iam/AssumeRolePolicyEvaluator.java b/src/main/java/io/floci/aws/services/iam/AssumeRolePolicyEvaluator.java index bb441e5555..672589a422 100644 --- a/src/main/java/io/floci/aws/services/iam/AssumeRolePolicyEvaluator.java +++ b/src/main/java/io/floci/aws/services/iam/AssumeRolePolicyEvaluator.java @@ -120,7 +120,7 @@ public class AssumeRolePolicyEvaluator { LOG.warnv("Failed to parse trust policy: {0}", e.getMessage()); return principals; } - if (!isWellFormed(statements)) { + if (!TrustPolicyStatements.wellFormed(statements)) { return principals; } Iterable statementList = statements.isArray() ? statements : List.of(statements); @@ -181,7 +181,7 @@ public class AssumeRolePolicyEvaluator { return TrustDecision.NEUTRAL; } JsonNode statements = root.path("Statement"); - if (!isWellFormed(statements)) { + if (!TrustPolicyStatements.wellFormed(statements)) { return TrustDecision.NEUTRAL; } String version = root.path("Version").asText(null); @@ -220,7 +220,7 @@ public class AssumeRolePolicyEvaluator { LOG.warnv("Failed to parse trust policy: {0}", e.getMessage()); return false; } - if (!isWellFormed(statements)) { + if (!TrustPolicyStatements.wellFormed(statements)) { return false; } boolean allow = false; @@ -257,7 +257,7 @@ public class AssumeRolePolicyEvaluator { return false; } JsonNode statements = root.path("Statement"); - if (!isWellFormed(statements)) { + if (!TrustPolicyStatements.wellFormed(statements)) { return false; } String version = root.path("Version").asText(null); @@ -280,50 +280,6 @@ public class AssumeRolePolicyEvaluator { private enum Match { ALLOW, DENY, NO_MATCH } - /** - * A statement without a valid {@code Effect}, or with a {@code Condition} that is not an object - * of non-empty operator objects, invalidates the whole document. IAM refuses such a document - * when it is written; Floci stores it as given, so it is refused here instead of being read as - * a statement with no condition. - */ - private boolean isWellFormed(JsonNode statements) { - if (statements.isArray()) { - if (statements.isEmpty()) { - return false; - } - for (JsonNode statement : statements) { - if (!validStatement(statement)) { - return false; - } - } - return true; - } - return validStatement(statements); - } - - private boolean validStatement(JsonNode statement) { - if (!statement.isObject()) { - return false; - } - String effect = statement.path("Effect").asText(""); - if (!"Allow".equals(effect) && !"Deny".equals(effect)) { - return false; - } - JsonNode condition = statement.get("Condition"); - if (condition == null) { - return true; - } - if (!condition.isObject() || condition.isEmpty()) { - return false; - } - for (JsonNode operator : condition) { - if (!operator.isObject() || operator.isEmpty()) { - return false; - } - } - return true; - } - private TrustDecision evaluateStatement(JsonNode stmt, String callerArn, String principalArn, String callerAccount, Map> context, String policyVersion) { diff --git a/src/main/java/io/floci/aws/services/iam/SAMLTrustPolicyEvaluator.java b/src/main/java/io/floci/aws/services/iam/SAMLTrustPolicyEvaluator.java index 009c717453..d3d197b077 100644 --- a/src/main/java/io/floci/aws/services/iam/SAMLTrustPolicyEvaluator.java +++ b/src/main/java/io/floci/aws/services/iam/SAMLTrustPolicyEvaluator.java @@ -22,6 +22,9 @@ public class SAMLTrustPolicyEvaluator { public boolean allows(String document, String providerArn, Map> claims) { try { JsonNode statements = mapper.readTree(document).path("Statement"); + if (!TrustPolicyStatements.wellFormed(statements)) { + return false; + } if (statements.isArray()) { boolean allowed = false; for (JsonNode statement : statements) { @@ -46,7 +49,7 @@ public class SAMLTrustPolicyEvaluator { || !conditionsSatisfied(statement.get("Condition"), claims)) { return 0; } - return "Deny".equalsIgnoreCase(statement.path("Effect").asText()) ? -1 : 1; + return TrustPolicyStatements.denies(statement) ? -1 : 1; } private boolean conditionsSatisfied(JsonNode condition, Map> claims) { diff --git a/src/main/java/io/floci/aws/services/iam/TrustPolicyStatements.java b/src/main/java/io/floci/aws/services/iam/TrustPolicyStatements.java new file mode 100644 index 0000000000..624cac5dca --- /dev/null +++ b/src/main/java/io/floci/aws/services/iam/TrustPolicyStatements.java @@ -0,0 +1,62 @@ +package io.floci.aws.services.iam; + +import com.fasterxml.jackson.databind.JsonNode; + +/** + * How a role's trust policy statements are read, the same way on every path that assumes the role: + * {@code sts:AssumeRole}, {@code sts:AssumeRoleWithWebIdentity} and {@code sts:AssumeRoleWithSAML}. + * + *

A statement without a valid {@code Effect}, or with a {@code Condition} that is not an object of + * non-empty operator objects, invalidates the whole document. IAM refuses such a document when it is + * written; Floci stores it as given, so it is refused here instead of being read as a statement with + * no condition, or as an allow. + */ +final class TrustPolicyStatements { + + private TrustPolicyStatements() { + } + + /** True when {@code statements}, the document's {@code Statement} element, can be read at all. */ + static boolean wellFormed(JsonNode statements) { + if (statements.isArray()) { + if (statements.isEmpty()) { + return false; + } + for (JsonNode statement : statements) { + if (!validStatement(statement)) { + return false; + } + } + return true; + } + return validStatement(statements); + } + + /** True when a statement of a {@linkplain #wellFormed well-formed} document is a {@code Deny}. */ + static boolean denies(JsonNode statement) { + return "Deny".equals(statement.path("Effect").asText()); + } + + private static boolean validStatement(JsonNode statement) { + if (!statement.isObject()) { + return false; + } + String effect = statement.path("Effect").asText(""); + if (!"Allow".equals(effect) && !"Deny".equals(effect)) { + return false; + } + JsonNode condition = statement.get("Condition"); + if (condition == null) { + return true; + } + if (!condition.isObject() || condition.isEmpty()) { + return false; + } + for (JsonNode operator : condition) { + if (!operator.isObject() || operator.isEmpty()) { + return false; + } + } + return true; + } +} diff --git a/src/main/java/io/floci/aws/services/iam/WebIdentityTrustPolicyEvaluator.java b/src/main/java/io/floci/aws/services/iam/WebIdentityTrustPolicyEvaluator.java index 67ff42c162..ce24229772 100644 --- a/src/main/java/io/floci/aws/services/iam/WebIdentityTrustPolicyEvaluator.java +++ b/src/main/java/io/floci/aws/services/iam/WebIdentityTrustPolicyEvaluator.java @@ -61,6 +61,9 @@ public class WebIdentityTrustPolicyEvaluator { LOG.warnv("Failed to parse web-identity trust policy: {0}", e.getMessage()); return false; } + if (!TrustPolicyStatements.wellFormed(statements)) { + return false; + } boolean allow = false; if (statements.isArray()) { @@ -92,7 +95,7 @@ public class WebIdentityTrustPolicyEvaluator { if (!conditionsSatisfied(stmt.get("Condition"), issuerKeyPrefix, claims)) { return Match.NO_MATCH; } - return "Deny".equalsIgnoreCase(stmt.path("Effect").asText("Allow")) ? Match.DENY : Match.ALLOW; + return TrustPolicyStatements.denies(stmt) ? Match.DENY : Match.ALLOW; } /** diff --git a/src/test/java/io/floci/aws/services/iam/TrustPolicyStatementsTest.java b/src/test/java/io/floci/aws/services/iam/TrustPolicyStatementsTest.java new file mode 100644 index 0000000000..2faacb1eee --- /dev/null +++ b/src/test/java/io/floci/aws/services/iam/TrustPolicyStatementsTest.java @@ -0,0 +1,96 @@ +package io.floci.aws.services.iam; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; + +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * One stored trust policy answers the same on every path that assumes the role: a statement whose {@code Effect} + * is not exactly {@code Allow} or {@code Deny}, or whose {@code Condition} is not an object of non-empty operator + * objects, refuses the whole document on {@code sts:AssumeRole}, {@code sts:AssumeRoleWithWebIdentity} and + * {@code sts:AssumeRoleWithSAML} alike. + */ +class TrustPolicyStatementsTest { + + private static final String ACCOUNT = "111111111111"; + private static final String USER = "arn:aws:iam::" + ACCOUNT + ":user/alice"; + private static final String OIDC_PREFIX = "oidc.eks.us-east-1.amazonaws.com/id/ABCDEF0123456789"; + private static final String OIDC_PROVIDER = "arn:aws:iam::" + ACCOUNT + ":oidc-provider/" + OIDC_PREFIX; + private static final String SAML_PROVIDER = "arn:aws:iam::" + ACCOUNT + ":saml-provider/corp"; + + private final ObjectMapper mapper = new ObjectMapper(); + private final AssumeRolePolicyEvaluator assumeRole = + new AssumeRolePolicyEvaluator(mapper, new IamPolicyEvaluator(mapper)); + private final WebIdentityTrustPolicyEvaluator webIdentity = new WebIdentityTrustPolicyEvaluator(mapper); + private final SAMLTrustPolicyEvaluator saml = new SAMLTrustPolicyEvaluator(mapper); + + /** A path that assumes a role, with the principal and action a statement names to reach it. */ + enum Path { + ASSUME_ROLE("{\"AWS\":\"" + USER + "\"}", "sts:AssumeRole"), + WEB_IDENTITY("{\"Federated\":\"" + OIDC_PROVIDER + "\"}", "sts:AssumeRoleWithWebIdentity"), + SAML("{\"Federated\":\"" + SAML_PROVIDER + "\"}", "sts:AssumeRoleWithSAML"); + + private final String principal; + private final String action; + + Path(String principal, String action) { + this.principal = principal; + this.action = action; + } + + /** A statement reaching this path, carrying {@code elements} (its Effect, a Condition) besides. */ + String statement(String elements) { + String rest = elements.isEmpty() ? "" : elements + ","; + return "{" + rest + "\"Principal\":" + principal + ",\"Action\":\"" + action + "\"}"; + } + } + + private boolean allows(Path path, String... statements) { + String document = "{\"Version\":\"2012-10-17\",\"Statement\":[" + String.join(",", statements) + "]}"; + return switch (path) { + case ASSUME_ROLE -> assumeRole.allows(document, new IamService.CallerArns(USER, USER), ACCOUNT, Map.of()); + case WEB_IDENTITY -> webIdentity.allows(document, OIDC_PROVIDER, OIDC_PREFIX, Map.of()); + case SAML -> saml.allows(document, SAML_PROVIDER, Map.of()); + }; + } + + @ParameterizedTest + @EnumSource(Path.class) + void anAllowGrantsAndADenyBesideItRefuses(Path path) { + assertTrue(allows(path, path.statement("\"Effect\":\"Allow\""))); + assertFalse(allows(path, path.statement("\"Effect\":\"Allow\""), path.statement("\"Effect\":\"Deny\""))); + } + + @ParameterizedTest + @EnumSource(Path.class) + void aStatementNamingNoEffectGrantsNothing(Path path) { + assertFalse(allows(path, path.statement(""))); + } + + @ParameterizedTest + @EnumSource(Path.class) + void anEffectThatIsNeitherEffectRefusesTheWholeDocument(Path path) { + String allow = path.statement("\"Effect\":\"Allow\""); + for (String effect : List.of("\"allow\"", "\"deny\"", "\"Permit\"", "\"\"", "1", "true", "null", + "[\"Allow\"]", "{\"Allow\":true}")) { + assertFalse(allows(path, allow, path.statement("\"Effect\":" + effect)), effect); + } + assertFalse(allows(path, allow, path.statement("")), "no Effect"); + } + + @ParameterizedTest + @EnumSource(Path.class) + void aMalformedConditionRefusesTheWholeDocument(Path path) { + String allow = path.statement("\"Effect\":\"Allow\""); + for (String condition : List.of("{}", "{\"StringEquals\":{}}", "{\"StringEquals\":\"x\"}", "\"x\"", "null")) { + assertFalse(allows(path, allow, path.statement("\"Effect\":\"Allow\",\"Condition\":" + condition)), + condition); + } + } +}