Skip to content

fix(iam): a condition key's name is matched case-insensitively (#704) - #728

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/704-condition-key-case-insensitive
Aug 22, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/704-condition-key-case-insensitive

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

The gap

AWS, in IAM JSON policy elements: Condition:

Context key names are not case-sensitive. For example, including the aws:SourceIP context key is equivalent to testing for AWS:SourceIp. Case-sensitivity of context key values depends on the condition operator that you use.

Substrate matched the name byte-for-byte, so a policy spelling a key ec2:createaction, AWS:RequestTag/Env or aws:tagkeys — every one of which real IAM evaluates — was silently unmatched. It failed in both directions:

  • an Allow written that way was an implicit deny: a false refusal, which a policy author sees immediately;
  • a Deny written that way was inert, allowing what AWS refuses — which a policy author does not see at all.

Global to the evaluator rather than specific to any key. Found while implementing the second ec2:CreateTags pass (#691), which left a bare comment pointing here; this PR replaces that comment with a real subtest.

What lands

Read-time resolution in the three functions that already take both context maps — condContextValue, condRequestValues, unsetConditionKeys — through one new helper, condResolveKey. No operator arm changes, so Null is fixed with them and evaluateConditionKey is untouched. An exact hit is still a single map read, so every canonically-spelled key (which is every key any substrate producer writes) behaves byte-identically and pays nothing.

Write-time canonicalization was rejected, and the code says why: requestTagKeys does a case-sensitive CutPrefix on aws:RequestTag/, so lowercasing the stored name would empty aws:TagKeys and flip every ForAllValues:StringEquals + Null: false policy — AWS's own recommended pattern, pinned in iam_set_qualifiers_test.go.

Simulator. ContextEntries is the only way two spellings of one key can enter substrate, so two such entries are collapsed to one — the later winning, under the earlier's spelling, in wire-index order — and a ContextEntry now overrides a derived aws:PrincipalArn/aws:ResourceAccount whatever its case. MissingContextValues still reports the policy's spelling (that string reaches the wire and the caller compares it against what they submitted), and a key the evaluator resolved by folding is no longer reported there at all.

What deliberately does not fold

Folded Not folded
The whole key name, tag suffix included The condition value, per each operator's own definition — which is why StringEqualsIgnoreCase is a separate operator
The service prefix (aws:, ec2:, sts:) A tag key carried as a value, in aws:TagKeys
Names supplied through ContextEntries The operator and its set qualifier: stringequals and forallvalues:StringEquals still deny

Two corrections to the issue

1. The tag suffix folds — the opposite of what #704's scope section asked for. AWS states it in the same breath as the key–value form:

Key names are not case-sensitive. This means that if you specify "aws:ResourceTag/TagKey1": "Value1" in the condition element of your policy, then the condition matches a resource tag key named either TagKey1 or tagkey1, but not both.

The case-sensitive rule the issue remembers is real, but it governs a tag key used as a condition value (aws:TagKeys, and substrate's own tag rules), which is a different thing from a key name. Both halves are pinned so the distinction cannot be collapsed later. Acceptance criterion 2 is therefore met in the opposite direction from the one it names, deliberately.

2. The "two producers" hazard does not exist as described. #704 asks that two producers writing the same key in different cases must not both survive. Every internal producer writes a canonical literal, so that cannot happen; the real entry point is caller-supplied ContextEntries, which is what this PR collapses.

Provenance

The tie-break is substrate's. AWS's "but not both" does not say which spelling wins, and AWS names the resulting hazard rather than resolving it:

AWS services that support these attributes might allow you to create multiple key names that differ only by case. For example, you might tag an Amazon EC2 instance with ec2=test1 and EC2=test2. When you use a condition such as "aws:ResourceTag/EC2": "test1" to allow access to that resource, the key name matches both tags, but only one value matches. This can result in unexpected condition failures.

Substrate answers with the first in sorted order — a property of the request, never of Go's randomized map iteration, which a decision that must replay identically from the event log cannot depend on. An exact hit always wins over a folded one. Documented as substrate's explicit choice rather than presented as AWS's.

Also worth recording: the sentence #704 quotes ("the condition key is not case-sensitive and the condition value is case-sensitive") has been superseded on the live page. The key-name half is unchanged; the value half now reads "Case-sensitivity of context key values depends on the condition operator that you use" — which is exactly why StringEqualsIgnoreCase exists.

Verification

gofmt / go build / go vet clean; golangci-lint run ./... 0 issues; make test green with the race detector; make coverage 83.2% emulator / 82.6% total; make docs-reference docs-reference-check docs-versions clean; VitePress build succeeded with no broken anchors; go vet -C test/e2e ./... && go test -C test/e2e ./... green.

Fail-before: with the fold reverted to an exact-only lookup, all 12 new tests go red.

Mutation testing — eleven mutations, each caught:

Mutation Result
M1 drop the exact-match fast path caught
M2 take the sorted-last folded match caught
M3 compare names exactly instead of folding caught (17 tests)
M4 condContextValue: single map exact only caught (14 tests)
M5 condContextValue: multi map exact only caught
M6 condRequestValues: multi map exact only caught
M7 condRequestValues: single map exact only caught
M8 unsetConditionKeys: single map exact only caught
M9 unsetConditionKeys: report the folded name, not the policy's caught (4 tests)
M10 simulationContextEntries: let both spellings survive caught
M11 simulationConditionContext: leave the derived key beside the caller's caught

M7, M8 and M10 survived the first pass and drove three test additions. M10 is the interesting one: without the entry-level collapse, the derived-key override iterates a map holding both spellings and deletes whichever folded sibling it meets first, so the decision differs run to run. A single round trip passed about half the time; the assertion is now looped, as the sorted-tie-break test already was.

Live run against the real AWS CLI v2 and a signed user access key — eleven sections, all as designed: a lowercase-keyed Allow now launches (i-b1ea05b6…) where it was refused; an EC2:CREATEACTION Deny is enforced rather than inert; StringEquals on the value still refuses runinstances for RunInstances; AWS:REQUESTTAG/env matches a request tagging Env; aws:TagKeys listing env still refuses a request tagging Env; a folded name is allowed with an empty MissingContextValues, while a genuinely absent AWS:MultiFactorAuthPresent is echoed back in the policy's own casing; case-variant ContextEntries resolved to the later entry on six consecutive runs; a lowercase stringequals still denies.

Closes #704

AWS, in *IAM JSON policy elements: Condition*: "Context key **names** are not
case-sensitive. For example, including the aws:SourceIP context key is
equivalent to testing for AWS:SourceIp. Case-sensitivity of context key
**values** depends on the condition operator that you use."

Substrate matched the name byte-for-byte, so a policy spelling a key
ec2:createaction, AWS:RequestTag/Env or aws:tagkeys was silently unmatched. It
failed in both directions: an Allow written that way was an implicit deny — a
false refusal — and a Deny written that way was inert, allowing what AWS
refuses. The second is the one a policy author cannot see.

The fold is a read-time resolution in the three functions that already take
both context maps — condContextValue, condRequestValues and
unsetConditionKeys — so no operator arm changes and Null is fixed with them.
An exact hit is still one map read, so every canonically-spelled key costs
nothing. Write-time canonicalization was rejected: requestTagKeys does a
case-sensitive CutPrefix on aws:RequestTag/, so lowercasing the stored name
would empty aws:TagKeys and flip AWS's own recommended ForAllValues + Null
pattern.

The tag suffix folds too, which is the opposite of what the issue asked for.
AWS states it in the same breath as the key–value form: "Key names are not
case-sensitive … the condition matches a resource tag key named either
TagKey1 or tagkey1, but not both." The case-sensitive rule the issue
remembers governs a tag key used as a condition *value* — aws:TagKeys — which
is a different thing. "But not both" leaves the winner unstated and AWS names
the hazard rather than resolving it, so substrate answers with the first in
sorted order, never with whichever key Go's map iteration reached.

Not folded: the condition value (per each operator's own definition, which is
why StringEqualsIgnoreCase exists), and the operator with its set qualifier.
A simulation's ContextEntries is the only way two spellings can enter, so two
such entries are collapsed to one — the later winning under the earlier's
spelling — and a ContextEntry overrides a derived aws:PrincipalArn whatever
its case. Without that collapse the two fixes interact and the decision
becomes map-order dependent, which mutation testing caught.

Closes #704
@codecov

codecov Bot commented Aug 22, 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 merged commit 1a48cc8 into main Aug 22, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the feat/704-condition-key-case-insensitive branch August 22, 2026 04:23
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: condition-key names are matched case-sensitively, but AWS documents them as case-insensitive

1 participant