Skip to content

fix(sts,iam): AssumeRole evaluates the role's trust policy (#593) - #596

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/593-assume-role-trust-policy
Aug 9, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/593-assume-role-trust-policy

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

sts:AssumeRole never read AssumeRolePolicyDocument. assumeRole loaded the role only to get RoleID for the AssumedRoleUser ARN, so every caller who could reach the operation got credentials for every role, and sts:ExternalId did nothing at all — the parameter was not even parsed. A consumer writing a cross-account integration pins access with a trust policy naming the partner account plus a StringEquals on sts:ExternalId; against substrate that pin was inert, so the test proving "a caller without the secret cannot assume this role" passed while verifying nothing, and passed identically with the policy deleted.

Closes #593.

The evaluator this needed did not exist

The issue suggested reusing the evaluator the IAM paths already use. Evaluate never read req.Principal — statementMatches consulted action ∧ resource ∧ condition only, and EvaluationRequest.Principal was populated by all four call sites and read by none. That is precisely why a trust policy could name one account and still admit every caller. No principal matcher existed anywhere, so building one is the bulk of this change rather than a wiring exercise.

principalMatches in emulator/iam_eval.go covers the forms IAM documents: "*", an exact ARN, a bare 12-digit account ID and the equivalent arn:aws:iam::<acct>:root (IAM treats them as identical, and the account-ID form is what the ExternalId guide uses), Service and Federated matched literally, and NotPrincipal inverted — authoritative when both appear, since IAM forbids the combination and the exclusion is the reading that cannot widen access.

One line is load-bearing: an absent Principal element matches. All four pre-existing Evaluate call sites are identity/permission-boundary paths whose documents carry no Principal element — IAM rejects it there — so reading absence as "matches nobody" would break every user, role and boundary evaluation. A mutant flipping that guard turns TestAuthController_AssumedRole and TestIAMAuthorize_AgreesWithCheckAccess red, which is the proof it's tested rather than incidental. resourceMatches already returned true for an empty resource, so a trust policy (which has no Resource) evaluates through Evaluate unchanged once the matcher is in place.

The gate

In assumeRole, the trust policy is evaluated before any credential is minted — checking after would leave a usable session in state behind a 403, and TestSTSAssumeRole_NoCredentialLeaksOnDenial asserts the session store is empty after a denial. ExternalId is parsed and bounds-checked at 2–1224 characters, returning ValidationError like DurationSeconds already does. On a non-Allow decision the response is AccessDenied / 403 with AWS's own two messages, selected on the decision:

  • DecisionImplicitDeny → …is not authorized to perform: sts:AssumeRole because no role trust policy allows the sts:AssumeRole action
  • DecisionDeny → …is not authorized to perform: sts:AssumeRole with an explicit deny in the role trust policy

Two paths stay unenforced by design. A role with no trust policy — a zero-value document means "not configured", so AssumeRole behaves exactly as before; writing a policy is the opt-in. That is the documented docs/testing-guide.md rule, existence in state is the opt-in, one level down: there enforcement starts when the principal exists, here when the role carries a trust policy. Strict fidelity would treat an empty document as an implicit deny, but AWS's CreateRole requires a trust policy, a state substrate permits, so denying it would refuse roles that cannot exist on AWS. A nil ctx.Principal — for the same reason CheckAccess returns nil on one: an unauthenticated caller resolves to no ARN, so there is nothing for a Principal element to be true of, and denying would break every in-process httptest caller.

Provenance

The code is AccessDenied, not the AccessDeniedException #593's Acceptance section names. This is observed behaviour, not modelled: the botocore STS model documents no access-denied error for AssumeRole at all (only MalformedPolicyDocument, PackedPolicyTooLarge, RegionDisabled, ExpiredToken). The code and both message strings come from AWS's own CLI output and the access-denied troubleshooting reference.

The pre-existing generic AccessDeniedException from CheckAccess is left alone — it is service-wide, and narrowing it for STS is a consumer-visible change for every service on that path. Filed as #595. #594 covers UpdateAssumeRolePolicy, which turned out not to exist, so CreateRole remains the only way to set a trust policy.

Compatibility

A test whose role carries a trust policy that does not admit the caller now fails where it previously passed. That is the fix, but it is a behaviour change: sts:ExternalId and Principal restrictions were inert and are now enforced. Two escape hatches, both intentional: a role with no trust policy stays unenforced, and an unauthenticated in-process caller is never gated. Nothing else moves — no existing test both set a trust policy and assumed that role, and CFN never runs AssumeRole (cfnAttribution.resolve uses a service role ARN directly), so no stack deploy passes the new gate.

Verification

All five pre-existing sts_plugin_test.go AssumeRole tests pass unchanged — the opt-in decision as an executable claim. New: TestEvaluate_PrincipalMatching (18 cases) plus two NotPrincipal/Deny tests, emulator/sts_trust_policy_test.go (~19 subtests across the enforced paths, both opt-outs, the no-leak assertion and the ExternalId bounds), and a real-SDK e2e journey asserting AccessDenied without the ExternalId and ASIA… credentials with it on the same role.

gofmt, go build, go vet, golangci-lint run ./... (0 issues), make test (race), make coverage (emulator 81.5%, total 80.7%), make docs-reference-check docs-versions, the docs site build and go test -C test/e2e are all green.

Replayed live against the real CLI, with a signed IAM user already granted sts:AssumeRole so the #411 identity gate passes first — that success was the defect, since there was no trust gate behind it. All four refusal paths returned ASIA… credentials before and are denied now; the negative control (a policy admitting the caller, correct ExternalId) and the no-trust-policy opt-out still return credentials. Both message forms confirmed on the wire.

Eight mutants, all CAUGHT — including principalMatches returning true unconditionally, which restores this bug exactly; the trust check moved after the mint; the sts:ExternalId condition context dropped; the two messages swapped; the empty-policy opt-out removed; and the account comparison widened.

AssumeRole loaded the role only to read RoleID for the AssumedRoleUser
ARN and never touched AssumeRolePolicyDocument, so every caller who
could reach the operation got credentials for every role and
sts:ExternalId did nothing. A consumer's confused-deputy test passed
identically with the trust policy deleted.

The evaluator #593 assumed exists did not: Evaluate never read
req.Principal, and no principal matcher existed anywhere. Add one in
iam_eval.go, covering "*", exact ARN, bare-account-ID and
account-root delegation, Service, Federated and NotPrincipal. An
absent Principal element must match — all four pre-existing Evaluate
call sites are identity/boundary paths whose documents carry none.

Evaluate the trust policy before any credential is minted, so a denial
cannot leave a usable session in state, and validate ExternalId's
2-1224 bound. A role with no trust policy stays unenforced: writing
one is the opt-in, the same shape as "existence in state is the
opt-in" one level down. AWS's two message forms map onto the two deny
decisions; the code is AccessDenied, observed from AWS's CLI output
rather than the STS API model, which documents no access-denied error
for this operation.
@codecov

codecov Bot commented Aug 9, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.50000% with 6 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/iam_eval.go 85.36% 3 Missing and 3 partials ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 08e38cc into main Aug 9, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the fix/593-assume-role-trust-policy branch August 9, 2026 17:48
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.

STS: AssumeRole does not evaluate the role's trust policy, so no ExternalId or Principal condition is enforced

1 participant