Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(s3): turn on Block Public Access for new buckets under AUTH=true (#2408)

iot-rocketbe4f9dc3c5ff
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b3839cac..05c45099 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
### Fixed
+- **S3 — Block Public Access on new buckets** — `GetPublicAccessBlock` on a new bucket, from `CreateBucket` or from a template without `PublicAccessBlockConfiguration`, answered 404 and nothing refused a public bucket policy or ACL; under `AUTH=true` a new bucket now has all four settings on, as on AWS, so a public `PutBucketPolicy`, `PutBucketAcl` or object ACL answers 403 `AccessDenied` and `CreateBucket` with a public ACL 400 `InvalidBucketAclWithBlockPublicAccessError` until `PutPublicAccessBlock` or `DeletePublicAccessBlock` changes them, and removing the property from a template turns all four on again. With `AUTH` unset nothing changes. Contributed by @iot-rocket.
- **S3 — CORS under `AUTH=true`** — every S3 preflight answered 200 with `Access-Control-Allow-Origin: *` and every response carried the same wildcard headers, whatever the bucket's CORS configuration; under `AUTH=true` a preflight to an existing bucket now has to match one of its CORS rules (403 `AccessForbidden` without a configuration or a matching rule, 400 `BadRequest` without `Origin`) and gets that rule's headers, and a request with an `Origin` carries CORS headers only when a rule allows its origin and method. Contributed by @iot-rocket.
- **IoT — MQTT 5 No Local follows AWS IoT** — a subscription with No Local kept the client's own messages from it; AWS IoT Core ignores that option and delivers them, and so does the broker now. Contributed by @iot-rocket.
- **CloudFront — a viewer-request function that leaves the query string alone no longer re-encodes it** — any function that returned the request it was given counted as a query-string rewrite, so the origin got the query rebuilt from the parsed parameters instead of as the viewer sent it: a bare key gained an `=` (`?flag` became `?flag=`). The query string is now rebuilt only when the function's returned `querystring` differs from the one in its event. Contributed by @jeffreyheller.
diff --git a/ministack/services/cloudformation/provisioners.py b/ministack/services/cloudformation/provisioners.py
index 559d276d..2781d2b8 100644
--- a/ministack/services/cloudformation/provisioners.py
+++ b/ministack/services/cloudformation/provisioners.py
@@ -1463,10 +1463,19 @@ def _s3_website_xml(website):
return tostring(root, encoding="utf-8")
+def _s3_default_public_access_block(name):
+ """A removed PublicAccessBlockConfiguration leaves the settings of a new bucket (all four on under AUTH=true)."""
+ default = _s3._new_bucket_public_access_block()
+ if default:
+ _s3._put_public_access_block(name, default["_public_access_block"].encode())
+ else:
+ _s3._delete_public_access_block(name)
+
+
_S3_BUCKET_CONFIGURATIONS = (
("CorsConfiguration", _s3_cors_xml, _s3._put_bucket_cors, _s3._delete_bucket_cors),
("PublicAccessBlockConfiguration", _s3_public_access_block_xml,
- _s3._put_public_access_block, _s3._delete_public_access_block),
+ _s3._put_public_access_block, _s3_default_public_access_block),
("WebsiteConfiguration", _s3_website_xml, _s3._put_bucket_website, _s3._delete_bucket_website),
)
@@ -1669,6 +1678,7 @@ def _s3_create(logical_id, props, stack_name):
"created": now_iso(),
"objects": {},
"region": get_region(),
+ **_s3._new_bucket_public_access_block(),
})
versioning = props.get("VersioningConfiguration", {})
if versioning.get("Status") == "Enabled":
diff --git a/ministack/services/s3.py b/ministack/services/s3.py
index 25d78f80..4a3f7a3a 100644
--- a/ministack/services/s3.py
+++ b/ministack/services/s3.py
@@ -2885,6 +2885,11 @@ def _create_bucket(name: str, body: bytes, headers: dict = None, endpoint_region
extra={"BucketName": name},
)
+ if not enforced and _public_acl(headers) and _new_bucket_public_access_block():
+ # The Block Public Access settings of the new bucket refuse it.
+ return _error("InvalidBucketAclWithBlockPublicAccessError",
+ "Bucket cannot have public ACLs set with BlockPublicAccess enabled", 400)
+
constraint = None
tags = {}
if body:
@@ -2909,7 +2914,7 @@ def _create_bucket(name: str, body: bytes, headers: dict = None, endpoint_region
# No explicit LocationConstraint: the bucket lands in the region the
# request was signed for, like real AWS.
region = _constraint_region(constraint) or get_region()
- _buckets[name] = {"created": now_iso(), "objects": {}, "region": region}
+ _buckets[name] = {"created": now_iso(), "objects": {}, "region": region, **_new_bucket_public_access_block()}
if constraint == "EU":
# GetBucketLocation answers EU for a bucket created with EU (observed on AWS 2026-10-20).
_buckets[name]["location_constraint"] = constraint
@@ -3740,6 +3745,24 @@ def _delete_bucket_ownership_controls(name: str):
return 204, {}, b""
+# Under AUTH=true a new bucket blocks all public access until PutPublicAccessBlock or DeletePublicAccessBlock
+# changes it.
+_NEW_BUCKET_PUBLIC_ACCESS_BLOCK = (
+ XML_DECL.decode() + "\n"
+ f'<PublicAccessBlockConfiguration xmlns="{S3_NS}">'
+ "<BlockPublicAcls>true</BlockPublicAcls><IgnorePublicAcls>true</IgnorePublicAcls>"
+ "<BlockPublicPolicy>true</BlockPublicPolicy><RestrictPublicBuckets>true</RestrictPublicBuckets>"
+ "</PublicAccessBlockConfiguration>"
+)
+
+
+
+def _new_bucket_public_access_block() -> dict:
+ """The Block Public Access record of a new bucket: all four settings on under AUTH=true, none otherwise."""
+ from ministack.app import AUTH
+
+ return {"_public_access_block": _NEW_BUCKET_PUBLIC_ACCESS_BLOCK} if AUTH else {}
+
def _put_public_access_block(name: str, body: bytes):
if name not in _buckets:
return _no_such_bucket(name)
@@ -7380,13 +7403,18 @@ _CANNED_ACL_GROUP_GRANTS = {
}
-def _public_acl_refused(bucket_name: str, headers: dict, acl_xml: str | bytes | None = None) -> tuple | None:
- """AccessDenied for a request that sets a public ACL on a bucket whose BlockPublicAcls is on."""
+def _public_acl(headers: dict, acl_xml: str | bytes | None = None) -> bool:
+ """Whether the request's canned ACL, grant headers or ACL body grant AllUsers or AuthenticatedUsers."""
public = (_ACL_GROUP_ALL_USERS, _ACL_GROUP_AUTHENTICATED)
uris = [uri for uri, _ in _CANNED_ACL_GROUP_GRANTS.get(headers.get("x-amz-acl", ""), [])]
uris += [uri for uri, _ in _acl_grants(acl_xml)]
grants = " ".join(v for k, v in headers.items() if k.lower().startswith("x-amz-grant-"))
- if not any(uri in public for uri in uris) and not any(group in grants for group in public):
+ return any(uri in public for uri in uris) or any(group in grants for group in public)
+
+
+def _public_acl_refused(bucket_name: str, headers: dict, acl_xml: str | bytes | None = None) -> tuple | None:
+ """AccessDenied for a request that sets a public ACL on a bucket whose BlockPublicAcls is on."""
+ if not _public_acl(headers, acl_xml):
return None
if bucket_name not in _buckets or not _public_access_blocked(get_account_id(), bucket_name, "BlockPublicAcls"):
return None
diff --git a/tests/test_cfn.py b/tests/test_cfn.py
index 273cd1a2..2395ecd0 100644
--- a/tests/test_cfn.py
+++ b/tests/test_cfn.py
@@ -913,6 +913,40 @@ def test_cfn_s3_bucket_cors_public_access_block_and_website(cfn, s3):
_wait_stack(cfn, "cfn-s3-configs")
+
+def test_cfn_s3_bucket_without_public_access_block_contract(auth_enabled, cfn, s3):
+ """Under AUTH=true a template bucket without PublicAccessBlockConfiguration, or whose update removes it, has all
+ four settings on; without AUTH it has none."""
+ bucket = "cfn-s3-pab-default-bucket"
+ all_on = dict.fromkeys(("BlockPublicAcls", "IgnorePublicAcls", "BlockPublicPolicy", "RestrictPublicBuckets"), True)
+
+ def template(props):
+ return json.dumps({"AWSTemplateFormatVersion": "2010-09-09", "Resources": {
+ "Bucket": {"Type": "AWS::S3::Bucket", "Properties": {"BucketName": bucket, **props}}}})
+
+ def settings():
+ try:
+ return s3.get_public_access_block(Bucket=bucket)["PublicAccessBlockConfiguration"]
+ except ClientError as exc:
+ return exc.response["Error"]["Code"]
+
+ cfn.create_stack(StackName="cfn-s3-pab-default", TemplateBody=template({}))
+ assert _wait_stack(cfn, "cfn-s3-pab-default")["StackStatus"] == "CREATE_COMPLETE"
+ try:
+ expected = all_on if auth_enabled else "NoSuchPublicAccessBlockConfiguration"
+ assert settings() == expected
+ cfn.update_stack(StackName="cfn-s3-pab-default", TemplateBody=template({"PublicAccessBlockConfiguration": {
+ "BlockPublicAcls": False, "IgnorePublicAcls": True}}))
+ assert _wait_stack(cfn, "cfn-s3-pab-default")["StackStatus"] == "UPDATE_COMPLETE"
+ assert settings()["IgnorePublicAcls"] is True
+ cfn.update_stack(StackName="cfn-s3-pab-default", TemplateBody=template({}))
+ assert _wait_stack(cfn, "cfn-s3-pab-default")["StackStatus"] == "UPDATE_COMPLETE"
+ assert settings() == expected
+ finally:
+ cfn.delete_stack(StackName="cfn-s3-pab-default")
+ _wait_stack(cfn, "cfn-s3-pab-default")
+
def test_cfn_iot_and_cognito_role_attachment(cfn, iot_client, cognito_identity):
"""AWS::IoT::ThingType, AWS::IoT::Policy, and
AWS::Cognito::IdentityPoolRoleAttachment provision onto their real services
diff --git a/tests/test_s3.py b/tests/test_s3.py
index 962cbe16..dc5de02d 100644
--- a/tests/test_s3.py
+++ b/tests/test_s3.py
@@ -1469,6 +1469,45 @@ def test_s3_public_access_block(s3):
assert exc.value.response["Error"]["Code"] == "NoSuchPublicAccessBlockConfiguration"
+
+def test_s3_new_bucket_blocks_public_access_contract(auth_enabled, s3):
+ """Under AUTH=true a new bucket has all four Block Public Access settings on until they are changed or deleted."""
+ bkt = f"intg-s3-pab-new-{_uuid_mod.uuid4().hex[:8]}"
+ public_policy = json.dumps({"Version": "2012-10-17", "Statement": [{
+ "Effect": "Allow", "Principal": "*", "Action": "s3:GetObject", "Resource": f"arn:aws:s3:::{bkt}/*"}]})
+ s3.create_bucket(Bucket=bkt, ObjectOwnership="ObjectWriter")
+ try:
+ if not auth_enabled:
+ with pytest.raises(ClientError) as exc:
+ s3.get_public_access_block(Bucket=bkt)
+ assert exc.value.response["Error"]["Code"] == "NoSuchPublicAccessBlockConfiguration"
+ s3.create_bucket(Bucket=f"{bkt}-x", ObjectOwnership="ObjectWriter", ACL="public-read")
+ s3.delete_bucket(Bucket=f"{bkt}-x")
+ return
+ assert s3.get_public_access_block(Bucket=bkt)["PublicAccessBlockConfiguration"] == dict.fromkeys(
+ ("BlockPublicAcls", "IgnorePublicAcls", "BlockPublicPolicy", "RestrictPublicBuckets"), True)
+ for call, kwargs in ((s3.put_bucket_policy, {"Policy": public_policy}),
+ (s3.put_bucket_acl, {"ACL": "public-read"}),
+ (s3.put_object, {"Key": "k", "Body": b"k", "ACL": "public-read"})):
+ with pytest.raises(ClientError) as exc:
+ call(Bucket=bkt, **kwargs)
+ assert exc.value.response["Error"]["Code"] == "AccessDenied", kwargs
+ with pytest.raises(ClientError) as exc:
+ s3.create_bucket(Bucket=f"{bkt}-x", ObjectOwnership="ObjectWriter", ACL="public-read")
+ assert exc.value.response["Error"]["Code"] == "InvalidBucketAclWithBlockPublicAccessError"
+ assert exc.value.response["ResponseMetadata"]["HTTPStatusCode"] == 400
+
+ s3.delete_public_access_block(Bucket=bkt)
+ with pytest.raises(ClientError) as exc:
+ s3.get_public_access_block(Bucket=bkt)
+ assert exc.value.response["Error"]["Code"] == "NoSuchPublicAccessBlockConfiguration"
+ s3.put_bucket_policy(Bucket=bkt, Policy=public_policy)
+ s3.put_bucket_acl(Bucket=bkt, ACL="public-read")
+ s3.delete_bucket_policy(Bucket=bkt)
+ finally:
+ s3.delete_bucket(Bucket=bkt)
+
def test_s3_block_public_policy_refuses_a_public_bucket_policy(s3):
bkt = f"intg-s3-bpp-{_uuid_mod.uuid4().hex[:8]}"
s3.create_bucket(Bucket=bkt)
@@ -9363,6 +9402,7 @@ def _anonymous_bucket(monkeypatch, auth=True, **bucket_headers):
monkeypatch.setattr(app_mod, "AUTH", auth)
bucket = f"anon-{uuid.uuid4().hex[:10]}"
assert _lock_call("PUT", f"/{bucket}", bucket_headers)[0] == 200
+ assert _lock_call("DELETE", f"/{bucket}", query={"publicAccessBlock": [""]})[0] == 204
assert _lock_call("PUT", f"/{bucket}/k", {}, b"secret")[0] == 200
return bucket
@@ -10958,6 +10998,7 @@ def test_s3_anonymous_copy_source_and_policy_action_contract(auth_enabled, s3):
s3.create_bucket(Bucket=bucket)
s3.put_object(Bucket=secret, Key="c", Body=b"secret")
s3.put_object(Bucket=upload, Key="kept", Body=b"kept")
+ s3.delete_public_access_block(Bucket=upload)
s3.put_bucket_policy(Bucket=upload, Policy=json.dumps({"Version": "2012-10-17", "Statement": [{
"Effect": "Allow", "Principal": "*", "Action": "s3:PutObject", "Resource": f"arn:aws:s3:::{upload}/*"}]}))
try: