Repository navigation
fix(iam): a condition key's name is matched case-insensitively (#704) - #728
Merged
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The gap
AWS, in IAM JSON policy elements: Condition:
Substrate matched the name byte-for-byte, so a policy spelling a key
ec2:createaction,AWS:RequestTag/Envoraws:tagkeys— every one of which real IAM evaluates — was silently unmatched. It failed in both directions:Allowwritten that way was an implicit deny: a false refusal, which a policy author sees immediately;Denywritten 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:CreateTagspass (#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, soNullis fixed with them andevaluateConditionKeyis 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:
requestTagKeysdoes a case-sensitiveCutPrefixonaws:RequestTag/, so lowercasing the stored name would emptyaws:TagKeysand flip everyForAllValues:StringEquals+Null: falsepolicy — AWS's own recommended pattern, pinned iniam_set_qualifiers_test.go.Simulator.
ContextEntriesis 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 aContextEntrynow overrides a derivedaws:PrincipalArn/aws:ResourceAccountwhatever its case.MissingContextValuesstill 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
StringEqualsIgnoreCaseis a separate operatoraws:,ec2:,sts:)aws:TagKeysContextEntriesstringequalsandforallvalues:StringEqualsstill denyTwo 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:
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:
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
StringEqualsIgnoreCaseexists.Verification
gofmt/go build/go vetclean;golangci-lint run ./...0 issues;make testgreen with the race detector;make coverage83.2% emulator / 82.6% total;make docs-reference docs-reference-check docs-versionsclean; 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:
condContextValue: single map exact onlycondContextValue: multi map exact onlycondRequestValues: multi map exact onlycondRequestValues: single map exact onlyunsetConditionKeys: single map exact onlyunsetConditionKeys: report the folded name, not the policy'ssimulationContextEntries: let both spellings survivesimulationConditionContext: leave the derived key beside the caller'sM7, 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
Allownow launches (i-b1ea05b6…) where it was refused; anEC2:CREATEACTIONDenyis enforced rather than inert;StringEqualson the value still refusesruninstancesforRunInstances;AWS:REQUESTTAG/envmatches a request taggingEnv;aws:TagKeyslistingenvstill refuses a request taggingEnv; a folded name isallowedwith an emptyMissingContextValues, while a genuinely absentAWS:MultiFactorAuthPresentis echoed back in the policy's own casing; case-variantContextEntriesresolved to the later entry on six consecutive runs; a lowercasestringequalsstill denies.Closes #704