Skip to content

fix(s3): DeleteBucket clears the bucket's namespace (#508) - #543

Merged
scttfrdmn merged 2 commits into
mainfrom
fix/508-delete-bucket-subresources
Aug 3, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
fix/508-delete-bucket-subresources

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #508.

DeleteBucket removed bucket:<name> and nothing else. Since a bucket-scoped
configuration is keyed by the bucket name alone, every one of them outlived its
bucket and was inherited by the next bucket created with that name — reproduced end
to end against a binary built from this branch's parent:

$ aws s3api put-bucket-versioning --bucket probe508 --versioning-configuration Status=Enabled
$ aws s3api delete-bucket --bucket probe508 && aws s3api create-bucket --bucket probe508
$ aws s3api get-bucket-versioning --bucket probe508
{ "Status": "Enabled" }        # a bucket nobody ever configured

What is cleared

All six: bucket_acl:, bucket_policy:, bucket_lifecycle:,
bucket_versioning:, notification:, bucket_public_access_block: — held in one
s3BucketSubresourcePrefixes slice, so adding a bucket-scoped configuration is a
change to that slice rather than a seventh open-coded delete. The drift between one
cleared key and five uncleared ones is what this bug was.

Two corrections to the issue's table, both verified in the source rather than
assumed: the notification prefix is notification:, not bucket_notification:; and
bucket tagging never leaked, because PutBucketTagging writes Tags into the
bucket: record rather than under a key of its own.

Object-scoped leftovers go too. An object ACL and a version index are stored under
their own keys and outlive the object they describe, so an empty bucket could
still own state a recreated bucket inherited.

On the issue's open question — best-effort vs propagated: propagated. A partial
clear is precisely the bug being fixed, so _ = state.Delete(...) would hide a
recurrence. The bucket: record is deleted last, so a failed clear leaves the
bucket present and retryable rather than half-deleted, and HeadBucket agrees with
the error instead of contradicting it.

The emptiness check was undercounting (not in the issue)

"All objects (including all object versions and delete markers) in the bucket must
be deleted before the bucket itself can be deleted", but the check listed only
object:<bucket>/. A bucket holding nothing but a deleted object's history deleted
"successfully" and stranded every version. It now counts object_version: as well,
which covers noncurrent versions and delete markers alike.

The sharpest case is a versioning-suspended bucket: substrate's DELETE removes
the current-version pointer outright there, so the bucket looked empty while the
version it wrote was still present. Real S3 keeps that version too — "when you
suspend versioning, existing objects in your bucket do not change" — so it is not
empty either way.

Multipart: aborted, not refused — correcting my own plan

The plan said an in-flight upload should refuse the delete. The reference says
otherwise, and the reference wins: the refusal is documented for directory
buckets
, which substrate does not model. For general purpose buckets the guidance
is only that "while emptying your bucket, we recommend that you also remove all
incomplete multipart uploads". An upload whose destination bucket is gone can never
be completed, so the delete aborts it — part records, upload record, and part bodies
— through the same helper AbortMultipartUpload now calls, so the two paths cannot
leave different residue. Only uploads whose Bucket field names the bucket being
deleted are touched: an upload is keyed by upload ID alone, so the scan sees every
bucket's.

A pre-existing defect that blocked this one outright

Permanently deleting a version by ID never updated the object: current-version
pointer. A versioned bucket could therefore never be emptied — the key stayed
listed with a 200 GET after its last version was gone — so the widened emptiness
check above could never have been satisfied. Fixing #508 required fixing this.

promoteCurrentVersion promotes the newest surviving version, since "if you delete
the current object version, … the version that is next in the version stack becomes
the current version", and removes the key entirely when none survives. The promoted
version's body moves with its record: a versioned PUT writes the body both
under .versions/<key>/<versionID> and at the unversioned path an unversioned GET
reads, so promoting the record alone served the deleted version's bytes under the
promoted version's ETag.

Two subtleties, each pinned by a test because each was found by a surviving mutant:
a delete marker has no body to promote (an absent versioned body is not an error),
and the promotion honours #534's s3ObjectHasBody guard — afero reads a directory
as an empty body with no error, so an unguarded promote truncates the object at
dir to zero bytes while promoting a version of the marker dir/, and reports
success.

Tests

emulator/s3_delete_bucket_state_test.go, all failing before the change:

Test Asserts
ClearsBucketScopedConfiguration one sub-test per key, asserting the state key after the delete and the read after the recreate
RecreatedBucketDoesNotInheritBPA behaviorally: a public-ACL write against the recreated bucket succeeds
RecreatedBucketDoesNotInheritVersioning behaviorally: a write reports no x-amz-version-id
RefusesNoncurrentVersions delete marker / noncurrent version / suspended-versioning leftover → BucketNotEmpty
AllVersionsRemovedIsEmpty the widened check did not make DeleteBucket impossible to satisfy
AbortsInFlightMultipartUploads 204, upload not inherited, not resumable, part records and bodies gone
AbortsOnlyThisBucketsUploads another bucket's upload survives and is still usable
ClearsObjectScopedLeftovers no key under any object prefix outlives its bucket
VersionDeletePromotesPredecessor the promoted body and its ETag
VersionDeletePromotesDeleteMarker promoting a marker is a 404, not a 500
PromotingADirectoryMarkerTouchesNoBody the child survives and is still deletable
PromotingAMarkerDoesNotClobberItsSibling the object at dir keeps its body
ErrorsPropagate one sub-case per key: 500 reported, and HeadBucket still 200

Two test-design notes worth stating, since both were cases of a test passing for the
wrong reason:

  • bucket_acl: is also cleared by CreateBucket, so a read after the recreate
    passes with no fix at all. Every sub-test therefore asserts on the state key
    immediately after the delete, which is what attributes the clear to DeleteBucket.
  • the notification sub-test passed before the fix because a body written the AWS
    way
    stores nothing: substrate unmarshals that XML by Go field name, so
    <QueueConfiguration><Queue> never matches. There was no leak to observe. Filed
    as S3: PutBucketNotificationConfiguration parses XML by Go field name, so a real-S3 request body stores nothing and returns 200 #542; the test carries a comment pointing there and uses substrate's shape so
    it fails for the right reason today.

Mutation testing

30 mutants across the clear list, the emptiness check, error propagation, delete
ordering, the multipart abort and the promotion. 27 applied and all CAUGHT, zero
survivors
; 3 were stale duplicate anchors superseded by working forms in the same
run.

Five survived initially, and each was a genuine test-design gap rather than an
equivalence, so the tests were strengthened rather than the mutants excused:

  • the emptiness check reverted to object: only — no case reached "object: empty,
    versions remaining" until the suspended-versioning sub-case was added, which the
    AWS reference confirmed is a real state rather than a substrate artefact;
  • the object-scoped scan dropped — the original test used the versioned delete
    path, which promoteCurrentVersion now tidies itself, so it needed the plain
    delete that genuinely strands an object_acl: key;
  • the abort ignoring upload.Bucket — no test had uploads in two buckets;
  • the promotion writing the record but not the body — no test read the promoted
    content;
  • the promotion's marker guard dropped — proved reachable and destructive with an
    afero probe first (truncates the sibling at dir to zero bytes, no error), then
    pinned end to end.

Verification

gofmt -l ., go build ./..., go vet ./..., golangci-lint run ./... → 0
issues
, make test (race), make docs-reference docs-reference-check docs-versions (no diff — no plugin added), npm --prefix docs run build,
go vet -C test/e2e ./..., go test -C test/e2e ./.... Plus the end-to-end
sequences above against substrate server: the recreated bucket reports the
unconfigured default for versioning and BPA, a bucket holding only a delete marker
is refused and deletes once its versions are gone, an in-flight upload is aborted
rather than refused and is not resumable, and the promoted version serves older
while the key stays listed.

Compatibility

No state-format change, so existing snapshots and recorded event logs replay
identically. Three behaviour changes a consumer may be asserting on:

  • a DeleteBucket on a bucket holding noncurrent versions or delete markers now
    returns 409 BucketNotEmpty where it returned 204. A test that deleted such a
    bucket successfully was relying on the leak.
  • a recreated bucket no longer reports its predecessor's configuration. A fixture
    that leaned on the inheritance — deliberately or not — will see defaults.
  • deleting an object version now changes what an unversioned GET and
    ListObjectsV2 report for that key, which is what real S3 does.

A bucket-scoped configuration is keyed by bucket name alone, so all six
outlived their bucket and were inherited by the next bucket of that name.
DeleteBucket now clears them, plus the object-scoped keys that outlive the
object they describe, and aborts in-flight multipart uploads. Errors
propagate and the bucket: record goes last, so a failed clear leaves the
bucket present and retryable.

The emptiness check counted only live objects, so a bucket holding only a
deleted object's history deleted successfully and stranded every version;
it now counts object_version: too.

Permanently deleting a version never updated the current-version pointer,
which made a versioned bucket impossible to empty and so blocked the
widened check outright. The promoted version's body moves with its
record.
@codecov

codecov Bot commented Aug 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…#508)

Every store or filesystem failure along the clear, and along the
current-version promotion, must surface as a 500 with the bucket left in
place. That is this PR's central design claim -- a partial clear reported
as a 204 is precisely the bug being fixed -- but the error returns
implementing it were untested, so a future `_ = state.Delete(...)` would
have reintroduced the leak silently.

Adds two seams the existing helpers could not provide:

- `s3PrefixFaultState` fails one operation for every key under a prefix.
  `pabFaultState` targets a single exact key and passes `List` straight
  through, so it cannot reach the prefix scans or the keys whose names are
  generated at request time (upload IDs, version IDs).
- `s3FaultFs` fails one filesystem operation. The object mirror is the
  other half of the plugin's state; a failure removing an upload's part
  bodies, or writing a promoted body, leaks exactly what a store failure
  leaks. One operation at a time, because the promotion does MkdirAll then
  WriteFile and failing both would only ever exercise the first.

`newS3TestServerWithFS` now delegates to `newS3TestServerWithFaultFS`
rather than constructing its own filesystem, so there is one setup path.

Also pins three non-error outcomes the fault tests sit against, each the
boundary that makes the corresponding 500 meaningful: an absent bucket is
a 404 and not a 500; an upload record already gone is nothing to abort
rather than a wedge; and a surviving version whose record is unreadable
leaves no `object:` pointer, rather than one aimed at something a GET
cannot read.

The explicit `AbortMultipartUpload` gets the same assertion, since it
shares `abortUploadState` with `DeleteBucket`.

Ten mutants applied against the propagation claim -- each error return
swallowed in turn, the bucket lookup's failure reported as `NoSuchBucket`,
an unreadable upload record skipped, and both directions of
`promoteVersionBody`'s absent-body tolerance -- all ten caught. Every line
this PR adds to s3_plugin.go is now covered.
@scttfrdmn
scttfrdmn merged commit 7be4db2 into main Aug 3, 2026
13 checks passed
@scttfrdmn
scttfrdmn deleted the fix/508-delete-bucket-subresources branch August 9, 2026 17:48
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: DeleteBucket leaves every bucket sub-resource behind, so a recreated bucket inherits the deleted one's configuration

1 participant