Skip to content

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

fix(s3): refuse ACLs on buckets with BucketOwnerEnforced (#2391)

iot-rocket41c6aeecb5ed
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b669ac9a..eab7fe20 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 — ACLs on buckets with `BucketOwnerEnforced`** — on a bucket with ACLs disabled, the default for new buckets, ACL writes answered 200 and a stored bucket grant still opened the bucket to other accounts and anonymous callers; `PutBucketAcl` and `PutObjectAcl` now answer 400 `AccessControlListNotSupported`, `PutObject`, `CopyObject`, `CreateMultipartUpload` and POST uploads refuse any ACL but an owner-only one with the same error, `CreateBucket` and a switch to `BucketOwnerEnforced` refuse an ACL that grants others with 400 `InvalidBucketAclWithObjectOwnership`, and ACLs stored earlier grant nothing and read as the owner's until ACLs are enabled again. Contributed by @iot-rocket.
- **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.
diff --git a/ministack/services/s3.py b/ministack/services/s3.py
index dc839f96..0063d78b 100644
--- a/ministack/services/s3.py
+++ b/ministack/services/s3.py
@@ -1144,10 +1144,13 @@ def _foreign_bucket_allows(owner_account: str, bucket_name: str, key: str,
wants = _bucket_acl_wants(method, bucket_name, key, query_params)
groups = [_ACL_GROUP_ALL_USERS] if not caller else [
_ACL_GROUP_ALL_USERS, _ACL_GROUP_AUTHENTICATED]
+ bucket = _buckets.get_scoped(owner_account, None, bucket_name)
+ # BucketOwnerEnforced disables the bucket ACL as well.
+ bucket_acl = None if bucket is None or _bucket_owner_enforced(bucket) else _bucket_acl.get_scoped(
+ owner_account, None, bucket_name)
if not _public_access_blocked(owner_account, bucket_name, "IgnorePublicAcls"):
- for stored, granted in ((_bucket_acl.get_scoped(owner_account, None, bucket_name), wants),
- (object_acl, object_wants)):
+ for stored, granted in ((bucket_acl, wants), (object_acl, object_wants)):
for uri, permission in _acl_grants(stored):
if uri in groups and permission in granted:
return True
@@ -2837,6 +2840,9 @@ def _create_bucket(name: str, body: bytes, headers: dict = None, endpoint_region
grant_acl, err = _grant_headers_acl(headers, enforced)
if err:
return err
+ if enforced and not _owner_only_acl(headers, ("private",)):
+ return _error("InvalidBucketAclWithObjectOwnership",
+ "Bucket cannot have ACLs set with ObjectOwnership's BucketOwnerEnforced setting", 400)
if endpoint_region:
err = _location_constraint_error(name, _location_constraint(body), endpoint_region)
if err is not None:
@@ -3512,6 +3518,8 @@ def _put_bucket_acl(name: str, body: bytes, headers: dict | None = None):
grant_acl, err = _grant_headers_acl(headers, enforced)
if err:
return err
+ if enforced and (err := _acls_disabled_error(headers, body, _CANNED_BUCKET_ACLS)):
+ return err
# Canned ACL from x-amz-acl header takes precedence and is mutually
# exclusive with an XML body per the AWS API reference. SDK callers send
@@ -3532,7 +3540,7 @@ def _put_bucket_acl(name: str, body: bytes, headers: dict | None = None):
return 200, {}, b""
if not body:
- return _missing_acl_error(headers, enforced)
+ return _error("MissingSecurityHeader", "Your request was missing a required header.", 400)
try:
# Validate XML well-formedness — real AWS rejects malformed bodies
# with MalformedACLError. We don't enforce grantee/permission
@@ -3587,6 +3595,11 @@ def _delete_bucket_tagging(name: str):
def _put_bucket_ownership_controls(name: str, body: bytes):
if name not in _buckets:
return _no_such_bucket(name)
+ stored_acl = _bucket_acl.get(name)
+ if b"BucketOwnerEnforced" in body and (_acl_grants(stored_acl) or any(
+ grantee != _canonical_owner_id() for grantee, _ in _acl_grants(stored_acl, "ID"))):
+ return _error("InvalidBucketAclWithObjectOwnership",
+ "Bucket cannot have ACLs set with ObjectOwnership's BucketOwnerEnforced setting", 400)
_buckets[name]["_ownership_controls"] = body.decode("utf-8", errors="replace")
_buckets[name].pop("_ownership_controls_deleted", None)
return 200, {}, b""
@@ -4639,6 +4652,8 @@ def _put_object(bucket_name: str, key: str, body: bytes, headers: dict):
grant_acl, err = _grant_headers_acl(headers, _bucket_owner_enforced(bucket))
if err:
return err
+ if _bucket_owner_enforced(bucket) and (err := _acls_disabled_error(headers)):
+ return err
refused = _public_acl_refused(bucket_name, headers)
if refused:
return refused
@@ -5036,6 +5051,8 @@ def _post_object(bucket_name: str, body: bytes, headers: dict):
canned_acl = synth.get("x-amz-acl")
if canned_acl and canned_acl not in _CANNED_OBJECT_ACLS:
return _error("InvalidArgument", f"Invalid x-amz-acl value: {canned_acl}", 400)
+ if _bucket_owner_enforced(bucket) and (err := _acls_disabled_error(synth)):
+ return err
refused = _public_acl_refused(bucket_name, synth)
if refused:
return refused
@@ -6197,6 +6214,8 @@ def _copy_object(bucket_name: str, dest_key: str, headers: dict):
grant_acl, err = _grant_headers_acl(headers, _bucket_owner_enforced(dest_bucket))
if err:
return err
+ if _bucket_owner_enforced(dest_bucket) and (err := _acls_disabled_error(headers)):
+ return err
refused = _public_acl_refused(bucket_name, headers)
if refused:
return refused
@@ -7264,9 +7283,7 @@ def _public_acl_refused(bucket_name: str, headers: dict, acl_xml: str | bytes |
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 None
- bucket = _buckets.get(bucket_name)
- if (bucket is None or _bucket_owner_enforced(bucket)
- or not _public_access_blocked(get_account_id(), bucket_name, "BlockPublicAcls")):
+ if bucket_name not in _buckets or not _public_access_blocked(get_account_id(), bucket_name, "BlockPublicAcls"):
return None
return _error("AccessDenied",
"public ACLs are prevented by the BlockPublicAcls setting in S3 Block Public Access.", 403)
@@ -7346,11 +7363,39 @@ def _grant_headers_acl(headers: dict, enforced: bool) -> tuple[str | None, tuple
), None
-def _missing_acl_error(headers: dict, enforced: bool) -> tuple:
- """The error for an ACL write with neither a canned ACL nor a body (grant headers with ACLs disabled included)."""
- if enforced and any(headers.get(name) for name, _ in _GRANT_HEADER_PERMISSIONS):
- return _error("AccessControlListNotSupported", "The bucket does not allow ACLs", 400)
- return _error("MissingSecurityHeader", "Your request was missing a required header.", 400)
+# Canned ACLs that leave the bucket owner the only grantee of an object the owner writes.
+_OWNER_ONLY_OBJECT_ACLS = ("private", "bucket-owner-read", "bucket-owner-full-control")
+
+
+def _owner_only_acl(headers: dict, canned_acls: tuple[str, ...]) -> bool:
+ """Whether the request sets no ACL, one of *canned_acls* or only ``x-amz-grant-full-control`` to the owner."""
+ canned = headers.get("x-amz-acl")
+ if canned and canned not in canned_acls:
+ return False
+ present = [name for name, _ in _GRANT_HEADER_PERMISSIONS if headers.get(name)]
+ if not present:
+ return True
+ if present != ["x-amz-grant-full-control"]:
+ return False
+ owner_id = _canonical_owner_id()
+ for item in headers["x-amz-grant-full-control"].split(","):
+ kind, _, grantee = item.partition("=")
+ if kind.strip().lower() != "id" or grantee.strip().strip('"') != owner_id:
+ return False
+ return True
+
+
+def _acls_disabled_error(headers: dict, body: bytes = b"", canned_acls=None) -> tuple | None:
+ """AccessControlListNotSupported for an ACL a BucketOwnerEnforced bucket refuses, else None.
+
+ With *canned_acls* (PutBucketAcl, PutObjectAcl) every ACL is refused; object writes keep an owner-only one.
+ """
+ if canned_acls is None:
+ refused = not _owner_only_acl(headers, _OWNER_ONLY_OBJECT_ACLS)
+ else:
+ refused = bool(body) or headers.get("x-amz-acl") in canned_acls or any(
+ headers.get(name) for name, _ in _GRANT_HEADER_PERMISSIONS)
+ return _error("AccessControlListNotSupported", "The bucket does not allow ACLs", 400) if refused else None
def _default_object_acl_xml() -> bytes:
@@ -7392,7 +7437,8 @@ def _get_object_acl(bucket_name: str, key: str, query_params: dict | None = None
if gone is not None:
return gone
version_id = _resolve_subresource_version(query_params, bucket, key)
- stored = _object_acl.get((bucket_name, key, version_id))
+ # BucketOwnerEnforced hides a stored ACL; it is back once ACLs are enabled again.
+ stored = None if _bucket_owner_enforced(bucket) else _object_acl.get((bucket_name, key, version_id))
body = stored.encode("utf-8") if stored else _default_object_acl_xml()
resp_headers = {"Content-Type": "application/xml"}
if version_id:
@@ -7425,6 +7471,8 @@ def _put_object_acl(bucket_name: str, key: str, body: bytes, headers: dict, quer
grant_acl, err = _grant_headers_acl(headers, enforced)
if err:
return err
+ if enforced and (err := _acls_disabled_error(headers, body, _CANNED_OBJECT_ACLS)):
+ return err
# Canned ACL from x-amz-acl header takes precedence and is mutually
# exclusive with an XML body per the AWS API reference. Either path
@@ -7445,7 +7493,7 @@ def _put_object_acl(bucket_name: str, key: str, body: bytes, headers: dict, quer
return 200, {}, b""
if not body:
- return _missing_acl_error(headers, enforced)
+ return _error("MissingSecurityHeader", "Your request was missing a required header.", 400)
try:
# Validate XML well-formedness — real AWS rejects malformed bodies
# with MalformedACLError. We don't enforce grantee/permission
@@ -8225,6 +8273,8 @@ def _create_multipart_upload(bucket_name: str, key: str, headers: dict):
lock_err = _object_lock_header_error(bucket_name, headers)
if lock_err:
return lock_err
+ if _bucket_owner_enforced(bucket) and (err := _acls_disabled_error(headers)):
+ return err
refused = _public_acl_refused(bucket_name, headers)
if refused:
return refused
diff --git a/tests/test_s3.py b/tests/test_s3.py
index 2ee57843..caee477e 100644
--- a/tests/test_s3.py
+++ b/tests/test_s3.py
@@ -1544,6 +1544,59 @@ def test_s3_block_public_acls_refuses_setting_a_public_acl(s3):
finally:
_purge_foreign(s3, bkt)
+
+def test_s3_bucket_owner_enforced_refuses_acls(s3):
+ """With ACLs disabled every ACL write is refused; object writes and CreateBucket keep an owner-only ACL."""
+ import requests
+
+ bkt = f"boe-acl-{uuid.uuid4().hex[:8]}"
+ s3.create_bucket(Bucket=bkt)
+ s3.put_object(Bucket=bkt, Key="k", Body=b"x")
+ owner_id = s3.list_buckets()["Owner"]["ID"]
+ owner, source = f'id="{owner_id}"', {"Bucket": bkt, "Key": "k"}
+ owner_policy = {"Owner": {"ID": owner_id}, "Grants": [
+ {"Grantee": {"Type": "CanonicalUser", "ID": owner_id}, "Permission": "FULL_CONTROL"}]}
+ try:
+ for kwargs in ({"ACL": "public-read"}, {"ACL": "log-delivery-write"}, {"GrantRead": owner}):
+ with pytest.raises(ClientError) as exc:
+ s3.create_bucket(Bucket=f"{bkt}-x", **kwargs)
+ assert exc.value.response["Error"]["Code"] == "InvalidBucketAclWithObjectOwnership", kwargs
+ for kwargs in ({"ACL": "private"}, {"GrantFullControl": owner}):
+ s3.create_bucket(Bucket=f"{bkt}-x", **kwargs)
+ s3.delete_bucket(Bucket=f"{bkt}-x")
+
+ for call, kwargs in (
+ (s3.put_bucket_acl, {"ACL": "private"}),
+ (s3.put_bucket_acl, {"ACL": "log-delivery-write"}),
+ (s3.put_bucket_acl, {"AccessControlPolicy": owner_policy}),
+ (s3.put_object_acl, {"Key": "k", "ACL": "bucket-owner-full-control"}),
+ (s3.put_object_acl, {"Key": "k", "AccessControlPolicy": owner_policy}),
+ (s3.put_object, {"Key": "n", "Body": b"n", "ACL": "public-read"}),
+ (s3.put_object, {"Key": "n", "Body": b"n", "ACL": "aws-exec-read"}),
+ (s3.put_object, {"Key": "n", "Body": b"n", "GrantRead": owner}),
+ (s3.copy_object, {"Key": "c", "CopySource": source, "ACL": "public-read"}),
+ (s3.create_multipart_upload, {"Key": "m", "GrantRead": owner}),
+ ):
+ with pytest.raises(ClientError) as exc:
+ call(Bucket=bkt, **kwargs)
+ assert exc.value.response["Error"]["Code"] == "AccessControlListNotSupported", kwargs
+ assert exc.value.response["ResponseMetadata"]["HTTPStatusCode"] == 400
+ post = s3.generate_presigned_post(
+ Bucket=bkt, Key="p", Fields={"acl": "public-read"}, Conditions=[{"acl": "public-read"}])
+ assert requests.post(post["url"], data=post["fields"], files={"file": ("p", b"x")}).status_code == 400
+ assert s3.list_objects_v2(Bucket=bkt)["KeyCount"] == 1
+
+ for acl in ("private", "bucket-owner-read", "bucket-owner-full-control"):
+ s3.put_object(Bucket=bkt, Key="n", Body=b"n", ACL=acl)
+ s3.put_object(Bucket=bkt, Key="n", Body=b"n", GrantFullControl=owner)
+ s3.copy_object(Bucket=bkt, Key="c", CopySource=source, ACL="private")
+ upload = s3.create_multipart_upload(Bucket=bkt, Key="m", ACL="bucket-owner-full-control")
+ s3.abort_multipart_upload(Bucket=bkt, Key="m", UploadId=upload["UploadId"])
+ assert [g["Permission"] for g in s3.get_bucket_acl(Bucket=bkt)["Grants"]] == ["FULL_CONTROL"]
+ assert [g["Permission"] for g in s3.get_object_acl(Bucket=bkt, Key="k")["Grants"]] == ["FULL_CONTROL"]
+ finally:
+ _purge_foreign(s3, bkt)
+
def test_s3_ownership_controls(s3):
bkt = "intg-s3-ownership"
s3.create_bucket(Bucket=bkt)
@@ -3282,7 +3335,7 @@ def test_s3_bucket_acl_canned(s3):
owner-only."""
import uuid as _u
bucket = f"acl-bkt-canned-{_u.uuid4().hex[:8]}"
- s3.create_bucket(Bucket=bucket, ACL="public-read")
+ s3.create_bucket(Bucket=bucket, ObjectOwnership="ObjectWriter", ACL="public-read")
grants = s3.get_bucket_acl(Bucket=bucket)["Grants"]
assert len(grants) == 2
group = [g for g in grants if g["Grantee"]["Type"] == "Group"]
@@ -3314,7 +3367,7 @@ def test_s3_put_bucket_acl_xml_body(s3):
"""A well-formed AccessControlPolicy XML body is accepted and round-trips."""
import uuid as _u
bucket = f"acl-bkt-xml-{_u.uuid4().hex[:8]}"
- s3.create_bucket(Bucket=bucket)
+ s3.create_bucket(Bucket=bucket, ObjectOwnership="ObjectWriter")
s3.put_bucket_acl(
Bucket=bucket,
AccessControlPolicy={
@@ -4103,7 +4156,7 @@ def test_s3_noncurrent_versions_keep_bytes_tags_and_acls_across_restart(s3_persi
after a noncurrent version is purged under a delete marker."""
s3mod = s3_persist
bucket = "qa-s3-persist-history"
- s3mod._create_bucket(bucket, b"")
+ s3mod._create_bucket(bucket, b"", {"x-amz-object-ownership": "ObjectWriter"})
s3mod._bucket_versioning[bucket] = "Enabled"
vids = [s3mod._put_object(bucket, "k", body, {"x-amz-tagging": f"n={body.decode()}"})[1]["x-amz-version-id"]
for body in (b"one", b"two", b"three")]
@@ -5348,7 +5401,7 @@ def test_s3_put_object_acl_canned(s3):
"""Canned ACL via x-amz-acl header is stored and round-trips via Get."""
import uuid as _u
bucket = f"acl-canned-{_u.uuid4().hex[:8]}"
- s3.create_bucket(Bucket=bucket)
+ s3.create_bucket(Bucket=bucket, ObjectOwnership="ObjectWriter")
s3.put_object(Bucket=bucket, Key="k", Body=b"x")
s3.put_object_acl(Bucket=bucket, Key="k", ACL="public-read")
acl = s3.get_object_acl(Bucket=bucket, Key="k")
@@ -5394,7 +5447,7 @@ def test_s3_put_object_acl_xml_body(s3):
"""A well-formed AccessControlPolicy XML body is accepted and round-trips."""
import uuid as _u
bucket = f"acl-xml-{_u.uuid4().hex[:8]}"
- s3.create_bucket(Bucket=bucket)
+ s3.create_bucket(Bucket=bucket, ObjectOwnership="ObjectWriter")
s3.put_object(Bucket=bucket, Key="k", Body=b"x")
s3.put_object_acl(
Bucket=bucket, Key="k",
@@ -5433,7 +5486,7 @@ def test_s3_object_acl_per_version(s3):
default ACL while the addressed version keeps what was set."""
import uuid as _u
bucket = f"acl-ver-{_u.uuid4().hex[:8]}"
- s3.create_bucket(Bucket=bucket)
+ s3.create_bucket(Bucket=bucket, ObjectOwnership="ObjectWriter")
s3.put_bucket_versioning(
Bucket=bucket, VersioningConfiguration={"Status": "Enabled"})
v1 = s3.put_object(Bucket=bucket, Key="k", Body=b"one")["VersionId"]
@@ -5463,7 +5516,7 @@ def test_s3_put_object_canned_acl_versioned(s3):
"""x-amz-acl on PutObject sticks to the version that PUT created."""
import uuid as _u
bucket = f"acl-put-ver-{_u.uuid4().hex[:8]}"
- s3.create_bucket(Bucket=bucket)
+ s3.create_bucket(Bucket=bucket, ObjectOwnership="ObjectWriter")
s3.put_bucket_versioning(
Bucket=bucket, VersioningConfiguration={"Status": "Enabled"})
v1 = s3.put_object(
@@ -5532,6 +5585,34 @@ def test_s3_object_acl_grant_opens_reads_to_another_account(s3):
owner.delete_bucket(Bucket=bucket)
+def test_s3_bucket_owner_enforced_needs_an_owner_only_bucket_acl(s3):
+ """BucketOwnerEnforced is refused while the bucket ACL grants others; object ACLs read as the owner's meanwhile."""
+ owner, reader = _s3_account("111111111111"), _s3_account("222222222222")
+ bucket = f"boe-switch-{uuid.uuid4().hex[:8]}"
+ _acl_enabled_bucket(owner, bucket)
+ enforced = {"Rules": [{"ObjectOwnership": "BucketOwnerEnforced"}]}
+ try:
+ owner.put_object(Bucket=bucket, Key="k", Body=b"one", ACL="public-read")
+ owner.put_bucket_acl(Bucket=bucket, ACL="public-read")
+ with pytest.raises(ClientError) as exc:
+ owner.put_bucket_ownership_controls(Bucket=bucket, OwnershipControls=enforced)
+ assert exc.value.response["Error"]["Code"] == "InvalidBucketAclWithObjectOwnership"
+ assert exc.value.response["ResponseMetadata"]["HTTPStatusCode"] == 400
+ assert reader.list_objects_v2(Bucket=bucket)["KeyCount"] == 1
+
+ owner.put_bucket_acl(Bucket=bucket, ACL="private")
+ owner.put_bucket_ownership_controls(Bucket=bucket, OwnershipControls=enforced)
+ assert [g["Permission"] for g in owner.get_object_acl(Bucket=bucket, Key="k")["Grants"]] == ["FULL_CONTROL"]
+ assert _denied(reader.get_object, Bucket=bucket, Key="k")
+
+ owner.put_bucket_ownership_controls(
+ Bucket=bucket, OwnershipControls={"Rules": [{"ObjectOwnership": "ObjectWriter"}]})
+ assert len(owner.get_object_acl(Bucket=bucket, Key="k")["Grants"]) == 2
+ assert reader.get_object(Bucket=bucket, Key="k")["Body"].read() == b"one"
+ finally:
+ _purge_foreign(owner, bucket)
+
+
def test_s3_object_acl_grant_to_another_account_follows_the_version(s3):
"""The grant stays on the version current when it was set; READ_ACP opens that version's ACL."""
owner, reader = _s3_account("111111111111"), _s3_account("222222222222")
@@ -5687,7 +5768,7 @@ def test_s3_post_object_acl_field(s3):
import requests
bucket = f"acl-post-{_u.uuid4().hex[:8]}"
- s3.create_bucket(Bucket=bucket)
+ s3.create_bucket(Bucket=bucket, ObjectOwnership="ObjectWriter")
post = s3.generate_presigned_post(
Bucket=bucket, Key="k",
Fields={"acl": "public-read"},
@@ -6857,7 +6938,7 @@ def test_s3_canned_acl_expands_to_group_grants(s3):
implies: public-read -> AllUsers READ; authenticated-read -> AuthenticatedUsers
READ; plus the owner's FULL_CONTROL. Invalid canned values are rejected. (#1322)"""
bkt = f"acl-{_uuid_mod.uuid4().hex[:8]}"
- s3.create_bucket(Bucket=bkt)
+ s3.create_bucket(Bucket=bkt, ObjectOwnership="ObjectWriter")
def grants(key):
acl = s3.get_object_acl(Bucket=bkt, Key=key)
@@ -7121,7 +7202,7 @@ def test_s3_list_object_versions_rolls_up_prefixes(s3):
def test_s3_copy_object_applies_canned_acl(s3):
"""x-amz-acl on a copy permissions the destination, as it does on a put."""
bkt = "s3-copy-acl"
- s3.create_bucket(Bucket=bkt)
+ s3.create_bucket(Bucket=bkt, ObjectOwnership="ObjectWriter")
s3.put_object(Bucket=bkt, Key="src.txt", Body=b"body")
s3.copy_object(Bucket=bkt, Key="dst.txt", ACL="public-read",
@@ -9137,7 +9218,7 @@ def test_delete_objects_in_an_unversioned_bucket(s3, sqs, notified):
def test_s3_create_owned_bucket_again(s3):
east = f"owned-east-{_uuid_mod.uuid4().hex[:8]}"
west = f"owned-west-{_uuid_mod.uuid4().hex[:8]}"
- s3.create_bucket(Bucket=east, ACL="public-read")
+ s3.create_bucket(Bucket=east, ObjectOwnership="ObjectWriter", ACL="public-read")
s3.create_bucket(Bucket=west, CreateBucketConfiguration={"LocationConstraint": "us-west-2"})
try:
s3.create_bucket(Bucket=east)
@@ -9247,6 +9328,20 @@ def test_anonymous_request_follows_a_public_bucket_policy_and_public_access_bloc
assert _anonymous_call("GET", f"/{bucket}/k")[0] == 403
+def test_s3_bucket_owner_enforced_ignores_a_stored_bucket_acl():
+ """A public bucket ACL stored before ACLs were disabled, as older persisted state can hold, grants nothing."""
+ from ministack.core.responses import request_scope
+ from ministack.services import s3 as s3mod
+
+ bucket = f"boe-stored-{uuid.uuid4().hex[:8]}"
+ headers = {"x-amz-object-ownership": "ObjectWriter", "x-amz-acl": "public-read"}
+ assert _lock_call("PUT", f"/{bucket}", headers)[0] == 200
+ with request_scope("000000000000", "us-east-1"):
+ assert s3mod._foreign_bucket_allows("000000000000", bucket, "", "GET", {}, "")
+ s3mod._buckets[bucket].pop("_ownership_controls")
+ assert not s3mod._foreign_bucket_allows("000000000000", bucket, "", "GET", {}, "")
+
+
def test_anonymous_request_is_served_without_auth(monkeypatch):
bucket = _anonymous_bucket(monkeypatch, auth=False)
status, _, resp = _anonymous_call("GET", f"/{bucket}/k")
@@ -9475,8 +9570,10 @@ def test_s3_unsigned_region_request_is_not_redirected(euw1_bucket):
eu-west-1 answers 200 wherever the request lands."""
import urllib.request
- _regional_client("s3", "eu-west-1").put_object(Bucket=euw1_bucket, Key="public.txt", Body=b"open",
- ACL="public-read")
+ euw1 = _regional_client("s3", "eu-west-1")
+ euw1.put_bucket_ownership_controls(
+ Bucket=euw1_bucket, OwnershipControls={"Rules": [{"ObjectOwnership": "ObjectWriter"}]})
+ euw1.put_object(Bucket=euw1_bucket, Key="public.txt", Body=b"open", ACL="public-read")
with urllib.request.urlopen(f"{ENDPOINT}/{euw1_bucket}/public.txt") as resp:
assert (resp.status, resp.read()) == (200, b"open")
@@ -10215,10 +10312,11 @@ def test_s3_canned_acl_condition_contract(auth_enabled, iam_user, auth_condition
@pytest.mark.auth_contract
-def test_s3_grant_header_condition_contract(auth_enabled, iam_user, auth_condition_bucket):
+def test_s3_grant_header_condition_contract(auth_enabled, iam_user, s3, auth_condition_bucket):
"""A bucket policy on s3:x-amz-grant-read sees the grant header of a PutObject."""
user = iam_user(policy=_ALLOW_ALL_S3)
bucket = auth_condition_bucket(user.arn, "s3:PutObject", {"Null": {"s3:x-amz-grant-read": "false"}})
+ s3.put_bucket_ownership_controls(Bucket=bucket, OwnershipControls={"Rules": [{"ObjectOwnership": "ObjectWriter"}]})
client = user.client("s3")
_assert_s3_condition(auth_enabled, True, lambda: client.put_object(Bucket=bucket, Key="plain", Body=b"x"))
grant = 'id="c4c1ede66af53448b93c283ce9448c4ba468c9432aa01d700d3878632f77d2d0"'