Skip to content

fix(tagging): the ARN resolver checks the resource type it strips (#845) - #851

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/tagging-arn-resolver-guards
Sep 14, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/tagging-arn-resolver-guards

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

The tagging API derived a state key from an identifier it never checked. resolveARN switches on an ARN's service and then strips the resource-type prefix with strings.TrimPrefix, which returns its input unchanged when the prefix does not match — so eight of its sixteen arms turned an ARN naming a different type under the same service into a well-formed key for the wrong kind of resource instead of failing. A Step Functions activity: ARN resolved to a state-machine key and tagged a same-named state machine; a Lambda layer ARN resolved to a function key; DynamoDB stream, DynamoDB index and Kinesis consumer ARNs each did the same to their service's real resource; and an S3 object ARN was keyed as a bucket, through an arm that stripped an empty prefix.

Every arm now checks what it strips. Seven already did, so the pattern was in the same function.

Three guards are more than a prefix test

Each is that way because AWS's own ARN format makes it so — all three formats from the Service Authorization Reference's Resource types tables:

  • Lambda — a version ARN function:${Name}:${Version} and an alias ARN function:${Name}:${Alias} are lexically identical, and AWS does not resolve the ambiguity. Any qualified function ARN is refused rather than guessed at, which costs nothing because tags belong to the function.
  • DynamoDB — an index (table/${Name}/index/…) and a stream (table/${Name}/stream/…) nest under the table prefix, so a remaining / is not a table.
  • Step Functions — the two taggable types are distinguished by literal case, stateMachine: against activity:. Case-sensitive on purpose, and asserted from both sides so the guard cannot be loosened without a test going red.

The recurrence #826's own audit missed

The DynamoDB arm built its key from the calling request's account rather than the ARN's, so TagResources naming another account's table tagged the caller's own same-named table and UntagResources stripped tags from it. It is the arm immediately below the SQS one #826 rewrote, and it carried the reason verbatim in a comment it did not follow.

resolveARN no longer receives a *RequestContext at all, so the invariant is structural rather than advisory: an arm that cannot reach the caller's account cannot regress this way again. ecsTagStateKey, which had the same defect, is the same.

Refusing a foreign-account ARN is substrate's reading, not AWS's — TagResources says only that "you can only tag resources that are located in the specified AWS Region for the AWS account", and an explicit refusal is documented for a partition mismatch but not for an account mismatch.

The error codes, which the issue had the wrong way round

An unsupported resource type now answers InternalServiceException/500 — what FailureInfo publishes for "the resource type in the request is not supported by the Resource Groups Tagging API". InvalidParameterException/400 is kept for an ARN that is not an ARN. Substrate splits them on whether the ARN parses, because that is the only distinction a caller can act on differently; the same page can be read either way, and that is stated as substrate's reading rather than AWS's rule.

Both handlers map through one pair of functions so TagResources and UntagResources cannot drift. And ErrorMessage no longer passes err.Error() through — that was publishing substrate's internal state-key layout in an API response. The detail goes to the log.

ECS answered 200 for a tag it never wrote

resourceStateKey returned ("", "") for any ARN it could not key and applyTagsToResource then returned success — so TagResource against a task definition answered the exact response AWS documents for a successful tag, "an HTTP 200 response with an empty HTTP body", having written nothing. UntagResource did the same, and a nonexistent resource behaved identically. Meanwhile ListTagsForResource refused the same ARN: the two sides of one tag disagreed and the write side was the one that lied.

Both write sides now refuse, with the codes ECS publishes — InvalidParameterException for an ARN that keys nothing (the code API_TagResource's own resourceArn description names for the one bad-ARN case AWS spells out), and ResourceNotFoundException for a well-formed ARN naming no stored resource. That not-found refusal moves from HTTP 404 to 400: every ECS exception except ServerException is documented at 400, and the 404 was the shape of a REST service rather than a JSON-protocol one.

ECS's key builder and the tagging API's ecs arm both built the cluster key by hand, from two different pieces of string concatenation. They now go through one ecsTagStateKey — the "one builder, so they cannot drift" structure #826 established — which is also what lets the tagging API reach an ECS service, task and task definition rather than a cluster only. A task definition is taggable, per AWS's own list of six ("capacity providers, tasks, services, task definitions, clusters, and container instances"); a capacity provider and a container instance are refused rather than silently accepted, because substrate stores neither, so there is no record a tag could land on or be read back from.

Two defects found on the way, neither previously filed

  • Tag order was nondeterministic. All four slice-returning merge helpers — EC2, IAM, ECS, EFS — built their result by ranging over a Go map, so two identical runs stored one resource's tags in different orders. That made ListTagsForResource, DescribeTags and GetResources report a different order each run for the same state, and it made a hash over the record differ when nothing about the record had changed — which an event-sourced emulator cannot afford, because a replay has to be able to compare state it did not change and find it equal. All four now sort by key, as DescribeStacks already does for a stack's own tags.
  • mergeResourceTags' ECS arm decoded every key into ECSCluster. Harmless only while the resolver could reach nothing but a cluster; a service or task definition would have been re-marshalled with every member ECSCluster lacks silently dropped. It now edits the stored record's tags member as raw JSON, through the same function ECS's own TagResource writes with.

One plan item reversed

The plan said mergeResourceTags' default arm and its three unsupported <SVC> resource key arms were dead once the resolver guards. They are not: the writer has three callers, and two are CloudFormation paths that build the namespace and key themselves rather than from an ARN (cfnStampResourceTags, cfnPropagateRecordStackTags). For those, the arms are the only thing between a stamp aimed at an unhandled namespace and a silent success — the defect class this PR removes, not relocates. They stay, with a doc paragraph saying why.

Tests

New emulator/tagging_arn_guards_test.go, 35 cases, all over the wire against a real server. Every tag is read back through the owning service's own tag call, never out of the state store (#765) — the putTest* helpers are deliberately not extended, because a helper that writes state directly cannot tell a tag that landed from a tag that was reported as landed, which is the whole of this defect class.

Fifteen wrong-type ARNs are asserted through both TagResources and UntagResources, because asserting only one is how the two came to disagree. The positive side is asserted too — a bare table/{name} and a stateMachine: ARN still resolve, and their tags read back through DynamoDB's ListTagsOfResource and Step Functions' ListTagsForResource — without which a resolver that refused everything would pass. Plus the cross-account case (the caller's own table untouched after both operations), the ECS refusals across all three tag operations, an RGTA-written task-definition tag read back through ECS, and the key-order assertion.

Documentation

docs/services.md's Resource Groups Tagging section corrects two overstatements v0.114.0 left: the claim that #826's audit found every arm agrees was narrower than it read, and the scanned-type list named seven types where GetResources scans sixteen. It now also states which types TagResources reaches that GetResources does not, so a tag readable through the owning service's own call but absent from a GetResources listing reads as the scanner half of #835 rather than as an inconsistency. A new table gives which failure gets which error code, with the substrate-vs-AWS provenance stated inline.

Verification

All nine gate commands green. Coverage holds at the baseline: emulator 83.8%, total 83.2%.

Closes #845.

Part of #835 — its resolver half, and the ECS service and task-definition rows on the write side. The eleven mergeResourceTags arms and the eleven GetResources scanners remain, with the revised mechanism, the verified ARN formats for all eleven, and what AWS does and does not publish recorded in a comment there.

One finding is filed separately rather than fixed here: resolveARN has no elasticloadbalancing arm at all, so the tagging API reaches no ELB resource today, v2 included. It needs the classic/v2 ARN-arity discrimination from #844 first.

`resolveARN` switched on an ARN's service and then built a state key with
`strings.TrimPrefix`, which returns its input unchanged when the prefix does
not match. Eight of its sixteen arms never checked the prefix first, so an ARN
naming a *different type* under the same service produced a well-formed key for
the wrong kind of resource rather than an error: a Step Functions `activity:`
ARN resolved to a state-machine key and tagged a same-named state machine, a
Lambda layer ARN resolved to a function key, and DynamoDB stream, index and
Kinesis consumer ARNs each did the same to their service's real resource. Every
arm now checks what it strips.

Three checks are more than a prefix test because AWS's own ARN formats make
them so. A Lambda version and alias ARN are lexically identical, so any
qualified function ARN is refused rather than guessed at. A DynamoDB index and
stream nest under the table prefix, so a remaining `/` is not a table. And Step
Functions distinguishes its two taggable types by the capital M in
`stateMachine:`, so that check is case-sensitive on purpose.

The DynamoDB arm was also a live recurrence of #826: it built its key from the
*calling request's* account rather than the ARN's, so `TagResources` naming
another account's table tagged the caller's own same-named table and
`UntagResources` stripped tags from it — the arm immediately below the SQS one
#826 rewrote, carrying the reason verbatim in a comment it did not follow.
`resolveARN` no longer receives a `*RequestContext` at all, so the invariant is
structural rather than advisory.

An unsupported resource type now answers `InternalServiceException`/500, which
is what `FailureInfo` publishes for it; `InvalidParameterException`/400 is kept
for an ARN that is not an ARN. Both handlers map through one pair of functions
so they cannot drift, and the message no longer passes `err.Error()` through —
that was publishing substrate's state-key layout in an API response.

ECS's two write sides stop answering the response AWS documents for a
*successful* tag over a resource nothing was written to. `resourceStateKey`
returned `("", "")` for any ARN it could not key and `applyTagsToResource` then
reported success, while `ListTagsForResource` refused the same ARN: the two
sides of one tag disagreed and the write side was the one that lied. Both now
refuse, with the codes ECS publishes, and `ResourceNotFoundException` moves
from HTTP 404 to the 400 every ECS exception except `ServerException` carries.
ECS's key builder had DynamoDB's account-attribution defect too, and the
tagging API built the cluster key a second time by hand; both now go through
one `ecsTagStateKey`, which is also what lets the tagging API reach an ECS
service, task and task definition rather than a cluster only.

Two defects found on the way, neither previously filed. All four
slice-returning tag merge helpers built their result by ranging over a Go map,
so two identical runs stored one resource's tags in different orders — which an
event-sourced emulator cannot afford, because a replay has to be able to
compare state it did not change and find it equal; all four now sort by key.
And `mergeResourceTags`' ECS arm decoded every key into `ECSCluster`, which
would have truncated a service or task-definition record on re-marshal once the
resolver could reach one; it now edits the stored record's `tags` member as raw
JSON.

The plan's instruction to delete that writer's `default` arm and its
per-namespace key fallbacks is reversed: two of its three callers are
CloudFormation paths that build the key themselves rather than from an ARN, so
for those the arms are the only thing between a stamp aimed at an unhandled
namespace and a silent success.

Closes #845.
Part of #835 — its resolver half, and the ECS service and task-definition rows
on the write side. The eleven `mergeResourceTags` arms and the `GetResources`
scanners stay open.
@scttfrdmn scttfrdmn added this to the v0.115.0 milestone Sep 14, 2026
@codecov

codecov Bot commented Sep 14, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.81503% with 28 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/ecs_plugin.go 82.47% 17 Missing ⚠️
emulator/tagging_plugin.go 85.52% 9 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tagging: TagResources reports a nonexistent resource as InternalServiceException/500, the code AWS reserves for an unsupported type

1 participant