Sub topic 11
Scoping trust safely
This chapter assumes you can read a GitHub Actions workflow, have seen an IAM trust policy, and know the claims aud, sub, and job_workflow_ref from earlier chapters. It covers how to narrow a cloud trust policy so that only the workflows you intend can get credentials.
Why scoping matters
OIDC removes stored secrets, but it does not decide who gets access. The trust policy decides. GitHub signs tokens for every repository on github.com, and your cloud provider trusts GitHub's signing keys. If the policy only checks the issuer, any repository owned by anyone could present a valid token.
Think of the signature as proof that GitHub issued the token. The conditions are your proof that the token belongs to your workflow.
Always pin the audience and the subject
Every trust policy should check two claims.
audshould equal the value you chose when requesting the token. For AWS this issts.amazonaws.com.subshould name the exact repository and the exact context (branch, tag, environment, or pull request) allowed to act.
Use exact-match operators such as StringEquals when you can. Switch to a pattern operator such as StringLike only when you need to cover several values on purpose.
Know the subject formats
The default sub value depends on how the job was triggered.
| Trigger context | Default `sub` |
|---|---|
| Push to a branch | `repo:octo-org/octo-repo:ref:refs/heads/main` |
| Push of a tag | `repo:octo-org/octo-repo:ref:refs/tags/v1.0.0` |
| Pull request event | `repo:octo-org/octo-repo:pull_request` |
| Job that uses an environment | `repo:octo-org/octo-repo:environment:production` |
When a job references an environment, the subject uses the environment name and no longer contains the branch. A policy that expects ref:refs/heads/main will therefore reject a job that you later move into a production environment. Choose one scheme per role and keep the workflow consistent with it.
Prefer environments for production access
A branch name tells you where the code came from. An environment tells you that the job passed the protection rules attached to it, such as required reviewers, wait timers, and allowed branches. Tying a role to an environment adds those controls to the cloud side without extra work.
{
"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:environment:production"
}
}
}
]
}The matching job declares the environment:
name: release
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/github-prod-deploy
aws-region: us-east-1
- run: aws sts get-caller-identityEnvironment protection rules are only as strong as the people who can edit them. Restrict who has admin access to the repository, because an admin can relax the rules.
Be careful with wildcards
Wildcards are sometimes needed, for example to allow several release branches. Each one widens the trust, so check what it matches.
{
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:octo-org/octo-repo:ref:refs/heads/release/*"
}
}Common wildcard mistakes:
repo:octo-org/*trusts every repository in the organization, including a new one created by someone with little review. Use it only for roles with almost no permissions.repo:octo-org/octo-repo*also matchesocto-org/octo-repo-sandboxand any later repository whose name starts the same way. End the pattern at a colon boundary, as inrepo:octo-org/octo-repo:*.*alone, or a missingsubcondition, trusts every repository on GitHub.- A
StringLikepattern forrefs/heads/*includes any branch someone can push, not only protected ones.
Repository names can be renamed, deleted, and recreated. If an old name is released, another account may be able to claim it and produce matching subjects. Remove trust policies for repositories you no longer use.
Think about pull requests and forks
Workflows that run on pull_request use the subject repo:octo-org/octo-repo:pull_request, which does not include the branch name. Do not give that subject a role with write access. Anyone who can open a pull request from a branch in the repository can trigger that workflow, and its code is whatever the pull request contains.
For pull requests from forks, GitHub does not give the job write access to ID tokens. This is a useful default, but do not rely on it alone. Be especially wary of pull_request_target, which runs with the base repository's context while possibly checking out untrusted code. If a job like that can request a token, an attacker's code may be able to use it.
A good pattern is to split roles by risk:
- A read-only role for pull request jobs, such as a
terraform plan. - A write role tied to
mainor to a protected environment, used for deployment.
Narrow what the role can do
The trust policy controls who can assume the role. The permissions policy controls what the role can do afterward. Apply least privilege to both.
- Grant only the actions and resources the pipeline needs. Avoid
AdministratorAccesson a CI role. - Use one role per purpose, such as an image publisher or an infrastructure deployer, instead of one role for everything.
- Set a short maximum session duration. The default one hour is plenty for most jobs.
- Consider permission boundaries or service control policies so that a mistake in one policy cannot grant broad access.
Request the token only where you need it
Set id-token: write on the specific job that needs cloud access, not at the workflow level. Other jobs in the workflow, which might run third-party actions, then cannot request tokens.
name: build-and-deploy
on:
push:
branches: [main]
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "tests run with no cloud access"
deploy:
needs: test
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/github-deploy
aws-region: us-east-1Pin third-party actions that run in a job with id-token: write to a full commit SHA. Such an action could request a token and use it, just as your own steps can.
Add claims beyond `sub` where your cloud supports it
The sub claim covers the repository and context. The token also carries job_workflow_ref, which identifies the workflow file that is running. Restricting on it means a role can be assumed only from a specific, reviewed workflow, even if another workflow in the same repository runs on main.
Not every cloud exposes every claim as a condition key. Check your provider's documentation to see which claims you can match. Where a claim is not available, you can customize the sub claim so it includes more information, which a later chapter covers.
A quick review process
Before you ship a trust policy, check that:
- It has both
audandsubconditions. - The
subnames one repository and one context. - Any wildcard ends at a boundary you intended.
- The role's permissions match the single task the job performs.
- Only the job that needs a token has
id-token: write.
Exercise
A trust policy for a deploy role uses StringLike with sub set to repo:octo-org/octo-repo*. Name two problems with this pattern and give a safer value for deploying only from main.
Show answer
The trailing * has no colon boundary, so it matches other repositories such as octo-org/octo-repo-sandbox, and it matches every context in the repository, including pull request jobs and any branch. A safer value is the exact string repo:octo-org/octo-repo:ref:refs/heads/main, used with StringEquals.
Exercise
You move a deploy job into a GitHub environment named production. The job now fails with an access denied error from AWS, even though the trust policy still lists repo:octo-org/octo-repo:ref:refs/heads/main. Why, and how do you fix it?
Show answer
When a job references an environment, the sub claim becomes repo:octo-org/octo-repo:environment:production and no longer contains the branch. The policy's sub condition no longer matches. Update the condition to the environment subject, or remove the environment key from the job if you want to keep the branch-based subject.
Exercise
Why should id-token: write be set on the individual deploy job instead of at the top of the workflow?
Show answer
Permissions set at the workflow level apply to every job. A test or lint job running third-party actions could then request an ID token and try to assume a role. Setting the permission only on the deploy job limits token issuance to the one job that needs it.