Skip to content

fix(s3): route ?publicAccessBlock so DELETE stops deleting the bucket (#446) - #459

Merged
scttfrdmn merged 2 commits into
mainfrom
fix/446-s3-public-access-block
Aug 1, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
fix/446-s3-public-access-block

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #446.

The defect

The ?publicAccessBlock subresource was unrouted on all three verbs. It is addressed
as a bare query key on the bucket itself, so each request fell through to the
bucket-level operation for its method:

Request Reached Consumer saw
DELETE /bucket?publicAccessBlock DeleteBucket 204 — and the bucket was gone
PUT /bucket?publicAccessBlock CreateBucket 409 BucketAlreadyExists
GET /bucket?publicAccessBlock ListObjects 200 + a ListBucketResult body

The DELETE case is data loss with no signal. On an empty bucket it returned 204 —
exactly what a successful DeletePublicAccessBlock returns — having destroyed the
bucket and its contents. The caller had nothing to go on: the next operation on that
bucket failed with NoSuchBucket for no visible reason. On a non-empty bucket it
returned 409 BucketNotEmpty, naming an operation the caller never issued.

PUT failed on exactly the shape SDKs send. PutPublicAccessBlock immediately after
CreateBucket is what the AWS SDKs and CloudFormation both do, so hardening a bucket
failed on the already-owned bucket it was meant to configure.

GET returned a confident wrong answer. A ListBucketResult body parses into an
empty PublicAccessBlockConfiguration, so a consumer read "public access is not
blocked" for a bucket whose settings substrate had never stored.

One correction to the report, which assumed GET was routed and returning an empty
configuration: it was not routed at all. The empty configuration the reporter observed
was their SDK parsing ListObjects' XML.

The fix

Each verb is routed ahead of its fall-through in parseS3Operation, and the three
operations are modeled in a new emulator/s3_publicaccessblock.go:

PUT    /bucket?publicAccessBlock  → 200, empty body
GET    /bucket?publicAccessBlock  → 200 + PublicAccessBlockConfiguration, or 404
DELETE /bucket?publicAccessBlock  → 204
  • PUT replaces the document wholesale rather than merging. The four settings are each
    Required: No, and every one the body omits is recorded — and reported back — as
    false, matching S3.
  • GET on a bucket with no configuration is 404 NoSuchPublicAccessBlockConfiguration, which keeps it distinguishable from the
    all-false configuration a consumer relaxing a block writes on purpose.
  • DELETE is idempotent and touches nothing but the configuration.

The configuration lives under its own state key rather than as a field on S3Bucket.
That is what makes DeletePublicAccessBlock a delete rather than a partial rewrite of
the bucket record — so it is structurally incapable of touching the bucket, not
merely tested against doing so.

On NoSuchPublicAccessBlockConfiguration

This was the one open question, and it is not answerable from the S3 API reference: the
code is absent from the S3 2006-03-01 model, and the bucket-level GetPublicAccessBlock
page has no Errors section at all. Rather than assume, it was corroborated from three
independent sources, recorded in the code comment:

  • terraform-provider-aws handles this exact code in its bucket-level
    internal/service/s3 package (not just s3control), on both GetPublicAccessBlock
    (→ NotFound) and DeletePublicAccessBlock (→ treated as success, which is why DELETE
    is idempotent here).
  • The s3control model's own shape documentation describes the same condition.
  • boto/boto3 #3627 supplies the live message string verbatim: "The public access
    block configuration was not found."

Two deliberate limits

Both are documented in docs/services.md rather than left to be discovered:

  1. Substrate does not apply S3's April 2023 default of enabling all four settings on
    a newly created bucket. The blog post confirms that default applies to buckets created
    via the API, CLI, SDKs and CloudFormation — but it is a property of AWS-managed
    account and organization state substrate does not model, and seeding every new bucket
    with a configuration would make the NoSuchPublicAccessBlockConfiguration path — the
    branch a consumer's error handling exists for — unreachable through the public API.
  2. The settings are recorded, not enforced. Nothing rejects a public ACL or bucket
    policy. Those are the resource-internal consequences of the setting rather than the
    setting itself, per the scope boundary in CLAUDE.md. Filed as S3: Block Public Access settings are recorded but not enforced #458, which also
    argues why RestrictPublicBuckets and IgnorePublicAcls should stay recorded-only
    until substrate models anonymous access evaluation.

Tests

New emulator/s3_publicaccessblock_test.go, 11 tests:

  • DeleteKeepsBucket — the regression guard, covering both of the reporter's
    reproductions: empty bucket (was silently deleted) and non-empty (was 409 BucketNotEmpty). Asserts the bucket still appears in ListBuckets, the object
    survives, and the configuration is gone.
  • RoundTrip — PUT → 200 with an empty body, then GET returns what was
    written. The GET assertion is what proves PUT was routed rather than merely not
    crashing.
  • GetUnset — 404 NoSuchPublicAccessBlockConfiguration, and no ListBucketResult.
  • AllFalseIsNotUnset — the two states stay distinguishable.
  • PartialBody / Overwrite — omitted settings read back false; PUT
    replaces rather than merges.
  • DeleteIsIdempotent, NoSuchBucket (all three verbs, and none of them creates
    the bucket it names), MalformedBody, VirtualHostStyle,
    SiblingSubresourcesUnaffected (ListObjects and DeleteBucket still reach the
    fall-throughs the new checks were inserted ahead of).

Mutation-tested, per the batch practice: each of the three routing sites removed, and
seven handler behaviours broken (GET-unset → all-false 200, PUT merging, both status
codes, DELETE not deleting, the bucket guard, the xmlns attr) — every one is caught by a
failing test. One mutation initially passed, revealing that the explicit
len(req.Body) == 0 check was unreachable-by-test dead code, since xml.Unmarshal
already errors into the same MalformedXML; it was removed and the reasoning kept in the
comment.

Verification

gofmt -l · go build ./... · go vet ./... · golangci-lint run ./... (0 issues) ·
make test (race) · cd test/e2e && go test ./... · make docs-reference-check docs-versions — all clean.

…#446)

The ?publicAccessBlock subresource was unrouted on all three verbs. Because it
is addressed as a bare query key on the bucket itself, each request fell through
to the bucket-level operation for its method:

  DELETE /bucket?publicAccessBlock  → DeleteBucket
  PUT    /bucket?publicAccessBlock  → CreateBucket
  GET    /bucket?publicAccessBlock  → ListObjects

The DELETE case is data loss with no signal. On an empty bucket it returned 204,
exactly as a successful DeletePublicAccessBlock does, having destroyed the
bucket; the next operation failed with NoSuchBucket for no visible reason. On a
non-empty bucket it returned 409 BucketNotEmpty, naming an operation the caller
never issued.

PUT answered 409 BucketAlreadyExists — and that shape is what SDKs and
CloudFormation send immediately after creating a bucket, so locking a bucket
down failed on the very bucket it was meant to configure. GET returned 200 with
a ListBucketResult body, which an SDK parses into an empty
PublicAccessBlockConfiguration: a confident wrong answer reading "public access
is not blocked" for a bucket whose settings substrate had never stored.

All three are now routed ahead of their fall-throughs and modeled:

  PUT    → 200, empty body; replaces the document wholesale, and settings the
           body omits are recorded and reported back as false, as S3 does
  GET    → 200 + the configuration, or 404
           NoSuchPublicAccessBlockConfiguration when none was ever written
  DELETE → 204, idempotent, and touches nothing but the configuration

The configuration lives under its own state key rather than as a field on
S3Bucket. That keeps "no configuration" (404) distinguishable from "all four
settings false" (200 with four false elements) — a consumer relaxing a block
writes the latter on purpose — and it makes DeletePublicAccessBlock a delete
rather than a partial rewrite of the bucket record, which is what structurally
keeps it from touching the bucket at all.

NoSuchPublicAccessBlockConfiguration is not in the S3 2006-03-01 model and the
bucket-level GetPublicAccessBlock reference documents no Errors section, so the
code's provenance is recorded in s3_publicaccessblock.go: the s3control model's
shape documentation describes the same condition, terraform-provider-aws handles
this exact code on bucket-level Get and Delete, and boto3 callers report the
message string verbatim.

Two deliberate limits, both documented:

  - Substrate does not apply S3's April 2023 default of enabling all four
    settings on a newly created bucket. That default is AWS-managed account and
    organization state substrate does not model, and seeding every bucket with a
    configuration would make the NoSuchPublicAccessBlockConfiguration path — the
    branch a consumer's error handling exists for — unreachable through the
    public API.
  - The settings are recorded, not enforced: nothing rejects a public ACL or
    bucket policy. Those are the resource-internal consequences of the setting
    rather than the setting itself. Tracked as #458.
@codecov

codecov Bot commented Aug 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.49123% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/s3_publicaccessblock.go 95.55% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

codecov/patch flagged the state-layer error branches as uncovered. They are
worth covering rather than waiving, because collapsing a store failure into one
of this subresource's own 404s is the same defect class #446 was: reporting a
settled answer for a transient one.

A NoSuchBucket or NoSuchPublicAccessBlockConfiguration tells a caller the answer
will not change and to stop retrying. A store failure is a blip. Reporting
"there is no configuration" for "I could not read the configuration" sends a
consumer down a permanent-failure path, and on DELETE it reports a teardown that
did not happen.

Adds pabFaultState, which fails one named key's operations once armed. It is
armed after setup rather than from construction because CreateBucket reads and
writes the same bucket: key the fault targets. Targeting a key rather than
counting calls, as errAfterGetsStateManager does, is what lets a test
distinguish the bucket lookup from the configuration lookup — a single request
does both.

Six cases, one per reachable branch, plus a corrupt-stored-configuration case
that must be a 500 rather than the unset 404. Mutation-tested: collapsing the
GET store error into `err != nil || raw == nil`, and swallowing the DELETE
store error, are both caught.

The remaining uncovered line is json.Marshal's error return, unreachable for a
struct of four bools. It stays checked per the never-discard convention, with a
comment saying why it cannot fire today.
@scttfrdmn
scttfrdmn merged commit b9485e0 into main Aug 1, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the fix/446-s3-public-access-block branch August 2, 2026 22:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

S3: ?publicAccessBlock is unrouted; PUT hits CreateBucket and DELETE deletes the bucket

1 participant