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

@@ -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"));
}
}