fix(iam): read a trust policy the same on every path that assumes the role (#8346)

AssumeRolePolicyEvaluator refuses a trust policy with a statement that
names no valid Effect, or whose Condition is not an object of non-empty
operator objects: IAM refuses such a document when it is written, and
Floci stores it as given, so the whole document is refused when it is
read. The two federated evaluators read the same stored document another
way. WebIdentityTrustPolicyEvaluator defaulted a missing Effect to Allow
and SAMLTrustPolicyEvaluator allowed on anything that was not a Deny, so
a statement naming a federated principal and no effect let
AssumeRoleWithWebIdentity and AssumeRoleWithSAML assume a role that
AssumeRole refused. An Effect spelled "allow" or "Permit" granted the
same way, and an empty Condition, or one whose operator was empty, was
read as no condition at all.

The reading moves into TrustPolicyStatements, and all three evaluators
use it, so one stored trust policy answers the same on each path: a
document carrying such a statement lets no one assume the role.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-14 15:13:28 -04:00
1 parent 3edf5f7415
commit 9cd601fea9
6 files changed
+175 -50

No files matched your search

+5
View File
@@ -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`,
@@ -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<JsonNode> 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<String, List<String>> context,
String policyVersion) {
@@ -22,6 +22,9 @@ public class SAMLTrustPolicyEvaluator {
public boolean allows(String document, String providerArn, Map<String, List<String>> 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<String, List<String>> claims) {
@@ -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}.
*
* <p>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;
}
}
@@ -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;
}
/**
@@ -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);
}
}
}