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

@@ -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;
}
/**