Skip to content

fix(cloudfront): add UntagResource, and stop routing an untag to the tag write (#883) - #900

Merged
scttfrdmn merged 2 commits into
mainfrom
fix/cloudfront-untag-resource
Sep 15, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
fix/cloudfront-untag-resource

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

CloudFront implemented TagResource and ListTagsForResource and not UntagResource, and parseCloudFrontOperation mapped any Resource-bearing POST to TagResource. So POST /2020-05-31/tagging?Operation=Untag ran the tag path: the <TagKeys> body decoded as <Tags> into an empty item list, the decode error was discarded, the distribution was marshalled back byte-identical, and the response was the tagging success. The failure was not "operation not supported", which a caller can branch on, but a successful no-op — the removal reported as done, ListTagsForResource still reporting the tag, and nothing anywhere saying otherwise. Same class as the applyTagsToResource silent-200 that v0.115.0 removed for ECS.

Closes #883.

What changed

  • The dispatch resolves a tagging POST on the Operation value and on nothing else. Operation=Tag → TagResource, Operation=Untag → UntagResource (case-insensitively, as Tag already was), GET carrying Resource → ListTagsForResource. A value that is neither resolves to no operation at all, so the request keeps its verb, reaches HandleRequest's default arm, and is refused with InvalidAction/400 — a write is never the fallback for a word the resolver cannot name. The duplicate /tagging arm further down the path switch is gone; one block now owns all three.
  • An UntagResource handler removes the keys named in <TagKeys><Items><Key> and answers the documented 204 with an empty body.
  • Neither tagging arm discards its decode error any more. Because the root element name is part of the request shape, a <Tags> document sent to the untag and a <TagKeys> document sent to the tag are now both refused with InvalidArgument/400 instead of read as an empty tag set. That closes the silent-no-op behaviour independently of the routing, which matters because the routing fix alone would leave the swallow in place for anyone who sent the wrong body deliberately. An untag with no body is refused for the same reason.
  • The three arms share one target resolver (resolveTagTarget) and the two writers share one state write (putDistribution), so a removal cannot drift from the tag it undoes on which resource an ARN names or which key it writes to.

What AWS publishes

Verified against the CloudFront API Reference; all three pages loaded and are quoted, not recalled.

  • API_UntagResource — request syntax POST /2020-05-31/tagging?Operation=Untag, body <TagKeys xmlns="http://cloudfront.amazonaws.com/doc/2020-05-31/"><Items><Key>{{string}}</Key></Items></TagKeys>; TagKeys "Required: Yes", Items "Required: No"; response HTTP/1.1 204 — "an HTTP 204 response with an empty HTTP body"; errors AccessDenied 403, InvalidArgument 400, InvalidTagging 400, NoSuchResource 404.
  • API_TagResource — the same path with Operation=Tag, a <Tags> body, 204, the same four errors.
  • API_ListTagsForResource — GET /2020-05-31/tagging?Resource={{Resource}}, and the only one of the three that documents the query parameter: "An ARN of a CloudFront resource. Pattern: arn:aws(-cn)?:cloudfront::[0-9]+:.* Required: Yes".

What could not be verified, and how it is labelled

Two things the reference does not settle. Both are labelled in the code as substrate's reading, with the reasoning, rather than presented as the API model.

  1. Untagging a key that is not present. The issue asked for this to be verified and not assumed — it cannot be verified from the reference. The page's Errors list names nothing for an absent key and the response is documented as an unconditional 204 with an empty body, but the case is not addressed either way. Substrate answers 204, because the alternative makes a consumer's second teardown pass fail, and because a response carrying no per-key result has nowhere to report a partial removal. Recorded on untagResource in bold as substrate's reading and pinned by TestCloudFrontUntagResource_AnAbsentKeySucceeds.
  2. Whether Resource is a required query parameter on Tag/Untag. Both pages say "The request does not use any URI parameters", which cannot be right for operations whose whole request syntax is a bare /tagging path with no other way to name a target, and ListTagsForResource documents it as required with a pattern. Substrate reads that as a documentation omission and requires it on all three; recorded on resolveTagTarget.

Also recorded rather than silently chosen: InvalidArgument for an unparsable body, since AWS does not publish which of InvalidArgument and InvalidTagging it answers for one.

Resource Groups Tagging API

Not affected, and deliberately not changed here. The issue's premise was that tagging_plugin.go resolves CloudFront ARNs today; re-checked against the tree, it does not — TaggingPlugin.resolveARN has no cloudfront arm at all (the file contains no occurrence of the string), so a CloudFront ARN falls to the default and answers a FailedResourcesMap entry of InternalServiceException/500, "the resource type in the request is not supported by the Resource Groups Tagging API". So UntagResources refuses loudly and has never had #883's silent-success shape. This is consistent with what cfn_resource_tags.go already records: CloudFront is one of the services that "keep tag state, but none has a mergeResourceTags arm that reaches it", filed as #835.

Rather than leave that on trust, a cloudfront distribution case was added to TestTaggingResolveARN_AWrongTypeARNIsRefusedRatherThanMisKeyed, which drives both TagResources and UntagResources. It passes on the parent commit as well as on this branch, which is the evidence that the hole is a different one. Giving CloudFront an arm there needs a mergeResourceTags case as well as a resolver case, and changes which resources GetResources enumerates — #835's work, not a misrouted request's. The reasoning is in a code comment at the head of the plugin's tagging section, not only here.

Tests

New emulator/cloudfront_untag_test.go, every case over the wire through StartTestServer against a real server, every tag assertion read back through CloudFront's own ListTagsForResource rather than out of the state store (#765's rule — a state read cannot tell a removal that happened from one that was reported as happening), and asserted on the raw XML, because an absent member and an empty one are the same value once decoded.

  • TestCloudFrontUntagResource_IsNotAnsweredAsTheTaggingSuccess — the issue's gate: 204 and the tag gone.
  • TestCloudFrontUntagResource_RemovesOnlyTheNamedKeys — a handler that cleared the map would pass the gate.
  • TestCloudFrontUntagResource_AnAbsentKeySucceeds, ..._AMissingARNIsRefused.
  • TestCloudFrontTagging_ARequestThatNamesNoOperationNeverWrites — five cases (no Operation, an unrecognized Operation, a <Tags> body sent to Untag, a <TagKeys> body sent to Tag, an untag with no body), each asserting the refusal and that the existing tag survived, since the refusal alone would pass on a handler that errored after writing.
  • TestCloudFrontTagging_OperationNamesComeFromTheQueryString — the name the pipeline resolves, which is what authorization, metering and the event log record; an untag named TagResource would also be authorized as cloudfront:TagResource.

Confirmed against the parent commit by restoring cloudfront_plugin.go from HEAD and re-running: 4 of the 6 new tests fail (12 subtests), including the gate. The two that pass on the old code are the absent-key and missing-ARN cases, which the old no-op happened to satisfy.

Request bodies carry the xmlns="http://cloudfront.amazonaws.com/doc/2020-05-31/" the reference publishes, so the decoders are exercised against what an SDK actually sends rather than a namespace-free simplification.

Verification

$ gofmt -l . && go build ./... && go vet ./...
gofmt/build/vet: OK          (no output from any of the three)

$ golangci-lint run ./...
0 issues.

$ make test                  (race detector on; exit 0)
ok  github.com/scttfrdmn/substrate/cmd/gen-authz-reference    1.386s
ok  github.com/scttfrdmn/substrate/cmd/gen-service-reference  1.535s
ok  github.com/scttfrdmn/substrate/cmd/substrate              1.976s
ok  github.com/scttfrdmn/substrate/emulator                  13.265s

$ make docs-reference docs-reference-check docs-versions authz-reference-check
docs/services.md already up to date (67 plugins)
                             (all four targets clean)

No test was relaxed, and nothing was silently passing on the old behaviour: the pre-existing TestCloudFrontPlugin_TagAndListTags only asserted that the list response was non-empty XML, which is why it never caught this.

Compatibility

Three behaviour changes a consumer could notice, all of them refusals that previously answered 204 having written nothing:

  • A tagging POST with no Operation, or with an Operation that is neither Tag nor Untag, is now InvalidAction/400 instead of being performed as TagResource. A caller relying on the bare-Resource POST as a tag write must send Operation=Tag, which is what the reference publishes and what every SDK sends.
  • A tagging body that is not the operation's documented root element is now InvalidArgument/400 instead of being read as an empty tag set.
  • An untag with no request body is now InvalidArgument/400.

An ARN naming no distribution still answers NoSuchDistribution/404 rather than the NoSuchResource all three tagging pages list, because that is what the rest of the plugin and loadDistribution answer and one plugin should not report a missing distribution two ways. Aligning all three arms on the published code is a separate change from a misrouted request; noted in resolveTagTarget and worth its own issue.

docs/services.md's hand-written CloudFront section now lists UntagResource and ListTagsForResource (the latter was implemented but undocumented), states how the three are told apart, and records the RGTA gap. CHANGELOG.md carries the full reasoning under ## [Unreleased] → ### Fixed.

@codecov

codecov Bot commented Sep 15, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.44262% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/cloudfront_plugin.go 93.44% 4 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn

Copy link
Copy Markdown
Owner Author

Follow-up commit adds TestCloudFrontTagging_ABadTargetIsRefusedTheSameWayByAllThreeOperations — a missing Resource, an ARN naming no distribution, and a well-formed ARN naming a distribution that does not exist, each asserted against TagResource, UntagResource and ListTagsForResource. That is worth having on its own terms, since the three now share one target resolver and a shared resolver is only meaningful if all three are actually driven through it; it also raised patch coverage by covering the resolver's two refusal branches, which the first pass reached from the untag arm alone.

What remains uncovered on the patch is five lines, all of them the marshal / state.Put / state.Get failure returns in putDistribution and loadDistribution. Reaching them needs a StateManager that fails on demand, and there is no such helper anywhere in the repo today — every other arm of this plugin (createDistribution, updateDistribution, deleteDistribution, listDistributions) leaves the equivalent branches uncovered for the same reason. Adding fault injection to StateManager is a broader change than #883 and would want its own issue rather than being smuggled in here.

…tag write (#883)

CloudFront implemented TagResource and ListTagsForResource and not
UntagResource, and parseCloudFrontOperation mapped any Resource-bearing POST
to TagResource — so POST /2020-05-31/tagging?Operation=Untag ran the tag
path, where the <TagKeys> body decoded as <Tags> into an empty item list,
the decode error was discarded, and the distribution was written back
byte-identical with a 204. The removal was reported as done and the tag was
still there.

A tagging POST now resolves on the Operation value alone, per the request
syntax the CloudFront API Reference publishes for the three operations
sharing the /tagging path. A value that is neither Tag nor Untag resolves to
no operation, so the request keeps its verb and is refused at the plugin's
default arm rather than performing a write. Neither tagging arm discards its
decode error any longer, so a body of the wrong shape is refused instead of
read as an empty tag set, and an untag with no body is refused because
TagKeys is documented Required: Yes.

Removing an absent key succeeds and the Resource parameter is required on
all three operations; AWS publishes neither, and both are labelled as
substrate's reading in the code. The Resource Groups Tagging API is
unaffected: resolveARN has no cloudfront arm, so UntagResources already
refuses with InternalServiceException rather than reporting a removal it did
not make, which its guard table now pins.

Closes #883.
…like

The three now share one target resolver, and a shared resolver is only worth
having if all three are asserted against it. Also covers the resolver's two
refusal branches, which the first pass reached from the untag arm only.
@scttfrdmn
scttfrdmn force-pushed the fix/cloudfront-untag-resource branch from 9829b0f to f2ab207 Compare September 15, 2026 22:33
@scttfrdmn
scttfrdmn merged commit 415abc0 into main Sep 15, 2026
17 checks passed
@scttfrdmn
scttfrdmn deleted the fix/cloudfront-untag-resource branch September 15, 2026 22:43
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.

CloudFront has no UntagResource, and an untag request is misrouted to TagResource and answers success

1 participant