Skip to content

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

fix(iot): authorize tagging calls on the resource ARN under AUTH=true (#2266)

iot-rocketcef027972560
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 301ec32f..6df93b40 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
### Fixed
+- **IoT — tagging calls are authorized on the tagged resource under `AUTH=true`** — `TagResource`, `UntagResource` and `ListTagsForResource` were checked against resource `*`, so a policy scoped to a thing group, job or other IoT ARN denied them even on that resource, and only `Resource: "*"` let them through; they are now checked against the `resourceArn` the request names. Contributed by @iot-rocket.
- **AppSync — data-plane error responses** — Event API `POST /event` refusals answered `{"__type", "message"}` with MiniStack's own messages, and the GraphQL endpoint answered 500 for a body that is not a JSON object, a message-only 400 for a missing query and 200 for an unparseable one; both now answer AWS's `{"errors": [{"errorType", "message"}]}` envelope with AWS's error types, messages and `x-amzn-ErrorType` header (`SerializationException`, `MalformedHttpRequestException`, `ResourceNotFoundException` for an unknown API, `UnknownOperationException` for another path on an Event API host), and the publish endpoint answers `application/json`. Contributed by @iot-rocket.
- **Cognito — OAuth scope checks for federation and `client_credentials`** — `/oauth2/authorize` with an `identity_provider` went on to the identity provider whatever the `scope`; it now redirects to the callback with `error_description=invalid_scope` (and the `state`) when a requested scope is not in the client's `AllowedOAuthScopes`, as the managed login does. The `client_credentials` grant answered `invalid_scope` to any request naming an undefined resource server scope; it now ignores those once a defined scope is requested, so `api/read bogus/x` is granted `api/read` and `api/admin bogus/x` answers `invalid_grant`, as AWS does. Contributed by @iot-rocket.
- **S3 — Block Public Access settings and public bucket ACL grants** — any one of the four Block Public Access settings shut off every public grant on a bucket, none refused setting a public ACL, and a public bucket ACL opened every read and write in the bucket. `BlockPublicAcls` now refuses a call that sets a public ACL on a bucket with ACLs enabled (403 `AccessDenied`) and leaves the grants in place, `IgnorePublicAcls` drops public ACL grants and `RestrictPublicBuckets` the bucket policy. A bucket ACL grants what its permission covers: `READ` the listings, `WRITE` adding and removing objects, `READ_ACP` and `WRITE_ACP` the bucket ACL. Contributed by @iot-rocket.
diff --git a/ministack/core/iam_actions.py b/ministack/core/iam_actions.py
index c5ba6c00..17f03800 100644
--- a/ministack/core/iam_actions.py
+++ b/ministack/core/iam_actions.py
@@ -1613,6 +1613,12 @@ def extract_resource_arn(service: str, method: str, path: str,
if len(parts) > 1 and parts[0] in ("topics", "retainedMessage"):
topic = "/".join(parts[1:])
return f"arn:aws:iot:{region}:{account_id}:topic/{topic}"
+ # The tagging calls authorize on the resource they name in resourceArn,
+ # read from where the handler reads it: the body of a POST, the query of a GET.
+ if parts in (["tags"], ["untag"]):
+ arn = (_safe_json_field(body, "resourceArn") if method == "POST"
+ else _query_param(query_params, "resourceArn"))
+ return arn if isinstance(arn, str) and arn else "*"
_IOT_RESOURCES = {
"things": "thing",
"thing-types": "thingtype",
diff --git a/tests/test_iam_auth.py b/tests/test_iam_auth.py
index 0f396523..b43ed3c9 100644
--- a/tests/test_iam_auth.py
+++ b/tests/test_iam_auth.py
@@ -2386,6 +2386,26 @@ class TestResourceArn:
from ministack.core.iam_actions import extract_resource_arn
assert extract_resource_arn("iot", "GET", "/jobs", {}, b"", {}, "us-east-1", "123") == "*"
+ def test_iot_tagging_calls_authorize_on_the_named_resource(self):
+ from ministack.core.iam_actions import extract_resource_arn
+ group = "arn:aws:iot:us-east-1:123:thinggroup/fleet"
+ tag = json.dumps({"resourceArn": group, "tags": [{"Key": "k", "Value": "v"}]}).encode()
+ untag = json.dumps({"resourceArn": group, "tagKeys": ["k"]}).encode()
+ assert extract_resource_arn("iot", "POST", "/tags", {}, tag, {}, "us-east-1", "123") == group
+ assert extract_resource_arn("iot", "POST", "/untag", {}, untag, {}, "us-east-1", "123") == group
+ assert extract_resource_arn(
+ "iot", "GET", "/tags", {}, b"", {"resourceArn": [group]}, "us-east-1", "123"
+ ) == group
+ assert extract_resource_arn("iot", "GET", "/tags", {}, b"", {}, "us-east-1", "123") == "*"
+ # Only where the handler reads it: a second resourceArn elsewhere is not the target.
+ other = {"resourceArn": ["arn:aws:iot:us-east-1:123:thinggroup/other"]}
+ assert extract_resource_arn("iot", "POST", "/tags", {}, tag, other, "us-east-1", "123") == group
+ assert extract_resource_arn(
+ "iot", "GET", "/tags", {}, tag, other, "us-east-1", "123"
+ ) == "arn:aws:iot:us-east-1:123:thinggroup/other"
+ bad = json.dumps({"resourceArn": [group]}).encode()
+ assert extract_resource_arn("iot", "POST", "/untag", {}, bad, {}, "us-east-1", "123") == "*"
+
def test_iot_job_policy_from_the_cdk_actually_allows_the_call(self):
"""The two halves together, which is where this showed up. The CDK's
AwsCustomResource emits exactly this policy for an IoT job, and every
diff --git a/tests/test_iot.py b/tests/test_iot.py
index 8720e267..a1131d95 100644
--- a/tests/test_iot.py
+++ b/tests/test_iot.py
@@ -1183,6 +1183,45 @@ def test_iot_tagging_refuses_unknown_and_foreign_arns(iot_client):
assert _iot_error(iot_client.list_tags_for_resource, resourceArn=target) == (400, message)
+def test_iot_tagging_authorizes_on_the_resource_arn(auth_enabled, iam_user, iot_client):
+ allowed, other = (iot_client.create_thing_group(thingGroupName=_unique("group"))["thingGroupArn"]
+ for _ in range(2))
+ iot_client.tag_resource(resourceArn=other, tags=[{"Key": "existing", "Value": "before"}])
+ thing_type = allowed.rsplit(":", 1)[0] + ":thingtype/" + _unique("type")
+ user = iam_user(policy={"Version": "2012-10-17", "Statement": [{
+ "Effect": "Allow", "Action": ["iot:TagResource", "iot:UntagResource", "iot:ListTagsForResource"],
+ "Resource": allowed,
+ }]})
+ client = user.client("iot")
+ try:
+ client.tag_resource(resourceArn=allowed, tags=[{"Key": "k", "Value": "v"}])
+ assert client.list_tags_for_resource(resourceArn=allowed)["tags"] == [{"Key": "k", "Value": "v"}]
+ client.untag_resource(resourceArn=allowed, tagKeys=["k"])
+ assert client.list_tags_for_resource(resourceArn=allowed)["tags"] == []
+
+ for action, call, arn, kwargs in (
+ ("TagResource", client.tag_resource, other, {"tags": [{"Key": "k", "Value": "v"}]}),
+ ("UntagResource", client.untag_resource, other, {"tagKeys": ["existing"]}),
+ ("ListTagsForResource", client.list_tags_for_resource, other, {}),
+ ("ListTagsForResource", client.list_tags_for_resource, thing_type, {}),
+ ):
+ if auth_enabled:
+ with pytest.raises(ClientError) as ei:
+ call(resourceArn=arn, **kwargs)
+ error = ei.value.response
+ assert error["ResponseMetadata"]["HTTPStatusCode"] == 403
+ assert error["Error"]["Code"] == "AccessDeniedException"
+ assert error["Error"]["Message"].startswith(
+ f"User: {user.arn} is not authorized to perform: iot:{action} ")
+ else:
+ assert call(resourceArn=arn, **kwargs)["ResponseMetadata"]["HTTPStatusCode"] == 200
+ assert _iot_tags(iot_client, other) == ({"existing": "before"} if auth_enabled else {"k": "v"})
+ finally:
+ for arn in (allowed, other):
+ iot_client.delete_thing_group(thingGroupName=arn.rsplit("/", 1)[1])
+
+
def test_iot_tag_limits_and_validation(iot_client):
from conftest import make_client