Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
fix(iam): authorize the remaining S3 operations as S3 documents them (#1627)
iot-rocketcb5ec267fcdc
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 998e4b7a..2248dd73 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,8 @@ Versioning follows [Semantic Versioning](https://semver.org/).## [Unreleased]+### Fixed+- **IAM enforcement — the remaining S3 operations authorize as S3 documents them** — with `AUTH=true`, a browser `POST Object` was checked as `s3:DeleteObject`, a request naming a `versionId` as the unversioned action, `DeleteObjects` once against the bucket ARN (so a grant on `arn:aws:s3:::bucket/*` denied every batch delete), `CopyObject` / `UploadPartCopy` without `s3:GetObject` on the source, `GetObjectAttributes` without its `s3:GetObject` companion, `x-amz-bypass-governance-retention` without `s3:BypassGovernanceRetention`, and the sub-resources without a table row (`accelerate`, `requestPayment`, `publicAccessBlock`, `ownershipControls`, `intelligent-tiering`, `metrics`, `analytics`, `inventory`, `object-lock`, `policyStatus`, `retention`, `legal-hold`, `attributes`, `select`, `torrent`) fell to the method default. Each now maps to the action the S3 reference lists, a `versionId` resolves to the `*Version*` action, and both enforcement sites run the extra per-request checks (per key, source object, attributes pair, governance bypass). Contributed by @iot-rocket.### Added- **RDS — `FailoverDBCluster` promotes replicated PostgreSQL readers** — with `MINISTACK_RDS_PG_CLUSTER_REPLICATION=1`, failover now calls `pg_promote()` on the selected hot standby, makes it the writer endpoint, and re-clones the former writer with `pg_basebackup` as a read-only standby. Metadata changes only after promotion succeeds; shared-container clusters retain their existing metadata-only behavior. Contributed by @kiran01bm.### Fixeddiff --git a/ministack/app.py b/ministack/app.pyindex 5a781fa2..791996e8 100644--- a/ministack/app.py+++ b/ministack/app.py@@ -1693,7 +1693,7 @@ async def _handle_s3_vhost_request(host: str, path: str, method: str, headers: d# IAM enforcement for S3 virtual-hosted requestsif AUTH:- from ministack.core.iam_actions import _s3_action, extract_resource_arn+ from ministack.core.iam_actions import _s3_action, extract_resource_arn, s3_additional_checkss3_action = _s3_action(method, vhost_path, query_params)if s3_action:@@ -1701,6 +1701,12 @@ async def _handle_s3_vhost_request(host: str, path: str, method: str, headers: ddenied = _enforce_data_plane("s3", f"s3:{s3_action}", headers, query_params, "", resource_arn=s3_resource)if denied:return denied+ # A copy also reads its source, a batch delete is one check per+ # key, an attributes call is a pair, a governance bypass its own action.+ for extra_action, extra_arn in s3_additional_checks(method, vhost_path, headers, body, query_params):+ denied = _enforce_data_plane("s3", extra_action, headers, query_params, "", resource_arn=extra_arn)+ if denied:+ return deniedtry:# Pass the original (pre-rewrite) URI as signed_path so a presigned@@ -2096,6 +2102,16 @@ async def _dispatch_service_request(service, method, path, headers, body, routing_params, region, get_account_id())denied = enforce(access_key, iam_action, service, region, resource_arn=resource_arn)+ # A copy also reads its source, a batch delete is one check per+ # key, an attributes call is a pair, a governance bypass its own action.+ if service == "s3" and not denied:+ from ministack.core.iam_actions import s3_additional_checks++ for extra_action, extra_arn in s3_additional_checks(method, path, headers, body, routing_params):+ denied = enforce(access_key, extra_action, service, region, resource_arn=extra_arn)+ if denied:+ iam_action = extra_action+ breakif denied:if isinstance(denied, AuthError):return access_denied_response(diff --git a/ministack/core/iam_actions.py b/ministack/core/iam_actions.pyindex 2688f8e8..d9036d89 100644--- a/ministack/core/iam_actions.py+++ b/ministack/core/iam_actions.py@@ -10,6 +10,9 @@ import jsonimport loggingimport osimport re+from urllib.parse import unquote++from defusedxml.ElementTree import ParseError, fromstringlogger = logging.getLogger("ministack")@@ -146,7 +149,7 @@ _S3_ACTIONS: dict[tuple[str, int], str] = {("DELETE", 2): "DeleteObject",("HEAD", 2): "GetObject",("POST", 2): "PutObject",- ("POST", 1): "DeleteObject", # DeleteObjects (batch)+ ("POST", 1): "PutObject", # POST Object (the browser form upload); ?delete is a table row}# S3 query-param sub-operations@@ -183,17 +186,56 @@ _S3_QUERY_ACTIONS: dict[str, dict[str, str]] = {"restore": {"POST": "RestoreObject"},}-# Sub-resources that only exist at the bucket level.+# Sub-resources that only exist at the bucket level. A DELETE of a+# configuration authorizes as its Put action, like the shared table's rows._S3_BUCKET_QUERY_ACTIONS: dict[str, dict[str, str]] = {"versions": {"GET": "ListBucketVersions"},+ "delete": {"POST": "DeleteObject"}, # DeleteObjects (batch)+ "accelerate": {"GET": "GetAccelerateConfiguration", "PUT": "PutAccelerateConfiguration"},+ "requestPayment": {"GET": "GetBucketRequestPayment", "PUT": "PutBucketRequestPayment"},+ "publicAccessBlock": {"GET": "GetBucketPublicAccessBlock", "PUT": "PutBucketPublicAccessBlock",+ "DELETE": "PutBucketPublicAccessBlock"},+ "ownershipControls": {"GET": "GetBucketOwnershipControls", "PUT": "PutBucketOwnershipControls",+ "DELETE": "PutBucketOwnershipControls"},+ "intelligent-tiering": {"GET": "GetIntelligentTieringConfiguration",+ "PUT": "PutIntelligentTieringConfiguration",+ "DELETE": "PutIntelligentTieringConfiguration"},+ "metrics": {"GET": "GetMetricsConfiguration", "PUT": "PutMetricsConfiguration",+ "DELETE": "PutMetricsConfiguration"},+ "analytics": {"GET": "GetAnalyticsConfiguration", "PUT": "PutAnalyticsConfiguration",+ "DELETE": "PutAnalyticsConfiguration"},+ "inventory": {"GET": "GetInventoryConfiguration", "PUT": "PutInventoryConfiguration",+ "DELETE": "PutInventoryConfiguration"},+ "object-lock": {"GET": "GetBucketObjectLockConfiguration",+ "PUT": "PutBucketObjectLockConfiguration"},+ "policyStatus": {"GET": "GetBucketPolicyStatus"},}# Sub-resources whose IAM action differs between the bucket and the object-# level; these win over the shared table for an object request.+# level, or that only exist on an object; these win over the shared table+# for an object request._S3_OBJECT_QUERY_ACTIONS: dict[str, dict[str, str]] = {"tagging": {"GET": "GetObjectTagging", "PUT": "PutObjectTagging","DELETE": "DeleteObjectTagging"},"acl": {"GET": "GetObjectAcl", "PUT": "PutObjectAcl"},+ "retention": {"GET": "GetObjectRetention", "PUT": "PutObjectRetention"},+ "legal-hold": {"GET": "GetObjectLegalHold", "PUT": "PutObjectLegalHold"},+ "attributes": {"GET": "GetObjectAttributes"},+ "select": {"POST": "GetObject"}, # SelectObjectContent reads the object+ "torrent": {"GET": "GetObject"},+}++# An object request that names a version (``?versionId=``) authorizes as+# the version-specific action.+_S3_VERSIONED_ACTIONS: dict[str, str] = {+ "GetObject": "GetObjectVersion",+ "DeleteObject": "DeleteObjectVersion",+ "GetObjectTagging": "GetObjectVersionTagging",+ "PutObjectTagging": "PutObjectVersionTagging",+ "DeleteObjectTagging": "DeleteObjectVersionTagging",+ "GetObjectAcl": "GetObjectVersionAcl",+ "PutObjectAcl": "PutObjectVersionAcl",+ "GetObjectAttributes": "GetObjectVersionAttributes",}@@ -201,6 +243,7 @@ def _s3_action(method: str, path: str, query_params: dict) -> str | None:parts = [p for p in path.split("/") if p]depth = min(len(parts), 2)+ action = None# Sub-operation query params first: the level-specific table, then the# shared one. A request for the service root has no sub-resources.if depth >= 1:@@ -210,9 +253,113 @@ def _s3_action(method: str, path: str, query_params: dict) -> str | None:if qp in query_params:a = action_map.get(method)if a:- return a+ action = a+ break+ if action:+ break+ if action is None:+ action = _S3_ACTIONS.get((method, depth))++ if depth == 2 and action and _query_param(query_params, "versionId"):+ action = _S3_VERSIONED_ACTIONS.get(action, action)+ return action+++# Operations that take x-amz-bypass-governance-retention; when the header says+# true they also need s3:BypassGovernanceRetention on the object.+_S3_GOVERNANCE_BYPASS_ACTIONS = frozenset({"DeleteObject", "DeleteObjectVersion", "PutObjectRetention"})+- return _S3_ACTIONS.get((method, depth))+def _s3_source_object(headers: dict) -> tuple[str, str] | None:+ """The ``(arn, version_id)`` of a CopyObject / UploadPartCopy source, from+ ``x-amz-copy-source`` (``/bucket/key`` or ``bucket/key``, optionally+ ``?versionId=``), or None when the header is absent or malformed."""+ src = headers.get("x-amz-copy-source", "")+ if not src:+ return None+ src, _, query = src.partition("?")+ src = unquote(src).lstrip("/")+ if "/" not in src:+ return None+ version_id = ""+ for pair in query.split("&"):+ k, _, v = pair.partition("=")+ if k == "versionId":+ version_id = unquote(v)+ return f"arn:aws:s3:::{src}", version_id+++def _s3_batch_delete_targets(bucket: str, body: bytes) -> list[tuple[str, str]]:+ """``(arn, version_id)`` for every ``<Object>`` of a DeleteObjects body.+ The SDKs send the elements in the S3 namespace; a bare body works too."""+ targets: list[tuple[str, str]] = []+ if not body:+ return targets+ try:+ root = fromstring(body)+ except (ParseError, ValueError): # ValueError: defusedxml's forbidden constructs+ return targets+ for obj in root.iter():+ if obj.tag.rpartition("}")[2] != "Object":+ continue+ key = version_id = ""+ for child in obj:+ tag = child.tag.rpartition("}")[2]+ if tag == "Key":+ key = child.text or ""+ elif tag == "VersionId":+ version_id = child.text or ""+ if key:+ targets.append((f"arn:aws:s3:::{bucket}/{key}", version_id))+ return targets+++def s3_additional_checks(method: str, path: str, headers: dict, body: bytes,+ query_params: dict) -> list[tuple[str, str]]:+ """``(iam_action, resource_arn)`` pairs an S3 request needs on top of the+ primary check, per the S3 reference:++ - CopyObject and UploadPartCopy read the source: ``s3:GetObject`` (or+ ``s3:GetObjectVersion``) on the ``x-amz-copy-source`` object.+ - GetObjectAttributes needs ``s3:GetObject`` next to+ ``s3:GetObjectAttributes`` (the ``*Version*`` pair with a versionId).+ - DeleteObjects is one ``s3:DeleteObject`` (``s3:DeleteObjectVersion``+ for a versioned entry) per key. The first key is the primary check's+ resource (see ``extract_resource_arn``); the rest are listed here.+ - ``x-amz-bypass-governance-retention: true`` on DeleteObject,+ DeleteObjects or PutObjectRetention adds+ ``s3:BypassGovernanceRetention`` on every object.+ """+ parts = [p for p in path.split("/") if p]+ action = _s3_action(method, path, query_params)+ if not parts or not action:+ return []+ checks: list[tuple[str, str]] = []+ bypass = headers.get("x-amz-bypass-governance-retention", "").strip().lower() == "true"++ if len(parts) >= 2:+ key_arn = f"arn:aws:s3:::{parts[0]}/{'/'.join(parts[1:])}"+ if action == "PutObject":+ source = _s3_source_object(headers)+ if source:+ arn, version_id = source+ checks.append(("s3:GetObjectVersion" if version_id else "s3:GetObject", arn))+ elif action == "GetObjectAttributes":+ checks.append(("s3:GetObject", key_arn))+ elif action == "GetObjectVersionAttributes":+ checks.append(("s3:GetObjectVersion", key_arn))+ if bypass and action in _S3_GOVERNANCE_BYPASS_ACTIONS:+ checks.append(("s3:BypassGovernanceRetention", key_arn))+ return checks++ if method == "POST" and "delete" in query_params:+ targets = _s3_batch_delete_targets(parts[0], body)+ for arn, version_id in targets[1:]:+ checks.append(("s3:DeleteObjectVersion" if version_id else "s3:DeleteObject", arn))+ if bypass:+ for arn, _ in targets:+ checks.append(("s3:BypassGovernanceRetention", arn))+ return checks# Lambda REST path → IAM action@@ -640,6 +787,13 @@ def extract_resource_arn(service: str, method: str, path: str,if len(parts) >= 2:key = "/".join(parts[1:])return f"arn:aws:s3:::{bucket}/{key}"+ if method == "POST" and "delete" in query_params:+ # DeleteObjects authorizes per object, not on the bucket: the+ # first key is the primary resource, s3_additional_checks()+ # carries the rest.+ targets = _s3_batch_delete_targets(bucket, body)+ if targets:+ return targets[0][0]return f"arn:aws:s3:::{bucket}"if service == "dynamodb":diff --git a/tests/test_iam_auth.py b/tests/test_iam_auth.pyindex fa39e1cd..9caeba26 100644--- a/tests/test_iam_auth.py+++ b/tests/test_iam_auth.py@@ -1214,7 +1214,58 @@ class TestS3ActionMapping:("DELETE", "/bucket", {"cors": ""}, "s3:PutBucketCORS"),("DELETE", "/bucket", {"policy": ""}, "s3:DeleteBucketPolicy"), # this one exists("GET", "/bucket/key", {"versions": ""}, "s3:GetObject"), # bucket-only sub-resource- ("GET", "/bucket/key", {"versionId": "v1"}, "s3:GetObject"), # not s3:GetObjectVersion (known gap)+ # a request that names a version authorizes as the version action+ ("GET", "/bucket/key", {"versionId": "v1"}, "s3:GetObjectVersion"),+ ("HEAD", "/bucket/key", {"versionId": "v1"}, "s3:GetObjectVersion"),+ ("DELETE", "/bucket/key", {"versionId": "v1"}, "s3:DeleteObjectVersion"),+ ("GET", "/bucket/key", {"versionId": "v1", "tagging": ""}, "s3:GetObjectVersionTagging"),+ ("PUT", "/bucket/key", {"versionId": "v1", "tagging": ""}, "s3:PutObjectVersionTagging"),+ ("DELETE", "/bucket/key", {"versionId": "v1", "tagging": ""}, "s3:DeleteObjectVersionTagging"),+ ("GET", "/bucket/key", {"versionId": "v1", "acl": ""}, "s3:GetObjectVersionAcl"),+ ("PUT", "/bucket/key", {"versionId": "v1", "acl": ""}, "s3:PutObjectVersionAcl"),+ ("GET", "/bucket/key", {"versionId": "v1", "attributes": ""}, "s3:GetObjectVersionAttributes"),+ ("GET", "/bucket/key", {"versionId": ["v1"]}, "s3:GetObjectVersion"), # parse_qs list form+ ("GET", "/bucket/key", {"versionId": ""}, "s3:GetObject"), # an empty versionId names nothing+ ("PUT", "/bucket/key", {"versionId": "v1", "retention": ""}, "s3:PutObjectRetention"), # no version action+ ("GET", "/bucket", {"versionId": "v1"}, "s3:ListBucket"), # bucket level: no version action+ # a POST on the bucket is the browser form upload unless it says ?delete+ ("POST", "/bucket", {}, "s3:PutObject"), # POST Object+ ("POST", "/bucket", {"delete": ""}, "s3:DeleteObject"), # DeleteObjects+ # object sub-resources+ ("POST", "/bucket/key", {"select": "", "select-type": "2"}, "s3:GetObject"), # SelectObjectContent+ ("GET", "/bucket/key", {"attributes": ""}, "s3:GetObjectAttributes"),+ ("GET", "/bucket/key", {"retention": ""}, "s3:GetObjectRetention"),+ ("PUT", "/bucket/key", {"retention": ""}, "s3:PutObjectRetention"),+ ("GET", "/bucket/key", {"legal-hold": ""}, "s3:GetObjectLegalHold"),+ ("PUT", "/bucket/key", {"legal-hold": ""}, "s3:PutObjectLegalHold"),+ ("GET", "/bucket/key", {"torrent": ""}, "s3:GetObject"),+ ("GET", "/bucket/key", {"object-lock": ""}, "s3:GetObject"), # bucket-only sub-resource+ # bucket sub-resources; a DELETE of a configuration is its Put action+ ("GET", "/bucket", {"accelerate": ""}, "s3:GetAccelerateConfiguration"),+ ("PUT", "/bucket", {"accelerate": ""}, "s3:PutAccelerateConfiguration"),+ ("GET", "/bucket", {"requestPayment": ""}, "s3:GetBucketRequestPayment"),+ ("PUT", "/bucket", {"requestPayment": ""}, "s3:PutBucketRequestPayment"),+ ("GET", "/bucket", {"publicAccessBlock": ""}, "s3:GetBucketPublicAccessBlock"),+ ("PUT", "/bucket", {"publicAccessBlock": ""}, "s3:PutBucketPublicAccessBlock"),+ ("DELETE", "/bucket", {"publicAccessBlock": ""}, "s3:PutBucketPublicAccessBlock"),+ ("GET", "/bucket", {"ownershipControls": ""}, "s3:GetBucketOwnershipControls"),+ ("PUT", "/bucket", {"ownershipControls": ""}, "s3:PutBucketOwnershipControls"),+ ("DELETE", "/bucket", {"ownershipControls": ""}, "s3:PutBucketOwnershipControls"),+ ("GET", "/bucket", {"intelligent-tiering": "", "id": "x"}, "s3:GetIntelligentTieringConfiguration"),+ ("PUT", "/bucket", {"intelligent-tiering": "", "id": "x"}, "s3:PutIntelligentTieringConfiguration"),+ ("DELETE", "/bucket", {"intelligent-tiering": "", "id": "x"}, "s3:PutIntelligentTieringConfiguration"),+ ("GET", "/bucket", {"metrics": "", "id": "x"}, "s3:GetMetricsConfiguration"),+ ("PUT", "/bucket", {"metrics": "", "id": "x"}, "s3:PutMetricsConfiguration"),+ ("DELETE", "/bucket", {"metrics": "", "id": "x"}, "s3:PutMetricsConfiguration"),+ ("GET", "/bucket", {"analytics": "", "id": "x"}, "s3:GetAnalyticsConfiguration"),+ ("PUT", "/bucket", {"analytics": "", "id": "x"}, "s3:PutAnalyticsConfiguration"),+ ("DELETE", "/bucket", {"analytics": "", "id": "x"}, "s3:PutAnalyticsConfiguration"),+ ("GET", "/bucket", {"inventory": "", "id": "x"}, "s3:GetInventoryConfiguration"),+ ("PUT", "/bucket", {"inventory": "", "id": "x"}, "s3:PutInventoryConfiguration"),+ ("DELETE", "/bucket", {"inventory": "", "id": "x"}, "s3:PutInventoryConfiguration"),+ ("GET", "/bucket", {"object-lock": ""}, "s3:GetBucketObjectLockConfiguration"),+ ("PUT", "/bucket", {"object-lock": ""}, "s3:PutBucketObjectLockConfiguration"),+ ("GET", "/bucket", {"policyStatus": ""}, "s3:GetBucketPolicyStatus"),("PUT", "/bucket/key", {}, "s3:PutObject"),("HEAD", "/bucket/key", {}, "s3:GetObject"),("GET", "/bucket", {}, "s3:ListBucket"),@@ -1236,6 +1287,237 @@ class TestS3ActionMapping:assert evaluate(ctx, [stmts]).decision == "Allow", action+class TestS3AdditionalChecks:+ """A copy reads its source, an attributes call is a pair, a batch delete+ is authorized per key and a governance bypass is its own action, so those+ requests carry checks beyond the (action, resource) pair the extractors+ return."""++ _NS = "http://s3.amazonaws.com/doc/2006-03-01/" # what boto3 sends+ _DELETE_BODY = (f'<Delete xmlns="{_NS}"><Object><Key>a.txt</Key></Object>'+ '<Object><Key>dir/b.txt</Key><VersionId>v7</VersionId></Object>'+ '<Quiet>true</Quiet></Delete>').encode()+ _BYPASS = {"x-amz-bypass-governance-retention": "true"}++ @staticmethod+ def _checks(method, path, headers=None, body=b"", query=None):+ from ministack.core.iam_actions import s3_additional_checks+ return s3_additional_checks(method, path, headers or {}, body, query or {})++ # -- copy source --------------------------------------------------------++ def test_copy_object_reads_the_source(self):+ assert self._checks("PUT", "/dst/key", {"x-amz-copy-source": "/src/a%20b.txt"}) == [+ ("s3:GetObject", "arn:aws:s3:::src/a b.txt")]++ def test_copy_source_may_omit_the_leading_slash_and_name_a_version(self):+ checks = self._checks("PUT", "/dst/key", {"x-amz-copy-source": "src/dir/a.txt?versionId=v3"},+ query={"uploadId": "u", "partNumber": "1"}) # UploadPartCopy+ assert checks == [("s3:GetObjectVersion", "arn:aws:s3:::src/dir/a.txt")]++ def test_plain_put_and_malformed_source_add_nothing(self):+ assert self._checks("PUT", "/dst/key") == []+ assert self._checks("PUT", "/dst/key", {"x-amz-copy-source": "nokey"}) == []+ assert self._checks("PUT", "/dst", {"x-amz-copy-source": "/src/k"}) == [] # CreateBucket+ assert self._checks("GET", "/") == []++ def test_copy_needs_read_on_the_source(self):+ stmts = parse_policy_document({"Statement": [{+ "Effect": "Allow", "Action": "s3:PutObject", "Resource": "arn:aws:s3:::dst/*"}]})+ (act, arn), = self._checks("PUT", "/dst/k", {"x-amz-copy-source": "/src/k"})+ assert evaluate(_ctx(action=act, resource=arn), [stmts]).decision == "ImplicitDeny"++ # -- attributes pair ----------------------------------------------------++ def test_attributes_also_need_get_object(self):+ assert self._checks("GET", "/b/dir/sub/k.txt", query={"attributes": ""}) == [+ ("s3:GetObject", "arn:aws:s3:::b/dir/sub/k.txt")]++ def test_versioned_attributes_pair_with_get_object_version(self):+ assert self._checks("GET", "/b/k", query={"attributes": "", "versionId": "v1"}) == [+ ("s3:GetObjectVersion", "arn:aws:s3:::b/k")]++ def test_a_plain_get_or_head_is_a_single_check(self):+ assert self._checks("GET", "/b/k") == []+ assert self._checks("HEAD", "/b/k", query={"versionId": "v1"}) == []++ # -- governance bypass --------------------------------------------------++ def test_bypass_header_adds_the_bypass_action_on_the_object(self):+ assert self._checks("DELETE", "/b/k", self._BYPASS) == [+ ("s3:BypassGovernanceRetention", "arn:aws:s3:::b/k")]+ assert self._checks("DELETE", "/b/k", self._BYPASS, query={"versionId": "v1"}) == [+ ("s3:BypassGovernanceRetention", "arn:aws:s3:::b/k")]+ assert self._checks("PUT", "/b/k", self._BYPASS, query={"retention": ""}) == [+ ("s3:BypassGovernanceRetention", "arn:aws:s3:::b/k")]++ def test_bypass_header_is_ignored_where_the_operation_has_none(self):+ assert self._checks("DELETE", "/b/k", {"x-amz-bypass-governance-retention": "false"}) == []+ assert self._checks("DELETE", "/b/k", self._BYPASS, query={"tagging": ""}) == []+ assert self._checks("PUT", "/b/k", self._BYPASS) == []+ assert self._checks("DELETE", "/b", self._BYPASS) == []++ def test_batch_delete_bypass_covers_every_key(self):+ checks = self._checks("POST", "/b", self._BYPASS, self._DELETE_BODY, {"delete": ""})+ assert checks == [+ ("s3:DeleteObjectVersion", "arn:aws:s3:::b/dir/b.txt"),+ ("s3:BypassGovernanceRetention", "arn:aws:s3:::b/a.txt"),+ ("s3:BypassGovernanceRetention", "arn:aws:s3:::b/dir/b.txt"),+ ]++ # -- batch delete -------------------------------------------------------++ def test_batch_delete_is_one_check_per_key(self):+ from ministack.core.iam_actions import extract_resource_arn+ primary = extract_resource_arn("s3", "POST", "/b", {}, self._DELETE_BODY, {"delete": ""}, "", "")+ assert primary == "arn:aws:s3:::b/a.txt"+ assert self._checks("POST", "/b", body=self._DELETE_BODY, query={"delete": ""}) == [+ ("s3:DeleteObjectVersion", "arn:aws:s3:::b/dir/b.txt"),+ ]++ def test_batch_delete_body_without_a_namespace_parses_too(self):+ body = b"<Delete><Object><Key>x</Key></Object><Object><Key>y</Key></Object></Delete>"+ assert self._checks("POST", "/b", body=body, query={"delete": ""}) == [+ ("s3:DeleteObject", "arn:aws:s3:::b/y")]++ def test_batch_delete_skips_an_object_without_a_key(self):+ body = (b"<Delete><Object><VersionId>v1</VersionId></Object>"+ b"<Object><Key></Key></Object><Object><Key>z</Key></Object></Delete>")+ from ministack.core.iam_actions import extract_resource_arn+ assert extract_resource_arn("s3", "POST", "/b", {}, body, {"delete": ""}, "", "") == "arn:aws:s3:::b/z"+ assert self._checks("POST", "/b", body=body, query={"delete": ""}) == []++ @pytest.mark.parametrize("body", [b"", b"<Delete>", b"<!DOCTYPE d [<!ENTITY e 'x'>]><Delete>&e;</Delete>"])+ def test_batch_delete_with_no_usable_body_keeps_the_bucket(self, body):+ from ministack.core.iam_actions import extract_resource_arn+ assert extract_resource_arn("s3", "POST", "/b", {}, body, {"delete": ""}, "", "") == "arn:aws:s3:::b"+ assert self._checks("POST", "/b", body=body, query={"delete": ""}) == []++ def test_a_plain_bucket_post_is_not_a_batch_delete(self):+ assert self._checks("POST", "/b", body=self._DELETE_BODY) == [] # POST Object++ def test_object_scoped_policy_allows_a_batch_delete(self):+ # The case from the report: a grant on arn:aws:s3:::b/* used to be+ # denied because the batch was evaluated against the bucket ARN.+ from ministack.core.iam_actions import extract_iam_action, extract_resource_arn+ stmts = parse_policy_document({"Statement": [{+ "Effect": "Allow", "Action": ["s3:DeleteObject", "s3:DeleteObjectVersion"],+ "Resource": "arn:aws:s3:::b/*"}]})+ action = extract_iam_action("s3", "POST", "/b", {}, self._DELETE_BODY, {"delete": ""})+ primary = extract_resource_arn("s3", "POST", "/b", {}, self._DELETE_BODY, {"delete": ""}, "", "")+ checks = [(action, primary)] + self._checks("POST", "/b", body=self._DELETE_BODY, query={"delete": ""})+ for act, arn in checks:+ assert evaluate(_ctx(action=act, resource=arn), [stmts]).decision == "Allow", (act, arn)+++class TestS3EnforcementSites:+ """Both S3 enforcement sites (virtual-hosted and path-style) run the+ additional checks after the primary one. The evaluator is stubbed with a+ policy so the tests drive the app functions in-process."""++ _NS = "http://s3.amazonaws.com/doc/2006-03-01/"+ _BATCH = (f'<Delete xmlns="{_NS}"><Object><Key>a.txt</Key></Object>'+ '<Object><Key>b.txt</Key></Object></Delete>').encode()++ @staticmethod+ def _stub_evaluator(monkeypatch, policy):+ """Route enforce() through a fixed policy; return the checks it saw."""+ import ministack.app as app_mod+ from ministack.core import iam_evaluator++ stmts = parse_policy_document(policy)+ seen = []++ def enforce_stub(access_key_id, iam_action, service, region, resource_arn="*"):+ seen.append((iam_action, resource_arn))+ result = evaluate(_ctx(action=iam_action, resource=resource_arn), [stmts])+ if result.decision == "Allow":+ return None+ result.principal_arn = "arn:aws:iam::000000000000:user/testuser"+ return result++ monkeypatch.setattr(app_mod, "AUTH", True, raising=False)+ monkeypatch.setattr(iam_evaluator, "enforce", enforce_stub)+ return seen++ @staticmethod+ def _vhost(bucket, path, method, headers, body, query):+ import asyncio++ import ministack.app as app_mod+ return asyncio.run(app_mod._handle_s3_vhost_request(+ f"{bucket}.localhost:4566", path, method, headers, body, query))++ @staticmethod+ def _path_style(method, path, headers, body, query):+ import asyncio++ import ministack.app as app_mod+ headers = {"host": "localhost:4566", **headers}+ return asyncio.run(app_mod._dispatch_service_request(method, path, headers, body, query, "req-1"))++ _ONLY_FIRST_KEY = {"Statement": [{+ "Effect": "Allow", "Action": "s3:DeleteObject", "Resource": "arn:aws:s3:::iam-sites-b/a.txt"}]}+ _EVERY_KEY = {"Statement": [{+ "Effect": "Allow", "Action": "s3:DeleteObject", "Resource": "arn:aws:s3:::iam-sites-b/*"}]}+ _WRITE_ONLY = {"Statement": [{+ "Effect": "Allow", "Action": "s3:PutObject", "Resource": "arn:aws:s3:::iam-sites-dst/*"}]}++ def test_vhost_batch_delete_is_denied_on_the_second_key(self, monkeypatch):+ seen = self._stub_evaluator(monkeypatch, self._ONLY_FIRST_KEY)+ status, _headers, body = self._vhost("iam-sites-b", "/", "POST", {}, self._BATCH, {"delete": ""})+ assert status == 403+ assert b"AccessDenied" in body+ assert seen == [("s3:DeleteObject", "arn:aws:s3:::iam-sites-b/a.txt"),+ ("s3:DeleteObject", "arn:aws:s3:::iam-sites-b/b.txt")]++ def test_vhost_batch_delete_with_an_object_scoped_grant_passes(self, monkeypatch):+ seen = self._stub_evaluator(monkeypatch, self._EVERY_KEY)+ status, _headers, _body = self._vhost("iam-sites-b", "/", "POST", {}, self._BATCH, {"delete": ""})+ assert status != 403+ assert [a for a, _ in seen] == ["s3:DeleteObject", "s3:DeleteObject"]++ def test_vhost_copy_without_read_on_the_source_is_denied(self, monkeypatch):+ seen = self._stub_evaluator(monkeypatch, self._WRITE_ONLY)+ status, _headers, body = self._vhost(+ "iam-sites-dst", "/k", "PUT", {"x-amz-copy-source": "/iam-sites-src/k"}, b"", {})+ assert status == 403+ assert b"AccessDenied" in body+ assert seen == [("s3:PutObject", "arn:aws:s3:::iam-sites-dst/k"),+ ("s3:GetObject", "arn:aws:s3:::iam-sites-src/k")]++ def test_path_style_batch_delete_is_denied_on_the_second_key(self, monkeypatch):+ seen = self._stub_evaluator(monkeypatch, self._ONLY_FIRST_KEY)+ status, _headers, body = self._path_style("POST", "/iam-sites-b", {}, self._BATCH, {"delete": ""})+ assert status == 403+ assert b"AccessDenied" in body+ assert seen == [("s3:DeleteObject", "arn:aws:s3:::iam-sites-b/a.txt"),+ ("s3:DeleteObject", "arn:aws:s3:::iam-sites-b/b.txt")]++ def test_path_style_batch_delete_with_an_object_scoped_grant_passes(self, monkeypatch):+ seen = self._stub_evaluator(monkeypatch, self._EVERY_KEY)+ status, _headers, _body = self._path_style("POST", "/iam-sites-b", {}, self._BATCH, {"delete": ""})+ assert status != 403+ assert [a for a, _ in seen] == ["s3:DeleteObject", "s3:DeleteObject"]++ def test_path_style_copy_without_read_on_the_source_is_denied(self, monkeypatch):+ seen = self._stub_evaluator(monkeypatch, self._WRITE_ONLY)+ status, _headers, body = self._path_style(+ "PUT", "/iam-sites-dst/k", {"x-amz-copy-source": "/iam-sites-src/k"}, b"", {})+ assert status == 403+ assert b"AccessDenied" in body+ assert seen == [("s3:PutObject", "arn:aws:s3:::iam-sites-dst/k"),+ ("s3:GetObject", "arn:aws:s3:::iam-sites-src/k")]++ def test_a_primary_denial_stops_before_the_extra_checks(self, monkeypatch):+ seen = self._stub_evaluator(monkeypatch, {"Statement": [+ {"Effect": "Allow", "Action": "s3:GetObject", "Resource": "*"}]})+ status, _headers, _body = self._path_style(+ "PUT", "/iam-sites-dst/k", {"x-amz-copy-source": "/iam-sites-src/k"}, b"", {})+ assert status == 403+ assert seen == [("s3:PutObject", "arn:aws:s3:::iam-sites-dst/k")]++# ---------------------------------------------------------------------------# AccessDenied response formatting# ---------------------------------------------------------------------------