Sub topic 11
Scoping trust safely
This chapter assumes you can write a GitHub Actions workflow, know what an IAM role and trust policy are, and have seen the sub, aud, and iss claims in a GitHub OIDC token. Here you will learn how to narrow a trust policy so only the workflows you intend can assume a role.
Why scope matters
GitHub signs every OIDC token with the same key set, and every repository on GitHub gets tokens from the same issuer. The cloud provider cannot tell your repository from anyone else's unless your trust policy says so. The policy conditions are the only thing standing between the role and every other workflow on GitHub.
A missing or loose condition means the wrong workflow can assume your role. Scoping is the main security control in this pattern.
Always check both audience and subject
A trust policy should check aud and sub. The audience confirms the token was minted for your cloud provider. The subject confirms which repository and which ref, environment, or other context is asking.
This policy checks both:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::123456789012:oidc-provider/token.actions.githubusercontent.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": "repo:octo-org/octo-repo:ref:refs/heads/main"
}
}
}
]
}Only a workflow in octo-org/octo-repo running on main matches.
Practices to follow
- Prefer
StringEqualsoverStringLike. An exact match cannot accidentally admit more than you meant. Use wildcards only when you need them. - Never leave
subunconstrained. A policy that checks onlyaudtrusts every repository on GitHub, because they all use the same audience. - Be careful with wildcards. A value like
repo:octo-org/*admits every repository in the organization, including ones created later. Use a wildcard on a segment only when every value it can match should have access. - Pin the branch or environment. Match
ref:refs/heads/mainorenvironment:productioninstead ofrepo:octo-org/octo-repo:*. Pull request workflows get a different subject (repo:octo-org/octo-repo:pull_request), so an exact match on a branch keeps them out. - Use one role per purpose. A deploy role and a read-only role should have separate trust policies and separate permissions. A compromised test workflow then cannot deploy.
- Keep permissions small. The trust policy decides who can assume the role. The role's permission policy decides what they can do once inside. Grant only what the job needs.
- Pin the workflow when it matters. For sensitive roles, add a condition on
job_workflow_refso only a specific workflow file can assume the role.
Using GitHub environments gives you a useful extra control. If the subject is repo:octo-org/octo-repo:environment:production, the job can only get that subject by targeting the production environment. Required reviewers and branch rules on that environment then gate access to the role.
Exercise
Exercise
A trust policy checks only token.actions.githubusercontent.com:aud equal to sts.amazonaws.com. Why is this unsafe, and what condition would you add to limit it to the production environment of octo-org/octo-repo?
Show answer
Every GitHub workflow requesting a token for sts.amazonaws.com carries that audience, so any repository on GitHub could assume the role. Add a StringEquals condition on token.actions.githubusercontent.com:sub with the value repo:octo-org/octo-repo:environment:production.