Skip to content

fix(s3): enforce BlockPublicAcls and BlockPublicPolicy (#458) - #466

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/458-block-public-access
Aug 2, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/458-block-public-access

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #458

#446 made the four Block Public Access settings storable and readable, and nothing acted on any of them. A consumer asserting "once we lock the bucket down, a public ACL is refused" got a green test that verified nothing — the assertion would have to be written a second time before it ever ran against AWS.

What changed

Setting Refuses Response
BlockPublicAcls PutBucketAcl, PutObjectAcl with a public ACL 403 AccessDenied / Access Denied
BlockPublicPolicy PutBucketPolicy with a public policy 403 AccessDenied / Access Denied

Both check before the write, so a rejection stores nothing and the resource keeps the ACL or policy it already had — "existing policies and ACLs for buckets and objects aren't modified". Deleting the configuration re-allows what it was refusing. PutObjectAcl reads the bucket's configuration: "Amazon S3 doesn't support block public access settings on a per-object basis".

The policy rule is inverted from the obvious reading

This is the substance of the change. S3 does not look for a wildcard principal — it "begins by assuming that the policy is public. It then evaluates the policy to determine whether it qualifies as non-public", which happens only when access is granted solely to fixed values (no *, no ?, no ${...} policy variable) through the Principal or a Condition on one of the documented keys.

Policy Public?
Principal: "*", no condition yes
Principal: "*" + StringLike aws:SourceVpc: "vpc-*" yes — the narrowing value is itself a wildcard
Principal: "*" + StringEquals aws:SourceVpc: "vpc-91237329" no
Principal: {"AWS": "arn:aws:iam::123456789012:root"} no
Principal: {"AWS": "arn:aws:iam::123456789012:user/*"} yes
Principal: "*" + aws:SourceIp: "203.0.113.0/24" no
Principal: "*" + aws:SourceIp: "0.0.0.0/1" yes — broader than /8, no wildcard character in sight
Effect: Deny, Principal: "*" no
A fixed cross-account grant plus one public statement yes

Also implemented: the aws:SourceIp breadth rule ("broader than /8 for IPv4 and /32 for IPv6, excluding RFC1918 private ranges") via net/netip, and the s3:DataAccessPointArn carve-out where a wildcard access-point name does not make a bucket policy public as long as the account ID is fixed. Only an Allow can make a policy public, and one surviving public statement makes the whole policy public — the guide's worked example, where a single public statement disables an otherwise-legal cross-account grant.

Public ACLs

Any permission granted to AllUsers or AuthenticatedUsers, matched on the grantee URI, with the permission not inspected (WRITE_ACP counts as much as READ). AuthenticatedUsers is every AWS account rather than every account in yours, which is why it counts despite the name and why it can only arrive through an XML body — the canned-ACL resolver never emits it.

All three documented forms are covered: the x-amz-acl canned header, an XML Grant naming a public group, and an x-amz-grant-* header whose grantee list contains one. The grant headers were previously unread anywhere in substrate; they are parsed for this check only, so an ACL set through them is still not stored (separate gap).

Provenance

None of PutBucketAcl, PutObjectAcl or PutBucketPolicy documents an Errors section covering this, so the AccessDenied / Access Denied / 403 triple comes from observed real-AWS behavior, not the API model — a blocked PutBucketPolicy surfaces through the CLI as An error occurred (AccessDenied) when calling the PutBucketPolicy operation: Access Denied. The definitions of "public" are quoted from the user guide's Blocking public access → The meaning of "public", fetched and read rather than recalled.

Deliberately not covered

  • IgnorePublicAcls and RestrictPublicBuckets stay recorded-only. Both govern how an incoming request is evaluated against an ACL or policy already in place rather than which write is refused, and substrate has no unauthenticated or cross-account request path to deny — every request it serves is already the bucket owner's.
  • PutObject and CreateBucket with a public ACL are not refused, because neither handler reads x-amz-acl at all. Covering them means modelling ACL-on-create first; follow-up to be filed.
  • The guide's unsupported-action clause (S3 treats a statement granting an action S3 does not support as potentially public), which would need an authoritative list of every supported s3: action and would go stale silently.
  • A body that parses as JSON but not as a policy document is not treated as public. PutBucketPolicy already rejects non-JSON with 400 MalformedPolicy, and the new check is not a second validity check.

Tests

emulator/s3_publicaccess_test.go, 88 subtests. The ACL cases run against both PutBucketAcl and PutObjectAcl from one table; the policy table has 30 rows.

The gate on not regressing #446: an unconfigured bucket, an all-false configuration, and one setting only the other three settings all accept every public ACL and a public policy exactly as before enforcement existed. Also asserted — nothing is stored on rejection (read back through GetBucketAcl/GetBucketPolicy), the existence checks still run first (NoSuchBucket, not the new AccessDenied), MalformedPolicy still fires at 400 rather than being displaced by the 403, deleting the configuration re-allows a public ACL, and one bucket's configuration does not govern another's.

Mutation testing

24 mutants, all caught. Three survived the first pass and each pointed at a real gap:

Mutant Fix
RFC1918 exclusion dropped No test could see it — under the inclusive /8 reading the exclusion cannot change an IPv4 answer, since the broadest private range is 10.0.0.0/8 and the bound already admits it. It is load-bearing for IPv6, where unique-local is fc00::/7. Added an fd00::/8 case and rewrote the doc comment, which had claimed the IPv4 rationale.
Unparseable JSON treated as public Only case was {"not":"a policy"}, which unmarshals fine into an empty PolicyDocument. Added {"Version":..., "Statement":"nope"} — a JSON object that clears MalformedPolicy but genuinely fails to unmarshal as a policy.
A nil Principal counts as fixed Untested. Added an Allow statement with no Principal; it must be public, which is the assume-public direction and the alternative would let a caller bypass the block by omitting the field.

Verification

From the repo root: gofmt -l . clean, go build ./..., go vet ./..., golangci-lint run ./... → 0 issues, make test (race) green, make docs-reference-check docs-versions green, go test -C test/e2e ./... ok.

The plan's consumer sequence, run end to end:

PutPublicAccessBlock(all true)
  → PutBucketAcl(public-read)         → 403 AccessDenied, ACL unchanged
  → PutBucketPolicy(Principal:*)      → 403 AccessDenied, no policy stored
  → PutBucketPolicy(fixed principal)  → 204, policy readable

Docs: docs/services.md §Block Public Access — the "settings are recorded, not enforced" paragraph is replaced with what is now enforced, the assume-public-then-qualify rule with the vpc-* example and the policy table, the ACL rule, and each of the four things above that stays uncovered with its reason. The S3PublicAccessBlockConfiguration doc comment, which stated the opposite, is updated too.

#446 made the four Block Public Access settings storable and readable, and
nothing acted on any of them. A consumer asserting "once we lock the bucket
down, a public ACL is refused" got a green test that verified nothing.

A bucket with BlockPublicAcls set now refuses a public ACL on PutBucketAcl and
PutObjectAcl; one with BlockPublicPolicy set refuses a public policy on
PutBucketPolicy. Both are 403 AccessDenied / "Access Denied", and both check
before the write, so a rejection stores nothing and the resource keeps the ACL
or policy it had. PutObjectAcl reads the bucket's configuration: Block Public
Access has no per-object setting.

The policy rule is the substance, and it is inverted from the obvious reading.
S3 does not look for a wildcard principal; it assumes the policy is public and
then tries to qualify it as non-public, which happens only when access is
granted solely to fixed values -- no wildcard, no IAM policy variable -- through
the Principal or a Condition on one of the documented keys. So Principal: "*"
narrowed by StringLike aws:SourceVpc: "vpc-*" is public, where StringEquals
aws:SourceVpc: "vpc-91237329" is not. Only an Allow can make a policy public,
and one surviving public statement makes the whole policy public. The
aws:SourceIp breadth rule (broader than /8 IPv4, /32 IPv6, RFC1918 excluded) and
the s3:DataAccessPointArn wildcard carve-out are implemented too.

A public ACL is one granting any permission to AllUsers or AuthenticatedUsers,
matched on the grantee URI, across all three forms it arrives in: the x-amz-acl
canned header, an XML Grant, and an x-amz-grant-* header. The grant headers were
previously unread anywhere in substrate and are parsed for this check only.

IgnorePublicAcls and RestrictPublicBuckets stay recorded-only: both govern how an
incoming request is evaluated rather than which write is refused, and substrate
has no unauthenticated or cross-account request path to deny. PutObject and
CreateBucket are not covered, since neither reads x-amz-acl at all.

An unconfigured bucket and an all-false configuration behave exactly as before,
which is the case that guards #446.
@scttfrdmn
scttfrdmn merged commit cc8bb73 into main Aug 2, 2026
15 checks passed
@codecov

codecov Bot commented Aug 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 78.52349% with 32 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/s3_publicaccess.go 82.35% 13 Missing and 11 partials ⚠️
emulator/s3_plugin.go 38.46% 3 Missing and 5 partials ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn deleted the fix/458-block-public-access 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: Block Public Access settings are recorded but not enforced

1 participant