Skip to content

fix(sns): a topic ARN resolves to the caller's own account, and a subscription ARN resolves to its own UUID #925

Closed

Description

@scttfrdmn

What is wrong

snsNameFromARN (emulator/sns_plugin.go:1130) is four lines:

func snsNameFromARN(arn string) string {
	parts := strings.Split(arn, ":")
	if len(parts) >= 6 {
		return parts[len(parts)-1]
	}
	return arn
}

Ten operations reach it — DeleteTopic, GetTopicAttributes, SetTopicAttributes, Subscribe, Unsubscribe, ListSubscriptionsByTopic, Publish, PublishBatch, and all three of TagResource/UntagResource/ListTagsForResource — and every one of them then keys loadTopic/saveTopic by ctx.AccountID/ctx.Region. Three defects compound.

1. The account and Region come from the caller, not from the ARN. So

arn:aws:sns:eu-west-1:999988887777:orders

presented by a us-east-1 caller in account 111122223333 addresses that caller's own topic named orders, in their own Region. TagResource writes tags onto the wrong topic and answers 200; UntagResource — the damaging direction — strips them and answers 200; Publish publishes to the wrong topic; DeleteTopic deletes it. A caller cannot tell any of these from a correct call.

This is the rule #826 established for SQS and DynamoDB, #845 carried through the tagging API's resolver, #910 applied to Step Functions, #918 to CloudFront and #922 to KMS: the account and Region come from the ARN, never from the request context, and a resolver that cannot reach for the context does not have to remember not to.

2. The last colon segment is not the topic name. A subscription ARN is arn:aws:sns:{region}:{account}:{topic-name}:{subscription-id}, so the last segment of

arn:aws:sns:us-east-1:111122223333:orders:8a21d249-4329-4871-acc6-7be709c6ea7f

is the subscription UUID, returned as a topic name. That is #910's strings.Contains(arn, ":stateMachine:") mistake in the other direction: instead of matching a type keyword too loosely, this matches no keyword at all and takes whatever is last. SNS's topic ARN carries no type keyword and no separator — the resource portion is the name — so the anchored rule here is that a resource portion containing a : is not a topic ARN, and ResourceArn on the three tag operations is documented as an ARN, not as a name.

3. A malformed ARN becomes a topic name. The len(parts) >= 6 guard falls through to return arn, so arn:aws:sns — or any string at all — is used verbatim as a topic name and looked up. Combined with defect 1 that is a lookup at a key nothing is stored at, which answers NotFound; but nothing refuses the input, and NotFound is not a code SNS publishes.

Two smaller defects in the same three handlers

4. Tags.Tag.N.Key and TagKeys.TagKey.N are not decoded. tagResource reads Tags.member.%d.Key/.Value only, and untagResource reads TagKeys.member.%d only. AWS documents the members as Tags.member.N and TagKeys.member.N, but AWS's own request examples wire Tags.Tag.1.Key, Tags.Tag.1.Value and TagKeys.TagKey.1, and a caller that sends those gets 200 with nothing written or nothing removed — the "tag parameter decoded by nobody" class of v0.114.0. Both spellings must be accepted.

5. CreateTopic's Tags parameter is not decoded at all. createTopic (:210) reads Name and DisplayName and ignores Tags, so a topic created with tags in one call reports none. SNSTopic.Tags already exists and TagResource writes it.

The wrong error code

All three handlers answer Code: "NotFound" at http.StatusNotFound. SNS publishes ResourceNotFound at 404 — the status is right and the code name is not, and a caller matching on the code sees something no SDK models. A malformed ResourceArn should be InvalidParameter/400, which all three pages publish and none of them answers today. Unlike #921 and #923 this is not a status change, so it does not need its own issue.

Why it should be fixed with the tagging row of #835

The two are the same change seen from two sides:

Acceptance criteria

  • An SNS topic ARN is parsed as an ARN, by a function taking no *RequestContext, and the account and Region used for the load and the store come from it.
  • A resource portion containing a : is refused rather than resolved: a subscription ARN does not address the topic it names, nor the subscription's UUID as a topic.
  • A malformed ARN answers InvalidParameter/400 rather than being used verbatim as a topic name.
  • ResourceNotFound replaces NotFound in all three tag handlers, at the published 404.
  • Tags.Tag.N.Key/.Value and TagKeys.TagKey.N are decoded alongside the member.N spellings, because AWS's own examples use them.
  • CreateTopic decodes Tags and the tags are readable through ListTagsForResource.
  • ListTagsForResource and the tagging API's GetResources report a topic's tags in the same order, so the two arms cannot disagree — the tagging: the four merge helpers store a resource's tags in Go map order, so one state hashes two ways #862 rule, reached here by the two arms sorting alike rather than by one of them being nondeterministic (see the correction below).
  • The five SNS key builders are free functions with no receiver state, so the resolver and the plugin cannot produce two layouts.
  • The tagging API's kind guard tests a colon-terminated prefix, because topic is a prefix of topic_names — the fourth namespace to need one after cert/cert_arns, cfdist/cfdist_ids and key/key_ids.
  • Wire tests for each of the above through StartTestServer, with every tag read back through SNS's own ListTagsForResource per cfn: the aws:cloudformation:* stamp covers EC2 only, so the rest of a stack is unstamped #765 and in the other direction through GetResources.
  • docs/services.md and CHANGELOG.md record the change, including what it deliberately leaves alone.

A correction to the record

The comment I left on #835 when the KMS row shipped says all three remaining services "build their merged tag slice from a Go map range with no sort". That is wrong for SNS: tagResource merges over indexed request parameters into a slice and untagResource filters that slice, both of which are already deterministic. The ordering criterion above is therefore not a nondeterminism fix here — it is that SNS's own arm preserves insertion order while the tagging API's arm emits key-sorted order, so one topic's tags come back in two different orders depending on which API is asked. The claim stands for Secrets Manager and SSM and will be re-verified when each is taken.

Deliberately out of scope

  • The <Value>-before-<Key> element order. AWS's published ListTagsForResource sample response emits <Value> before <Key>; substrate emits <Key> first. XML member order is not significant to any SDK's parser, and no test can assert a difference a caller can act on, so this is recorded rather than changed.
  • The 10 TPS limit on SNS tagging actions. Substrate models no rate limiting for these operations; that is a seedable-throttle question, not an ARN one.

Provenance

The topic ARN format, the subscription ARN format, ResourceArn's 1011-character maximum, the Tags.Tag.N/TagKeys.TagKey.N request examples, the Key/Value field names and lengths, the absence of pagination on ListTagsForResource, and the error set — AuthorizationError 403, ConcurrentAccess 400, InvalidParameter 400, ResourceNotFound 404, StaleTag 400, TagLimitExceeded 400, TagPolicy 400 — are quoted from API_TagResource, API_UntagResource, API_ListTagsForResource, API_CreateTopic and API_Subscribe in the SNS API reference. That no cross-account statement appears on the three tagging pages is a negative finding, recorded as such rather than as permission.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions