Guides

Cross-account role trust and the confused deputy problem

How to review IAM role trust policies across AWS accounts, when to use sts:ExternalId, aws:PrincipalOrgID and aws:SourceAccount, and how to list risky trusts.

Updated

Every IAM role has a trust policy that says who may assume it. In a multi-account organization those trust policies are where accounts, vendors, CI systems and AWS services meet, and where a single missing condition can let the wrong party in.

What a trust policy grants

A role's trust policy is a resource-based policy whose Principal element names who can assume the role (AWS: Principal). When the principal is another account, such as arn:aws:iam::123456789012:root, the trust delegates to that account, and its own administrators decide which of their identities may use it.

Review every trust that names an account, *, a federated identity provider or an AWS service, and ask what restricts it.

Third parties: sts:ExternalId

When a vendor assumes a role in your account, they serve many customers, and any of those customers could give the vendor your role's ARN. Without a check, the vendor could be tricked into using your role on someone else's behalf. This is the confused deputy problem (AWS: The confused deputy problem).

The fix is an sts:ExternalId condition with a value the vendor generates, unique per customer, and sends in every AssumeRole call (same page):

{
  "Effect": "Allow",
  "Principal": { "AWS": "arn:aws:iam::123456789012:root" },
  "Action": "sts:AssumeRole",
  "Condition": { "StringEquals": { "sts:ExternalId": "a-value-the-vendor-gave-you" } }
}

The ExternalId isn't a password; it stops the vendor acting for the wrong customer.

Your own accounts: aws:PrincipalOrgID and aws:PrincipalArn

For trusts between your own accounts, two global condition keys narrow who can use the trust (AWS: global condition keys):

AWS services: aws:SourceAccount and aws:SourceArn

When an AWS service principal such as cloudtrail.amazonaws.com is granted access, AWS recommends adding aws:SourceArn, aws:SourceAccount, aws:SourceOrgID or aws:SourceOrgPaths, so the service can act only on behalf of the resources, accounts or organization you expect (AWS: cross-service confused deputy prevention).

CI systems: OIDC subject conditions

Trusting an OIDC provider such as GitHub Actions without restricting the token's subject lets workflows from repositories you don't control assume the role. AWS's guidance is to limit token.actions.githubusercontent.com:sub to specific repositories or branches (AWS: Configuring a role for GitHub OIDC identity provider). IAM now checks for that condition when a trust policy for GitHub's provider is created or updated, but roles created earlier are worth reviewing.

List cross-account trusts without conditions

This lists roles whose trust policy allows * or another account, in a statement with no Condition at all. Set ACCOUNT to the account you're checking:

ACCOUNT=111122223333
aws iam list-roles --output json \
| jq -r --arg me "$ACCOUNT" '
  def arr: if type == "array" then . else [.] end;
  .Roles[] | .RoleName as $role
  | .AssumeRolePolicyDocument.Statement | arr[]
  | select(.Effect == "Allow" and .Condition == null)
  | (.Principal.AWS? // empty) | arr[]
  | select(. == "*"
      or (test(":\\d{12}:") and (test(":" + $me + ":") | not))
      or (test("^\\d{12}$") and . != $me))
  | "\($role)\t\(.)"'

list-roles returns each role's trust policy (AWS CLI reference). The check is deliberately simple: a statement with a condition may still be too loose, and federated and service principals need the checks above.

Doing this in AccessLens

AccessLens's trust policy analyzer checks every role in every account you add for: roles anyone can assume, cross-account trusts without an ExternalId or other restricting condition, trusts to accounts outside your organization, service trusts without source conditions, and OIDC or SAML trusts without audience or subject conditions. The trust graph then shows where those trusts lead, and highlights paths from external entry points to admin roles.

AccessLens's own role follows the same advice: its trust policy names your hub account, narrows it with aws:PrincipalArn, requires a random per-account sts:ExternalId, and adds aws:PrincipalOrgID when your organization is known. Read it here.

Check every account at once.

Sign in with IAM Identity Center, create one read-only role, and scan. Your findings stay on your machine.