mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
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:
1 parent
cd89be4a80
commit
b512d273e1
2 files changed
+71
-13
No files matched your search
@@ -17,6 +17,7 @@ import io.github.hectorvent.floci.core.storage.WriteProfile;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.ResourcePolicyDecision;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.services.iam.IamService.CallerArns;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
|
||||
@@ -1067,7 +1068,8 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
Bucket bucket = ownedBucketEntry.value();
|
||||
String bucketOwner = ownedBucketEntry.account();
|
||||
|
||||
String principalArn = resolvePrincipalArn(authorization.accessKeyId()).orElse(null);
|
||||
CallerArns caller = resolveCaller(authorization.accessKeyId()).orElse(null);
|
||||
String principalArn = caller == null ? null : caller.callerArn();
|
||||
boolean sameAccountAsOwner = isSameAccountAsBucketOwner(authorization.accessKeyId(), principalArn, bucketOwner);
|
||||
|
||||
if (isBucketPolicyAction(action) && !sameAccountAsOwner) {
|
||||
@@ -1091,8 +1093,8 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
throw accessDeniedException(bucketName, key);
|
||||
}
|
||||
|
||||
Map<String, List<String>> conditionCtx = principalArn != null
|
||||
? Map.of("aws:PrincipalArn", List.of(principalArn),
|
||||
Map<String, List<String>> conditionCtx = caller != null
|
||||
? Map.of("aws:PrincipalArn", List.of(caller.principalArn()),
|
||||
"aws:PrincipalIsAWSService", List.of("false"))
|
||||
: Map.of("aws:PrincipalIsAWSService", List.of("false"));
|
||||
|
||||
@@ -1115,18 +1117,25 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
throw accessDeniedException(bucketName, key);
|
||||
}
|
||||
|
||||
private Optional<String> resolvePrincipalArn(String accessKeyId) {
|
||||
/**
|
||||
* The signing caller's two ARNs: the caller ARN a bucket policy's {@code Principal} is matched
|
||||
* against, and the request's {@code aws:PrincipalArn}, which for a role session is the role's ARN,
|
||||
* path included, rather than the session's. A key IAM does not know is treated as its account's
|
||||
* root, which is both.
|
||||
*/
|
||||
private Optional<CallerArns> resolveCaller(String accessKeyId) {
|
||||
if (accessKeyId == null || accessKeyId.isBlank()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
if (iamService != null) {
|
||||
Optional<String> callerArn = iamService.resolveCallerArn(accessKeyId);
|
||||
if (callerArn.isPresent()) {
|
||||
return callerArn;
|
||||
Optional<CallerArns> callerArns = iamService.resolveCallerArns(accessKeyId);
|
||||
if (callerArns.isPresent()) {
|
||||
return callerArns;
|
||||
}
|
||||
}
|
||||
String account = accessKeyId.matches("\\d{12}") ? accessKeyId : ownerId();
|
||||
return Optional.of(regionResolver.buildGlobalArn("iam", account, "root"));
|
||||
String rootArn = regionResolver.buildGlobalArn("iam", account, "root");
|
||||
return Optional.of(new CallerArns(rootArn, rootArn));
|
||||
}
|
||||
|
||||
private boolean isSameAccountAsBucketOwner(String accessKeyId, String principalArn, String bucketOwnerAccount) {
|
||||
@@ -1159,8 +1168,8 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
return new SignedPrincipalResourcePolicyEvaluation(ResourcePolicyDecision.NEUTRAL, null);
|
||||
}
|
||||
|
||||
Optional<String> principalArn = resolvePrincipalArn(authorization.accessKeyId());
|
||||
if (principalArn.isEmpty()) {
|
||||
Optional<CallerArns> caller = resolveCaller(authorization.accessKeyId());
|
||||
if (caller.isEmpty()) {
|
||||
return new SignedPrincipalResourcePolicyEvaluation(ResourcePolicyDecision.NEUTRAL, null);
|
||||
}
|
||||
|
||||
@@ -1172,13 +1181,13 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
objectMapper,
|
||||
ownedBucket.value().getPolicy(),
|
||||
"AWS",
|
||||
principalArn.get(),
|
||||
caller.get().callerArn(),
|
||||
action,
|
||||
resourceArn,
|
||||
Map.of("aws:PrincipalArn", principalArn.get(),
|
||||
Map.of("aws:PrincipalArn", caller.get().principalArn(),
|
||||
"aws:PrincipalIsAWSService", "false"));
|
||||
ResourcePolicyDecision decision = switch (policyEvaluation.decision()) {
|
||||
case ALLOW -> policyEvaluation.directPrincipalAllow() && principalArn.get().contains(":user/")
|
||||
case ALLOW -> policyEvaluation.directPrincipalAllow() && caller.get().callerArn().contains(":user/")
|
||||
? ResourcePolicyDecision.ALLOW_DIRECT_IAM_USER
|
||||
: ResourcePolicyDecision.ALLOW;
|
||||
case DENY -> ResourcePolicyDecision.EXPLICIT_DENY;
|
||||
|
||||
Reference in new issue
Block a user