Sub topic 5
The subject claim: who is asking?
This chapter assumes you have read the earlier chapters on the token claims and on how GitHub issues OIDC tokens. You should know that a workflow gets a signed JWT and that a cloud provider checks it against a trust policy. Here we look at one claim, sub, and why it is usually the claim your trust policy depends on most.
Why `sub` matters
Many claims in a GitHub OIDC token describe the run: repository, ref, workflow, actor, event_name. The sub (subject) claim is different because it is the one identifier that OIDC standardizes for "who this token is about". Cloud providers expose it directly in policy conditions. AWS offers it as token.actions.githubusercontent.com:sub. Google Cloud and Azure map it to an attribute or match it as the federated credential's subject.
Everything else in the token is optional context. The iss claim tells the cloud the token came from GitHub, but it says nothing about which repository is asking. The aud claim only says who the token is for. If your trust policy checks only iss and aud, any workflow in any repository on GitHub can pass it. The sub condition is what narrows trust from "anyone using GitHub Actions" to "this repository, in this context".
The sub value is a plain string that GitHub builds from the run's context. Your trust policy matches that string.
The default formats
By default, GitHub builds sub from the repository and one piece of run context. Which piece depends on the job and the event.
| Situation | `sub` value |
|---|---|
| Push or other event on a branch | `repo:octo-org/octo-repo:ref:refs/heads/main` |
| Run on a tag | `repo:octo-org/octo-repo:ref:refs/tags/v1.2.0` |
| Job that declares an `environment` | `repo:octo-org/octo-repo:environment:production` |
| `pull_request` event, no environment | `repo:octo-org/octo-repo:pull_request` |
The format follows a precedence order, shown in the diagram below.
Three details follow from this order.
- An environment replaces the ref. A job on
mainthat declaresenvironment: productiongetsrepo:octo-org/octo-repo:environment:production. The branch no longer appears insub. Therefclaim still carries the branch, butsubdoes not. - Pull request tokens have no branch. A
pull_requestrun getsrepo:octo-org/octo-repo:pull_request, whatever the source branch. You cannot tellfeature/afromfeature/bbysubalone. - The ref is the full ref. For branches it is
refs/heads/<name>, and for tags it isrefs/tags/<name>. A condition written asrepo:octo-org/octo-repo:ref:mainnever matches.
Seeing your own `sub`
Do not guess the value. Request a token in a throwaway workflow and print only the claims you need. The token itself is a credential for its short lifetime, so never echo the raw JWT.
name: show-sub
on: workflow_dispatch
permissions:
id-token: write
contents: read
jobs:
inspect:
runs-on: ubuntu-latest
steps:
- name: Print selected 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" \
| jq -R 'split(".")[1] | gsub("-";"+") | gsub("_";"/") | @base64d | fromjson | {sub, ref, environment, event_name}'The JWT has three base64url parts separated by dots. The middle part is the payload, so the jq filter splits on ., takes index 1, converts base64url to base64, decodes it, and parses the JSON. Decoding does not verify the signature. That is fine for debugging. A cloud provider must always verify it.
Run this workflow from different branches, with and without an environment, and compare the output with the table above.
Writing conditions against `sub`
Once you know the strings, the trust policy is a matter of choosing how strictly to match them. Here is a policy that allows only the main branch and the production environment. In an AWS condition, a list of values means "any one of these".
{
"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:ref:refs/heads/main",
"repo:octo-org/octo-repo:environment:production"
]
}
}
}
]
}StringEquals requires an exact match. Use StringLike only when you need a wildcard, such as every tag of one repository:
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:octo-org/octo-repo:ref:refs/tags/v*"
}Two mistakes come up often. First, a * inside a StringEquals value is a literal asterisk, so it will not match anything useful. Second, a StringLike value of repo:octo-org/* matches every repository in the organization, including ones created next year. A single * can stand for any run of characters, including colons and slashes, so a loose pattern is wider than it looks.
Choosing between ref and environment
Both are valid ways to say "this deployment is allowed", but they enforce different things.
- Ref-based
subanswers "which branch or tag ran this". Anyone who can push to that branch can obtain the role. Branch protection rules are the control that matters. - Environment-based
subanswers "which named deployment target is this job using". Environments can have required reviewers, wait timers, and branch restrictions. A job that declaresenvironment: productionpauses until those rules pass, and only then does the job start and request its token.
For production access, the environment form is usually the better choice. The approval rules sit in GitHub, and the trust policy only has to say "the production environment of this repo". Pair it with a deployment branch restriction on the environment, because otherwise a job on any branch can declare that environment name.
Names, not identities
The default sub is built from names. If you rename octo-org/octo-repo, existing trust policies stop matching, and the failure looks like a subject mismatch. If you delete a repository and someone creates a new one with the same name, a policy that matched the old name now matches the new repository. This is most relevant after transfers and organization changes.
Where this risk matters to you, GitHub lets you change the format of sub, for example by adding the repository ID or owner ID so the claim does not depend on a name alone. Customization is covered in its own chapter. For now, remember that the default format is convenient and readable, and that readable names are a weaker identity than numeric IDs.
Reusable workflows
When a job calls a reusable workflow, the default sub still describes the caller's repository and ref. It does not say which workflow file did the work. If you want to trust a specific shared workflow, you need the job_workflow_ref claim or a customized sub. A later chapter on reusable workflows covers this.
Checklist for a `sub` condition
- Print the real
subfor each context you plan to allow, as shown above. - Match with
StringEqualsfirst, and reach forStringLikeonly when a wildcard is necessary. - Include the full repository name (
owner/repo), never just the owner. - Choose the narrowest context: an environment for production, an exact branch for staging.
- Keep the
audcondition as well.subandaudcheck different things.
Exercise
A job on the main branch of octo-org/octo-repo declares environment: staging. Your trust policy has a StringEquals condition for repo:octo-org/octo-repo:ref:refs/heads/main. Will the job be able to assume the role? Explain, and give the sub value the job will present.
Show answer
No. A job that declares an environment gets an environment-based subject, so the sub is repo:octo-org/octo-repo:environment:staging. The ref no longer appears in sub, so the branch condition does not match and the exchange fails. To fix it, add the environment string to the condition, or remove the environment key from the job.
Exercise
A teammate proposes this condition so that every repository in the organization can deploy: StringEquals with "token.actions.githubusercontent.com:sub": "repo:octo-org/*". Give two problems with it.
Show answer
First, StringEquals treats * as a literal character, so no real sub matches and every deployment fails. Second, if it were changed to StringLike, it would match every repository in the organization, current and future, plus any ref or environment. That is much broader than a deploy role should allow. A better policy lists the specific repositories, and the specific contexts, that need access.