Repository navigation
fix(s3): route ?publicAccessBlock so DELETE stops deleting the bucket (#446) - #459
Merged
Merged
Conversation
…#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 Report❌ Patch coverage is
📢 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #446.
The defect
The
?publicAccessBlocksubresource was unrouted on all three verbs. It is addressedas a bare query key on the bucket itself, so each request fell through to the
bucket-level operation for its method:
DELETE /bucket?publicAccessBlockDeleteBucket204— and the bucket was gonePUT /bucket?publicAccessBlockCreateBucket409 BucketAlreadyExistsGET /bucket?publicAccessBlockListObjects200+ aListBucketResultbodyThe DELETE case is data loss with no signal. On an empty bucket it returned
204—exactly what a successful
DeletePublicAccessBlockreturns — having destroyed thebucket and its contents. The caller had nothing to go on: the next operation on that
bucket failed with
NoSuchBucketfor no visible reason. On a non-empty bucket itreturned
409 BucketNotEmpty, naming an operation the caller never issued.PUT failed on exactly the shape SDKs send.
PutPublicAccessBlockimmediately afterCreateBucketis what the AWS SDKs and CloudFormation both do, so hardening a bucketfailed on the already-owned bucket it was meant to configure.
GET returned a confident wrong answer. A
ListBucketResultbody parses into anempty
PublicAccessBlockConfiguration, so a consumer read "public access is notblocked" for a bucket whose settings substrate had never stored.
One correction to the report, which assumed
GETwas routed and returning an emptyconfiguration: 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 threeoperations are modeled in a new
emulator/s3_publicaccessblock.go:PUTreplaces the document wholesale rather than merging. The four settings are eachRequired: No, and every one the body omits is recorded — and reported back — asfalse, matching S3.GETon a bucket with no configuration is404 NoSuchPublicAccessBlockConfiguration, which keeps it distinguishable from theall-false configuration a consumer relaxing a block writes on purpose.
DELETEis 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
DeletePublicAccessBlocka delete rather than a partial rewrite ofthe bucket record — so it is structurally incapable of touching the bucket, not
merely tested against doing so.
On
NoSuchPublicAccessBlockConfigurationThis 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
GetPublicAccessBlockpage has no Errors section at all. Rather than assume, it was corroborated from three
independent sources, recorded in the code comment:
internal/service/s3package (not justs3control), on bothGetPublicAccessBlock(→ NotFound) and
DeletePublicAccessBlock(→ treated as success, which is why DELETEis idempotent here).
block configuration was not found."
Two deliberate limits
Both are documented in
docs/services.mdrather than left to be discovered: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
NoSuchPublicAccessBlockConfigurationpath — thebranch a consumer's error handling exists for — unreachable through the public API.
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 alsoargues why
RestrictPublicBucketsandIgnorePublicAclsshould stay recorded-onlyuntil substrate models anonymous access evaluation.
Tests
New
emulator/s3_publicaccessblock_test.go, 11 tests:DeleteKeepsBucket— the regression guard, covering both of the reporter'sreproductions: empty bucket (was silently deleted) and non-empty (was
409 BucketNotEmpty). Asserts the bucket still appears inListBuckets, the objectsurvives, and the configuration is gone.
RoundTrip—PUT→200with an empty body, thenGETreturns what waswritten. The
GETassertion is what provesPUTwas routed rather than merely notcrashing.
GetUnset—404 NoSuchPublicAccessBlockConfiguration, and noListBucketResult.AllFalseIsNotUnset— the two states stay distinguishable.PartialBody/Overwrite— omitted settings read backfalse;PUTreplaces rather than merges.
DeleteIsIdempotent,NoSuchBucket(all three verbs, and none of them createsthe bucket it names),
MalformedBody,VirtualHostStyle,SiblingSubresourcesUnaffected(ListObjectsandDeleteBucketstill reach thefall-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) == 0check was unreachable-by-test dead code, sincexml.Unmarshalalready errors into the same
MalformedXML; it was removed and the reasoning kept in thecomment.
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.