fix(s3): give a role session's aws:PrincipalArn as its role's ARN in S3's bucket-policy checks (#4838)

Follow-up from the review of #4810.

S3 evaluates bucket policies itself in two places: the signed-request check
(authorizeSignedBucketPolicy, behind s3.enforce-auth) and the CopyObject and
UploadPartCopy source check (signedPrincipalResourcePolicyDecision). Both took
the caller from S3Service.resolvePrincipalArn, which read IamService
.resolveCallerArn, so for a role session the same value, the assumed-role
session ARN, served both as the principal a policy's Principal is matched
against and as aws:PrincipalArn. AWS puts the ARN of the role that was assumed,
path included, in aws:PrincipalArn, as the enforcement filter does since #4810.
A bucket-policy Deny keyed on the session ARN therefore fired, and refused the
read or the copy, where AWS would not apply it.

S3 now reads both through IamService.resolveCallerArns, added in #4810: the
caller ARN still drives Principal matching and the same-account test, and the
principal ARN fills aws:PrincipalArn. A key IAM does not know keeps S3's
account-root fallback, which is both. The private helper is renamed
resolveCaller, so it is not mistaken for IamService.resolvePrincipalArn.

S3BucketPolicyEnforcementIntegrationTest gets a role under /team/ whose
session reads and copies from a bucket with two Denies: one keyed on the role's
ARN, which refuses it, and one keyed on the session ARN, which does not. It
fails on main, where the read the session-ARN Deny should not touch gets 403.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-01 21:18:14 -05:00
1 parent cd89be4a80
commit b512d273e1
2 files changed
+71 -13

No files matched your search

@@ -192,6 +192,55 @@ class S3BucketPolicyEnforcementIntegrationTest {
.body(equalTo("data"));
}
@Test
void aRoleSessionsPrincipalArnInABucketPolicyIsItsRolesArn() {
// S3 evaluates the bucket policy itself (s3.enforce-auth, and the CopyObject source check),
// and there aws:PrincipalArn is the ARN of the role that was assumed, not the session's: a
// Deny keyed on the role's ARN fires, one keyed on the session ARN does not.
String suffix = UUID.randomUUID().toString().substring(0, 8);
String bucket = "bp-role-session-" + suffix;
String role = "bp-role-" + suffix;
String roleArn = "arn:aws:iam::000000000000:role/team/" + role;
createBucket(bucket);
putObject(bucket, "denied/x.txt", "denied");
putObject(bucket, "session/x.txt", "session");
given().formParam("Action", "CreateRole").formParam("RoleName", role).formParam("Path", "/team/")
.formParam("AssumeRolePolicyDocument", """
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
"Principal":{"AWS":"arn:aws:iam::000000000000:root"},"Action":"sts:AssumeRole"}]}""")
.header("Authorization", auth("000000000000", "iam")).when().post("/").then().statusCode(200);
given().formParam("Action", "PutRolePolicy").formParam("RoleName", role).formParam("PolicyName", "s3")
.formParam("PolicyDocument", """
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"s3:*","Resource":"*"}]}""")
.header("Authorization", auth("000000000000", "iam")).when().post("/").then().statusCode(200);
ExtractableResponse<Response> assumed = given().formParam("Action", "AssumeRole")
.formParam("RoleArn", roleArn).formParam("RoleSessionName", "s")
.header("Authorization", auth("000000000000", "sts")).when().post("/")
.then().statusCode(200).extract();
String sessionArn = assumed.path("AssumeRoleResponse.AssumeRoleResult.AssumedRoleUser.Arn");
S3RequestSigner session = S3RequestSigner.signedAs(
assumed.path("AssumeRoleResponse.AssumeRoleResult.Credentials.AccessKeyId"),
assumed.path("AssumeRoleResponse.AssumeRoleResult.Credentials.SecretAccessKey"),
assumed.path("AssumeRoleResponse.AssumeRoleResult.Credentials.SessionToken"));
putBucketPolicy(bucket, """
{"Version":"2012-10-17","Statement":[
{"Effect":"Deny","Principal":"*","Action":"s3:GetObject","Resource":"arn:aws:s3:::%1$s/denied/*",
"Condition":{"ArnEquals":{"aws:PrincipalArn":"%2$s"}}},
{"Effect":"Deny","Principal":"*","Action":"s3:GetObject","Resource":"arn:aws:s3:::%1$s/session/*",
"Condition":{"ArnEquals":{"aws:PrincipalArn":"%3$s"}}}]}""".formatted(bucket, roleArn, sessionArn));
given().filter(session).when().get("/" + bucket + "/session/x.txt")
.then().statusCode(200).body(equalTo("session"));
given().filter(session).when().get("/" + bucket + "/denied/x.txt")
.then().statusCode(403);
given().filter(session).header("x-amz-copy-source", "/" + bucket + "/session/x.txt")
.when().put("/" + bucket + "/copied-session.txt")
.then().statusCode(200);
given().filter(session).header("x-amz-copy-source", "/" + bucket + "/denied/x.txt")
.when().put("/" + bucket + "/copied-denied.txt")
.then().statusCode(403);
}
@Test
void enforcesBucketPolicyForSignedCallers() {
String suffix = UUID.randomUUID().toString().substring(0, 8);