Sub topic 14
Common mistakes that widen trust
This chapter assumes you can read a role trust policy, know that the OIDC token's sub claim names the repository and context, and have written a policy that pins a branch or environment. Here we look at the mistakes that quietly widen who can assume a role, and how to fix each one.
Mistake 1: No `sub` condition at all
A trust policy with only an aud check accepts a token from any repository on GitHub. All GitHub tokens use the same issuer and the same audience, sts.amazonaws.com. The audience proves only that the token was meant for AWS. It does not prove which repository asked.
{
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
}
}
}Anyone who knows your role ARN can attempt to assume this role from their own repository. Always add a sub condition.
Mistake 2: Wildcards in the wrong place
Wildcards need StringLike. Under StringEquals, an asterisk is a literal character, so the policy matches nothing useful. The opposite error is more dangerous: a wildcard that matches more than you meant.
{
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
},
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:example-org/*"
}
}
}This trusts every repository in the organization, including ones created later and ones with weak review rules. Compare the narrower form that still allows several branches:
{
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
},
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:example-org/example-repo:ref:refs/heads/release/*"
}
}
}Keep the repository name exact, and put the wildcard only in the last segment.
Mistake 3: Trusting a branch without protecting it
A policy that allows refs/heads/main is only as strong as the rules on main. If any contributor can push to main, any contributor can reach the role. Turn on branch protection, require reviews, and restrict who can push.
Mistake 4: Assuming an environment is a gate
An environment in the sub claim is only a name. If the policy matches environment:production but that environment has no protection rules, any workflow that declares environment: production gets the token immediately. Add required reviewers and a deployment branch rule in the repository settings.
Mistake 5: Forgetting pull request and tag subjects
Pull request workflows produce a sub of repo:example-org/example-repo:pull_request. Tags produce ...:ref:refs/tags/<name>. A broad pattern such as repo:example-org/example-repo:* accepts both. Then a pull request from a branch with a modified workflow file can request the role.
Mistake 6: Mismatched claim values
If a role fails with Not authorized to perform sts:AssumeRoleWithWebIdentity, check these in order:
- The
subvalue in the policy matches the token exactly, including case. - The workflow has
permissions: id-token: write. - The job's
environment:key matches the policy, since the environment changes thesubformat. - A custom
subtemplate, if set, changed the format.
How the mistakes map to fixes
| Mistake | Effect | Fix |
|---|---|---|
| Missing `sub` condition | Any repository can assume the role | Add an exact `sub` condition |
| `repo:org/*` | Every repository in the org | Name the repository |
| Unprotected branch | Any pusher reaches the role | Branch protection |
| Unprotected environment | Any job can claim the name | Reviewers and branch rules |
| Trailing `*` after repo | Pull requests and tags allowed | Pin `ref:refs/heads/main` |
Exercise
A trust policy has StringLike on sub with the value repo:example-org/example-repo:*. Name two kinds of workflow runs, other than pushes to main, that can assume the role, and give a narrower value that allows only main.
Show answer
Pull request runs (...:pull_request) and tag runs (...:ref:refs/tags/<name>) both match, as do runs for any other branch or environment. A narrower value is repo:example-org/example-repo:ref:refs/heads/main, used with StringEquals.