fix(iam): populate PrincipalIsAWSService for signed requests (#4695)

* fix(iam): set PrincipalIsAWSService for signed IAM requests

* fix(iam): apply global conditions to additional resources

* fix(iam): accept JSON booleans in Bool conditions
This commit is contained in:
XuJian authored and GitHub committed 2026-09-29 20:37:36 -04:00
1 parent 43ed79d15b
commit e6894119f4
11 files changed
+255 -15

No files matched your search

@@ -101,7 +101,9 @@ public class IamConditionContextResolver {
putIfPresent(conditions, "aws:ResourceAccount", resourceAccount);
putIfPresent(conditions, "aws:PrincipalAccount", accountId);
putIfPresent(conditions, "aws:RequestedRegion", region);
return conditions.isEmpty() ? null : conditions;
// Enforcement evaluates signed IAM credentials, not direct AWS service principals.
conditions.put("aws:PrincipalIsAWSService", List.of("false"));
return conditions;
}
private static String accountFromArn(String arn) {
@@ -727,22 +727,24 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
conditionContext.put("aws:PrincipalArn", List.of(principalArn.get()));
}
Map<String, List<String>> effectiveContext = conditionContext;
ResourcePolicyDecision effectiveDecision = resourcePolicyDecision;
String effectiveOwnerAccountId = resourceOwnerAccountId;
List<String> policyDocs = null;
if (effectiveDecision == null) {
List<ResourcePolicyProvider.ResourcePolicy> resourcePolicies = resolveResourcePolicies(
credentialScope, resource);
effectiveOwnerAccountId = resourcePolicies.isEmpty()
? null : resourcePolicies.getFirst().ownerAccountId();
List<String> policyDocs = resourcePolicies.stream()
policyDocs = resourcePolicies.stream()
.map(ResourcePolicyProvider.ResourcePolicy::policyDocument)
.filter(doc -> doc != null && !doc.isBlank())
.toList();
String region = requestContext.getRegion() == null
? config.defaultRegion() : requestContext.getRegion();
effectiveContext = IamConditionContextResolver.withGlobalContext(
conditionContext, resource, region, accountId, effectiveOwnerAccountId);
}
String region = requestContext.getRegion() == null
? config.defaultRegion() : requestContext.getRegion();
Map<String, List<String>> effectiveContext = IamConditionContextResolver.withGlobalContext(
conditionContext, resource, region, accountId, effectiveOwnerAccountId);
if (effectiveDecision == null) {
effectiveDecision = evaluator.evaluateResourcePolicy(
policyDocs.isEmpty() ? null : policyDocs,
caller.principalArn(), action, resource, effectiveContext);
@@ -1230,8 +1230,12 @@ public class IamPolicyEvaluator {
Map<String, Map<String, List<String>>> result = new LinkedHashMap<>();
condNode.fields().forEachRemaining(opEntry -> {
Map<String, List<String>> kvMap = new LinkedHashMap<>();
opEntry.getValue().fields().forEachRemaining(kvEntry ->
kvMap.put(kvEntry.getKey(), nodeToList(kvEntry.getValue())));
boolean boolOperator = "Bool".equals(parseOperator(opEntry.getKey()).baseOp());
opEntry.getValue().fields().forEachRemaining(kvEntry -> {
JsonNode value = kvEntry.getValue();
kvMap.put(kvEntry.getKey(), boolOperator && value.isBoolean()
? List.of(value.asText()) : nodeToList(value));
});
result.put(opEntry.getKey(), kvMap);
});
return result.isEmpty() ? null : result;
@@ -481,14 +481,17 @@ final class S3PublicAccessEvaluator {
|| "ArnLike".equalsIgnoreCase(operator.getKey());
boolean exact = "StringEquals".equalsIgnoreCase(operator.getKey())
|| "ArnEquals".equalsIgnoreCase(operator.getKey());
if ((!glob && !exact) || !operator.getValue().isObject()) {
boolean bool = "Bool".equalsIgnoreCase(operator.getKey());
if ((!glob && !exact && !bool) || !operator.getValue().isObject()) {
return false;
}
Iterator<Map.Entry<String, JsonNode>> entries = operator.getValue().fields();
while (entries.hasNext()) {
Map.Entry<String, JsonNode> entry = entries.next();
String actual = contextValue(context, entry.getKey());
if (actual == null || !conditionValueMatches(entry.getValue(), actual, glob)) {
if (actual == null || !(bool
? booleanConditionMatches(entry.getValue(), actual)
: conditionValueMatches(entry.getValue(), actual, glob))) {
return false;
}
}
@@ -508,6 +511,25 @@ final class S3PublicAccessEvaluator {
return null;
}
private static boolean booleanConditionMatches(JsonNode expected, String actual) {
if (!"true".equalsIgnoreCase(actual) && !"false".equalsIgnoreCase(actual)) {
return false;
}
if (expected.isBoolean() || expected.isTextual()) {
String value = expected.asText();
return ("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value))
&& value.equalsIgnoreCase(actual);
}
if (expected.isArray()) {
for (JsonNode item : expected) {
if (booleanConditionMatches(item, actual)) {
return true;
}
}
}
return false;
}
private static boolean conditionValueMatches(JsonNode expected, String actual, boolean glob) {
if (expected == null || expected.isNull()) {
return false;
@@ -1082,8 +1082,9 @@ public class S3Service implements Resettable, ResourceProvider {
}
Map<String, List<String>> conditionCtx = principalArn != null
? Map.of("aws:PrincipalArn", List.of(principalArn))
: Map.of();
? Map.of("aws:PrincipalArn", List.of(principalArn),
"aws:PrincipalIsAWSService", List.of("false"))
: Map.of("aws:PrincipalIsAWSService", List.of("false"));
ResourcePolicyDecision decision = policyEvaluator.evaluateResourcePolicy(
List.of(policy),
@@ -1164,7 +1165,8 @@ public class S3Service implements Resettable, ResourceProvider {
principalArn.get(),
action,
resourceArn,
Map.of("aws:PrincipalArn", principalArn.get()));
Map.of("aws:PrincipalArn", principalArn.get(),
"aws:PrincipalIsAWSService", "false"));
ResourcePolicyDecision decision = switch (policyEvaluation.decision()) {
case ALLOW -> policyEvaluation.directPrincipalAllow() && principalArn.get().contains(":user/")
? ResourcePolicyDecision.ALLOW_DIRECT_IAM_USER
@@ -507,6 +507,7 @@ class IamConditionContextResolverTest {
assertEquals(List.of("000000000000"), conditions.get("aws:ResourceAccount"));
assertEquals(List.of("000000000000"), conditions.get("aws:PrincipalAccount"));
assertEquals(List.of("eu-west-2"), conditions.get("aws:RequestedRegion"));
assertEquals(List.of("false"), conditions.get("aws:PrincipalIsAWSService"));
}
@Test
@@ -649,6 +649,24 @@ class IamPolicyEvaluatorTest {
caller, null, "s3:GetObject", "arn:aws:s3:::b/k", null));
}
@Test
void booleanResourcePolicyConditionAcceptsJsonBooleanValue() {
String denySignedPrincipal = """
{"Version":"2012-10-17","Statement":[{
"Effect":"Deny","Principal":"*","Action":"s3:GetObject","Resource":"*",
"Condition":{"Bool":{"aws:PrincipalIsAWSService":false}}
}]}""";
String principal = "arn:aws:iam::123456789012:user/alice";
String resource = "arn:aws:s3:::bucket/key";
assertEquals(ResourcePolicyDecision.EXPLICIT_DENY, evaluator.evaluateResourcePolicy(
List.of(denySignedPrincipal), principal, "s3:GetObject", resource,
Map.of("aws:PrincipalIsAWSService", List.of("false"))));
assertEquals(ResourcePolicyDecision.NEUTRAL, evaluator.evaluateResourcePolicy(
List.of(denySignedPrincipal), principal, "s3:GetObject", resource,
Map.of("aws:PrincipalIsAWSService", List.of("true"))));
}
@Test
void crossAccountEvaluationRequiresBothIdentityAndResourcePolicies() {
String bucketPolicy = """
@@ -2356,6 +2356,47 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(204);
}
@Test
@Order(57)
void signedIamPrincipalMatchesPrincipalIsAwsServiceFalseInBucketPolicy() {
String bucket = "auth-principal-is-service-bucket";
given().filter(LOCAL_SIGNER).when().put("/" + bucket).then().statusCode(200);
given().filter(LOCAL_SIGNER).body("private data")
.when().put("/" + bucket + "/data.txt")
.then().statusCode(200);
String denyForIamPrincipals = """
{
"Version": "2012-10-17",
"Statement": {
"Effect": "Deny",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::%s/*",
"Condition": {"Bool": {"aws:PrincipalIsAWSService": "false"}}
}
}""".formatted(bucket);
given().filter(LOCAL_SIGNER).contentType("application/json").body(denyForIamPrincipals)
.when().put("/" + bucket + "?policy")
.then().statusCode(200);
given().filter(LOCAL_SIGNER)
.when().get("/" + bucket + "/data.txt")
.then().statusCode(403)
.body(containsString("AccessDenied"));
String denyForServicePrincipals = denyForIamPrincipals.replace(
"\"aws:PrincipalIsAWSService\": \"false\"",
"\"aws:PrincipalIsAWSService\": \"true\"");
given().filter(LOCAL_SIGNER).contentType("application/json").body(denyForServicePrincipals)
.when().put("/" + bucket + "?policy")
.then().statusCode(200);
given().filter(LOCAL_SIGNER)
.when().get("/" + bucket + "/data.txt")
.then().statusCode(200)
.body(equalTo("private data"));
}
private static String publicObjectActionPolicy(String bucket, String action) {
return publicObjectActionPolicy(bucket, "*", action);
}
@@ -160,7 +160,36 @@ class S3BucketPolicyEnforcementIntegrationTest {
.when()
.put("/" + bucketName + "?policy")
.then()
.statusCode(200);
.statusCode(200);
}
@Test
void signedIamUserMatchesPrincipalIsAwsServiceFalseInIdentityPolicy() {
String suffix = UUID.randomUUID().toString().substring(0, 8);
String bucket = "principal-service-" + suffix;
String userName = "principal-service-" + suffix;
createBucket(bucket);
putObject(bucket, "data.txt", "data");
Credentials user = createUserWithCredentials(userName);
putUserPolicy(userName, "ReadWhenIamPrincipal", """
{
"Version": "2012-10-17",
"Statement": {
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::%s/data.txt",
"Condition": {"Bool": {"aws:PrincipalIsAWSService": "false"}}
}
}""".formatted(bucket), "000000000000");
given()
.filter(user.signer())
.when()
.get("/" + bucket + "/data.txt")
.then()
.statusCode(200)
.body(equalTo("data"));
}
@Test
@@ -94,6 +94,103 @@ class S3CopyObjectSourcePermissionIntegrationTest {
.statusCode(200);
}
@Test
void copySourceReadHonorsSignedPrincipalGlobalCondition() {
String suffix = UUID.randomUUID().toString().substring(0, 8);
String sourceBucket = "copy-global-source-" + suffix;
String destBucket = "copy-global-dest-" + suffix;
String userName = "copy-global-user-" + suffix;
createBucketAsRoot(sourceBucket);
createBucketAsRoot(destBucket);
putObjectAsRoot(sourceBucket, "source.txt", "copy source");
UserCredentials caller = createUser(userName);
putUserPolicy(userName, "ConditionalSourceRead", """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"s3:GetObject","Resource":"arn:aws:s3:::%1$s/*",
"Condition":{"Bool":{"aws:PrincipalIsAWSService":"false"}}},
{"Effect":"Allow","Action":"s3:PutObject","Resource":"arn:aws:s3:::%2$s/*"}
]} """.formatted(sourceBucket, destBucket));
given()
.filter(caller.signer())
.when()
.get("/" + sourceBucket + "/source.txt")
.then()
.statusCode(200);
given()
.filter(caller.signer())
.header("x-amz-copy-source", "/" + sourceBucket + "/source.txt")
.when()
.put("/" + destBucket + "/copied.txt")
.then()
.statusCode(200);
String uploadId = initiateMultipartUploadAsRoot(destBucket, "multipart.txt");
given()
.filter(caller.signer())
.header("x-amz-copy-source", "/" + sourceBucket + "/source.txt")
.queryParam("uploadId", uploadId)
.queryParam("partNumber", 1)
.when()
.put("/" + destBucket + "/multipart.txt")
.then()
.statusCode(200);
}
@Test
void jsonBooleanSourcePolicyDenyAppliesToDirectAndCopiedReads() {
String suffix = UUID.randomUUID().toString().substring(0, 8);
String sourceBucket = "copy-bool-source-" + suffix;
String destBucket = "copy-bool-dest-" + suffix;
String userName = "copy-bool-user-" + suffix;
createBucketAsRoot(sourceBucket);
createBucketAsRoot(destBucket);
putObjectAsRoot(sourceBucket, "source.txt", "protected source");
putBucketPolicyAsRoot(sourceBucket, """
{"Version":"2012-10-17","Statement":[{
"Effect":"Deny","Principal":"*","Action":"s3:GetObject",
"Resource":"arn:aws:s3:::%s/*",
"Condition":{"Bool":{"aws:PrincipalIsAWSService":false}}
}]}""".formatted(sourceBucket));
UserCredentials caller = createUser(userName);
putUserPolicy(userName, "SourceReadDestWrite", """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"s3:GetObject","Resource":"arn:aws:s3:::%1$s/*"},
{"Effect":"Allow","Action":"s3:PutObject","Resource":"arn:aws:s3:::%2$s/*"}
]}""".formatted(sourceBucket, destBucket));
given()
.filter(caller.signer())
.when()
.get("/" + sourceBucket + "/source.txt")
.then()
.statusCode(403);
given()
.filter(caller.signer())
.header("x-amz-copy-source", "/" + sourceBucket + "/source.txt")
.when()
.put("/" + destBucket + "/copied.txt")
.then()
.statusCode(403);
String uploadId = initiateMultipartUploadAsRoot(destBucket, "multipart.txt");
given()
.filter(caller.signer())
.header("x-amz-copy-source", "/" + sourceBucket + "/source.txt")
.queryParam("uploadId", uploadId)
.queryParam("partNumber", 1)
.when()
.put("/" + destBucket + "/multipart.txt")
.then()
.statusCode(403);
}
@Test
void copyObjectSucceedsWhenSourceBucketPolicyAllowsCaller() {
String suffix = UUID.randomUUID().toString().substring(0, 8);
@@ -128,6 +128,28 @@ class S3PublicAccessEvaluatorTest {
"arn:aws:cloudfront::000000000000:distribution/EOTHER")));
}
@Test
void principalIsAwsServiceBoolRequiresTheMatchingContextValue() {
String userArn = "arn:aws:iam::123456789012:user/alice";
String policy = """
{"Version":"2012-10-17","Statement":{
"Effect":"Allow",
"Principal":{"AWS":"%s"},
"Action":"s3:GetObject",
"Resource":"arn:aws:s3:::public-bucket/*",
"Condition":{"Bool":{"aws:PrincipalIsAWSService":"false"}}
}}""".formatted(userArn);
assertEquals(ALLOW, S3PublicAccessEvaluator.principalPolicyDecision(
OBJECT_MAPPER, policy, "AWS", userArn, "s3:GetObject", OBJECT_ARN,
Map.of("aws:PrincipalIsAWSService", "false")));
assertEquals(NEUTRAL, S3PublicAccessEvaluator.principalPolicyDecision(
OBJECT_MAPPER, policy, "AWS", userArn, "s3:GetObject", OBJECT_ARN,
Map.of("aws:PrincipalIsAWSService", "true")));
assertEquals(NEUTRAL, S3PublicAccessEvaluator.principalPolicyDecision(
OBJECT_MAPPER, policy, "AWS", userArn, "s3:GetObject", OBJECT_ARN, Map.of()));
}
@Test
void cloudFrontOaiPrincipalMatchesAwsPrincipal() {
String oaiArn =