fix(s3): check the extra IAM actions a CreateBucket needs (#5961)

* fix(s3): check the extra IAM actions a CreateBucket needs

A CreateBucket whose headers configure the new bucket needs more than
s3:CreateBucket, as the CreateBucket API reference lists:

- s3:PutBucketAcl for a canned ACL other than private, or a grant header
- s3:PutBucketObjectLockConfiguration and s3:PutBucketVersioning for
  x-amz-bucket-object-lock-enabled: true
- s3:PutBucketOwnershipControls for x-amz-object-ownership

S3Controller now checks these actions before the bucket is created, so
a denied request creates nothing.

* chore(s3): move the CreateBucket header actions next to the object ones

S3ObjectActions.bucketCreationActions now gives the actions a
CreateBucket needs from its headers, and reuses GRANT_HEADERS.
EnforcementFixtures.putUserPolicy replaces the test's own PutUserPolicy
helper.
This commit is contained in:
Aymen JAMAAOUI authored and GitHub committed 2026-10-19 09:30:22 -04:00
1 parent 3a913ca8d6
commit 09b14e9d10
5 files changed
+233

No files matched your search

@@ -459,6 +459,7 @@ public class S3Controller {
new String(body, StandardCharsets.UTF_8), "Tag", "Key", "Value");
}
String region = bucketRegionForCreate(locationConstraint, regionResolver.resolveRegion(httpHeaders));
authorizeBucketCreationSettings(httpHeaders, uriInfo, bucket, region);
s3Service.createBucket(bucket, region,
httpHeaders.getHeaderString("x-amz-object-ownership"),
httpHeaders.getHeaderString("x-amz-acl"),
@@ -4496,6 +4497,24 @@ public class S3Controller {
s3Service.authorizeObjectCreationSetting(destBucket, destKey, action, authorization, null);
}
/**
* Authorizes the actions a CreateBucket needs besides {@code s3:CreateBucket}
* (see {@link S3ObjectActions#bucketCreationActions}) on the new bucket. It runs before the bucket
* is created, so a denied request creates nothing.
*/
private void authorizeBucketCreationSettings(HttpHeaders httpHeaders, UriInfo uriInfo, String bucket,
String region) {
List<String> actions = S3ObjectActions.bucketCreationActions(httpHeaders::getHeaderString);
if (actions.isEmpty()) {
return;
}
String credential = iamCredential(httpHeaders, uriInfo);
String resource = S3PublicAccessEvaluator.bucketArn(AwsRegions.partitionFor(region), bucket);
for (String action : actions) {
iamEnforcementFilter.authorizeAdditionalResource(credential, action, resource);
}
}
/**
* The credential IAM evaluates a check S3Controller makes itself against: the Authorization header,
* or a presigned request's query credential. A blank header counts as absent, as it does in
@@ -105,6 +105,32 @@ public final class S3ObjectActions {
return actions;
}
/**
* The actions a CreateBucket needs besides {@code s3:CreateBucket}, one for each thing its headers
* set on the new bucket, as the CreateBucket API reference lists them: an ACL other than
* {@code private}, or any grant header, needs s3:PutBucketAcl; Object Lock needs
* s3:PutBucketObjectLockConfiguration and s3:PutBucketVersioning; an Object Ownership needs
* s3:PutBucketOwnershipControls. A blank header counts as absent.
*
* @param header reads one request header, null when it is absent
*/
public static List<String> bucketCreationActions(Function<String, String> header) {
List<String> actions = new ArrayList<>();
String cannedAcl = header.apply("x-amz-acl");
if ((!isBlank(cannedAcl) && !"private".equals(cannedAcl))
|| GRANT_HEADERS.stream().anyMatch(name -> !isBlank(header.apply(name)))) {
actions.add("s3:PutBucketAcl");
}
if ("true".equalsIgnoreCase(header.apply("x-amz-bucket-object-lock-enabled"))) {
actions.add("s3:PutBucketObjectLockConfiguration");
actions.add("s3:PutBucketVersioning");
}
if (!isBlank(header.apply("x-amz-object-ownership"))) {
actions.add("s3:PutBucketOwnershipControls");
}
return actions;
}
/**
* The condition keys an object creation offers {@code s3:PutObject} and each of
* {@link #objectCreationActions}: {@code s3:RequestObjectTag/<key>} and
@@ -262,4 +288,8 @@ public final class S3ObjectActions {
return null;
}
}
private static boolean isBlank(String value) {
return value == null || value.isBlank();
}
}