Skip to content

feat(iam): evaluate ForAllValues/ForAnyValue, and populate aws:TagKeys (#690) - #703

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/690-set-qualifiers
Aug 21, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/690-set-qualifiers

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #690

ForAllValues: and ForAnyValue: were never parsed anywhere in substrate. "ForAllValues:StringEquals" reached evaluateConditionKey whole, matched none of the nine operators it knows, and fell through to its // Unknown operator — deny by default. The statement was discarded before its condition key was ever looked at.

The issue's premise is inverted, and the truth is worse in both directions. It reported that an unpopulated aws:TagKeys made such a statement allow. In fact an Allow so written was a false deny — including all three of AWS's documented ec2:DeleteTags example policies, none of which permitted anything — and a Deny so written was silently inert, so a guardrail written the way AWS recommends refused nothing at all. Populating aws:TagKeys alone would have changed nothing; the qualifier parse is the load-bearing half.

What changed

  • EvaluationRequest.MultiContext map[string][]string, beside Context. Two maps rather than one widened to map[string][]string, because IAM itself draws that line: a key is single- or multivalued by its nature, "multivalued context keys require a condition set operator", and AWS says outright not to use a set operator on a single-valued key. Keeping them apart leaves all nine existing operators' ctx[key] lookup byte-identical, so adding a multivalued key cannot change a decision that never named one.
  • The qualifier is split off the operator and applied with AWS's rules verbatim: ForAllValues is true when every request value satisfies the base operator and true when the key is absent or empty ("It also returns true if there are no context keys in the request"); ForAnyValue needs one match and is false when absent or empty ("For no matching context key or if the key does not exist, the condition returns false"). An unrecognized qualifier — ForSomeValues:, a lowercase forallvalues: — still does not match, which is the deny-by-default an unknown operator already got.
  • Null consults both maps. AWS's Important note says to always pair ForAllValues with "Null": {"<key>": "false"}, and all four of its aws:TagKeys examples do. Substrate's Null compares a single string, so a key carried only in MultiContext read as null — without this the recommended pattern would still deny every request it was written to allow, and the qualifier fix would have looked like it worked while changing nothing for a real policy.
  • aws:TagKeys is populated in CheckAccess, sorted.
  • The simulator keeps every ContextKeyValues.member.N value instead of the first, and unsetConditionKeys consults MultiContext so a key that decided the call is not also reported as missing.

Deviation from the plan: aws:TagKeys is derived, not gathered

The plan had addRequestTags gain the multi map as a parameter and each of its seven service arms populate it. It is instead derived from the aws:RequestTag/<key> entries that function already produces: the keys of aws:RequestTag/* are the request's tag keys, so one reading of the request covers all seven arms — EC2's TagSpecification.N.Tag.M.Key and a direct CreateTags/DeleteTags' Tag.N.Key, IAM's and Organizations' Tags lists, Lambda's Tags map, Config's Tags, S3's x-amz-tagging — an arm added later is covered for free, and the two cannot drift into disagreeing about what the request asked for.

The sort is load-bearing but unobservable through any API surface. Three of those arms iterate a Go map, whose order is randomized per run, and a ForAllValues decision quantifies over the slice — an unsorted value would make a denial depend on map iteration order, which an event log that must replay identically cannot tolerate. But every operator that reads the key is set-semantic, so reordering changes no decision, and no request can observe it. It is therefore asserted through RequestTagKeysForTest in export_test.go, following the S3PersistedContentEncodingForTest precedent, rather than through a test that pretends an API observes it.

Fail-before

Recorded at main (75fbf6d) in a throwaway worktree, with only the new test file copied in. Two of the new tests cannot be recorded there at all — they exercise requestTagKeys, which this PR introduces — and the unit-level ones do not compile against a EvaluationRequest with no MultiContext, so what ran is the two tests that use only pre-existing API:

--- FAIL: TestCheckAccess_TagKeysGatesATaggedRequest
    --- FAIL: .../a_launch_tagging_with_approved_keys
    --- PASS: .../a_launch_tagging_with_an_unapproved_key
    --- PASS: .../an_untagged_launch,_which_Null:false_refuses
    --- FAIL: .../a_direct_CreateTags_naming_approved_keys
    --- PASS: .../a_direct_CreateTags_naming_an_unapproved_key
    --- FAIL: .../a_DeleteTags_naming_a_key_and_no_value
--- FAIL: TestSimulate_ContextEntriesKeepEveryValue
    --- FAIL: .../every_supplied_value_satisfies_the_policy
    --- PASS: .../a_second_value_the_policy_does_not_list_refuses
    --- PASS: .../a_key_named_with_no_values_is_set_but_empty

The shape is the inverted premise itself: every row AWS's policy permits fails, and every refusal "passes" because everything was refused.

AccessDenied: User: arn:aws:iam::123456789012:user/tagkeys is not authorized to perform:
ec2:RunInstances on resource: arn:aws:ec2:us-east-1::image/ami-0abcdef1234567890

Regression pinned explicitly

AmazonRDSFullAccess and AmazonRDSReadOnlyAccess each carry a real ForAllValues:StringEquals over devops-guru:ServiceNames, paired with Null: false. Both statements were discarded before this PR. TestIAMManaged_RDSPoliciesStillWithholdDevOpsGuru asserts they still withhold the grant from a request naming no service names (the Null arm refuses it) and now make it when one names RDS — two bundled policies quietly widening would be the fix escaping its own blast radius.

Live run

substrate server --address :4678 from a scratch directory, real AWS CLI, AWS_MAX_ATTEMPTS=1, torn down with lsof -ti:4678 | xargs kill. AWS's "may only apply approved tags" policy (ForAllValues:StringEquals over aws:TagKeys + Null: false):

Call Result
run-instances tagging Department launched i-045a59b0fa812e9f — a false deny before this PR
the same launch adding Environment AccessDenied on ec2:RunInstances
an untagged launch AccessDenied — the Null: false arm, per AWS's note
create-tags Key=CostCenter tagged
create-tags Key=Environment AccessDenied on ec2:CreateTags
delete-tags Key=CostCenter (no value) untagged
a ForAnyValue Deny on Secret, then tagging Secret AccessDenied — the inert guardrail now fires
the same Deny, tagging Department tagged — the Deny does not overreach
simulate-custom-policy, ContextKeyValues=Department,CostCenter allowed, no missing context values
the same with Department,Environment implicitDeny — the second value is evaluated, not discarded

Mutation testing

Nine mutants, anchor count asserted == 1 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/ plus go test -C test/e2e ./... with no -run filter, restore verified byte-for-byte.

Mutant Verdict
the qualifier split falls through to the unqualified path KILLED
ForAllValues is false on an absent or empty key KILLED
ForAnyValue is true on an absent or empty key KILLED
an unrecognized qualifier matches instead of denying KILLED
Null ignores MultiContext KILLED
unsetConditionKeys ignores MultiContext SURVIVED, then killed
the aws:TagKeys sort is dropped KILLED
CheckAccess does not populate aws:TagKeys KILLED
the simulator keeps only the first ContextKeyValues value KILLED

The survivor is disclosed because it found a real hole in the test, not in the code. MissingContextValues is gathered only for a statement ruled out by its condition, so the assertion I had written — on an allowed request — never reached that code at all. The case a caller actually meets is a refusal: a policy that lists Department and a request naming Environment is denied, and reporting aws:TagKeys as a missing context value there would send the caller looking for the wrong bug when the value it supplied is precisely why the call failed. That assertion was added and the mutant re-run: KILLED.

Compatibility

A previously-inert Deny will start firing, and a previously-denied Allow may start permitting. Both are visible to any consumer using AWS's documented tag-scoped policies. Two bundled managed policies newly grant devops-guru:SearchInsights to a request naming ServiceNames=RDS — which is what their real documents say.

Verification

gofmt -l ., go build ./..., go vet ./..., golangci-lint run ./... (0 issues), make test (race), make coverage (82.9% on emulator), make docs-reference-check, make docs-versions, npm --prefix docs run build, go vet -C test/e2e ./... and go test -C test/e2e ./... all green.

Docs

A new ### Multivalued condition keys: ForAllValues and ForAnyValue section in the IAM chapter: the per-qualifier table including both absent-key rules, AWS's Null pairing with the JSON it recommends, the note that the same vacuity bites a Deny the other way, which keys are multivalued and where each is populated from, why the value is sorted, and the one authorization path (iam: actions inside the IAM plugin) that populates neither map. The EC2 authorization chapter's "Not covered — aws:TagKeys" bullet is replaced by a pointer to it; the second ec2:CreateTags pass stays listed as not covered, since that is #691.

#690)

Neither set qualifier was parsed anywhere in substrate, so
"ForAllValues:StringEquals" reached the operator switch whole, matched none
of the nine operators it knows, and fell through to its deny-by-default —
the statement was discarded before its condition key was ever looked at.

The issue reported this as an unpopulated aws:TagKeys making such a
statement allow. It is the opposite, and worse in both directions: an Allow
so written was a false deny, including AWS's own documented ec2:DeleteTags
examples, and a Deny so written was silently inert, so a guardrail written
the way AWS recommends refused nothing.

EvaluationRequest gains MultiContext for AWS's multivalued keys, the
qualifier is parsed off the operator with AWS's absent-key rules for each,
an unrecognized qualifier still does not match, Null consults both maps so
AWS's recommended ForAllValues + Null:false pairing works, aws:TagKeys is
derived from the aws:RequestTag entries CheckAccess already read, and the
simulator keeps every ContextKeyValues value instead of the first.
@codecov

codecov Bot commented Aug 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.43590% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/iam_eval.go 95.23% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 9be4dee into main Aug 21, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the feat/690-set-qualifiers branch August 21, 2026 07:24
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.

iam: aws:TagKeys is populated nowhere, so ForAllValues:StringEquals on it cannot be satisfied

1 participant