fix(s3): enforce auth on bucket-level config writes when enforce-auth is enabled (#3536)

* fix(s3): enforce auth on bucket-level config writes when enforce-auth is enabled

With FLOCI_SERVICES_S3_ENFORCE_AUTH=true, unsigned object reads are refused
unless a policy or ACL makes them public, and since #2326 unsigned object
writes are refused the same way. Bucket management was never covered:
CreateBucket, DeleteBucket, and every PUT/DELETE on a bucket subresource
(?policy, ?acl, ?cors, ?lifecycle, ?versioning, ?website, ?notification,
?logging, ?encryption, ?publicAccessBlock, ?ownershipControls,
?requestPayment, ?tagging, ?accelerate, ?replication, ?object-lock,
?metrics) behaved identically with the flag on or off. An anonymous caller
could PUT ?policy a public-read grant onto any bucket and then read what the
previous request had correctly refused.

Authorization in S3Controller is opt-in per branch, and only the three
newest subresources called authorizeBucketWrite. createBucket and
deleteBucket now parse the request's credentials once and check every
branch against the bucket's policy with the matching s3:* action before the
mutator runs, and s3:DeleteBucket before the bare delete. CreateBucket gets
its own authorizeCreateBucket: there is no policy to consult and AWS never
accepts it anonymously, so an unsigned request is AccessDenied and a signed
one must carry a known key; authorizeBucketWrite would have answered
NoSuchBucket, the wrong code and an existence oracle.

Nothing changes with the flag off. With it on, anonymous bucket management
now fails with 403, as it does on AWS. The enforce-auth tests created their
buckets and policies anonymously, which was this bug running green; those
38 setup calls now sign with the class's legacy key, and four new tests pin
CreateBucket/DeleteBucket, the policy-rewrite exploit, every subresource,
and the policy-granted anonymous case.

Closes #2335

* fix(s3): never let a bucket policy grant PutBucketPolicy or DeleteBucketPolicy anonymously

AWS lets only an identity in the bucket owner's account manage the bucket
policy, so the policy itself can never grant s3:PutBucketPolicy or
s3:DeleteBucketPolicy to an anonymous caller. authorizeBucketWrite honoured
a Principal "*" grant for those two actions like any other, which would have
let an unsigned caller rewrite or delete a policy that named them. Refuse
them before consulting the policy, and pin it with a test that installs a
self-granting policy and checks anonymous PUT and DELETE ?policy are still
AccessDenied and the policy survives.

Raised in review on #3536.
This commit is contained in:
David authored and GitHub committed 2026-09-13 09:25:09 -04:00
1 parent e9454af855
commit 28105c08ec
4 files changed
+361 -28

No files matched your search

@@ -259,34 +259,45 @@ public class S3Controller {
byte[] body) {
try {
validateRawUri();
S3Service.RequestAuthorization authorization = S3RequestAuthorizationParser.parseIfRequired(
s3Service.isAuthEnforced(), httpHeaders, uriInfo);
if (hasQueryParam(uriInfo, "notification")) {
s3Service.authorizeBucketWrite(bucket, "s3:PutBucketNotification", authorization);
return handlePutBucketNotification(bucket, body);
}
if (hasQueryParam(uriInfo, "versioning")) {
s3Service.authorizeBucketWrite(bucket, "s3:PutBucketVersioning", authorization);
return handlePutBucketVersioning(bucket, body);
}
if (hasQueryParam(uriInfo, "tagging")) {
s3Service.authorizeBucketWrite(bucket, "s3:PutBucketTagging", authorization);
return handlePutBucketTagging(bucket, body);
}
if (hasQueryParam(uriInfo, "object-lock")) {
s3Service.authorizeBucketWrite(bucket, "s3:PutBucketObjectLockConfiguration", authorization);
return handlePutObjectLockConfiguration(bucket, body);
}
if (hasQueryParam(uriInfo, "website")) {
s3Service.authorizeBucketWrite(bucket, "s3:PutBucketWebsite", authorization);
return handlePutBucketWebsite(bucket, body);
}
if (hasQueryParam(uriInfo, "logging")) {
s3Service.authorizeBucketWrite(bucket, "s3:PutBucketLogging", authorization);
s3Service.putBucketLogging(bucket, new String(body, StandardCharsets.UTF_8));
return Response.ok().build();
}
if (hasQueryParam(uriInfo, "policy")) {
s3Service.authorizeBucketWrite(bucket, "s3:PutBucketPolicy", authorization);
s3Service.putBucketPolicy(bucket, new String(body, StandardCharsets.UTF_8));
return Response.ok().build();
}
if (hasQueryParam(uriInfo, "cors")) {
s3Service.authorizeBucketWrite(bucket, "s3:PutBucketCORS", authorization);
s3Service.putBucketCors(bucket, new String(body, StandardCharsets.UTF_8));
return Response.ok().build();
}
if (hasQueryParam(uriInfo, "lifecycle")) {
s3Service.authorizeBucketWrite(bucket, "s3:PutLifecycleConfiguration", authorization);
String requestedSize = httpHeaders.getHeaderString("x-amz-transition-default-minimum-object-size");
String storedSize = s3Service.putBucketLifecycle(bucket,
new String(body, StandardCharsets.UTF_8), requestedSize);
@@ -295,6 +306,7 @@ public class S3Controller {
.build();
}
if (hasQueryParam(uriInfo, "acl")) {
s3Service.authorizeBucketWrite(bucket, "s3:PutBucketAcl", authorization);
s3Service.putBucketAcl(bucket, new String(body, StandardCharsets.UTF_8),
httpHeaders.getHeaderString("x-amz-acl"),
httpHeaders.getHeaderString("x-amz-grant-read"),
@@ -305,57 +317,59 @@ public class S3Controller {
return Response.ok().build();
}
if (hasQueryParam(uriInfo, "encryption")) {
s3Service.authorizeBucketWrite(bucket, "s3:PutEncryptionConfiguration", authorization);
s3Service.putBucketEncryption(bucket, new String(body, StandardCharsets.UTF_8));
return Response.ok().build();
}
if (hasQueryParam(uriInfo, "publicAccessBlock")) {
s3Service.authorizeBucketWrite(bucket, "s3:PutBucketPublicAccessBlock", authorization);
s3Service.putPublicAccessBlock(bucket, new String(body, StandardCharsets.UTF_8));
return Response.ok().build();
}
if (hasQueryParam(uriInfo, "ownershipControls")) {
s3Service.authorizeBucketWrite(bucket, "s3:PutBucketOwnershipControls", authorization);
s3Service.putBucketOwnershipControls(bucket, new String(body, StandardCharsets.UTF_8));
return Response.ok().build();
}
if (hasQueryParam(uriInfo, "requestPayment")) {
s3Service.authorizeBucketWrite(bucket, "s3:PutBucketRequestPayment", authorization);
s3Service.putBucketRequestPayment(bucket, new String(body, StandardCharsets.UTF_8));
return Response.ok().build();
}
if (hasQueryParam(uriInfo, "accelerate")) {
s3Service.authorizeBucketWrite(bucket, "s3:PutAccelerateConfiguration", authorization);
s3Service.putBucketAccelerateConfiguration(bucket, new String(body, StandardCharsets.UTF_8));
return Response.ok().build();
}
if (hasQueryParam(uriInfo, "replication")) {
s3Service.authorizeBucketWrite(bucket, "s3:PutReplicationConfiguration", authorization);
s3Service.putBucketReplication(bucket, new String(body, StandardCharsets.UTF_8));
return Response.ok().build();
}
// Must be handled here: an unmatched subresource falls through to CreateBucket below,
// which answers a metrics call with BucketAlreadyOwnedByYou.
if (hasQueryParam(uriInfo, "metrics")) {
s3Service.authorizeBucketWrite(bucket, "s3:PutMetricsConfiguration", authorization);
return handlePutBucketMetricsConfiguration(bucket, uriInfo, body);
}
// Same fall-through hazard as metrics: an intelligent-tiering PUT must not become a
// CreateBucket.
if (hasQueryParam(uriInfo, "intelligent-tiering")) {
S3Service.RequestAuthorization authorization = S3RequestAuthorizationParser.parseIfRequired(
s3Service.isAuthEnforced(), httpHeaders, uriInfo);
s3Service.authorizeBucketWrite(bucket, "s3:PutIntelligentTieringConfiguration", authorization);
return handlePutBucketIntelligentTieringConfiguration(bucket, uriInfo, body);
}
// Same fall-through hazard as metrics: an analytics or inventory PUT must not become
// a CreateBucket.
if (hasQueryParam(uriInfo, "analytics")) {
S3Service.RequestAuthorization authorization = S3RequestAuthorizationParser.parseIfRequired(
s3Service.isAuthEnforced(), httpHeaders, uriInfo);
s3Service.authorizeBucketWrite(bucket, "s3:PutAnalyticsConfiguration", authorization);
return handlePutBucketAnalyticsConfiguration(bucket, uriInfo, body);
}
if (hasQueryParam(uriInfo, "inventory")) {
S3Service.RequestAuthorization authorization = S3RequestAuthorizationParser.parseIfRequired(
s3Service.isAuthEnforced(), httpHeaders, uriInfo);
s3Service.authorizeBucketWrite(bucket, "s3:PutInventoryConfiguration", authorization);
return handlePutBucketInventoryConfiguration(bucket, uriInfo, body);
}
s3Service.authorizeCreateBucket(authorization);
String locationConstraint = null;
if (body != null && body.length > 0) {
locationConstraint = XmlParser.extractFirst(new String(body, StandardCharsets.UTF_8),
@@ -401,51 +415,61 @@ public class S3Controller {
@Context HttpHeaders httpHeaders) {
try {
validateRawUri();
S3Service.RequestAuthorization authorization = S3RequestAuthorizationParser.parseIfRequired(
s3Service.isAuthEnforced(), httpHeaders, uriInfo);
if (hasQueryParam(uriInfo, "tagging")) {
s3Service.authorizeBucketWrite(bucket, "s3:PutBucketTagging", authorization);
s3Service.deleteBucketTagging(bucket);
return Response.noContent().build();
}
if (hasQueryParam(uriInfo, "website")) {
s3Service.authorizeBucketWrite(bucket, "s3:DeleteBucketWebsite", authorization);
s3Service.deleteBucketWebsite(bucket);
return Response.noContent().build();
}
if (hasQueryParam(uriInfo, "policy")) {
s3Service.authorizeBucketWrite(bucket, "s3:DeleteBucketPolicy", authorization);
s3Service.deleteBucketPolicy(bucket);
return Response.noContent().build();
}
if (hasQueryParam(uriInfo, "cors")) {
s3Service.authorizeBucketWrite(bucket, "s3:PutBucketCORS", authorization);
s3Service.deleteBucketCors(bucket);
return Response.noContent().build();
}
if (hasQueryParam(uriInfo, "lifecycle")) {
s3Service.authorizeBucketWrite(bucket, "s3:PutLifecycleConfiguration", authorization);
s3Service.deleteBucketLifecycle(bucket);
return Response.noContent().build();
}
if (hasQueryParam(uriInfo, "encryption")) {
s3Service.authorizeBucketWrite(bucket, "s3:PutEncryptionConfiguration", authorization);
s3Service.deleteBucketEncryption(bucket);
return Response.noContent().build();
}
if (hasQueryParam(uriInfo, "publicAccessBlock")) {
s3Service.authorizeBucketWrite(bucket, "s3:PutBucketPublicAccessBlock", authorization);
s3Service.deletePublicAccessBlock(bucket);
return Response.noContent().build();
}
if (hasQueryParam(uriInfo, "ownershipControls")) {
s3Service.authorizeBucketWrite(bucket, "s3:PutBucketOwnershipControls", authorization);
s3Service.deleteBucketOwnershipControls(bucket);
return Response.noContent().build();
}
if (hasQueryParam(uriInfo, "replication")) {
s3Service.authorizeBucketWrite(bucket, "s3:PutReplicationConfiguration", authorization);
s3Service.deleteBucketReplication(bucket);
return Response.noContent().build();
}
if (hasQueryParam(uriInfo, "metrics")) {
// Likewise this must not fall through to deleting the bucket.
s3Service.authorizeBucketWrite(bucket, "s3:PutMetricsConfiguration", authorization);
s3Service.deleteBucketMetricsConfiguration(bucket, requireMetricsId(uriInfo));
return Response.noContent().build();
}
if (hasQueryParam(uriInfo, "intelligent-tiering")) {
// Likewise this must not fall through to deleting the bucket.
S3Service.RequestAuthorization authorization = S3RequestAuthorizationParser.parseIfRequired(
s3Service.isAuthEnforced(), httpHeaders, uriInfo);
s3Service.authorizeBucketWrite(bucket, "s3:PutIntelligentTieringConfiguration", authorization);
s3Service.deleteBucketIntelligentTieringConfiguration(bucket,
requireIntelligentTieringId(uriInfo));
@@ -453,16 +477,12 @@ public class S3Controller {
}
if (hasQueryParam(uriInfo, "analytics")) {
// Likewise this must not fall through to deleting the bucket.
S3Service.RequestAuthorization authorization = S3RequestAuthorizationParser.parseIfRequired(
s3Service.isAuthEnforced(), httpHeaders, uriInfo);
s3Service.authorizeBucketWrite(bucket, "s3:PutAnalyticsConfiguration", authorization);
s3Service.deleteBucketAnalyticsConfiguration(bucket, requireAnalyticsId(uriInfo));
return Response.noContent().build();
}
if (hasQueryParam(uriInfo, "inventory")) {
// Likewise this must not fall through to deleting the bucket.
S3Service.RequestAuthorization authorization = S3RequestAuthorizationParser.parseIfRequired(
s3Service.isAuthEnforced(), httpHeaders, uriInfo);
s3Service.authorizeBucketWrite(bucket, "s3:PutInventoryConfiguration", authorization);
s3Service.deleteBucketInventoryConfiguration(bucket, requireInventoryId(uriInfo));
return Response.noContent().build();
@@ -473,6 +493,7 @@ public class S3Controller {
throw new AwsException("MethodNotAllowed",
"The specified method is not allowed against this resource.", 405);
}
s3Service.authorizeBucketWrite(bucket, "s3:DeleteBucket", authorization);
s3Service.deleteBucket(bucket);
return Response.noContent().build();
} catch (AwsException e) {
@@ -764,6 +764,20 @@ public class S3Service implements Resettable, ResourceProvider {
authorizeS3Read(bucketName, null, null, action, bucketArn, authorization);
}
/**
* CreateBucket is never anonymous on AWS: there is no bucket policy to consult yet, so an
* unsigned request is denied outright and a signed one only needs a known access key.
*/
void authorizeCreateBucket(RequestAuthorization authorization) {
if (!enforceAuth) {
return;
}
authorizeSignedRequest(authorization);
if (isUnsignedRequest(authorization)) {
throw new AwsException("AccessDenied", "Access Denied", 403);
}
}
void authorizeBucketWrite(String bucketName, String action, RequestAuthorization authorization) {
if (!enforceAuth) {
return;
@@ -780,6 +794,12 @@ public class S3Service implements Resettable, ResourceProvider {
Bucket bucket = bucketStore.get(bucketName)
.orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404));
// AWS lets only an identity in the bucket owner's account manage the bucket policy; the
// policy itself can never grant PutBucketPolicy or DeleteBucketPolicy to an anonymous caller.
if (isBucketPolicyAction(action)) {
throw new AwsException("AccessDenied", "Access Denied", 403);
}
String bucketArn = S3PublicAccessEvaluator.bucketArn(bucketName);
S3PublicAccessEvaluator.PublicAccessDecision policyDecision =
S3PublicAccessEvaluator.publicPolicyDecision(objectMapper, bucket.getPolicy(), action, bucketArn);
@@ -931,6 +951,10 @@ public class S3Service implements Resettable, ResourceProvider {
return "s3:PutObject".equals(action);
}
private static boolean isBucketPolicyAction(String action) {
return "s3:PutBucketPolicy".equals(action) || "s3:DeleteBucketPolicy".equals(action);
}
private static boolean isUnsignedRequest(RequestAuthorization authorization) {
return authorization == null || !authorization.signed();
}