fix(s3): enforce the default Block Public Access on buckets nobody configured (#7036)

This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-10-31 05:20:19 -04:00
1 parent ebdac90574
commit 76b7eed371
18 files changed
+181 -25

No files matched your search

@@ -153,6 +153,7 @@ class CloudFrontS3AuthServingTest {
s3Service.putObject(bucket, "index.html", body.getBytes(StandardCharsets.UTF_8),
"text/html", Map.of());
if (publicRead) {
s3Service.deletePublicAccessBlock(bucket);
s3Service.putBucketPolicy(bucket, publicReadPolicy(bucket));
}
@@ -67,6 +67,7 @@ class ResourcePolicyPrincipalTypeEnforcementIntegrationTest {
}
private static void putBucketPolicy(String bucket, String policy) {
given().filter(OWNER).when().delete("/" + bucket + "?publicAccessBlock").then().statusCode(204);
given().filter(OWNER).contentType("application/json").body(policy)
.when().put("/" + bucket + "?policy").then().statusCode(200);
}
@@ -181,6 +181,7 @@ class StsIssuedSessionEnforcementIntegrationTest {
iam("DeleteUserPolicy", "UserName", userName, "PolicyName", "inline");
iam("DeleteUser", "UserName", userName);
iam("CreateUser", "UserName", userName);
given().filter(admin).when().delete("/" + bucket + "?publicAccessBlock").then().statusCode(204);
given().filter(admin).contentType("application/json").body("""
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":"*",
"Action":"s3:GetObject","Resource":"arn:aws:s3:::%s/*"}]}""".formatted(bucket))
@@ -35,6 +35,12 @@ class S3AclIntegrationTest {
.put("/" + BUCKET + "?ownershipControls")
.then()
.statusCode(200);
given()
.when()
.delete("/" + BUCKET + "?publicAccessBlock")
.then()
.statusCode(204);
}
@Test
@@ -85,6 +85,15 @@ class S3AuthEnforcementIntegrationTest {
.then().statusCode(400).body(containsString("AuthorizationHeaderMalformed"));
}
private static void createBucketWithoutPublicAccessBlock(String bucket) {
given().filter(LOCAL_SIGNER).when().put("/" + bucket).then().statusCode(200);
deletePublicAccessBlock(bucket);
}
private static void deletePublicAccessBlock(String bucket) {
given().filter(LOCAL_SIGNER).when().delete("/" + bucket + "?publicAccessBlock").then().statusCode(204);
}
private static void enableLegacyAcls(String bucket) {
given().filter(LOCAL_SIGNER)
.body("<OwnershipControls><Rule><ObjectOwnership>ObjectWriter</ObjectOwnership></Rule></OwnershipControls>")
@@ -114,6 +123,13 @@ class S3AuthEnforcementIntegrationTest {
.then()
.statusCode(200);
given()
.filter(LOCAL_SIGNER)
.when()
.delete("/" + PUBLIC_BUCKET + "?publicAccessBlock")
.then()
.statusCode(204);
given()
.filter(LOCAL_SIGNER)
.contentType("application/json")
@@ -270,7 +286,7 @@ class S3AuthEnforcementIntegrationTest {
@Test
@Order(12)
void websiteRootAuthorizesIndexObjectReadNotBucketList() {
given().filter(LOCAL_SIGNER).when().put("/" + WEBSITE_BUCKET).then().statusCode(200);
createBucketWithoutPublicAccessBlock(WEBSITE_BUCKET);
given()
.filter(LOCAL_SIGNER)
@@ -319,7 +335,7 @@ class S3AuthEnforcementIntegrationTest {
@Test
@Order(13)
void websiteRootUsesErrorDocumentForDeniedIndexObject() {
given().filter(LOCAL_SIGNER).when().put("/" + WEBSITE_ERROR_BUCKET).then().statusCode(200);
createBucketWithoutPublicAccessBlock(WEBSITE_ERROR_BUCKET);
given()
.filter(LOCAL_SIGNER)
@@ -371,7 +387,7 @@ class S3AuthEnforcementIntegrationTest {
@Test
@Order(14)
void bucketAclPublicReadAllowsUnsignedList() {
given().filter(LOCAL_SIGNER).when().put("/" + BUCKET_ACL_BUCKET).then().statusCode(200);
createBucketWithoutPublicAccessBlock(BUCKET_ACL_BUCKET);
enableLegacyAcls(BUCKET_ACL_BUCKET);
given()
@@ -402,7 +418,7 @@ class S3AuthEnforcementIntegrationTest {
@Test
@Order(15)
void explicitBucketPolicyDenyOverridesPublicObjectAcl() {
given().filter(LOCAL_SIGNER).when().put("/" + DENY_BUCKET).then().statusCode(200);
createBucketWithoutPublicAccessBlock(DENY_BUCKET);
enableLegacyAcls(DENY_BUCKET);
given()
@@ -517,7 +533,7 @@ class S3AuthEnforcementIntegrationTest {
@Test
@Order(20)
void missingObjectRequiresListBucketToReturnNoSuchKey() {
given().filter(LOCAL_SIGNER).when().put("/" + GET_ONLY_BUCKET).then().statusCode(200);
createBucketWithoutPublicAccessBlock(GET_ONLY_BUCKET);
given()
.filter(LOCAL_SIGNER)
@@ -546,7 +562,7 @@ class S3AuthEnforcementIntegrationTest {
@Test
@Order(21)
void versionedObjectReadRequiresGetObjectVersion() {
given().filter(LOCAL_SIGNER).when().put("/" + VERSION_BUCKET).then().statusCode(200);
createBucketWithoutPublicAccessBlock(VERSION_BUCKET);
given()
.filter(LOCAL_SIGNER)
@@ -859,7 +875,7 @@ class S3AuthEnforcementIntegrationTest {
@Test
@Order(32)
void bucketPolicyCanExplicitlyAllowAnonymousWrite() {
given().filter(LOCAL_SIGNER).when().put("/" + PUBLIC_WRITE_BUCKET).then().statusCode(200);
createBucketWithoutPublicAccessBlock(PUBLIC_WRITE_BUCKET);
given()
.filter(LOCAL_SIGNER)
@@ -1154,7 +1170,7 @@ class S3AuthEnforcementIntegrationTest {
// Per AWS's ACL docs, a bucket-ACL WRITE grant to a non-owner (like AllUsers) "denies
// non-owners the ability to overwrite or delete existing objects" -- it only lets them
// create new ones.
given().filter(LOCAL_SIGNER).when().put("/" + PUBLIC_WRITE_ACL_BUCKET).then().statusCode(200);
createBucketWithoutPublicAccessBlock(PUBLIC_WRITE_ACL_BUCKET);
enableLegacyAcls(PUBLIC_WRITE_ACL_BUCKET);
given()
@@ -1199,7 +1215,7 @@ class S3AuthEnforcementIntegrationTest {
@Test
@Order(39)
void explicitPolicyDenyOverridesPublicWriteAcl() {
given().filter(LOCAL_SIGNER).when().put("/" + DENY_WRITE_ACL_BUCKET).then().statusCode(200);
createBucketWithoutPublicAccessBlock(DENY_WRITE_ACL_BUCKET);
enableLegacyAcls(DENY_WRITE_ACL_BUCKET);
given()
@@ -1275,7 +1291,7 @@ class S3AuthEnforcementIntegrationTest {
@Test
@Order(41)
void deleteObjectPolicyGrantDoesNotAuthorizeVersionedDelete() {
given().filter(LOCAL_SIGNER).when().put("/" + DELETE_VERSION_BUCKET).then().statusCode(200);
createBucketWithoutPublicAccessBlock(DELETE_VERSION_BUCKET);
given()
.filter(LOCAL_SIGNER)
@@ -1325,6 +1341,7 @@ class S3AuthEnforcementIntegrationTest {
// that's protected by governance mode", so the header alone asks for nothing more.
given().filter(LOCAL_SIGNER).header("x-amz-bucket-object-lock-enabled", "true")
.when().put("/" + BYPASS_BUCKET).then().statusCode(200);
deletePublicAccessBlock(BYPASS_BUCKET);
String free = given().filter(LOCAL_SIGNER).body("unlocked")
.when().put("/" + BYPASS_BUCKET + "/free.txt")
.then().statusCode(200).extract().header("x-amz-version-id");
@@ -1398,7 +1415,7 @@ class S3AuthEnforcementIntegrationTest {
// Floci has no request context to evaluate a Condition against, so a conditional Deny
// is treated as applying (fail closed) -- consistent with S3PublicAccessEvaluatorTest's
// conditionalDenyFailsClosedAndOverridesAllow, this must also override the ACL fallback.
given().filter(LOCAL_SIGNER).when().put("/" + CONDITIONAL_DENY_ACL_BUCKET).then().statusCode(200);
createBucketWithoutPublicAccessBlock(CONDITIONAL_DENY_ACL_BUCKET);
enableLegacyAcls(CONDITIONAL_DENY_ACL_BUCKET);
given()
@@ -1430,7 +1447,7 @@ class S3AuthEnforcementIntegrationTest {
@Test
@Order(44)
void batchDeleteObjectsPolicyGrantDoesNotAuthorizeVersionedDelete() {
given().filter(LOCAL_SIGNER).when().put("/" + BATCH_DELETE_VERSION_BUCKET).then().statusCode(200);
createBucketWithoutPublicAccessBlock(BATCH_DELETE_VERSION_BUCKET);
given()
.filter(LOCAL_SIGNER)
@@ -1497,6 +1514,7 @@ class S3AuthEnforcementIntegrationTest {
.put("/" + bucket)
.then()
.statusCode(200);
deletePublicAccessBlock(bucket);
String unlockedVersion = given()
.filter(LOCAL_SIGNER)
@@ -1677,6 +1695,7 @@ class S3AuthEnforcementIntegrationTest {
.put("/" + BUCKET_CONFIG_BUCKET)
.then()
.statusCode(200);
deletePublicAccessBlock(BUCKET_CONFIG_BUCKET);
given()
.when()
@@ -2335,7 +2354,7 @@ class S3AuthEnforcementIntegrationTest {
@Order(56)
void conditionalDeleteByETagAlsoNeedsGetObject() {
String bucket = "auth-conditional-delete-bucket";
given().filter(LOCAL_SIGNER).when().put("/" + bucket).then().statusCode(200);
createBucketWithoutPublicAccessBlock(bucket);
String eTag = given().filter(LOCAL_SIGNER).body("etag-guarded")
.when().put("/" + bucket + "/etag.txt")
.then().statusCode(200).extract().header("ETag");
@@ -2455,7 +2474,7 @@ class S3AuthEnforcementIntegrationTest {
<Rule><ObjectOwnership>BucketOwnerEnforced</ObjectOwnership></Rule>
</OwnershipControls>
""";
given().filter(LOCAL_SIGNER).when().put("/" + bucket).then().statusCode(200);
createBucketWithoutPublicAccessBlock(bucket);
enableLegacyAcls(bucket);
given().filter(LOCAL_SIGNER).header("x-amz-acl", "public-read").body("public data")
.when().put("/" + bucket + "/data.txt")
@@ -2499,7 +2518,7 @@ class S3AuthEnforcementIntegrationTest {
@Order(59)
void anAnonymousReadNamingTheNullVersionIsCheckedAgainstThatVersionsAcl() {
String bucket = "auth-null-version-acl-bucket";
given().filter(LOCAL_SIGNER).when().put("/" + bucket).then().statusCode(200);
createBucketWithoutPublicAccessBlock(bucket);
enableLegacyAcls(bucket);
given().filter(LOCAL_SIGNER).body("old").when().put("/" + bucket + "/old").then().statusCode(200);
given().filter(LOCAL_SIGNER)
@@ -117,6 +117,7 @@ class S3BlockPublicAccessEnforcementIntegrationTest {
@Test
void ignorePublicAclsWithholdsAnonymousListBucketOnAPublicBucketAcl() {
String bucket = createBucket();
deleteBucketPublicAccessBlock(bucket);
putObject(bucket, "object.txt");
given().filter(OWNER).header("x-amz-acl", "public-read")
.when().put("/" + bucket + "?acl")
@@ -142,6 +143,7 @@ class S3BlockPublicAccessEnforcementIntegrationTest {
private static String bucketWithPublicPolicy() {
String bucket = createBucket();
deleteBucketPublicAccessBlock(bucket);
putObject(bucket, "object.txt");
putBucketPolicy(bucket, PUBLIC_READ_POLICY.formatted(bucket));
return bucket;
@@ -149,6 +151,7 @@ class S3BlockPublicAccessEnforcementIntegrationTest {
private static String bucketWithPublicObjectAcl() {
String bucket = createBucket();
deleteBucketPublicAccessBlock(bucket);
putObject(bucket, "object.txt");
given().filter(OWNER).header("x-amz-acl", "public-read")
.when().put("/" + bucket + "/object.txt?acl")
@@ -177,6 +180,10 @@ class S3BlockPublicAccessEnforcementIntegrationTest {
.then().statusCode(200);
}
private static void deleteBucketPublicAccessBlock(String bucket) {
given().filter(OWNER).when().delete("/" + bucket + "?publicAccessBlock").then().statusCode(204);
}
private static void putBucketPublicAccessBlock(String bucket, boolean blockPublicAcls,
boolean ignorePublicAcls, boolean blockPublicPolicy,
boolean restrictPublicBuckets) {
@@ -86,9 +86,20 @@ class S3BlockPublicAccessIntegrationTest {
}
@Test
void aPublicBucketPolicyIsAcceptedWithoutBlockPublicPolicy() {
void aPublicBucketPolicyIsRefusedWithoutBlockPublicPolicyConfigured() {
String bucket = createBucket();
given().body(PUBLIC_POLICY.formatted(bucket))
.when().put("/" + bucket + "?policy")
.then().statusCode(403)
.body(containsString("AccessDenied"));
}
@Test
void aPublicBucketPolicyIsAcceptedOnceBlockPublicPolicyIsDeleted() {
String bucket = createBucket();
deleteBucketPublicAccessBlock(bucket);
given().body(PUBLIC_POLICY.formatted(bucket))
.when().put("/" + bucket + "?policy")
.then().statusCode(200);
@@ -97,6 +108,7 @@ class S3BlockPublicAccessIntegrationTest {
@Test
void blockPublicPolicyDoesNotRemoveAPolicyStoredBeforeItWasSet() {
String bucket = createBucket();
deleteBucketPublicAccessBlock(bucket);
given().body(PUBLIC_POLICY.formatted(bucket))
.when().put("/" + bucket + "?policy")
.then().statusCode(200);
@@ -197,6 +209,10 @@ class S3BlockPublicAccessIntegrationTest {
return bucket;
}
private static void deleteBucketPublicAccessBlock(String bucket) {
given().when().delete("/" + bucket + "?publicAccessBlock").then().statusCode(204);
}
private static void putBucketPublicAccessBlock(String bucket, boolean blockPublicAcls,
boolean ignorePublicAcls, boolean blockPublicPolicy,
boolean restrictPublicBuckets) {
@@ -569,6 +569,7 @@ class S3CopyObjectSourcePermissionIntegrationTest {
}
private static void putPublicObjectAsRoot(String bucket, String key, String body) {
deletePublicAccessBlockAsRoot(bucket, ROOT_SIGNER);
given()
.filter(ROOT_SIGNER)
.header("x-amz-acl", "public-read")
@@ -610,6 +611,7 @@ class S3CopyObjectSourcePermissionIntegrationTest {
private static void putBucketPolicyAsRoot(
String bucket, String policy, S3RequestSigner signer) {
deletePublicAccessBlockAsRoot(bucket, signer);
given()
.filter(signer)
.contentType("application/json")
@@ -620,6 +622,10 @@ class S3CopyObjectSourcePermissionIntegrationTest {
.statusCode(200);
}
private static void deletePublicAccessBlockAsRoot(String bucket, S3RequestSigner signer) {
given().filter(signer).when().delete("/" + bucket + "?publicAccessBlock").then().statusCode(204);
}
private static String denyGetObjectPolicy(String bucket) {
return """
{"Version":"2012-10-17","Statement":[
@@ -61,12 +61,88 @@ class S3DefaultPublicAccessBlockTest {
}
@Test
void theDefaultIsReportedNotStoredSoEnforcementIsUnchanged() {
void theDefaultIsReportedNotStored() {
Bucket bucket = s3Service.createBucket("report-only", "us-east-1");
assertEquals(null, bucket.getPublicAccessBlockConfiguration());
}
@Test
void aPublicPolicyIsRefusedOnABucketNobodyConfigured() {
s3Service.createBucket("fresh-policy", "us-east-1");
AwsException e = assertThrows(AwsException.class,
() -> s3Service.putBucketPolicy("fresh-policy", publicPolicy("fresh-policy")));
assertEquals("AccessDenied", e.getErrorCode());
assertThrows(AwsException.class, () -> s3Service.getBucketPolicy("fresh-policy"));
}
@Test
void aPolicyLimitedToTheAccountIsAcceptedOnAFreshBucket() {
s3Service.createBucket("private-policy", "us-east-1");
s3Service.putBucketPolicy("private-policy", policyFor("{\"AWS\":\"arn:aws:iam::000000000000:root\"}",
"private-policy"));
assertEquals(true, s3Service.getBucketPolicy("private-policy").contains("root"));
}
@Test
void aPublicPolicyIsAcceptedOnceTheConfigurationIsExplicitlyOff() {
s3Service.createBucket("explicit-off", "us-east-1");
s3Service.putPublicAccessBlock("explicit-off", "<PublicAccessBlockConfiguration>"
+ "<BlockPublicAcls>false</BlockPublicAcls><IgnorePublicAcls>false</IgnorePublicAcls>"
+ "<BlockPublicPolicy>false</BlockPublicPolicy><RestrictPublicBuckets>false</RestrictPublicBuckets>"
+ "</PublicAccessBlockConfiguration>");
s3Service.putBucketPolicy("explicit-off", publicPolicy("explicit-off"));
assertEquals(true, s3Service.getBucketPolicy("explicit-off").contains("GetObject"));
}
@Test
void aPublicPolicyIsAcceptedOnceTheConfigurationIsDeleted() {
s3Service.createBucket("deleted-policy", "us-east-1");
s3Service.deletePublicAccessBlock("deleted-policy");
s3Service.putBucketPolicy("deleted-policy", publicPolicy("deleted-policy"));
assertEquals(true, s3Service.getBucketPolicy("deleted-policy").contains("GetObject"));
}
@Test
void aPublicBucketAclIsRefusedOnAnObjectWriterBucketNobodyConfigured() {
s3Service.createBucket("object-writer", "us-east-1");
s3Service.putBucketOwnershipControls("object-writer",
"<OwnershipControls><Rule><ObjectOwnership>ObjectWriter</ObjectOwnership></Rule></OwnershipControls>");
AwsException e = assertThrows(AwsException.class, () -> s3Service.putBucketAcl("object-writer", null,
"public-read", null, null, null, null, null));
assertEquals("AccessDenied", e.getErrorCode());
assertEquals(403, e.getHttpStatus());
}
@Test
void aPublicBucketAclIsAcceptedOnceTheConfigurationIsDeleted() {
s3Service.createBucket("object-writer-open", "us-east-1");
s3Service.putBucketOwnershipControls("object-writer-open",
"<OwnershipControls><Rule><ObjectOwnership>ObjectWriter</ObjectOwnership></Rule></OwnershipControls>");
s3Service.deletePublicAccessBlock("object-writer-open");
s3Service.putBucketAcl("object-writer-open", null, "public-read", null, null, null, null, null);
}
private static String publicPolicy(String bucket) {
return policyFor("\"*\"", bucket);
}
private static String policyFor(String principal, String bucket) {
return "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":" + principal
+ ",\"Action\":\"s3:GetObject\",\"Resource\":\"arn:aws:s3:::" + bucket + "/*\"}]}";
}
@Test
void repeatingCreateBucketKeepsAChangedConfiguration() {
s3Service.createBucket("again", "us-east-1");
@@ -443,6 +443,7 @@ class S3GlobalBucketNamespaceTest {
caller.set(ACCOUNT_A);
globalNs.createBucket("shared-metadata-bucket", "us-east-1");
globalNs.deletePublicAccessBlock("shared-metadata-bucket");
globalNs.putBucketOwnershipControls("shared-metadata-bucket", "<OwnershipControls><Rule>"
+ "<ObjectOwnership>ObjectWriter</ObjectOwnership></Rule></OwnershipControls>");
globalNs.putBucketVersioning("shared-metadata-bucket", "Enabled");
@@ -46,8 +46,11 @@ class S3NullVersionLockPromotionTest {
case "legal-hold" -> service.putObjectLegalHold("bucket", "key", "null", "ON");
case "retention" -> service.putObjectRetention("bucket", "key", "null", "COMPLIANCE",
Instant.now().plusSeconds(3600), false);
case "acl" -> service.putObjectAcl("bucket", "key", "null", "", "public-read",
null, null, null, null, null);
case "acl" -> {
service.deletePublicAccessBlock("bucket");
service.putObjectAcl("bucket", "key", "null", "", "public-read",
null, null, null, null, null);
}
default -> throw new IllegalArgumentException(operation);
}
objects.promotion.get(5, TimeUnit.SECONDS);
@@ -156,6 +156,7 @@ class S3ObjectOperationsBucketPolicyIntegrationTest {
String bucket = bucket(false);
owner().body("<OwnershipControls><Rule><ObjectOwnership>ObjectWriter</ObjectOwnership></Rule></OwnershipControls>")
.when().put(bucket + "?ownershipControls").then().statusCode(200);
owner().when().delete(bucket + "?publicAccessBlock").then().statusCode(204);
owner().header("x-amz-acl", "public-read-write").when().put(bucket + "?acl").then().statusCode(200);
given().header("x-amz-acl", "bucket-owner-full-control").body("acl").when().put(bucket + "/acl")
@@ -157,6 +157,10 @@ class S3OwnerIdentityIntegrationTest {
.when().put("/" + bucket + "?ownershipControls")
.then().statusCode(200);
given().header("Authorization", authorization(ACCOUNT_TWO))
.when().delete("/" + bucket + "?publicAccessBlock")
.then().statusCode(204);
given().header("Authorization", authorization(ACCOUNT_TWO))
.header("x-amz-acl", "public-read")
.when().put("/" + bucket + "/public.txt")
@@ -84,6 +84,7 @@ class S3PolicyStatusIntegrationTest {
String bucket = "policy-status-" + UUID.randomUUID();
cleanup.register(() -> EnforcementFixtures.removeBucket(baseUri.getPort(), auth("test", "s3"), bucket));
admin.createBucket(request -> request.bucket(bucket));
admin.deletePublicAccessBlock(request -> request.bucket(bucket));
return bucket;
}