Skip to content

fix(sns): a topic operation checks that its topic exists, and DeleteTopic stops checking (#926, #992) - #994

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/sns-topic-existence
Sep 17, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/sns-topic-existence

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Four SNS operations never checked that the topic they addressed exists, and a fifth checked where AWS documents that it must not. The two are one audit run in both directions, so they ship together: after this change every SNS topic operation's existence behaviour matches its own reference page.

#926 — four operations that answered 200 for a topic that does not exist

Subscribe, Publish, PublishBatch and ListSubscriptionsByTopic parsed the TopicArn, derived a state key from it, and then read only the subscription index. An absent topic was therefore indistinguishable from a real topic with no subscribers, and all four answered 200: Publish and PublishBatch minted a MessageId for a message no topic had accepted, Subscribe handed back a subscription ARN, and ListSubscriptionsByTopic reported an empty list. Each of the four pages publishes NotFound at 404, glossed "Indicates that the requested resource does not exist."

The consequence went beyond the code name. A CreateTopic → Publish → DeleteTopic → Publish sequence — a producer's teardown, or a test asserting that one surfaces an error once its topic is gone — could not be written at all, because the second Publish succeeded. An empty subscription list and an absent topic are now different answers rather than the same one, which is asserted in both directions.

Subscribe was the worst of the four because it is the one that writes: a subscription record and two index entries survived the refused call, reported by ListSubscriptions ever after, and would have started receiving messages had a topic of that name later been created. That is the assertion that failed hardest before the fix.

One helper, six call sites

All six operations that resolve a TopicArn to a topic record — the four above plus GetTopicAttributes and SetTopicAttributes, which already checked — go through the new (*SNSPlugin).requireTopic. That is the #961/#969 precedent: each of those issues found one operation refusing nothing its siblings refused, which is what a per-handler existence check invites. TestSNSExistence_OneHelperAnswersForEverySite asserts the code, the status and the message text are identical across the six, so a seventh operation added later that hand-rolls its own check shows up as a difference.

The load is keyed by the ARN's own account and Region, never the caller's, so the check cannot reintroduce the resolution defect #925 fixed. snsParseTopicARN takes no *RequestContext, so a cross-account or cross-Region ARN builds a state key nothing is stored at and the topic reads as absent — the Region half is independently required by API_Publish ("you can publish messages only to topics and endpoints in the same AWS Region"), and the account half needs no guard of its own. TestSNSExistence_TheCheckIsKeyedByTheARNsAccountAndRegion creates one topic named shared in us-west-2 only and asserts, for each of the six, that its own ARN is served while a same-named us-east-1 ARN and a same-named ARN in another account are both refused.

The check runs after the parse, so a string that is not an ARN still answers InvalidParameter/400 rather than being reported as a topic that does not exist — asserted for four malformed forms across all six operations.

PublishBatch answers at the top level

#926 explicitly declined to guess this one. API_PublishBatch has two failure shapes — a top-level error, and a per-entry BatchResultErrorEntry inside a 200 — and the page does not say which applies to an absent topic. It is the top level: TopicArn is a request-level parameter, so every entry addresses the same topic and a per-entry rendering would report the identical failure on each of them while still answering 200; NotFound appears in the operation's top-level Errors list; and the batch-scoped codes the page publishes — BatchEntryIdsNotDistinct, InvalidBatchEntryId, ParameterValueInvalid — are each about an individual message. This reading is substrate's. The test sends a three-entry batch, so a per-entry implementation would be visible as three Failed members, and asserts the body carries no Successful, no Failed and no MessageId.

Unsubscribe

Answers NotFound/404 for a subscription ARN naming no subscription, where substrate answered 200 behind an unsourced "Idempotent — silently succeed" comment. API_Unsubscribe publishes the code and states no idempotence anywhere, and SubscriptionArn is the operation's only request parameter and only named resource, so the published code can only be about the subscription. A previously discarded Delete error is now checked.

#992 — DeleteTopic went the other way

API_DeleteTopic's description, verbatim: "Deletes a topic and all its subscriptions. … This action is idempotent, so deleting a topic that does not exist does not result in an error." Substrate answered NotFound/404, so a teardown helper that deletes unconditionally, or a rollback that retries a delete, was broken here and correct in production — the reverse of the usual direction.

The same page also publishes NotFound/404 in its Errors list, alongside ConcurrentAccess, InvalidState, StaleTag and TagPolicy, so it contradicts itself. The description sentence governs: it names the condition, names the outcome and calls the property by name, while the error-list entry names no condition at all. Recording the contradiction rather than silently resolving it is the house rule; the resolution is corroborated by API_Unsubscribe publishing the same code with no such sentence — AWS states idempotence where it means it. A malformed ARN still answers InvalidParameter/400: idempotence licenses a topic that does not exist, not a string that is not an ARN. Two errors the handler previously discarded on the delete path are now checked, and a test asserts the idempotent path leaves the owning Region's topic index alone rather than rewriting it to itself.

Deliberately out of scope

The subscription record and the two subscription indexes stay keyed by the calling account rather than the topic's, as docs/services.md already explains: a cross-account subscription is not modeled at all, so moving them would put the index under an account the subscriptions are not stored in and Publish would silently stop delivering.

GetTopicAttributes' attribute set is wrong in a way this PR does not touch — it reports an invented SubscriptionsCount hardcoded to "0" and none of AWS's three subscription counts — filed as #993 rather than folded in.

Provenance

  • AWS, quoted: the NotFound/404 entry and its gloss on API_Subscribe, API_Publish, API_PublishBatch, API_ListSubscriptionsByTopic, API_GetTopicAttributes and API_SetTopicAttributes; API_Publish's same-Region requirement; API_DeleteTopic's idempotence sentence and its contradicting Errors list; API_Unsubscribe's NotFound and its silence on idempotence; API_PublishBatch's top-level error list and its batch-scoped codes.
  • Substrate's reading: that PublishBatch renders the refusal top-level rather than per entry; that API_DeleteTopic's description governs over its own error list; that NotFound rather than ResourceNotFound is the code for the topic operations (only the three tagging pages publish the latter, per fix(sns): a topic ARN resolves to the caller's own account, and a subscription ARN resolves to its own UUID #925).

Compatibility

Subscribe, Publish, PublishBatch and ListSubscriptionsByTopic against a topic that does not exist — including one in another account or Region — now answer NotFound/404 where they answered 200; Unsubscribe of an absent subscription likewise. DeleteTopic against a topic that does not exist now answers 200 where it answered 404. A test that relied on any of those old answers will need updating; none in this tree did.

Verification

The nine-command gate passes: gofmt/build/vet, golangci-lint (0 issues), make test, GOTOOLCHAIN=go1.26.6 make test, GOTOOLCHAIN=go1.26.6 make coverage (84.5% emulator / 84.0% total, up from 84.4/83.9), make docs-reference docs-reference-check docs-versions authz-reference-check, npm --prefix docs run build, and the e2e vet + suite. Every assertion in the new file goes over the wire through emulator.StartTestServer with the topic created by CreateTopic (#765), and each asserts the HTTP status alongside the code (#923) — the status matters more than usual here, because the defect was a status.

Closes #926.
Closes #992.

…opic stops checking (#926, #992)

Subscribe, Publish, PublishBatch and ListSubscriptionsByTopic parsed the TopicArn, derived a
state key from it, and then read only the subscription index — so an absent topic was
indistinguishable from a real topic with no subscribers and all four answered 200. Every one of
the four pages publishes NotFound at 404. Subscribe was the worst of them because it writes: a
subscription record and two index entries survived the call.

All six operations that resolve a TopicArn to a topic record now go through one helper, on the
#961/#969 precedent that a per-handler existence check drifts. The load is keyed by the ARN's own
account and Region, so the check cannot reintroduce what #925 fixed, and it runs after the parse
so a malformed ARN is still InvalidParameter/400. PublishBatch answers at the top level rather
than per entry, argued from the page's own structure. Unsubscribe answers NotFound/404 where it
answered 200 behind an unsourced "idempotent" comment.

DeleteTopic is the inverse and is fixed in the same pass, so every SNS topic operation's existence
behaviour matches its own page: API_DeleteTopic's description states the action is idempotent, and
substrate answered NotFound/404. The same page also publishes NotFound in its error list; the
description sentence governs, and the contradiction is recorded rather than silently resolved.

Closes #926.
Closes #992.
@scttfrdmn scttfrdmn added this to the v0.118.0 milestone Sep 17, 2026
@codecov

codecov Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.19608% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/sns_plugin.go 89.13% 5 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit ce6d6f5 into main Sep 17, 2026
17 checks passed
@scttfrdmn
scttfrdmn deleted the fix/sns-topic-existence branch September 17, 2026 06:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant