Sub topic 14
Common mistakes that widen trust
This chapter assumes you have read the earlier chapters of this tutorial. You should be able to read a GitHub Actions OIDC token's sub and aud claims and write an AWS IAM trust policy with a Condition block. Here you will review trust policies for mistakes that let the wrong repository, branch, or pull request assume a role.
Most of these mistakes share one trait. The workflow still deploys, so nobody notices the policy is too loose. A trust policy fails silently in the permissive direction, so you have to look for these mistakes on purpose.
The mistakes at a glance
| Mistake | What it allows | Fix |
|---|---|---|
| No `sub` condition | Any repository on GitHub | Always match `sub` |
| Wildcard in the repository part | Other repositories in the organization, or with similar names | Name the exact repository |
| Trailing `:*` on the subject | Every branch, tag, pull request, and environment | Match one full subject |
| Missing `aud` condition | Tokens minted for other audiences | Require `sts.amazonaws.com` |
| Pull request subject allowed for a powerful role | Anyone who can open a pull request | Use a separate, read-only role |
| Unprotected environment | Anyone who can push a branch | Add protection rules |
| Negated operators | Everything except the listed values | Use positive matches |
| Mixing branch and environment subjects | Nothing, or more than intended | Match the subject the job actually produces |
Mistake 1: trusting the provider without checking the subject
The GitHub OIDC provider is shared by every repository on GitHub. A trust policy that names the provider as the principal and checks only the audience accepts a token from any repository.
{
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
}
}
}An attacker only has to create a repository and a workflow that requests a token. The signature is valid because GitHub signed it. The signature proves that GitHub issued the token. It does not prove that the token belongs to your repository. Your sub condition is what binds the token to your code.
Mistake 2: wildcards that match more than you expect
In StringLike, * matches any run of characters, including colons and slashes. A single * can therefore span several parts of the subject.
{
"Condition": {
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:example-org/*"
}
}
}This trusts every repository in example-org, including ones created next year by a team you have never heard of. These variations are subtler:
repo:example-org/example-repo*also matchesexample-repo-sandboxandexample-repo-fork.repo:example-org/example-repo:*matches every branch, tag, pull request, and environment of that one repository.repo:example-org/*:ref:refs/heads/mainmatches themainbranch of every repository.
The first fix is to write the whole subject and use StringEquals:
{
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": "repo:example-org/example-repo:ref:refs/heads/main"
}
}
}StringEquals treats * as a literal character. If you use StringEquals with a pattern, the policy never matches, and the job fails with an access error. That failure is safe. The opposite habit is not: reaching for StringLike everywhere because it "just works."
When you need several exact values, give the key a list. The condition passes if any entry matches:
{
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": [
"repo:example-org/example-repo:ref:refs/heads/main",
"repo:example-org/example-repo:environment:production"
]
}
}
}A list of exact values is usually safer than one wildcard that happens to cover them.
Mistake 3: wildcards on branches
Trusting release/* is a common design. It is acceptable only if you control who can create branches with that prefix.
{
"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/*"
}
}
}Anyone with write access to the repository can push release/anything, edit the workflow file on that branch, and assume the role. The policy narrows the trust to branch names, and your repository settings decide who can create those names. Pair a wildcard like this with a branch ruleset that restricts creation of release/* branches.
Mistake 4: forgetting the audience, or loosening it
The aud claim says which service the token is meant for. Without the check, a token minted for another purpose can be replayed against your role. Keep aud as a StringEquals condition with the value sts.amazonaws.com. If you set a custom audience in your workflow, change the policy to match, and do not widen it to a wildcard.
Mistake 5: allowing the pull request subject
For jobs triggered by the pull_request event, the subject is repo:example-org/example-repo:pull_request, whatever the branch. It does not carry a branch name, so it cannot be restricted by branch.
A pull request workflow runs the workflow file from the pull request. Anyone who can open a pull request from a branch in your repository can change that file to run any command with the role's credentials. So the role behind a pull_request subject should be able to do very little, such as read from a cache bucket. Never put the deploy role behind it.
Mistake 6: relying on an unprotected environment
An environment condition looks strong because it matches repo:example-org/example-repo:environment:production. But a job claims an environment simply by writing environment: production in its YAML. If the environment has no protection rules, any workflow run that can name it gets the token. Anyone who can push a branch can add that line to a workflow file.
The trust policy proves which environment the job named. It does not prove the job was approved. Add required reviewers and a deployment branch rule to the environment so that only approved runs from main can use it.
Mistake 7: matching the wrong subject for the job
When a job declares an environment, the subject switches to the environment form. It no longer contains the branch. So this job:
jobs:
deploy:
runs-on: ubuntu-latest
environment: productionproduces repo:example-org/example-repo:environment:production. A trust policy that requires ref:refs/heads/main will reject it. Teams often respond by loosening the policy to repo:example-org/example-repo:*, which is the wrong fix. Instead, match the environment subject, and enforce the branch with the environment's deployment branch rule.
Mistake 8: using negated operators
StringNotEquals and StringNotLike tempt people who want to say "anything except forks" or "any branch except dev." When a condition key is missing from the request, a negated operator evaluates to true. A typo in the condition key name therefore opens the role instead of closing it. Prefer a positive list of exactly what you accept.
Mistake 9: relying on names that can change
The default subject is built from names, such as the organization, repository, and branch. Names can be reused. If a repository is renamed or deleted, and someone later creates a new one with the old name in the same organization, the old trust policy matches the new repository. Review trust policies when you rename or delete repositories.
You can also customize the subject template to include stable identifiers, such as the repository ID. A custom template changes every subject, so existing policies stop matching until you update them. The same applies to the gh api call that sets the template. If you remove claim keys, the subject carries less information, and a policy written for the shorter subject may accept more runs than you intended.
Mistake 10: narrow trust, broad permissions
A perfect trust policy does not protect you if the role's permissions policy grants AdministratorAccess. Treat the trust policy as the entry check and size the permissions to the job. A role that only uploads to one bucket is a small loss if something goes wrong. A role with administrator access is a large one.
A review checklist
- Does every statement check both
audandsub? - Is
StringEqualsused wherever a literal value works? - Does every wildcard stay inside one segment, and who can create names that match it?
- Does the subject type (branch, environment, pull request, tag) match what the job produces?
- Do environments used by trusted roles have reviewers and a branch rule?
- Are there any negated operators?
- Are the role's permissions as small as the job needs?
Exercise
A trust policy has StringEquals on aud set to sts.amazonaws.com and StringLike on sub set to repo:example-org/example-repo:*. Name two kinds of workflow runs, beyond the main branch, that can assume this role, and say how you would tighten it for a job that deploys with environment: production.
Show answer
Runs on any other branch, on pull requests, on tags, and in any environment can assume it, because * also matches the colon-separated parts. For the deploy job, switch to StringEquals with the exact subject repo:example-org/example-repo:environment:production, and add required reviewers and a deployment branch rule to the production environment.
Exercise
A teammate changes the policy from StringEquals to StringLike because a deploy job with environment: production was denied while the policy required repo:example-org/example-repo:ref:refs/heads/main. What was the actual cause, and what is the right fix?
Show answer
The job declared an environment, so its subject was repo:example-org/example-repo:environment:production, not the branch form. The right fix is to change the policy to match that exact subject with StringEquals, and restrict the environment to the main branch with a deployment branch rule. Loosening the match with a wildcard widens trust without addressing the cause.