Repository navigation
fix(sts,iam): AssumeRole evaluates the role's trust policy (#593) - #596
Merged
Merged
Conversation
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 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.
sts:AssumeRolenever readAssumeRolePolicyDocument.assumeRoleloaded the role only to getRoleIDfor theAssumedRoleUserARN, so every caller who could reach the operation got credentials for every role, andsts:ExternalIddid 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 aStringEqualsonsts: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.
Evaluatenever readreq.Principal—statementMatchesconsulted action ∧ resource ∧ condition only, andEvaluationRequest.Principalwas 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.principalMatchesinemulator/iam_eval.gocovers the forms IAM documents:"*", an exact ARN, a bare 12-digit account ID and the equivalentarn:aws:iam::<acct>:root(IAM treats them as identical, and the account-ID form is what the ExternalId guide uses),ServiceandFederatedmatched literally, andNotPrincipalinverted — 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
Principalelement matches. All four pre-existingEvaluatecall sites are identity/permission-boundary paths whose documents carry noPrincipalelement — IAM rejects it there — so reading absence as "matches nobody" would break every user, role and boundary evaluation. A mutant flipping that guard turnsTestAuthController_AssumedRoleandTestIAMAuthorize_AgreesWithCheckAccessred, which is the proof it's tested rather than incidental.resourceMatchesalready returnedtruefor an empty resource, so a trust policy (which has noResource) evaluates throughEvaluateunchanged 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, andTestSTSAssumeRole_NoCredentialLeaksOnDenialasserts the session store is empty after a denial.ExternalIdis parsed and bounds-checked at 2–1224 characters, returningValidationErrorlikeDurationSecondsalready does. On a non-Allow decision the response isAccessDenied/ 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 actionDecisionDeny→…is not authorized to perform: sts:AssumeRole with an explicit deny in the role trust policyTwo paths stay unenforced by design. A role with no trust policy — a zero-value document means "not configured", so
AssumeRolebehaves exactly as before; writing a policy is the opt-in. That is the documenteddocs/testing-guide.mdrule, 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'sCreateRolerequires a trust policy, a state substrate permits, so denying it would refuse roles that cannot exist on AWS. A nilctx.Principal— for the same reasonCheckAccessreturns nil on one: an unauthenticated caller resolves to no ARN, so there is nothing for aPrincipalelement to be true of, and denying would break every in-processhttptestcaller.Provenance
The code is
AccessDenied, not theAccessDeniedException#593's Acceptance section names. This is observed behaviour, not modelled: the botocore STS model documents no access-denied error forAssumeRoleat all (onlyMalformedPolicyDocument,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
AccessDeniedExceptionfromCheckAccessis 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 coversUpdateAssumeRolePolicy, which turned out not to exist, soCreateRoleremains 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:ExternalIdandPrincipalrestrictions 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 runsAssumeRole(cfnAttribution.resolveuses a service role ARN directly), so no stack deploy passes the new gate.Verification
All five pre-existing
sts_plugin_test.goAssumeRole tests pass unchanged — the opt-in decision as an executable claim. New:TestEvaluate_PrincipalMatching(18 cases) plus twoNotPrincipal/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 assertingAccessDeniedwithout the ExternalId andASIA…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 andgo test -C test/e2eare all green.Replayed live against the real CLI, with a signed IAM user already granted
sts:AssumeRoleso the #411 identity gate passes first — that success was the defect, since there was no trust gate behind it. All four refusal paths returnedASIA…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
principalMatchesreturningtrueunconditionally, which restores this bug exactly; the trust check moved after the mint; thests:ExternalIdcondition context dropped; the two messages swapped; the empty-policy opt-out removed; and the account comparison widened.