feat(iam): resolve policy variables in Resource patterns and Condition values (#5639)

A ${...} in a Resource pattern or a Condition value was matched as the text it
is written as, so a statement scoped to arn:aws:iam::*:user/${aws:username}
matched no user at all. AWS's own IAMUserChangePassword policy is written that
way and floci vendors it verbatim, so under enforcement it authorized nothing,
and it is not alone: 244 of the 1586 vendored managed policy documents carry a
variable, and of the 1635 occurrences in them 1443 sit in a Condition value
and 192 in a Resource. The Condition share is where the loss bites hardest,
since a variable that cannot resolve quietly drops a Deny that AWS honours.

The keys a variable resolves against arrived with #5568, which put
aws:username, aws:userid and aws:PrincipalType into the request context. This
is the other half of that work: without substitution the keys answer nobody,
and without the keys substitution has nothing to find.

Variables now resolve wherever the evaluator matches a statement, which covers
every document it reads: identity policies, resource policies, session
policies, permissions boundaries, SCPs, and the Condition of a role trust
policy, whose own Version now reaches the condition check. A trust policy is
the narrow case, since sts:AssumeRole is authorized before there is a session
to describe: that context carries aws:PrincipalArn, the account and region
keys, sts:RoleSessionName and sts:ExternalId, so a variable naming
aws:username, aws:userid or aws:PrincipalType finds nothing there, which
iam.md now says.

Two of the rules are easy to miss. Substitution is gated on "Version":
"2012-10-17", because under the older 2008-10-17, which is also the default
when the element is absent, the user guide says variables "are instead treated
as literal strings in the policy"; the version therefore rides on each parsed
statement, since statements from many documents are evaluated as one list. And
a variable with no value is not an empty string: in Resource the pattern "will
not match any resource", in NotResource that same rule excludes nothing, and
in a Condition the positive operators do not match while the inverted ones do,
which is what makes the Deny AWS documents for an unset principal tag fire.

The rest, also from the user guide: a variable resolves only in the resource
portion of an ARN, after the fifth colon; only with the string and ARN
condition operators, never Numeric, Date, Bool, IpAddress or Null; only for a
single-valued key, a multivalued one having no one value to stand in, which a
default does not rescue, since a default answers a key with no value rather
than one with several; and case-insensitively by key name, a name that holds
no space, the guide allowing one inside a quoted default and nowhere else. A
spaced name, an empty ${}, a missing brace and an unquoted default are the
forms the guide's policy checks call errors, and each stays the text it is.
${*}, ${?} and ${$} stand for a literal character, so the glob gained an
escape marker, honoured only on a pattern that came back from substitution and
stripped from whatever arrived in the policy: a document can write the marker
as a JSON escape, and reading it as one would turn a wildcard of the
document's own into a literal character and narrow the statement it sits in. A
resolved value is not escaped, matching AWS, whose policy checks warn that
variables "can be evaluated as effective wildcards".

Verified at this commit: 7442 tests across the 439 IAM, core, enforcement and
resource-policy classes the evaluator is reached from, Checkstyle and
docs-check clean. AWS's own IAMUserChangePassword document is attached to a
user and called on the request path, both of its patterns included, the second
scoping a user under a path. Each rule was probed by reverting it alone, which
is how two weak tests were found: a valueless variable beside a wildcard and
the operator restriction both needed a case whose answer flips. The escape
marker is probed from both sides, by letting the resolver keep one that
arrived in a policy and by honouring markers on every pattern rather than only
a substituted one, and each side fails a different case of the same test.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-14 22:22:06 -05:00
1 parent a55352bf9d
commit 425cd3167d
8 files changed
+1213 -58

No files matched your search

@@ -390,6 +390,42 @@ class AssumeRolePolicyEvaluatorTest {
"arn:aws:appsync:us-east-1:111111111111:apis/example", "111111111111"));
}
/**
* A trust policy is a resource policy, and those take policy variables too. The caller's own
* account is one of the keys the request carries, so a session name can be held to it.
*/
@Test
void aTrustConditionResolvesAPolicyVariable() {
String doc = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Principal":{"AWS":"111111111111"},"Action":"sts:AssumeRole",
"Condition":{"StringLike":{"sts:RoleSessionName":"${aws:PrincipalAccount}-*"}}}]}
""";
assertTrue(evaluator.allows(doc, CALLER_ARN, CALLER_ARN, CALLER_ACCOUNT,
sessionNamed(CALLER_ACCOUNT + "-build")));
assertFalse(evaluator.allows(doc, CALLER_ARN, CALLER_ARN, CALLER_ACCOUNT,
sessionNamed("222222222222-build")));
}
/** The same condition under the version that predates variables compares the text itself. */
@Test
void aTrustConditionVariableStaysLiteralWithoutTheVersion() {
String doc = """
{"Statement":[
{"Effect":"Allow","Principal":{"AWS":"111111111111"},"Action":"sts:AssumeRole",
"Condition":{"StringLike":{"sts:RoleSessionName":"${aws:PrincipalAccount}-*"}}}]}
""";
assertFalse(evaluator.allows(doc, CALLER_ARN, CALLER_ARN, CALLER_ACCOUNT,
sessionNamed(CALLER_ACCOUNT + "-build")));
assertTrue(evaluator.allows(doc, CALLER_ARN, CALLER_ARN, CALLER_ACCOUNT,
sessionNamed("${aws:PrincipalAccount}-build")));
}
private static Map<String, List<String>> sessionNamed(String sessionName) {
return Map.of("aws:PrincipalAccount", List.of(CALLER_ACCOUNT),
"sts:RoleSessionName", List.of(sessionName));
}
@Test
void aGrantNamingTheCallerIsToldApartFromAGrantToItsAccount() {
assertEquals(TrustDecision.ALLOW, evaluator.decide(trust("{\"AWS\":\"" + CALLER_ARN + "\"}"),
@@ -0,0 +1,236 @@
package io.github.hectorvent.floci.services.iam;
import io.github.hectorvent.floci.testing.EnforcementFixtures;
import io.github.hectorvent.floci.testing.IamEnforcementProfile;
import io.github.hectorvent.floci.testing.PartitionCleanup;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.restassured.response.ValidatableResponse;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import java.net.URI;
import java.util.Map;
import java.util.UUID;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
/**
* A policy variable resolved on the real request path, rather than against a context handed
* straight to the evaluator. {@code aws:PrincipalAccount} is one of the keys the enforcement
* filter already builds for every request, and a bucket named after the account that owns it is
* what AWS's own examples scope this way.
*
* <p>Runs in an account of its own: the emulator is shared, and both buckets here are named after
* an account id, which is the whole point of the grant being tested.
*/
@QuarkusTest
@TestProfile(IamEnforcementProfile.class)
class PolicyVariableEnforcementIntegrationTest {
private static final String ACCOUNT_ID = "000000000777";
private static final String OTHER_ACCOUNT_ID = "999988887777";
private static final String PASSWORD = "Sup3r$ecret!";
private static final String NEW_PASSWORD = "An0ther$ecret!";
@RegisterExtension
final PartitionCleanup cleanup = new PartitionCleanup();
@TestHTTPResource("/")
URI baseUri;
/** Allows listing the one bucket whose name ends in the caller's own account id. */
private static String allowingOwnAccountBucket(String version, String prefix) {
return """
{"Version":"%s","Statement":[
{"Effect":"Allow","Action":"s3:ListBucket",
"Resource":"arn:aws:s3:::%s-${aws:PrincipalAccount}"}]}"""
.formatted(version, prefix);
}
@Test
void aResourceVariableBindsTheGrantToTheCallersOwnAccount() {
String prefix = "policy-vars-" + suffix();
String own = createBucket(prefix + "-" + ACCOUNT_ID);
String other = createBucket(prefix + "-" + OTHER_ACCOUNT_ID);
String accessKeyId = userWith(allowingOwnAccountBucket("2012-10-17", prefix));
list(accessKeyId, own).statusCode(200);
list(accessKeyId, other).statusCode(403).body(containsString("AccessDenied"));
}
/**
* The same policy under the version that predates variables, where {@code ${aws:...}} is the
* text it is written as and names no bucket at all.
*/
@Test
void theSameGrantUnderTheOlderVersionNamesNoBucket() {
String prefix = "literal-vars-" + suffix();
String own = createBucket(prefix + "-" + ACCOUNT_ID);
String literal = userWith(allowingOwnAccountBucket("2008-10-17", prefix));
list(literal, own).statusCode(403).body(containsString("AccessDenied"));
// The bucket is reachable with the account spelled out, so it is the variable that is
// inert under the older version and not the grant or the bucket.
String spelledOut = userWith("""
{"Version":"2008-10-17","Statement":[
{"Effect":"Allow","Action":"s3:ListBucket","Resource":"arn:aws:s3:::%s"}]}"""
.formatted(own));
list(spelledOut, own).statusCode(200);
}
/**
* The policy the change exists for: AWS's own {@code IAMUserChangePassword}, attached as the
* managed policy floci vendors rather than retyped, so a drift in what is vendored fails here
* too. It scopes {@code iam:ChangePassword} to {@code arn:aws:iam::*:user/${aws:username}} and
* to {@code arn:aws:iam::*:user/*}{@code /${aws:username}}, the second for a user under a
* path, so both forms are called.
*/
@Test
void theVendoredChangePasswordPolicyBindsToTheCallersOwnUser() {
Caller flat = consoleUser(null);
attachChangePassword(flat.userName());
changePassword(flat.accessKeyId(), PASSWORD, NEW_PASSWORD).statusCode(200);
Caller underAPath = consoleUser("/eng/");
attachChangePassword(underAPath.userName());
changePassword(underAPath.accessKeyId(), PASSWORD, NEW_PASSWORD).statusCode(200);
// Without the policy the same call is refused, so it is the grant that resolves the
// variable and not enforcement letting the operation through.
Caller ungranted = consoleUser(null);
changePassword(ungranted.accessKeyId(), PASSWORD, NEW_PASSWORD)
.statusCode(403).body(containsString("AccessDenied"));
}
/**
* A permissions boundary resolves variables as an identity policy does, which `iam.md` claims
* for every document the evaluator reads. The user may list any bucket; the boundary narrows
* that to the one named after its own account.
*/
@Test
void aPermissionsBoundaryVariableNarrowsTheGrant() {
String prefix = "pv-boundary-" + suffix();
String own = createBucket(prefix + "-" + ACCOUNT_ID);
String other = createBucket(prefix + "-" + OTHER_ACCOUNT_ID);
String userName = "pv-bounded-" + suffix();
String accessKeyId = EnforcementFixtures.createUser(cleanup, port(),
EnforcementFixtures.auth(ACCOUNT_ID, "iam"), userName).accessKeyId();
iam("PutUserPolicy", Map.of("UserName", userName, "PolicyName", "wide", "PolicyDocument", """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"s3:ListBucket","Resource":"*"}]}"""))
.statusCode(200);
String boundaryArn = iam("CreatePolicy", Map.of("PolicyName", "pv-boundary-" + suffix(),
"PolicyDocument", allowingOwnAccountBucket("2012-10-17", prefix)))
.statusCode(200)
.extract().path("CreatePolicyResponse.CreatePolicyResult.Policy.Arn");
cleanup.register(() -> EnforcementFixtures.removePolicy(port(),
EnforcementFixtures.auth(ACCOUNT_ID, "iam"), boundaryArn));
iam("PutUserPermissionsBoundary",
Map.of("UserName", userName, "PermissionsBoundary", boundaryArn)).statusCode(200);
cleanup.register(() -> iam("DeleteUserPermissionsBoundary",
Map.of("UserName", userName)).statusCode(200));
list(accessKeyId, own).statusCode(200);
list(accessKeyId, other).statusCode(403).body(containsString("AccessDenied"));
}
/** A user name with the access key that signs for it. */
private record Caller(String userName, String accessKeyId) {}
/** An IAM user holding a console password, under {@code path} when one is given. */
private Caller consoleUser(String path) {
String userName = "pv-cp-" + suffix();
String auth = EnforcementFixtures.auth(ACCOUNT_ID, "iam");
cleanup.register(() -> EnforcementFixtures.removeUser(port(), auth, userName));
iam("CreateUser", path == null
? Map.of("UserName", userName)
: Map.of("UserName", userName, "Path", path)).statusCode(200);
iam("CreateLoginProfile", Map.of("UserName", userName, "Password", PASSWORD))
.statusCode(200);
cleanup.register(() -> iam("DeleteLoginProfile", Map.of("UserName", userName))
.statusCode(200));
return new Caller(userName, iam("CreateAccessKey", Map.of("UserName", userName))
.statusCode(200)
.extract().path("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey"
+ ".AccessKeyId"));
}
private void attachChangePassword(String userName) {
iam("AttachUserPolicy", Map.of("UserName", userName,
"PolicyArn", "arn:aws:iam::aws:policy/IAMUserChangePassword")).statusCode(200);
}
private static ValidatableResponse changePassword(String accessKeyId, String oldPassword,
String newPassword) {
return given()
.header("Authorization", EnforcementFixtures.auth(accessKeyId, "iam"))
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "ChangePassword")
.formParam("Version", "2010-05-08")
.formParam("OldPassword", oldPassword)
.formParam("NewPassword", newPassword)
.when()
.post("/")
.then();
}
private String createBucket(String bucket) {
cleanup.register(() -> EnforcementFixtures.removeBucket(port(), s3Auth(ACCOUNT_ID), bucket));
given()
.header("Authorization", s3Auth(ACCOUNT_ID))
.when()
.put("/" + bucket)
.then()
.statusCode(200);
return bucket;
}
/** An IAM user in {@link #ACCOUNT_ID} carrying {@code policy} inline. */
private String userWith(String policy) {
String user = "policy-vars-" + suffix();
String accessKeyId = EnforcementFixtures.createUser(cleanup, port(),
EnforcementFixtures.auth(ACCOUNT_ID, "iam"), user).accessKeyId();
iam("PutUserPolicy", Map.of("UserName", user, "PolicyName", "p", "PolicyDocument", policy))
.statusCode(200);
return accessKeyId;
}
private static ValidatableResponse list(String accessKeyId, String bucket) {
return given()
.header("Authorization", s3Auth(accessKeyId))
.queryParam("list-type", "2")
.when()
.get("/" + bucket)
.then();
}
private static ValidatableResponse iam(String action, Map<String, String> params) {
RequestSpecification spec = given()
.header("Authorization", EnforcementFixtures.auth(ACCOUNT_ID, "iam"))
.contentType("application/x-www-form-urlencoded")
.formParam("Action", action)
.formParam("Version", "2010-05-08");
params.forEach(spec::formParam);
return spec.when().post("/").then();
}
private static String s3Auth(String accessKeyId) {
return EnforcementFixtures.auth(accessKeyId, "s3");
}
private int port() {
return baseUri.getPort();
}
private static String suffix() {
return UUID.randomUUID().toString().substring(0, 8);
}
}
@@ -0,0 +1,499 @@
package io.github.hectorvent.floci.services.iam;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.Decision;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Policy variables: the {@code ${...}} placeholders a policy writes where it cannot know a value
* until the request arrives. The evaluator resolved none of them before, so a statement scoped to
* {@code user/${aws:username}} matched no user at all, which is how AWS writes its own
* {@code IAMUserChangePassword} policy and how a great many of the managed policies Floci vendors
* scope themselves to the caller's account.
*
* <p>Both directions are checked for every rule, because the half that matters is often the Deny:
* a pattern that matches nothing silently drops a refusal AWS honours.
*/
class PolicyVariableSubstitutionTest {
private static final String OWN_USER = "arn:aws:iam::111122223333:user/Alice";
private static final String OTHER_USER = "arn:aws:iam::111122223333:user/Bob";
private final IamPolicyEvaluator evaluator = new IamPolicyEvaluator(new ObjectMapper());
private static String allowingUserResource(String version, String resource) {
return """
{"Version":"%s","Statement":[
{"Effect":"Allow","Action":"iam:ChangePassword","Resource":"%s"}]}"""
.formatted(version, resource);
}
@Test
void aResourceVariableResolvesToTheCallersOwnUser() {
String policy = allowingUserResource("2012-10-17", "arn:aws:iam::*:user/${aws:username}");
assertEquals(Decision.ALLOW, decide(policy, OWN_USER, Map.of("aws:username", List.of("Alice"))));
assertEquals(Decision.DENY, decide(policy, OTHER_USER, Map.of("aws:username", List.of("Alice"))));
}
/**
* The version gate, which is easy to miss and changes everything: "If you don't include the
* Version element and set it to an appropriate version date, variables like ${aws:username}
* are treated as literal strings in the policy." 2008-10-17 is the only other version IAM
* accepts, and is the default when the element is absent.
*/
@Test
void aVariableStaysLiteralTextWithoutTheVersionThatIntroducedThem() {
Map<String, List<String>> context = Map.of("aws:username", List.of("Alice"));
String resource = "arn:aws:iam::*:user/${aws:username}";
assertEquals(Decision.DENY, decide(allowingUserResource("2008-10-17", resource), OWN_USER, context));
assertEquals(Decision.DENY, decide("""
{"Statement":[
{"Effect":"Allow","Action":"iam:ChangePassword","Resource":"%s"}]}"""
.formatted(resource), OWN_USER, context));
// The same document with the name spelled out is allowed either way, so it is the
// variable that is inert under the older version and not the statement.
assertEquals(Decision.ALLOW, decide(
allowingUserResource("2008-10-17", OWN_USER), OWN_USER, context));
}
/** A Deny scoped by a variable has to bind, or the policy refuses nothing. */
@Test
void aDenyScopedByAResourceVariableRefusesTheCallersOwnUser() {
String policy = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"iam:*","Resource":"*"},
{"Effect":"Deny","Action":"iam:ChangePassword",
"Resource":"arn:aws:iam::*:user/${aws:username}"}]}""";
Map<String, List<String>> context = Map.of("aws:username", List.of("Alice"));
assertEquals(Decision.DENY, decide(policy, OWN_USER, context));
assertEquals(Decision.ALLOW, decide(policy, OTHER_USER, context));
}
/**
* "If a variable that has no value in the authorization context is used as part of the
* Resource or NotResource element of a policy, the resource that includes a policy variable
* with no value will not match any resource."
*/
@Test
void aResourceVariableWithNoValueMatchesNoResource() {
String policy = allowingUserResource("2012-10-17", "arn:aws:iam::*:user/${aws:username}");
assertEquals(Decision.DENY, decide(policy, OWN_USER, Map.of()));
assertEquals(Decision.DENY, decide(policy, OWN_USER, Map.of("aws:username", List.of())));
}
/**
* The discriminating half of that rule: a variable with no value is not an empty string. Beside
* a wildcard an empty one would collapse the pattern to {@code reports/*} and grant the whole
* prefix, where AWS matches no resource at all.
*/
@Test
void aResourceVariableWithNoValueMatchesNothingEvenBesideAWildcard() {
String policy = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"s3:GetObject",
"Resource":"arn:aws:s3:::reports/${aws:PrincipalTag/team}*"}]}""";
assertEquals(Decision.DENY, decideS3(policy, "arn:aws:s3:::reports/blue/q3.csv", Map.of()));
assertEquals(Decision.ALLOW, decideS3(policy, "arn:aws:s3:::reports/blue/q3.csv",
Map.of("aws:principaltag/team", List.of("blue"))));
}
/**
* The same sentence covers NotResource, where it reads the other way round: the pattern
* excludes no resource, so the statement covers every one of them.
*/
@Test
void aNotResourceVariableWithNoValueExcludesNothing() {
String policy = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"iam:ChangePassword",
"NotResource":"arn:aws:iam::*:user/${aws:username}"}]}""";
assertEquals(Decision.ALLOW, decide(policy, OWN_USER, Map.of()));
// With a value it excludes the one user it names, and nobody else.
assertEquals(Decision.DENY, decide(policy, OWN_USER, Map.of("aws:username", List.of("Alice"))));
assertEquals(Decision.ALLOW, decide(policy, OTHER_USER, Map.of("aws:username", List.of("Alice"))));
}
/** The same, in a NotResource, where an empty value would exclude the whole prefix instead. */
@Test
void aNotResourceVariableWithNoValueExcludesNothingEvenBesideAWildcard() {
String policy = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"s3:GetObject",
"NotResource":"arn:aws:s3:::reports/${aws:PrincipalTag/team}*"}]}""";
assertEquals(Decision.ALLOW, decideS3(policy, "arn:aws:s3:::reports/blue/q3.csv", Map.of()));
assertEquals(Decision.DENY, decideS3(policy, "arn:aws:s3:::reports/blue/q3.csv",
Map.of("aws:principaltag/team", List.of("blue"))));
}
/**
* "You can use a policy variable in the Resource element, but only in the resource portion of
* the ARN. This portion of the ARN appears after the fifth colon." An account written as a
* variable is therefore the literal text, and matches no account.
*/
@Test
void aVariableBeforeTheFifthColonIsNotSubstituted() {
Map<String, List<String>> context = Map.of("aws:principalaccount", List.of("111122223333"),
"aws:username", List.of("Alice"));
assertEquals(Decision.DENY, decide(allowingUserResource("2012-10-17",
"arn:aws:iam::${aws:PrincipalAccount}:user/Alice"), OWN_USER, context));
// The resource portion of the same ARN still resolves, so the pattern is not refused whole.
assertEquals(Decision.ALLOW, decide(allowingUserResource("2012-10-17",
"arn:aws:iam::111122223333:user/${aws:username}"), OWN_USER, context));
}
/** "Key names are case-insensitive", and the request context is keyed in lower case. */
@Test
void variableKeyNamesAreCaseInsensitive() {
assertEquals(Decision.ALLOW, decide(allowingUserResource("2012-10-17",
"arn:aws:iam::*:user/${AWS:UserName}"), OWN_USER,
Map.of("aws:username", List.of("Alice"))));
}
/** "You can't use a multivalued condition key as a variable": there is no one value to use. */
@Test
void aMultiValuedKeyDoesNotResolve() {
assertEquals(Decision.DENY, decide(allowingUserResource("2012-10-17",
"arn:aws:iam::*:user/${aws:username}"), OWN_USER,
Map.of("aws:username", List.of("Alice", "Bob"))));
// Nor does a default rescue it: the guide gives a default the key with no value, not the
// one holding several, so the pattern resolves to nothing rather than widening.
assertEquals(Decision.DENY, decide(allowingUserResource("2012-10-17",
"arn:aws:iam::*:user/${aws:username, 'Alice'}"), OWN_USER,
Map.of("aws:username", List.of("Alice", "Bob"))));
}
/** A tag variable, which is the form AWS's own examples use most. */
@Test
void aPrincipalTagVariableResolvesInBothElements() {
String policy = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"s3:GetObject",
"Resource":"arn:aws:s3:::reports/${aws:PrincipalTag/team}/*"}]}""";
assertEquals(Decision.ALLOW, decideS3(policy, "arn:aws:s3:::reports/yellow/q3.csv",
Map.of("aws:principaltag/team", List.of("yellow"))));
assertEquals(Decision.DENY, decideS3(policy, "arn:aws:s3:::reports/blue/q3.csv",
Map.of("aws:principaltag/team", List.of("yellow"))));
}
/**
* "To add a default value to a variable, surround the default value with single quotes (' '),
* and separate the variable text and the default value with a comma and space (, )."
*/
@Test
void aDefaultValueStandsInWhenTheKeyHasNone() {
String policy = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"s3:GetObject",
"Resource":"arn:aws:s3:::reports-${aws:PrincipalTag/team, 'company-wide'}/*"}]}""";
assertEquals(Decision.ALLOW, decideS3(policy, "arn:aws:s3:::reports-company-wide/joining.txt",
Map.of()));
assertEquals(Decision.DENY, decideS3(policy, "arn:aws:s3:::reports-company-wide/joining.txt",
Map.of("aws:principaltag/team", List.of("yellow"))));
assertEquals(Decision.ALLOW, decideS3(policy, "arn:aws:s3:::reports-yellow/q3.csv",
Map.of("aws:principaltag/team", List.of("yellow"))));
}
/**
* A default value runs to its closing quote, so one carrying a brace stays whole. Without the
* quotes the guide requires there is no default to read, and the variable keeps the text it
* is written as.
*/
@Test
void aDefaultValueIsReadToItsClosingQuote() {
String braced = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"s3:GetObject",
"Resource":"arn:aws:s3:::reports/${aws:PrincipalTag/team, 'a}b'}"}]}""";
assertEquals(Decision.ALLOW, decideS3(braced, "arn:aws:s3:::reports/a}b", Map.of()));
String unquoted = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"s3:GetObject",
"Resource":"arn:aws:s3:::reports/${aws:PrincipalTag/team, fallback}"}]}""";
assertEquals(Decision.DENY, decideS3(unquoted, "arn:aws:s3:::reports/fallback", Map.of()));
assertEquals(Decision.ALLOW, decideS3(unquoted,
"arn:aws:s3:::reports/${aws:PrincipalTag/team, fallback}", Map.of()));
}
/**
* The three predefined variables stand for a character, so they have to survive glob matching
* as themselves: "inserting an * asterisk in the string would be interpreted as a wildcard,
* matching any characters, instead of as a literal *".
*/
@Test
void theSingleCharacterEscapesMatchThemselvesRatherThanWildcards() {
String star = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"s3:GetObject",
"Resource":"arn:aws:s3:::reports/${*}"}]}""";
assertEquals(Decision.ALLOW, decideS3(star, "arn:aws:s3:::reports/*", Map.of()));
assertEquals(Decision.DENY, decideS3(star, "arn:aws:s3:::reports/q3.csv", Map.of()));
String question = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"s3:GetObject",
"Resource":"arn:aws:s3:::reports/${?}"}]}""";
assertEquals(Decision.ALLOW, decideS3(question, "arn:aws:s3:::reports/?", Map.of()));
assertEquals(Decision.DENY, decideS3(question, "arn:aws:s3:::reports/a", Map.of()));
String dollar = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"s3:GetObject",
"Resource":"arn:aws:s3:::reports/${$}cost"}]}""";
assertEquals(Decision.ALLOW, decideS3(dollar, "arn:aws:s3:::reports/$cost", Map.of()));
assertEquals(Decision.DENY, decideS3(dollar, "arn:aws:s3:::reports/cost", Map.of()));
}
/** An escape keeps its literal meaning around the wildcards the pattern really does carry. */
@Test
void anEscapeAndAWildcardCoexistInOnePattern() {
String policy = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"s3:GetObject",
"Resource":"arn:aws:s3:::reports/*${*}.csv"}]}""";
assertEquals(Decision.ALLOW, decideS3(policy, "arn:aws:s3:::reports/draft*.csv", Map.of()));
assertEquals(Decision.DENY, decideS3(policy, "arn:aws:s3:::reports/draft.csv", Map.of()));
}
/** A condition value resolves the same way a Resource pattern does. */
@Test
void aConditionVariableComparesAgainstTheResolvedValue() {
String policy = conditionPolicy("StringEquals", "${aws:PrincipalTag/team}");
assertEquals(Decision.ALLOW, decideS3(policy, "arn:aws:s3:::reports/q3.csv",
Map.of("aws:principaltag/team", List.of("yellow"),
"s3:existingobjecttag/team", List.of("yellow"))));
assertEquals(Decision.DENY, decideS3(policy, "arn:aws:s3:::reports/q3.csv",
Map.of("aws:principaltag/team", List.of("yellow"),
"s3:existingobjecttag/team", List.of("blue"))));
}
/**
* The asymmetry a null value creates: "Condition operators like StringEquals or StringLike do
* not match, and the policy statement does not take effect. Inverted condition operators like
* StringNotEquals or StringNotLike do match against a null value."
*/
@Test
void aConditionVariableWithNoValueFailsPositiveOperatorsAndPassesInvertedOnes() {
Map<String, List<String>> context = Map.of("s3:existingobjecttag/team", List.of("yellow"));
assertEquals(Decision.DENY, decideS3(conditionPolicy("StringEquals",
"${aws:PrincipalTag/team}"), "arn:aws:s3:::reports/q3.csv", context));
assertEquals(Decision.DENY, decideS3(conditionPolicy("StringLike",
"${aws:PrincipalTag/team}"), "arn:aws:s3:::reports/q3.csv", context));
assertEquals(Decision.ALLOW, decideS3(conditionPolicy("StringNotEquals",
"${aws:PrincipalTag/team}"), "arn:aws:s3:::reports/q3.csv", context));
assertEquals(Decision.ALLOW, decideS3(conditionPolicy("StringNotLike",
"${aws:PrincipalTag/team}"), "arn:aws:s3:::reports/q3.csv", context));
}
/** And in a condition value, where an empty one would leave a bare wildcard to match on. */
@Test
void aConditionVariableWithNoValueIsNotAnEmptyString() {
String policy = conditionPolicy("StringLike", "${aws:PrincipalTag/team}*");
Map<String, List<String>> tagged = Map.of("s3:existingobjecttag/team", List.of("yellow"));
assertEquals(Decision.DENY, decideS3(policy, "arn:aws:s3:::reports/q3.csv", tagged));
assertEquals(Decision.ALLOW, decideS3(policy, "arn:aws:s3:::reports/q3.csv",
Map.of("s3:existingobjecttag/team", List.of("yellow"),
"aws:principaltag/team", List.of("yell"))));
}
/**
* Which makes the Deny AWS documents for exactly this case fire: access "is explicitly denied
* when aws:principaltag/Team is not set".
*/
@Test
void theDocumentedDenyOnAnUnsetPrincipalTagFires() {
String policy = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"s3:GetObject","Resource":"*"},
{"Effect":"Deny","Action":"s3:GetObject","Resource":"arn:aws:s3:::reports/*",
"Condition":{"StringNotEquals":
{"s3:ExistingObjectTag/Team":"${aws:PrincipalTag/Team}"}}}]}""";
assertEquals(Decision.DENY, decideS3(policy, "arn:aws:s3:::reports/q3.csv",
Map.of("s3:existingobjecttag/team", List.of("yellow"))));
assertEquals(Decision.ALLOW, decideS3(policy, "arn:aws:s3:::reports/q3.csv",
Map.of("s3:existingobjecttag/team", List.of("yellow"),
"aws:principaltag/team", List.of("yellow"))));
}
/**
* "You can't use a policy variable with other operators, such as Numeric, Date, Boolean,
* Binary, IP Address, or Null operators", so with one of those the text stays as written and
* matches nothing, rather than resolving behind the policy author's back.
*/
@Test
void anOperatorThatTakesNoVariableComparesTheTextAsWritten() {
// The request time against itself: resolved it would be greater than or equal to itself
// and allow, so a Deny here is the text being compared as written and failing to parse.
assertEquals(Decision.DENY, decideS3(conditionPolicy("DateGreaterThanEquals",
"${aws:CurrentTime}", "aws:CurrentTime"),
"arn:aws:s3:::reports/q3.csv", Map.of("aws:currenttime", List.of("2026-10-13T00:00:00Z"))));
// Null reads its own values before any substitution could reach them, so a variable there
// is neither "true" nor "false" and the key it guards is simply absent.
assertEquals(Decision.DENY, decideS3(conditionPolicy("Null",
"${aws:PrincipalTag/team}", "aws:PrincipalTag/team"),
"arn:aws:s3:::reports/q3.csv", Map.of()));
}
/** An escape in a condition value is compared as the character, not as the marker. */
@Test
void anEscapeInAConditionValueComparesAsTheCharacter() {
assertEquals(Decision.ALLOW, decideS3(conditionPolicy("StringEquals", "${*}"),
"arn:aws:s3:::reports/q3.csv", Map.of("s3:existingobjecttag/team", List.of("*"))));
assertEquals(Decision.DENY, decideS3(conditionPolicy("StringEquals", "${*}"),
"arn:aws:s3:::reports/q3.csv", Map.of("s3:existingobjecttag/team", List.of("yellow"))));
assertEquals(Decision.ALLOW, decideS3(conditionPolicy("StringLike", "${*}"),
"arn:aws:s3:::reports/q3.csv", Map.of("s3:existingobjecttag/team", List.of("*"))));
assertEquals(Decision.DENY, decideS3(conditionPolicy("StringLike", "${*}"),
"arn:aws:s3:::reports/q3.csv", Map.of("s3:existingobjecttag/team", List.of("yellow"))));
}
/** An ARN operator is the other family that takes a variable. */
@Test
void anArnConditionResolvesAVariable() {
String policy = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"s3:GetObject","Resource":"*",
"Condition":{"ArnEquals":{"aws:SourceArn":"${aws:PrincipalArn}"}}}]}""";
String principal = "arn:aws:iam::111122223333:user/Alice";
assertEquals(Decision.ALLOW, decideS3(policy, "arn:aws:s3:::reports/q3.csv",
Map.of("aws:principalarn", List.of(principal), "aws:sourcearn", List.of(principal))));
assertEquals(Decision.DENY, decideS3(policy, "arn:aws:s3:::reports/q3.csv",
Map.of("aws:principalarn", List.of(principal),
"aws:sourcearn", List.of("arn:aws:iam::111122223333:user/Bob"))));
}
/**
* A name is not a variable once it holds a space. The policy-check reference calls it an
* error, "A space is not supported within the policy variable text", and resolving it anyway
* would grant the caller their own user through text AWS does not read as a variable.
*/
@Test
void aVariableNameHoldingASpaceIsNotAVariable() {
Map<String, List<String>> context = Map.of("aws:username", List.of("Alice"));
assertEquals(Decision.DENY, decide(allowingUserResource("2012-10-17",
"arn:aws:iam::*:user/${ aws:username }"), OWN_USER, context));
assertEquals(Decision.DENY, decide(allowingUserResource("2012-10-17",
"arn:aws:iam::*:user/${aws:username }"), OWN_USER, context));
// The space is the only difference, so the same pattern without it still resolves.
assertEquals(Decision.ALLOW, decide(allowingUserResource("2012-10-17",
"arn:aws:iam::*:user/${aws:username}"), OWN_USER, context));
}
/**
* The three forms the guide's policy checks call errors, each left as the text it is: an empty
* variable, a missing closing brace, and a default without its quotes. A space is allowed
* inside a quoted default, which is the one place the guide permits one.
*/
@Test
void aMalformedVariableStaysTheTextItIs() {
Map<String, List<String>> context = Map.of("aws:username", List.of("Alice"));
assertEquals(Decision.DENY, decide(allowingUserResource("2012-10-17",
"arn:aws:iam::*:user/${}Alice"), OWN_USER, context));
assertEquals(Decision.DENY, decide(allowingUserResource("2012-10-17",
"arn:aws:iam::*:user/${aws:username"), OWN_USER, context));
assertEquals(Decision.DENY, decide(allowingUserResource("2012-10-17",
"arn:aws:iam::*:user/${aws:username, Alice}"), OWN_USER, context));
assertEquals(Decision.ALLOW, decide(allowingUserResource("2012-10-17",
"arn:aws:iam::*:user/${aws:username, 'Alice Smith'}"),
"arn:aws:iam::111122223333:user/Alice Smith", Map.of()));
// Staying text and resolving to nothing both refuse a Resource, so the two are told
// apart on a Condition instead, where a value that stayed text still compares and one
// that resolved to nothing is null: equal fails and not-equal holds.
Map<String, List<String>> tagged = Map.of("s3:existingobjecttag/team", List.of("${}x"));
assertEquals(Decision.ALLOW, decideS3(conditionPolicy("StringEquals", "${}x"),
"arn:aws:s3:::reports/q3.csv", tagged));
assertEquals(Decision.DENY, decideS3(conditionPolicy("StringNotEquals", "${}x"),
"arn:aws:s3:::reports/q3.csv", tagged));
}
/**
* A resolved value is not escaped, so one carrying a wildcard widens the pattern it lands in.
* That is AWS's behaviour, whose policy checks warn that variables "can be evaluated as
* effective wildcards", and it is worth pinning because the widening also reaches a Deny.
*/
@Test
void aResolvedValueCarryingAWildcardWidensThePattern() {
String policy = allowingUserResource("2012-10-17", "arn:aws:iam::*:user/${aws:username}");
Map<String, List<String>> wildcardName = Map.of("aws:username", List.of("*"));
assertEquals(Decision.ALLOW, decide(policy, OWN_USER, wildcardName));
assertEquals(Decision.ALLOW, decide(policy, OTHER_USER, wildcardName));
}
/**
* The escape marker belongs to the matcher, and a document can write one as the JSON escape
* {@code \\u0000}. Honouring it there would turn a wildcard of the document's own into a
* literal character and shrink the statement it sits in, so a marker is dropped on the way in
* and the matcher reads one only on a pattern that came back from substitution.
*/
@Test
void aDocumentCannotForgeTheEscapeMarker() {
String deny = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"s3:GetObject","Resource":"*"},
{"Effect":"Deny","Action":"s3:GetObject",
"Resource":"arn:aws:s3:::reports/\\u0000*"}]}""";
assertEquals(Decision.DENY, decideS3(deny, "arn:aws:s3:::reports/q3.csv", Map.of()));
assertEquals(Decision.DENY, decideS3(conditionPolicy("StringNotLike", "\\u0000*"),
"arn:aws:s3:::reports/q3.csv", Map.of("s3:existingobjecttag/team", List.of("yellow"))));
// And under the version that substitutes nothing, where the marker cannot be the
// matcher's own: it is an ordinary character the value has to carry, so the Deny matches
// no object at all, not even the one named with the asterisk it was shielding. Reading it
// as a marker instead would make this Deny refuse that object.
assertEquals(Decision.ALLOW, decideS3("""
{"Version":"2008-10-17","Statement":[
{"Effect":"Allow","Action":"s3:GetObject","Resource":"*"},
{"Effect":"Deny","Action":"s3:GetObject",
"Resource":"arn:aws:s3:::reports/\\u0000*"}]}""",
"arn:aws:s3:::reports/*", Map.of()));
}
private static String conditionPolicy(String operator, String value) {
return conditionPolicy(operator, value, "s3:ExistingObjectTag/Team");
}
private static String conditionPolicy(String operator, String value, String conditionKey) {
return """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"s3:GetObject","Resource":"*",
"Condition":{"%s":{"%s":"%s"}}}]}"""
.formatted(operator, conditionKey, value);
}
private Decision decide(String policy, String resource, Map<String, List<String>> context) {
return evaluator.simulateCustomPolicy(List.of(policy), "iam:ChangePassword", resource, context);
}
private Decision decideS3(String policy, String resource, Map<String, List<String>> context) {
return evaluator.simulateCustomPolicy(List.of(policy), "s3:GetObject", resource, context);
}
}