Skip to content

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

fix(s3): refuse bucket policy writes through an access point (#2373)

iot-rocket3ffa0702887c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 575e0e29..07c595cf 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
### Fixed
+- **S3 — bucket policy writes through an access point** — under `AUTH=true` `PutBucketPolicy` and `DeleteBucketPolicy` sent through an access point alias or ARN changed the bucket's policy; they now get 403 `AccessDenied` with an explicit deny on the access point, while `GetBucketPolicy` through it and both writes on the bucket itself work as before. Contributed by @iot-rocket.
- **S3 — SigV2 requests through an access point** — under `AUTH=true` a SigV2 presigned URL for an access point ARN failed its signature check, because the check signed the access point host as a bucket, and a SigV2 `Authorization` header through an access point alias or ARN was served. The presigned URL is now checked on its path as the SDK signs it, and the header request gets 400 `InvalidRequest`. Contributed by @iot-rocket.
- **IoT — thing-group membership authorized on the group and the thing** — with `AUTH=true`, `AddThingToThingGroup` and `RemoveThingFromThingGroup` were authorized on `thinggroup/addThingToThingGroup` (the request path), so a policy granting exactly the thing group and the thing was denied; both calls are now checked on the thing group and then the thing named in the body (by name or ARN), and a denial names the resource that failed. Contributed by @iot-rocket.
- **API Gateway — `AWS_IAM` methods refuse an `Authorization` header that is not SigV4** — under `AUTH=true` a REST API method with `AWS_IAM` authorization served a bearer token or an algorithm in lower case, and answered the SigV4a algorithm name without a signed `x-amz-region-set` or an unknown `AWS4-` algorithm with `UnrecognizedClientException`; it now answers 403 `IncompleteSignatureException` with API Gateway's message for each, while a method without `AWS_IAM` still serves such calls. Contributed by @iot-rocket.
diff --git a/ministack/core/iam_evaluator.py b/ministack/core/iam_evaluator.py
index 0d9fad98..02664f70 100644
--- a/ministack/core/iam_evaluator.py
+++ b/ministack/core/iam_evaluator.py
@@ -1624,6 +1624,10 @@ def enforce(access_key_id: str, iam_action: str, service: str,
result = evaluate(ctx, principal.policies, *principal.limits)
found = _resource_policy(service, resource_arn)
if found is None and service == "s3" and "s3:dataaccesspointarn" in context:
+ if (iam_action in ("s3:PutBucketPolicy", "s3:DeleteBucketPolicy") and result.decision != "Deny"
+ and resource_arn == context["s3:dataaccesspointarn"]):
+ # An access point refuses bucket policy writes as an explicit Deny of its own.
+ return EvalResult("Deny", principal_arn=principal.arn, resource_policy_arn=resource_arn)
# A data request through an access point meets the access point policy on the access point ARN.
from ministack.services.s3 import access_point_policy_for_arn
diff --git a/tests/test_s3_access_points.py b/tests/test_s3_access_points.py
index fab0666f..be2ebc39 100644
--- a/tests/test_s3_access_points.py
+++ b/tests/test_s3_access_points.py
@@ -510,6 +510,34 @@ def test_access_point_policy_contract(auth_enabled, iam_user, bucket, access_poi
_assert_access(auth_enabled, grant in ("bucket", "both"), lambda: client.get_object(Bucket=bucket, Key="k.txt"))
[email protected]("via", ["AccessPointArn", "Alias"])
+def test_access_point_refuses_bucket_policy_writes_contract(auth_enabled, iam_user, bucket, access_point, via):
+ """Under AUTH an access point refuses PutBucketPolicy and DeleteBucketPolicy and serves GetBucketPolicy."""
+ _name, created = access_point
+ arn = created["AccessPointArn"]
+ client = iam_user(policy={"Version": "2012-10-17", "Statement": [{
+ "Effect": "Allow", "Action": ["s3:PutBucketPolicy", "s3:GetBucketPolicy", "s3:DeleteBucketPolicy"],
+ "Resource": [f"arn:aws:s3:::{bucket}", arn]}]}).client("s3", region="us-east-1")
+ policy = json.dumps({"Version": "2012-10-17", "Statement": [{
+ "Effect": "Allow", "Principal": {"AWS": f"arn:aws:iam::{ACCOUNT}:root"}, "Action": "s3:GetObject",
+ "Resource": f"arn:aws:s3:::{bucket}/*"}]})
+ client.put_bucket_policy(Bucket=bucket, Policy=policy)
+ assert client.get_bucket_policy(Bucket=created[via])["Policy"] == policy
+ for write in (lambda: client.delete_bucket_policy(Bucket=created[via]),
+ lambda: client.put_bucket_policy(Bucket=created[via], Policy=policy)):
+ if auth_enabled:
+ with pytest.raises(ClientError) as exc:
+ write()
+ error = exc.value.response["Error"]
+ assert error["Code"] == "AccessDenied"
+ assert error["Message"].endswith(f'on resource: "{arn}" with an explicit deny in a resource-based policy')
+ assert client.get_bucket_policy(Bucket=bucket)["Policy"] == policy
+ else:
+ write()
+ client.delete_bucket_policy(Bucket=bucket)
+
+
@pytest.mark.auth_contract
def test_access_point_refuses_unsigned_requests_contract(auth_enabled, bucket, access_point):
"""Under AUTH an unsigned request through an access point is refused; the bucket still serves it."""