Repository navigation
fix(iam): substitute a policy variable before comparing it (#745) - #772
Merged
Merged
Conversation
AWS substitutes ${…} before it compares, so arn:aws:s3:::home/${aws:username}/*
is the documented shape for "a prefix per user". Substrate compared the fifteen
characters ${aws:username} against the ARN in hand, which matched nothing — so a
policy of exactly that shape granted nothing, including the bundled
IAMUserChangePassword. Under the positive forms that is a false deny; under the
negated ones it is a false allow, and reachable through SimulateCustomPolicy with
no new producer at all.
Substitution runs in the elements AWS names and nowhere else: Resource,
NotResource, and condition values under the string and ARN operators. The Version
element gates the feature, key names resolve case-insensitively, ${key, 'default'}
honours its default, and only the single-valued context is consulted.
${*}, ${?} and ${$} stay literal, and so does a * inside a resolved value: a
resolved pattern carries a per-byte mask of what is pattern and what is text, so
an escape cannot become a wildcard and a tag value cannot widen a statement. The
recursive glob matcher is replaced by an iterative one that reads the mask, so
there is one matcher rather than two that could disagree.
aws:username is the producer this needed, recorded rather than derived: Principal
carries the name from the credential lookup, presence is keyed off the recorded
name rather than off Principal.Type (GetSessionToken and AssumeRole are both typed
AssumedRole while AWS publishes the key for only the first), a stack carries its
creator's name so a rollback authorizes as the create did, and the simulator is the
one place a name is read out of an ARN because there CallerArn is the caller's own
assertion. IAM's second authorization door publishes it too, for the reason it was
taught aws:PrincipalArn in #714.
Codecov Report❌ Patch coverage is
📢 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.
Closes #745.
The bug
AWS substitutes a policy variable before it compares.
arn:aws:s3:::home/${aws:username}/*is the shape AWS's own documentation gives for "a prefix per user". Substrate compared the
fifteen characters
${aws:username}against the ARN in hand, which matched no ARN at all — soa policy of exactly that shape granted nothing, including the bundled
IAMUserChangePassword,the one bundled policy that uses a variable.
The issue's central claim is inverted, and this PR does not implement it as written. #745
says the divergence is an unresolved variable under negated forms. There is no such
divergence: AWS applies one sentence to both elements — "If a variable that has no value … is
used as part of the
ResourceorNotResourceelement … will not match any resource" —and substrate's literal comparison reached the same answer, because an ARN never contains a
${…}. #744 already corrected the docs on that point.The real divergence is a variable that would have resolved. Positive forms are a false deny;
negated forms are a false allow — a
StringNotEqualscomparing a tag against${aws:ResourceTag/Team}is false on AWS when the two agree and true here, which makes such astatement inert on a
Denyand granting on anAllow. That was reachable with no new producerat all, since
SimulateCustomPolicycopies a caller'sContextEntriesinto the context thesame evaluator reads — which is also why the substituter was independently testable, contrary
to the issue's ordering constraint.
Where substitution runs
Resource,NotResource, and condition values under the string and ARN operators. AWS'ssentence on the rest is exhaustive — "You can't use a policy variable with other operators,
such as Numeric, Date, Boolean, Binary, IP Address, or Null operators" — so the test is the
operator family's prefix rather than a name list that would drift from
condEvaluate's ownswitch.
Actionis not on AWS's list and keeps its plain glob.Four rules from AWS's Variables and tags page decide the edges: the
Versionelement gatesthe feature ("variables like
${aws:username}are treated as literal strings in the policy"otherwise), key names are case-insensitive so resolution goes through
condResolveKeyratherthan a raw map index,
${key, 'default'}honours its default, and only the single-valuedcontext is consulted per "You can't use a multivalued condition key as a variable".
The
${*}hazard, resolved rather than shipped${*},${?}and${$}are AWS's escapes for characters the glob reads as pattern.Substituting them into a plain string and handing it to
globMatchwould turn an escape for aliteral asterisk into a wildcard — widening a statement, the one direction a deliberate escape
must never take. A resolved value is a
policyPatterncarrying a per-byte mask of whichcharacters are pattern and which are text, so an escape stays literal and a
*inside aresolved value is literal too: a tag value is data the request carried, not pattern the policy
author wrote. AWS documents no rule for that second case; it is substrate's reading, stated in
the docs, and it is the reading that cannot widen a statement on behalf of whoever set the tag.
globMatchRecis replaced by an iterativeglobMatchMaskthat consults the mask, so there isone matcher rather than two that could disagree.
globMatchis now a wrapper passing a nilmask, which is every
Actionand everyResourcenaming no variable — the common pathallocates nothing for the distinction.
The producer
aws:username, recorded rather than derived.Principalgained aUserNamecarried fromthe credential lookup, which had the IAM user's name in hand and spent it into an ARN. Deriving
it back out of that ARN would be wrong twice: a registry hit with no IAM entity behind it
synthesizes
…:user/<access-key-id>, so the last segment is a credential ID, and an assumedrole's is a session name where AWS publishes no
aws:usernameat all.Presence is keyed off the recorded name and not off
Principal.Type, becauseresolvePrincipaltypes both STS formsAssumedRolewhile AWS publishes the key forGetSessionTokenand not forAssumeRole— soSTSSessionCredentialsgained the field too. Astack's own resource calls carry the creator's name, recorded on
CFNStackStateso a rollback'sdeletes authorize as the create did.
SimulateCustomPolicy/SimulatePrincipalPolicyis the oneplace a name is read out of an ARN, because there
CallerArnis the caller's own assertion ofwho to simulate as.
IAM's second authorization door —
IAMPlugin.authorize, which passes"*"and builds its ownsmall context — publishes
aws:usernameas well, for the reason it was taughtaws:PrincipalArnin #714: an
iam:statement scoped to${aws:username}, the shape of "let a user manage theirown credentials", resolved at one door and matched nothing at the other.
What this does not fix
Substitution alone does not make
IAMUserChangePasswordgrant anything. ItsResourcenowcorrectly reads
arn:aws:iam::*:user/alice, but every IAM request's resource is built asarn:aws:iam::<account>:*, so there is nothing user-shaped to match. Filed as #770 and statedin
docs/services.mdrather than left for a reader to discover. The two caller keys stillwithout a producer,
aws:useridandaws:PrincipalTag/, are #771.Verification
Full gate green:
gofmt,go build,go vet,golangci-lint run(0 issues),make test(race on),
make coverage— emulator 83.4%, total 82.9% —make docs-reference docs-reference-check docs-versions,npm --prefix docs run buildplus thehref="#…"vsid="…"diff overdist/services.html(empty), andgo vet -C test/e2e ./... && go test -C test/e2e ./....Two fail-before probes were run and reverted: forcing
policyVarsSupportedfalse failed 11subtests, and removing the
aws:usernamewrite plus bothUserNameassignments failed 3.Live proof against a built binary, the same script against
mainand against this branch:mainhome/alice/*implicitDenyallowedhome/bob/*implicitDenyimplicitDenyaws:usernameimplicitDenyimplicitDenyVersion: 2008-10-17implicitDenyimplicitDenyhome/${*}againsthome/alice/file.txtimplicitDenyimplicitDeny${aws:PrincipalTag/team, 'company-wide'}implicitDenyallowed${aws:username}underNumericEqualsimplicitDenyimplicitDenyThe four rows that do not move are the point: the
Versiongate, the escape, the absentproducer and the unsubstituted operator family each keep the answer substrate already gave.