Skip to content

fix(kms): a KeyId resolves from its own ARN, and the tagging API reaches a key (#922) (#835) - #924

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/kms-arn-resolution
Sep 16, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/kms-arn-resolution

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #922. Part of #835 — the third of its eleven rows; eight remain.

Why one PR and not two

KMSPlugin.resolveKeyID split a KeyId on / and returned the last component, and its eighteen callers then keyed the load and the store by the caller's own AccountID/Region. The tagging row of #835 needs the same key layout from an ARN alone, so the two are one change seen from two sides — a resolver that re-derived the layout would be a second producer of it, the drift #826 found for SQS and #918 for CloudFront.

More decisively: fixing the resolution is what makes the cross-account case reachable. All three KMS tagging operations publish "Cross-account use: No", but while the account came from the request context a foreign-account key ARN could only ever hit the caller's own record, so the prohibition had nothing to refuse. Honouring the ARN's account without adding the refusal would have turned a wrong-record write into a genuine cross-account write. Hence kmsRequireLocal, and hence one commit.

What changes

Resolution. kmsParseARN takes no *RequestContext, so the rule #826/#845/#910/#918 established is structural rather than remembered. The cut is on the first / and the remainder is not required to be a single segment — AWS's own example of an AWS managed key's alias is alias/aws/s3, which resolved today to the key ID s3, and a parser refusing a slashed identifier outright would reject a legal ARN. For a key the extra segment is refused, because a key ID is a UUID (or mrk- plus a de-hyphenated one) and carries no /. An alias ARN is followed through the pointer stored in the account and Region the ARN names; CreateAlias/UpdateAlias refuse a TargetKeyId outside the caller's own. A bare key ID and a bare alias/ name still resolve against the caller's context, because KeyId documents four forms and two are not ARNs. The five KMS key builders become free functions with no receiver state.

decrypt deliberately keeps the request context, and says so: its key ID comes out of the ciphertext, not out of a KeyId parameter, so there is no ARN to take an account from.

The #835 row. Resolver arm, merge arm and scanner, sharing the parser above. An alias ARN is refused at the tagging API rather than followed — the developer guide's "You cannot tag aliases, custom key stores, AWS managed keys, AWS owned keys, or KMS keys in other AWS accounts" — since resolving it would tag a resource AWS says cannot be tagged and the caller would read the tag back off the key. The merge is the first over an array-shaped tags member (KMS spells its fields TagKey/TagValue, alone among the remaining rows), so mergeRecordTagListTags takes the field names as parameters, is written once for the rows that follow, and re-emits sorted by key per #862. Its guard tests a colon-terminated prefix — the third namespace to need one — because key is a prefix of key_ids/key_policy and alias of alias_names, and key_ids is a JSON array a bare-prefix test would report taggable.

Account isolation at the tagging arm is emergent rather than guarded, as ACM's is: the resolver builds an account- and Region-qualified key, so a foreign-account ARN builds a key nothing is stored at and the merge fails "resource not found".

Deliberately left, and recorded

Provenance

The ARN formats, the alias/aws/s3 example, the four accepted KeyId forms, the TagKey/TagValue field names, InvalidArnException/400, NotFoundException/400 and "Cross-account use: No" are quoted from the KMS developer guide's Key identifiers and Tagging keys pages and from API_Tag, API_TagResource, API_UntagResource and API_ListResourceTags. Mapping the cross-account prohibition onto NotFoundException is substrate's reading: AWS publishes the prohibition and the code but does not join them.

Compatibility

A TagResource/UntagResource/ListResourceTags naming a key outside the caller's account or Region now answers NotFoundException where it previously reached the caller's own key of that ID and answered 200. An alias ARN presented to any KeyId operation now resolves through the alias pointer rather than to its last path component. TagResource and ListResourceTags return tags ordered by key.

Verification

Nine-command gate, all green; coverage holds at 84.1% emulator / 83.6% total. 17 new wire tests through StartTestServer, with every tag read back through KMS's own ListResourceTags per #765 and in the other direction through GetResources.

…hes a key

resolveKeyID split the identifier on "/" and returned the last component,
and its eighteen callers keyed the load and the store by the caller's own
account and Region. So a foreign-account, foreign-Region key ARN addressed
the caller's own key of that ID: TagResource wrote onto the wrong key and
UntagResource stripped it, both answering 200.

The type match was unanchored too, so an alias ARN resolved to its last
path component -- and AWS's own example of an AWS managed key's alias is
alias/aws/s3, which resolved to the key ID "s3". The cut is now on the
first "/" only, and a slashed identifier is refused for a key alone,
because a key ID is a UUID or "mrk-" plus a de-hyphenated one.

kmsParseARN takes no *RequestContext, so the rule is structural. The five
KMS key builders become free functions, so the resolver and the plugin
cannot produce two layouts of one key. decrypt deliberately keeps the
request context: its key ID comes out of the ciphertext, not out of a
KeyId parameter.

Fixing the resolution is what made the cross-account case reachable, so
the refusal lands in the same commit: all three tagging operations publish
"Cross-account use: No", but while the account came from the request
context the prohibition had nothing to refuse.

The same context-free parser gives the tagging API its kms arm, the third
row of #835. An alias ARN is refused there rather than followed, per the
developer guide's list of what cannot be tagged. The merge is the first
over an array-shaped tags member -- KMS spells its fields TagKey/TagValue
-- so mergeRecordTagListTags takes the field names as parameters and sorts
by key per #862, and its guard tests a colon-terminated prefix because
"key" is a prefix of key_ids and key_policy.

Closes #922.
Part of #835.
@scttfrdmn
scttfrdmn merged commit 30213dd into main Sep 16, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the fix/kms-arn-resolution branch September 16, 2026 01:41
@codecov

codecov Bot commented Sep 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.12876% with 23 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/tagging_plugin.go 82.35% 11 Missing and 1 partial ⚠️
emulator/kms_plugin.go 92.17% 9 Missing ⚠️
emulator/kms_tags.go 96.00% 2 Missing ⚠️

📢 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.

fix(kms): a key ARN resolves to the caller's own account, and an alias ARN resolves to its own name

1 participant