Sub topic 4
How the subject claim encodes repository, branch, and environment
This chapter covers the sub (subject) claim in a GitHub Actions OIDC token. It explains how GitHub builds the claim, which formats it can take, and which formats your trust policy has to match. It assumes you know GitHub Actions workflow YAML, basic IAM roles and policies, and what a JSON Web Token (JWT) is. Later chapters cover the trust policy conditions themselves in detail.
Why the subject claim matters
When a job requests an OIDC token, GitHub signs a JWT that describes where the job is running. AWS verifies the signature. After that, the only way your role can tell one workflow from another is by comparing claims against the conditions in its trust policy.
The aud claim says who the token is for. The sub claim says who is asking. A trust policy that checks aud and nothing else accepts a valid token from any repository on GitHub, so sub is the main control.
In AWS the claim appears in a condition under the key token.actions.githubusercontent.com:sub. The prefix is the issuer URL of the OIDC provider you registered in IAM, without https://.
The default subject formats
By default, GitHub builds sub as a colon-separated string that starts with the repository and ends with a context. The context depends on what triggered the job and whether the job targets an environment.
| Situation | 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.2.0` |
| Pull request event | `repo:example-org/example-repo:pull_request` |
| Job that declares an environment | `repo:example-org/example-repo:environment:production` |
Each value has the same parts:
repo:followed byowner/name.- A context type:
ref,pull_request, orenvironment. - For
refandenvironment, the value of that context.
A branch name containing slashes keeps them. A job on release/2026-01 has the subject repo:example-org/example-repo:ref:refs/heads/release/2026-01. Pattern matching on those slashes matters later, because a wildcard can match across them.
Which format a job gets
The format is chosen by the job, not by your trust policy. Two rules decide it:
- If the job has an
environment:key, the subject uses the environment form. It does not contain the branch. - If the job has no environment, the subject depends on the event. Pull request events produce the
pull_requestform. Other events produce arefform, withrefs/heads/for branches andrefs/tags/for tags.
The environment rule causes the most confusion. Suppose a deploy job runs on main and declares environment: production. Its subject is repo:example-org/example-repo:environment:production. A trust policy that requires repo:example-org/example-repo:ref:refs/heads/main will reject it, even though the push was to main.
Reading the real token
Don't guess what a workflow's subject looks like. Print it. This workflow requests a token and shows only the claims you need to write a policy. It never prints the signed token itself.
name: inspect-oidc
on:
workflow_dispatch:
permissions:
id-token: write
contents: read
jobs:
inspect:
runs-on: ubuntu-latest
steps:
- name: Print selected OIDC claims
run: |
TOKEN=$(curl -sS \
-H "Authorization: Bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \
"$ACTIONS_ID_TOKEN_REQUEST_URL&audience=sts.amazonaws.com" \
| jq -r .value)
echo "$TOKEN" | python3 -c '
import sys, json, base64
payload = sys.stdin.read().strip().split(".")[1]
payload += "=" * (-len(payload) % 4)
claims = json.loads(base64.urlsafe_b64decode(payload))
keys = ["iss", "aud", "sub", "ref", "environment", "event_name", "job_workflow_ref"]
print(json.dumps({k: claims.get(k) for k in keys}, indent=2))
'A JWT has three base64url-encoded parts separated by dots: header, payload, and signature. The script decodes the second part and restores the padding that base64url omits. Run it from the main branch with no environment, and the output looks like this:
{
"iss": "https://token.actions.githubusercontent.com",
"aud": "sts.amazonaws.com",
"sub": "repo:example-org/example-repo:ref:refs/heads/main",
"ref": "refs/heads/main",
"environment": null,
"event_name": "workflow_dispatch",
"job_workflow_ref": "example-org/example-repo/.github/workflows/inspect-oidc.yml@refs/heads/main"
}Add environment: production to the job and run it again. The sub value changes to the environment form. The separate ref claim stays refs/heads/main, and the environment claim now holds production. The branch information is still in the token. It just isn't in sub.
From subject to trust policy condition
A trust policy condition compares the subject string with the pattern you give it. For an exact match, the value in the policy is the full string from the table above:
{
"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 is case-sensitive and compares the whole string. A value with a different owner, a different repository, a different branch, or a trailing character will not match. Copy the exact casing of the owner and repository name from the token you printed.
Every part of sub needs a condition. A policy that pins only the repository has to use a wildcard, such as repo:example-org/example-repo:*. That accepts every branch, tag, pull request, and environment in the repository. Whether that's acceptable depends on who can push branches or open pull requests there. Chapter sections on matching operators and common mistakes cover this trade-off.
Subjects you may not expect
Several cases produce subjects that surprise people.
- Pull requests. Every pull request event in the repository uses the same subject,
repo:example-org/example-repo:pull_request. The subject doesn't include the PR number or the source branch. Pull requests from forks don't receiveid-token: write, so they can't request a token. Pull requests from branches in the same repository can. - Reusable workflows. The subject describes the calling repository and its context. The called workflow's location appears in
job_workflow_ref, not insub. A role that must trust only one shared workflow has to check that claim. - Environment names. The subject contains the environment name exactly as written in the workflow.
Productionandproductionare different strings. Environment names are not tied to a branch, so any branch allowed to deploy to that environment gets the same subject. - Branch names with slashes.
feature/loginbecomesrefs/heads/feature/login. A wildcard such asrefs/heads/feature/*matches it, and also matchesfeature/a/b/c, because the wildcard crosses slashes. - Tags. A tag-triggered job has a
refs/tags/...subject. Anyone who can create a tag in the repository can produce one, so a loose tag pattern widens who can assume the role.
A repository-level customization
The default format can be changed per repository or organization. The following call tells GitHub to build sub from only the repo and context claims:
gh api -X PUT repos/example-org/example-repo/actions/oidc/customization/sub \
-F use_default=false \
-f 'include_claim_keys[]=repo' \
-f 'include_claim_keys[]=context'After this call, sub is built from the selected keys in the order you listed them, so your trust policy must match the new shape. A policy written for the default format will stop matching, and role assumption fails until you update it. Change the template and the trust policies together, and test with the inspection workflow first.
Debugging a mismatch
When role assumption fails, work through these checks in order:
- Print the claims with the inspection workflow in the same job configuration that failed. Include the same
environment:setting and the same trigger. - Compare the printed
subwith the policy value character by character. Check the owner, repository, and environment casing. - Check whether the job declares an environment. If it does, the policy needs the
environment:form. - Check
aud. The default inconfigure-aws-credentialsissts.amazonaws.com, and the policy must match it. - Confirm the workflow has
permissions: id-token: write. Without it, no token is issued at all.
Exercise
A deploy job runs on a push to main in example-org/example-repo. The job declares environment: staging. Its role trust policy requires sub to equal repo:example-org/example-repo:ref:refs/heads/main, and aud is correct. Does role assumption succeed? If not, what value would the policy need to match, and where in the token can you still find the branch?
Show answer
It fails. Because the job declares an environment, sub is repo:example-org/example-repo:environment:staging, which does not equal the branch-form string. The policy would need to match that environment-form value. The branch is still available in the separate ref claim (refs/heads/main), but a condition on sub alone never sees it.
Exercise
A pull request is opened from a branch named fix/typo in the same repository. What is the sub value in the token its workflow receives, and why can a policy not tell this pull request apart from another one in the same repository using sub alone?
Show answer
The sub value is repo:example-org/example-repo:pull_request. The default pull request subject contains only the repository and the pull_request context. It does not include the PR number or the source branch name, so all pull request events in that repository share one subject.