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

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