Sub topic 4
How the subject claim encodes repository, branch, and environment
This chapter assumes you know what a JWT is, can read GitHub Actions workflow YAML, and understand IAM roles and trust policies at a basic level. It covers the sub claim, which is the value your trust policy matches against.
What the subject claim is
Every OIDC token that GitHub issues to a job carries a sub (subject) claim. The claim is a single string that says which workflow context asked for the token. Your cloud provider can't see your workflow file. It sees only the token, so sub is the main way a trust policy tells one caller from another.
GitHub builds the string by joining parts with colons. The default format always starts with repo: followed by the owner and repository name. The rest depends on what triggered the job.
Default formats
| Trigger or setting | Subject value |
|---|---|
| Job on a branch | `repo:example-org/example-repo:ref:refs/heads/main` |
| Job on a tag | `repo:example-org/example-repo:ref:refs/tags/v1.0.0` |
| Job that uses an environment | `repo:example-org/example-repo:environment:production` |
| Job on a pull request event | `repo:example-org/example-repo:pull_request` |
Two details matter here.
- The environment form replaces the branch form. If a job declares
environment: production, the subject ends inenvironment:production, even when the job runs onmain. The branch no longer appears insub. - The pull request form carries no branch name. The source branch and the target branch are not in the string.
Matching the subject in a trust policy
In AWS, the token's sub is exposed as a condition key named after the provider host, token.actions.githubusercontent.com:sub. Use StringEquals for an exact match:
{
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": "repo:example-org/example-repo:ref:refs/heads/main"
}
}
}This condition accepts a job only when both the audience and the full subject string match. A job from example-org/other-repo fails. A job on main in the right repository, but one that declares an environment, also fails, because its subject is the environment: form.
Pairing the workflow with the policy
The workflow decides which subject form is produced. This job produces repo:example-org/example-repo:environment:production:
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
permissions:
id-token: write
contents: read
steps:
- run: echo "sub ends in environment:production"A trust policy written for the ref:refs/heads/main form will reject this job. When a role assumption fails with "Not authorized to perform sts:AssumeRoleWithWebIdentity", compare the subject your job produces with the string in the policy before changing anything else.
Exercise
A job runs on the main branch of example-org/example-repo and declares environment: staging. What is its sub claim, and would a trust policy that matches repo:example-org/example-repo:ref:refs/heads/main accept it?
Show answer
The sub claim is repo:example-org/example-repo:environment:staging. The policy would not accept it, because declaring an environment replaces the branch form, so the string no longer matches.