Skip to content

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

fix(s3): restrict only public bucket policies with RestrictPublicBuckets (#2390)

iot-rocketb00863a9792f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index de64bc97..b669ac9a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
- **Auth — SigV4a signatures are verified under `AUTH=true`** — requests signed with `AWS4-ECDSA-P256-SHA256`, which SDKs use for Multi-Region Access Points, were refused for every IAM user. Header and presigned S3 signatures are now checked with the P-256 key SigV4a derives from the secret, with their date or expiry, and an `X-Amz-Region-Set` that leaves out the serving region is refused (`RegionSetMismatch` on S3, the unknown-key error elsewhere). Contributed by @iot-rocket.
### Fixed
+- **S3 — `RestrictPublicBuckets` and policies that are not public** — the setting dropped the bucket policy for every caller from another account, so a policy that names one account stopped granting it; it now drops the policy only when the policy is public, as the S3 user guide describes. Contributed by @iot-rocket.
- **IoT — thing-group membership by ARN** — `AddThingToThingGroup` and `RemoveThingFromThingGroup` ignored `thingGroupArn` and `thingArn`, so a body with ARNs answered 400 or 404; both calls now act on the thing group and thing the ARNs name (in the request's region, as on AWS), refuse a name and ARN that differ, an ARN of another type or account and a body without the group or the thing with 400 `InvalidRequestException` and AWS's messages, and removing a thing from a group that does not exist answers 200 also when the group is given by ARN. Under `AUTH=true` such an ARN is authorized as that resource. Contributed by @iot-rocket.
- **Reset — `/_ministack/reset` no longer removes another MiniStack's containers** — the OpenSearch, OpenSearch Serverless, DSQL, ECS and EKS resets removed every container carrying the service's label on the Docker daemon, including those of other MiniStack instances sharing it. They now remove only this instance's (`ministack.instance`), as the EC2, Lambda microVM and Flink resets and the boot and shutdown sweeps already do. Contributed by @jeffreyheller.
- **S3 — Multi-Region Access Point requests are authorized** — under `AUTH=true`, requests through an MRAP hostname were served without an IAM check. They are now authorized on the member bucket and then on the MRAP, whose policy is evaluated like a bucket policy, and answer `AccessDenied` naming the resource that refused them. Contributed by @iot-rocket.
diff --git a/ministack/services/s3.py b/ministack/services/s3.py
index 2a7c6b9b..dc839f96 100644
--- a/ministack/services/s3.py
+++ b/ministack/services/s3.py
@@ -1133,7 +1133,7 @@ def _foreign_bucket_allows(owner_account: str, bucket_name: str, key: str,
ACL, a grant to the caller's canonical ID on the object ACL, or an allow
in the bucket policy. Nothing granted means the request is denied, which
is S3's default for a bucket nobody has opened up. IgnorePublicAcls drops
- the Group grants and RestrictPublicBuckets the policy.
+ the Group grants and RestrictPublicBuckets a public policy.
"""
object_acl, object_wants = _foreign_object_acl(owner_account, bucket_name, key, method, query_params)
caller_id = _canonical_owner_id(caller) if caller else ""
@@ -1153,7 +1153,8 @@ def _foreign_bucket_allows(owner_account: str, bucket_name: str, key: str,
return True
policy = _bucket_policies.get_scoped(owner_account, None, bucket_name)
- if policy and not _public_access_blocked(owner_account, bucket_name, "RestrictPublicBuckets"):
+ if policy and not (_public_access_blocked(owner_account, bucket_name, "RestrictPublicBuckets")
+ and _policy_is_public(policy)):
from ministack.core.iam_actions import _s3_action, s3_service_context
from ministack.core.iam_evaluator import EvalContext, evaluate_resource_policy
diff --git a/tests/test_s3.py b/tests/test_s3.py
index 826ac16d..2ee57843 100644
--- a/tests/test_s3.py
+++ b/tests/test_s3.py
@@ -5634,6 +5634,23 @@ def test_s3_block_public_access_settings_each_cover_their_own_grants(s3):
_purge_foreign(owner, bucket)
+def test_s3_restrict_public_buckets_keeps_a_policy_that_is_not_public(s3):
+ """RestrictPublicBuckets drops only a public policy; a policy naming another account still grants it."""
+ owner, reader = _s3_account("111111111111"), _s3_account("222222222222")
+ bucket = f"bpa-named-{uuid.uuid4().hex[:8]}"
+ owner.create_bucket(Bucket=bucket)
+ try:
+ owner.put_object(Bucket=bucket, Key="k", Body=b"k")
+ owner.put_public_access_block(Bucket=bucket, PublicAccessBlockConfiguration={"RestrictPublicBuckets": True})
+ owner.put_bucket_policy(Bucket=bucket, Policy=json.dumps({"Version": "2012-10-17", "Statement": [{
+ "Effect": "Allow", "Principal": {"AWS": "arn:aws:iam::222222222222:root"}, "Action": "s3:GetObject",
+ "Resource": f"arn:aws:s3:::{bucket}/*"}]}))
+ assert reader.get_object(Bucket=bucket, Key="k")["Body"].read() == b"k"
+ finally:
+ owner.delete_bucket_policy(Bucket=bucket)
+ _purge_foreign(owner, bucket)
+
+
def test_s3_public_bucket_acl_grants_only_what_the_bucket_permissions_cover(s3):
"""Bucket READ lists and WRITE adds and removes objects; neither opens objects, configuration or the ACL."""
owner, reader = _s3_account("111111111111"), _s3_account("222222222222")