Skip to content

fix(ec2,iam): authorize a tagging call against every resource it names (#674) - #682

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/674-tag-authz
Aug 20, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/674-tag-authz

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

CreateTags and DeleteTags accept up to 1000 resource IDs of mixed types, and substrate decided all of them against a single ARN built from InstanceId.1 — a parameter neither operation carries. Every tagging call was therefore decided against the literal string "*".

That broke in both directions, and the reason is resourceMatches: it passes the statement's own Resource to globMatch as the pattern, so "*" as the request resource matches only a statement whose Resource itself begins with "*".

  • A Deny scoped to one instance, subnet, volume or security group was inert. A guardrail fencing a shared or production resource off from a pipeline that re-tags everything it can see read as a boundary and was not one.
  • A least-privilege Allow naming those ARNs could not grant a tagging call at all.

Each named resource now resolves to its own ARN paired with its own tags, so aws:ResourceTag conditions and the permission boundary are evaluated per resource, and the denial names the first resource the policy does not allow.

The ARN resource type is stated, not derived

Five of the nine taggable ID prefixes have a state-key abbreviation that differs from the ARN resource type: sg→security-group, igw→internet-gateway, rtb→route-table, eip→elastic-ip, nat→natgateway. String-munging the state key would produce arn:…:sg/sg-…, which matches nothing a caller writes. ec2TaggableResource now returns (stateKey, arnType, ok) and ec2TaggableStateKey calls it, so the two lists cannot drift.

An unrecognized ID prefix is skipped, not denied and not widened

AWS answers a tagging ID it cannot parse with InvalidID — "The specified ID for the resource you are trying to tag is not valid" — not AccessDenied, and it is the handler's answer to owe. Widening to "*" would match a broad Allow and quietly grant more than the caller wrote. So the ID is omitted from the resource list, following ec2AuthzRunInstancesResources' rule for a resource the request does not name; a request naming only such IDs returns nil and is decided exactly as before.

The aws:RequestTag fix is coupled and mandatory

addRequestTags' ec2 arm read only TagSpecification.N.Tag.M.Key/Value. A direct CreateTags/DeleteTags sends Tag.N.Key/Tag.N.Value, so aws:RequestTag/* was empty for every direct tagging call. Resolving the ARNs without this would have turned AWS's own documented CreateTags policies from a false allow into a false deny. The TagSpecification walk is untouched, including its #468 comment about deliberately not filtering by resource type. DeleteTags' optional tag value records as "", which conditionMatches cannot distinguish from an absent key for any operator including Null — so no new denial arises from it.

Atomicity comes free

CheckAccess runs entirely before the handler, so a refused mixed-resource call writes nothing — not even to the resources the policy permits. That is the same all-or-nothing shape createTags' own reserved-key and tag-limit checks already have, and the e2e journey asserts it.

Fail-before evidence

Unit tier, with the source reverted and the new tests in place: 38 failing cases across the 6 new tests — every scoped-Deny and least-privilege subtest resolving to "*" instead of the expected ARN, and the aws:RequestTag subtests denied with not authorized … on resource: *.

SDK tier, test/e2e/journey_ec2_tag_authz_test.go against the unfixed emulator:

journey_ec2_tag_authz_test.go:139: tagging a volume the policy denies: got <nil> (<nil>), want an API error

Honest caveat: TestEC2_TagAuthz_PermissionBoundaryCoversEveryResource passes both before and after. The boundary check is already inside the decision loop, so that criterion is satisfied by construction — the test is a regression guard, not a fail-before proof.

Mutation testing

Green baseline; anchor counts asserted over the whole file; gofmt -w && go vet so a non-compiling mutant is BROKEN rather than a result; full-package go test -race -count=1 ./emulator/ with no -run filter. All four targets killed, no survivors:

Mutant Result
the tag resolver never resolves anything KILLED by 5 tests
only the first ResourceId is evaluated KILLED by 6 tests
the ARN type falls back to the state-key abbreviation (sg) KILLED by 4 tests
the aws:RequestTag walk over Tag.N is dropped KILLED by 1 test

Live run

substrate server --address :4678 from a scratch directory, real AWS CLI, AWS_MAX_ATTEMPTS=1, a real IAM user with an access key (the built-in key resolves to no principal and is unenforced):

  1. create-tags --resources <instance> <volume> under Allow ec2:* + Deny ec2:CreateTags on the volume ARN → AccessDenied, naming the volume ARN — and both resources came back with [] tags.
  2. The same call without the denied resource → succeeds, instance carries Env=test.
  3. A least-privilege Allow naming both ARNs → succeeds, volume carries Env=prod.
  4. Dropping the volume ARN from that policy → AccessDenied, naming the volume ARN.
  5. Condition: StringEquals aws:RequestTag/Env=test → Env=test succeeds; Env=staging denied.
  6. delete-tags under a Deny on the volume ARN → AccessDenied, and Env=prod survives.
  7. create-tags --resources tgw-… under a broad Allow → 200, no AccessDenied.

Provenance

Neither action has a required resource type — the Service Authorization Reference marks zero of the 105 resource types it lists for either one. Authorizing against every named resource instead rests on AWS's general rule for an action naming several resources, which the Organizations counterpart (#660) and RunInstances (#662) already follow here, and on AWS's own scoped tagging examples, which write instance/* as the Resource of an ec2:CreateTags statement and would be pointless if only one resource of a batch were evaluated. The nine ARN formats come from the SAR's resource-types table: arn:${Partition}:ec2:${Region}:${Account}:{type}/{id} for all nine — none has the image ARN's deliberately empty account field.

Two AWS behaviours are not modelled and are named in the docs as such: aws:TagKeys is populated nowhere in the emulator, so the ForAllValues:StringEquals form three of AWS's DeleteTags examples use cannot be satisfied; and the second authorization pass AWS performs on ec2:CreateTags when a resource-creating action carries tags, keyed on ec2:CreateAction, which substrate does not model at all. DeleteTags does not carry ec2:CreateAction, so the two actions are identical in resource resolution and differ only in condition-key surface.

Compatibility

A multi-resource CreateTags/DeleteTags that a broad-allow-plus-scoped-deny policy previously carried may now be denied — that is the defect, and the denial is the correct answer. An aws:RequestTag condition on a direct tagging call now evaluates against keys that were previously absent, so a policy requiring one now grants the calls that satisfy it and denies the ones that do not; previously it could grant neither. Callers with no principal (the built-in unenforced key) are unaffected.

Verification

gofmt -l .                              clean
go build ./... && go vet ./...          ok
golangci-lint run ./...                 0 issues
make test                               ok (race)
make coverage                           emulator 82.8%, total 82.1%
make docs-reference docs-reference-check docs-versions   clean (67 plugins)
npm --prefix docs run build             build complete
go vet -C test/e2e ./... && go test -C test/e2e -count=1 ./...   ok

Closes #674

#674)

CreateTags and DeleteTags accept up to 1000 resource IDs of mixed types, and
substrate decided all of them against a single ARN built from InstanceId.1 — a
parameter neither operation carries — so every tagging call was decided against
the literal string "*".

That broke in both directions, because resourceMatches passes the statement's
own Resource to globMatch as the pattern: an ARN-scoped Deny never matched, so a
guardrail fencing off a shared resource was silently inert, and a
least-privilege Allow naming the ARNs could not grant a tagging call at all.

Each named resource now resolves to its own ARN paired with its own tags, so
aws:ResourceTag conditions and the permission boundary are evaluated per
resource. The ARN resource type is stated rather than derived from substrate's
state key: five of the nine taggable prefixes differ (sg/security-group,
igw/internet-gateway, rtb/route-table, eip/elastic-ip, nat/natgateway), so one
function now returns both and they cannot drift.

The aws:RequestTag fix is coupled and mandatory. addRequestTags read only
TagSpecification.N.Tag.M.*, so the Tag.N.Key/Tag.N.Value form a direct tagging
call sends left aws:RequestTag/* empty — resolving the ARNs without it would
have turned AWS's own documented CreateTags policies from a false allow into a
false deny.

Closes #674
@scttfrdmn
scttfrdmn merged commit 0bde828 into main Aug 20, 2026
16 checks passed
@codecov

codecov Bot commented Aug 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn deleted the fix/674-tag-authz branch August 20, 2026 23:01
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.

ec2:CreateTags is authorized against one ARN regardless of how many resources it names

1 participant