Repository navigation
fix(tagging): the ARN resolver checks the resource type it strips (#845) - #851
Merged
Merged
Conversation
`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.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This was referenced Sep 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The tagging API derived a state key from an identifier it never checked.
resolveARNswitches on an ARN's service and then strips the resource-type prefix withstrings.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 Functionsactivity: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 typestables:function:${Name}:${Version}and an alias ARNfunction:${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.table/${Name}/index/…) and a stream (table/${Name}/stream/…) nest under the table prefix, so a remaining/is not a table.stateMachine:againstactivity:. 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
TagResourcesnaming another account's table tagged the caller's own same-named table andUntagResourcesstripped 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.resolveARNno longer receives a*RequestContextat 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 —
TagResourcessays 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 — whatFailureInfopublishes 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
TagResourcesandUntagResourcescannot drift. AndErrorMessageno longer passeserr.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
resourceStateKeyreturned("", "")for any ARN it could not key andapplyTagsToResourcethen returned success — soTagResourceagainst 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.UntagResourcedid the same, and a nonexistent resource behaved identically. MeanwhileListTagsForResourcerefused 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 —
InvalidParameterExceptionfor an ARN that keys nothing (the codeAPI_TagResource's ownresourceArndescription names for the one bad-ARN case AWS spells out), andResourceNotFoundExceptionfor a well-formed ARN naming no stored resource. That not-found refusal moves from HTTP 404 to 400: every ECS exception exceptServerExceptionis 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
ecsarm both built the cluster key by hand, from two different pieces of string concatenation. They now go through oneecsTagStateKey— 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
ListTagsForResource,DescribeTagsandGetResourcesreport 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, asDescribeStacksalready does for a stack's own tags.mergeResourceTags' ECS arm decoded every key intoECSCluster. Harmless only while the resolver could reach nothing but a cluster; a service or task definition would have been re-marshalled with every memberECSClusterlacks silently dropped. It now edits the stored record'stagsmember as raw JSON, through the same function ECS's ownTagResourcewrites with.One plan item reversed
The plan said
mergeResourceTags'defaultarm and its threeunsupported <SVC> resource keyarms 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) — theputTest*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
TagResourcesandUntagResources, because asserting only one is how the two came to disagree. The positive side is asserted too — a baretable/{name}and astateMachine:ARN still resolve, and their tags read back through DynamoDB'sListTagsOfResourceand 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 whereGetResourcesscans sixteen. It now also states which typesTagResourcesreaches thatGetResourcesdoes not, so a tag readable through the owning service's own call but absent from aGetResourceslisting 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
mergeResourceTagsarms and the elevenGetResourcesscanners 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:
resolveARNhas noelasticloadbalancingarm at all, so the tagging API reaches no ELB resource today, v2 included. It needs the classic/v2 ARN-arity discrimination from #844 first.