Skip to content

fix(iam): substitute a policy variable before comparing it (#745) - #772

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/745-policy-variables
Aug 23, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/745-policy-variables

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

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 — so
a 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 Resource or NotResource element … 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 StringNotEquals comparing a tag against
${aws:ResourceTag/Team} is false on AWS when the two agree and true here, which makes such a
statement inert on a Deny and granting on an Allow. That was reachable with no new producer
at all, since SimulateCustomPolicy copies a caller's ContextEntries into the context the
same 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's
sentence 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 own
switch. Action is not on AWS's list and keeps its plain glob.

Four rules from AWS's Variables and tags page decide the edges: the Version element gates
the feature ("variables like ${aws:username} are treated as literal strings in the policy"
otherwise), key names are case-insensitive so resolution goes through condResolveKey rather
than a raw map index, ${key, 'default'} honours its default, and only the single-valued
context 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 globMatch would turn an escape for a
literal asterisk into a wildcard — widening a statement, the one direction a deliberate escape
must never take. A resolved value is a policyPattern carrying a per-byte mask of which
characters are pattern and which are text, so an escape stays literal and a * inside a
resolved 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.

globMatchRec is replaced by an iterative globMatchMask that consults the mask, so there is
one matcher rather than two that could disagree. globMatch is now a wrapper passing a nil
mask, which is every Action and every Resource naming no variable — the common path
allocates nothing for the distinction.

The producer

aws:username, recorded rather than derived. Principal gained a UserName carried from
the 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 assumed
role's is a session name where AWS publishes no aws:username at all.

Presence is keyed off the recorded name and not off Principal.Type, because
resolvePrincipal types both STS forms AssumedRole while AWS publishes the key for
GetSessionToken and not for AssumeRole — so STSSessionCredentials gained the field too. A
stack's own resource calls carry the creator's name, recorded on CFNStackState so a rollback's
deletes authorize as the create did. SimulateCustomPolicy/SimulatePrincipalPolicy is the one
place a name is read out of an ARN, because there CallerArn is the caller's own assertion of
who to simulate as.

IAM's second authorization door — IAMPlugin.authorize, which passes "*" and builds its own
small context — publishes aws:username as well, for the reason it was taught aws:PrincipalArn
in #714: an iam: statement scoped to ${aws:username}, the shape of "let a user manage their
own credentials", resolved at one door and matched nothing at the other.

What this does not fix

Substitution alone does not make IAMUserChangePassword grant anything. Its Resource now
correctly reads arn:aws:iam::*:user/alice, but every IAM request's resource is built as
arn:aws:iam::<account>:*, so there is nothing user-shaped to match. Filed as #770 and stated
in docs/services.md rather than left for a reader to discover. The two caller keys still
without a producer, aws:userid and aws: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 build plus the href="#…" vs
id="…" diff over dist/services.html (empty), and go vet -C test/e2e ./... && go test -C test/e2e ./....

Two fail-before probes were run and reverted: forcing policyVarsSupported false failed 11
subtests, and removing the aws:username write plus both UserName assignments failed 3.

Live proof against a built binary, the same script against main and against this branch:

Case main This branch
alice reaching home/alice/* implicitDeny allowed
alice reaching home/bob/* implicitDeny implicitDeny
an assumed role, which has no aws:username implicitDeny implicitDeny
the same policy at Version: 2008-10-17 implicitDeny implicitDeny
home/${*} against home/alice/file.txt implicitDeny implicitDeny
${aws:PrincipalTag/team, 'company-wide'} implicitDeny allowed
${aws:username} under NumericEquals implicitDeny implicitDeny

The four rows that do not move are the point: the Version gate, the escape, the absent
producer and the unsubstituted operator family each keep the answer substrate already gave.

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

codecov Bot commented Aug 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.13514% with 9 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/iam_policy_variables.go 95.32% 3 Missing and 2 partials ⚠️
emulator/cfn_deployer.go 25.00% 3 Missing ⚠️
emulator/authz.go 85.71% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 3e942ff into main Aug 23, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the fix/745-policy-variables branch August 23, 2026 08:05
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: policy variables are compared literally, so a negated ${aws:username} is more permissive than on AWS

1 participant