fix(s3): report the default Block Public Access settings for a new bucket (#5434)

GetPublicAccessBlock answered NoSuchPublicAccessBlockConfiguration for a
bucket nobody had configured. AWS has applied all four settings to every
new bucket since April 2023, so a fresh bucket reads back with
BlockPublicAcls, IgnorePublicAcls, BlockPublicPolicy and
RestrictPublicBuckets all true. The 404 only follows a
DeletePublicAccessBlock.

Bucket now remembers whether its configuration was deleted, and Get
returns the default when none is stored and none was deleted. The default
is reported, not stored, so enforcement on ACLs and policies is unchanged.
This commit is contained in:
Nazimuddin Tajuddin Basha authored and GitHub committed 2026-10-13 15:14:44 -04:00
1 parent daef71ca76
commit e52ce672fe
4 files changed
+109 -3

No files matched your search

@@ -4893,20 +4893,38 @@ public class S3Service implements Resettable, ResourceProvider {
bucketStore.put(bucketName, bucket);
}
/**
* What AWS reports for a bucket nobody configured: since April 2023 every new bucket starts with
* all four Block Public Access settings on. This is only what GetPublicAccessBlock answers. The
* bucket stores no configuration, so enforcement on the write paths is unchanged.
*/
static final String DEFAULT_PUBLIC_ACCESS_BLOCK_XML = new XmlBuilder()
.start("PublicAccessBlockConfiguration", AwsNamespaces.S3)
.elem("BlockPublicAcls", true)
.elem("IgnorePublicAcls", true)
.elem("BlockPublicPolicy", true)
.elem("RestrictPublicBuckets", true)
.end("PublicAccessBlockConfiguration")
.build();
public String getPublicAccessBlock(String bucketName) {
Bucket bucket = bucketStore.get(bucketName)
.orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404));
if (bucket.getPublicAccessBlockConfiguration() == null) {
if (bucket.getPublicAccessBlockConfiguration() != null) {
return bucket.getPublicAccessBlockConfiguration();
}
if (bucket.isPublicAccessBlockDeleted()) {
throw new AwsException("NoSuchPublicAccessBlockConfiguration",
"The public access block configuration was not found", 404);
}
return bucket.getPublicAccessBlockConfiguration();
return DEFAULT_PUBLIC_ACCESS_BLOCK_XML;
}
public void putPublicAccessBlock(String bucketName, String xml) {
Bucket bucket = bucketStore.get(bucketName)
.orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404));
bucket.setPublicAccessBlockConfiguration(xml);
bucket.setPublicAccessBlockDeleted(false);
bucketStore.put(bucketName, bucket);
}
@@ -4914,6 +4932,7 @@ public class S3Service implements Resettable, ResourceProvider {
Bucket bucket = bucketStore.get(bucketName)
.orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404));
bucket.setPublicAccessBlockConfiguration(null);
bucket.setPublicAccessBlockDeleted(true);
bucketStore.put(bucketName, bucket);
}
@@ -27,6 +27,7 @@ public class Bucket {
private String encryptionConfiguration; // XML string
private String replicationConfiguration; // XML string
private String publicAccessBlockConfiguration; // XML string
private boolean publicAccessBlockDeleted;
private String ownershipControlsConfiguration; // XML string
private String requestPaymentPayer; // "BucketOwner" (default) or "Requester"; null until first PUT
private String accelerateStatus; // "Enabled" or "Suspended"; null until first PUT
@@ -113,6 +114,12 @@ public class Bucket {
this.publicAccessBlockConfiguration = publicAccessBlockConfiguration;
}
/** True once DeletePublicAccessBlock removed the configuration; a never-configured bucket is false. */
public boolean isPublicAccessBlockDeleted() { return publicAccessBlockDeleted; }
public void setPublicAccessBlockDeleted(boolean publicAccessBlockDeleted) {
this.publicAccessBlockDeleted = publicAccessBlockDeleted;
}
public String getOwnershipControlsConfiguration() { return ownershipControlsConfiguration; }
public void setOwnershipControlsConfiguration(String ownershipControlsConfiguration) {
this.ownershipControlsConfiguration = ownershipControlsConfiguration;
@@ -133,7 +133,9 @@ class CloudFormationS3BucketPropertiesIntegrationTest {
given().when().get("/" + bucket + "?versioning").then().statusCode(200).body(not(containsString("<Status>")));
given().when().get("/" + bucket + "?lifecycle").then().statusCode(404);
given().when().get("/" + bucket + "?publicAccessBlock").then().statusCode(404);
// A bucket nobody configured reports the AWS default rather than a 404.
given().when().get("/" + bucket + "?publicAccessBlock").then().statusCode(200)
.body(containsString("<BlockPublicAcls>true</BlockPublicAcls>"));
}
@Test
@@ -0,0 +1,78 @@
package io.github.hectorvent.floci.services.s3;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.s3.model.Bucket;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* Since April 2023 AWS creates every bucket with all four Block Public Access settings on, so
* GetPublicAccessBlock on a bucket nobody configured returns them rather than a 404. Only a bucket
* whose configuration was deleted answers NoSuchPublicAccessBlockConfiguration.
*/
class S3DefaultPublicAccessBlockTest {
@TempDir
Path tempDir;
private S3Service s3Service;
@BeforeEach
void setUp() {
s3Service = new S3Service(new InMemoryStorage<>(), new InMemoryStorage<>(), tempDir.resolve("s3"), false);
}
@Test
void newBucketReportsAllFourSettingsBlocked() {
s3Service.createBucket("fresh", "us-east-1");
S3BlockPublicAccessSettings settings = S3BlockPublicAccessSettings.parse(s3Service.getPublicAccessBlock("fresh"));
assertEquals(new S3BlockPublicAccessSettings(true, true, true, true), settings);
}
@Test
void deletingTheConfigurationLeavesNothingToRead() {
s3Service.createBucket("deleted", "us-east-1");
s3Service.deletePublicAccessBlock("deleted");
AwsException e = assertThrows(AwsException.class, () -> s3Service.getPublicAccessBlock("deleted"));
assertEquals("NoSuchPublicAccessBlockConfiguration", e.getErrorCode());
assertEquals(404, e.getHttpStatus());
}
@Test
void anExplicitConfigurationReplacesTheDefault() {
s3Service.createBucket("custom", "us-east-1");
String xml = "<PublicAccessBlockConfiguration><BlockPublicAcls>false</BlockPublicAcls>"
+ "<IgnorePublicAcls>false</IgnorePublicAcls><BlockPublicPolicy>false</BlockPublicPolicy>"
+ "<RestrictPublicBuckets>false</RestrictPublicBuckets></PublicAccessBlockConfiguration>";
s3Service.putPublicAccessBlock("custom", xml);
assertEquals(xml, s3Service.getPublicAccessBlock("custom"));
}
@Test
void theDefaultIsReportedNotStoredSoEnforcementIsUnchanged() {
Bucket bucket = s3Service.createBucket("report-only", "us-east-1");
assertEquals(null, bucket.getPublicAccessBlockConfiguration());
}
@Test
void repeatingCreateBucketKeepsAChangedConfiguration() {
s3Service.createBucket("again", "us-east-1");
s3Service.deletePublicAccessBlock("again");
s3Service.createBucket("again", "us-east-1");
assertThrows(AwsException.class, () -> s3Service.getPublicAccessBlock("again"));
}
}