Skip to content

fix(stepfunctions): tag the resource the ARN names, in the shape AWS publishes (#910) - #911

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/sfn-tag-arn-account
Sep 15, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/sfn-tag-arn-account

Conversation

@scttfrdmn

@scttfrdmn scttfrdmn commented Sep 15, 2026 •

Copy link
Copy Markdown
Owner

Closes #910. Part of #835 — two more of its remaining rows (Step Functions state machine and activity), leaving five.

The account defect

TagResource (stepfunctions_plugin.go:1015), UntagResource (:1068) and ListTagsForResource (:1120) each did this:

name := extractSMNameFromARN(input.ResourceArn)
if strings.Contains(input.ResourceArn, ":stateMachine:") {
	sm, err := p.loadStateMachine(goCtx, ctx.AccountID, ctx.Region, name)

extractSMNameFromARN is arn[strings.LastIndexByte(arn, ':')+1:] — the last segment and nothing else. The account and Region segments were never read. So an ARN naming another account's same-named state machine resolved the caller's own, and UntagResource stripped tags from it: the damaging direction, since removing a tag turns an aws:ResourceTag Deny into an allow, and it answered 200 while doing it. A cross-Region ARN in the caller's own account did the same.

This is the class #826 closed for SQS and DynamoDB and #845 closed across the tagging API's resolver. One builder, no request context: sfnResolveARN(arn) (ns, key string, err error) takes no *RequestContext at all, and the plugin's smKey/activityKey are now thin wrappers over the two package-level key functions it calls — so there is exactly one producer of a statemachine:/activity: key in the tree, and the tagging API's states arm goes through the same one. That is ecsTagStateKey's arrangement, adopted for the same reason: two derivations of one key drift, and where they drift a tag lands on a record the other side does not read.

The type check was not a type check

strings.Contains(arn, ":stateMachine:") is a substring test over the whole ARN, satisfied by a name as readily as by the type segment — arn:aws:states:{region}:{acct}:activity:x:stateMachine:y took the state-machine branch. The comparison is now against parts[5] alone, and stays case-sensitive, because AWS distinguishes its two taggable resources by the literal segment: stateMachine with a capital M against activity. The tagging API's arm already did this correctly and recorded why; the two now share the function rather than the reasoning.

Two things AWS publishes that substrate did not answer

ResourceNotFound is HTTP 400. All three operation pages list it as "Could not find the referenced resource." with "HTTP Status Code: 400". Substrate answered 404 — a status no Step Functions endpoint returns — so a consumer branching on the status rather than the code saw something impossible. Verified by fetch on API_TagResource and API_ListTagsForResource, not from recollection.

tags is an array of {key, value} objects, not an object. This is the one that made the operations unusable:

{"tags": [{"key": "env", "value": "test"}]}

Substrate decoded Tags map[string]string on TagResource's request and rendered a map on ListTagsForResource's response — while CreateStateMachine (:301) and CreateActivity (:830) in the same plugin already decoded []map[string]string. So the plugin disagreed with itself about the wire shape of its own tags: a tag set at create time could not be read back in a shape any SDK decodes, and TagResource could not be called by an SDK at all (its array body fails to unmarshal into a map, answering InvalidRequest/400).

That is why the shape fix is in this PR rather than its own. It is not scope creep in the direction of "more": without it there is no way to write an over-the-wire test in AWS's actual shape, so the ARN fix would be asserted only on a path no consumer can reach.

ListTagsForResource sorts the array by key. AWS documents no order, so this is substrate's reading, justified by the replay promise — an order following Go's map hash seed differs between two identical calls in one run and cannot replay from the event log (#862).

#835's two rows

Sharing the resolver was again only half of it. The statesNamespace merge arm (tagging_plugin.go:1266) decoded StateMachineState whatever the key named, so tagging an activity through the tagging API replaced its record with a state-machine-shaped one. ActivityArn has no counterpart in that struct, so it vanished outright — nothing refused, and the tag itself was correct, which is exactly why a test asserting on the tag cannot see it. The arm now goes through the shared mergeRecordStringMapTags #908 generalized out of ecsMergeRecordTags, behind sfnKeyIsTaggable, because the states namespace also holds executions and three index keys (statemachine_names:, execution_ids:, activity_names:) that no ARN addresses and that store no tags. The guard and the raw-JSON merge fix different failures and neither substitutes for the other; the arm's comment names both separately.

GetResources gained scanStepFunctionsActivities, since a resolver arm alone makes a resource taggable by name while leaving it invisible to a caller discovering resources — the two halves of #835's criterion per row. Nineteen resource types are now scanned.

Provenance

AWS's, verified by fetch against the API reference:

  • resourceArn is "the Amazon Resource Name (ARN) for the Step Functions state machine or activity" on all three operations — so those two are the whole taggable set and an execution ARN is not admissible.
  • TagResource's errors: InvalidArn/400, ResourceNotFound/400, TooManyTags/400. ListTagsForResource': InvalidArn/400, ResourceNotFound/400. Both publish ResourceNotFound at 400, not 404.
  • The Tag shape and the array-valued tags member, from API_TagResource's Request Syntax and API_ListTagsForResource's Response Syntax.

Substrate's reading, stated as such in the code and in docs/services.md:

  • InvalidArn for an execution ARN. It is well-formed and names a resource these operations do not accept; AWS publishes no code for that case specifically. InvalidArn says the ARN does not belong at this operation, where ResourceNotFound would claim a resource is missing that may well exist. Substrate answered InvalidArn before this change too, but by falling off the end of the strings.Contains chain — it is now a decision.
  • Sorting tags by key, per tagging: the four merge helpers store a resource's tags in Go map order, so one state hashes two ways #862.
  • Refusing a name containing / or :. AWS's Name pattern excludes both; a key built from one addresses nothing, and reporting the ARN malformed rather than the resource absent is the difference between a caller fixing its ARN and a caller waiting for a resource to appear.
  • TooManyTags is not implemented and no tag-count cap is enforced here, which is unchanged by this PR.

Tests

emulator/stepfunctions_tags_test.go, 11 top-level tests, all over the wire through StartTestServer. Every resource is created through its own operation and every ARN taken out of the create response rather than composed — a composed ARN would let the test agree with a resolver that disagreed with what CreateStateMachine reports, which is the divergence being fixed. No putTest* helper (#765).

Coverage: 84.1% emulator / 83.6% total, up from 84.0% / 83.5%.

Mutation test, three stages (by cp, never git stash)

1. Account and Region from the request context restored (name from the last segment, strings.Contains branch): exactly the two cross-boundary tests fail and nothing else —

--- FAIL: TestSFNTags_ACrossRegionARNDoesNotTagTheCallersResource
--- FAIL: TestSFNTags_AForeignAccountARNDoesNotTagTheCallersResource/state_machine
--- FAIL: TestSFNTags_AForeignAccountARNDoesNotTagTheCallersResource/activity

2. The StateMachineState decode restored in the merge arm:

tagging truncated the activity record:
 before {ActivityArn:arn:aws:states:us-east-1:123456789012:activity:fulfill Name:fulfill CreationDate:1.7895...}
 after  {ActivityArn:                                                       Name:fulfill CreationDate:1.7895...}

TestSFNTags_BothTypesAreReachableFromTheTaggingAPI/activity fails with it, because the scanner reports the now-empty ActivityArn and GetResources can no longer find the resource it just tagged.

3. The map wire shape restored on both the request and the response: 8 of the 11 fail. That breadth is the point — every test that reads a tag back reads it through ListTagsForResource, so the shape is load-bearing for all of them.

What is asserted

  • Cross-readability both directions for a state machine and an activity: tag through Step Functions → read through Step Functions; tag through the tagging API → read through ListTagsForResource; tag through Step Functions → read through GetResources; UntagResources → gone. The removal direction is never the untested one.
  • Non-truncation for both kinds, on the members that are not tags (activityArn/name/creationDate, definition/roleArn/type/status). Each test first asserts the create path stored those members, so it cannot pass vacuously against an empty record.
  • ResourceNotFound at 400 across all three operations for both kinds — the read side and the write side resolved separately before, so they could disagree about the code.
  • Refusal of an execution ARN, a lowercase statemachine: segment, a capitalized Activity: segment, a name carrying :stateMachine: as a substring, a name carrying /, an empty name, another service's ARN, and a truncated ARN. A state machine is created first so none of these can be passing merely because nothing exists.
  • The foreign-account and cross-Region cases for both kinds, asserting on the caller's own tags and not only on the response code.
  • The wire shape: a tag set at create time in AWS's array shape reads back in AWS's array shape, and an object tags member is refused rather than half-accepted — with a follow-up assertion that the refused call wrote nothing.
  • Key order asserted twice, since one call cannot distinguish a sorted order from a map order that came out sorted.
  • GetResources reporting both types.

Three existing tests asserted the map shape and now assert the array (stepfunctions_plugin_test.go, tagging_arn_guards_test.go, cfn_stamp_beyond_ec2_test.go). They were passing on a shape no SDK produces, so this is a finding recorded rather than a test relaxed — each carries a comment saying so.

Recorded, not fixed

Eleven non-tagging operations are still on the old path — filed as #912, not folded in here. DescribeStateMachine, UpdateStateMachine, DeleteStateMachine, StartExecution, StartSyncExecution, StopExecution, DescribeExecution, GetExecutionHistory, ListExecutions, DescribeActivity and DeleteActivity all use the same extractSMNameFromARN (:278) + ctx.AccountID/ctx.Region pair, so a cross-account or cross-Region ARN reaches the caller's own resource there too. Four of them mutate it: DeleteStateMachine and DeleteActivity delete the caller's own same-named resource and answer 200, which is this PR's UntagResource finding one severity worse. Executions also need a third resolver arm with per-type segment arity, since sfnResolveARN's ContainsAny(name, "/:") guard deliberately refuses the two-segment execution form. That is a rewrite of the plugin's execution and lifecycle paths, which does not belong in a tagging diff — hence its own issue rather than a note implied by this one.

Compatibility

ListTagsForResource and TagResource change wire shape. tags is now an array of {key, value} objects on the response and required as one on the request — a caller that sent or decoded an object must change, and a caller using an AWS SDK now works where it previously could not. ResourceNotFound answers 400 where it previously answered 404. An ARN naming another account's or another Region's resource now answers ResourceNotFound rather than silently tagging the caller's own. An activity: ARN now succeeds at the tagging API where it previously resolved to a same-named state machine or wrote a phantom record, and GetResources reports activities it previously omitted. tags is ordered by key, so an assertion that happened to pass on Go's map order may fail.

Verification

gofmt -l . && go build ./... && go vet ./...        ✓
golangci-lint run ./...                             ✓ (0 issues)
make test                                           ✓
GOTOOLCHAIN=go1.26.6 make test                      ✓
GOTOOLCHAIN=go1.26.6 make coverage                  ✓ 84.1% / 83.6%
make docs-reference docs-reference-check docs-versions authz-reference-check   ✓
npm --prefix docs run build                         ✓
go vet -C test/e2e ./... && go test -C test/e2e ./...   ✓

docs/services.md brings the Step Functions operations table up to date (it listed 7 of 18), and adds three sections: the taggable ARN table with the account-from-ARN rule and the execution-ARN decision, the two-code error table with the 400 status called out, and the array tag shape. The tagging section gains a Step Functions paragraph beside the RDS one and its scanner count goes from eighteen to nineteen. Prose cites #910, never a version.

…publishes (#910)

TagResource, UntagResource and ListTagsForResource took the resource name from the
ARN's last colon-separated segment and the account and Region from the calling
request, so an ARN naming another account's state machine reached the caller's
same-named one. UntagResource is the damaging direction: stripping a tag can turn
an aws:ResourceTag Deny into an allow, and it answered 200 while doing it. A
cross-Region ARN did the same. That is the rule #826 established for SQS and
DynamoDB and #845 carried across the tagging resolver; these three were never
audited against it.

All four readers — the three operations and the tagging API's states arm — now key
through one sfnResolveARN that takes no *RequestContext, so the account coming from
the ARN is structural rather than remembered at each call site. The type check was
strings.Contains(arn, ":stateMachine:"), a substring test satisfied by a name as
readily as by a type segment; it now compares the resource segment alone, still
case-sensitively, because AWS distinguishes stateMachine from activity by the
literal segment.

Three more in the same functions: ResourceNotFound answers 400, the status AWS
publishes for it, where substrate answered a 404 no Step Functions endpoint
returns; an execution ARN's InvalidArn is now a decision rather than the end of a
Contains chain; and tags is an array of {key, value} objects on the wire, which
CreateStateMachine and CreateActivity in the same plugin already decoded — so
TagResource could not be called by any SDK and ListTagsForResource answered a shape
none could decode.

For #835's two Step Functions rows: the states merge arm decoded StateMachineState
whatever the key named, so tagging an activity dropped its activityArn. It now
edits the raw JSON's tags member through the shared helper, behind a kind guard for
the executions and index keys no ARN addresses, and GetResources gained an
activities scanner.

Closes #910. Part of #835 — two more of its remaining rows.
@codecov

codecov Bot commented Sep 15, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.00000% with 14 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/stepfunctions_tags.go 92.66% 8 Missing ⚠️
emulator/tagging_plugin.go 79.31% 6 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.

Step Functions: all three tag operations take the account from the request, not the ARN, so a foreign-account ARN tags the caller's own state machine

1 participant