Sub topic 13
Customizing the Subject Claim
This chapter assumes you know how to write a role trust policy for GitHub Actions and AWS IAM. It also assumes you have read the earlier chapters on the default sub formats for branches, environments, pull requests, and tags. Here you will change what the sub claim contains, learn what that does to your trust policies, and decide when a custom template is worth the effort.
Why change the default
By default, GitHub builds sub from fixed parts. Examples are repo:example-org/example-repo:ref:refs/heads/main and repo:example-org/example-repo:environment:production. This works for most teams, but it has limits.
- Names can change. A repository or organization can be renamed or transferred. A trust policy that matches the name
example-org/example-repomay later match a different repository that took over the name. - Parts you need are missing. The default
subhas no field for the workflow file, the actor, or the repository's numeric ID. - Policies are wildcard-heavy. If you need "any branch in this repository", you end up with a
StringLikepattern such asrepo:example-org/example-repo:*. That pattern also matches pull request, tag, and environment subjects.
A custom sub template lets you choose which claims go into the subject. You can then match on a smaller, more stable string.
How a template works
A subject claim template is a list of claim keys. GitHub reads the keys in order, looks up each claim's value for the current job, and joins the results with colons to form sub. The key names appear in the string next to their values.
The context key is a special case. It stands for the "where am I running" part of the default subject: the environment name, the branch or tag ref, or the pull request marker. Including context keeps that information in the subject.
Templates can be set at two levels:
| Level | What it affects | Notes |
|---|---|---|
| Organization | Repositories that have not overridden it | Set with the organization endpoint. A repository can opt out. |
| Repository | One repository | Setting `use_default=true` makes the repository follow the organization template. Setting it to `false` lets the repository define its own keys. |
Usable claim keys include repo, repository, repository_id, repository_owner, repository_owner_id, context, ref, ref_type, environment, job_workflow_ref, workflow, workflow_ref, actor, actor_id, and event_name. Check GitHub's current documentation for the full list, because it grows over time.
Setting a repository template
The call below sets a repository-level template containing only repo and context.
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'Here is what each part does:
-X PUTreplaces the repository's current template.use_default=falsetells GitHub to use the keys you list instead of the organization or GitHub default.- Each
include_claim_keys[]entry adds one claim to the subject, in the order given.
To go back to the default behavior, send use_default=true and no keys:
gh api -X PUT repos/example-org/example-repo/actions/oidc/customization/sub \
-F use_default=trueYou need admin access to the repository to change its template. The organization-level endpoint, orgs/example-org/actions/oidc/customization/sub, needs organization admin rights.
You can read back the current setting at any time:
gh api repos/example-org/example-repo/actions/oidc/customization/subAlways read the real token
Do not guess the new sub string. The exact format depends on the keys you choose, their order, and the event that triggered the job. Print it from a real run instead. This workflow requests a token and prints only a few non-secret claims. It never prints the token itself.
name: inspect-oidc-claims
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" | python3 -c '
import sys, base64, json
payload = sys.stdin.read().strip().split(".")[1]
payload += "=" * (-len(payload) % 4)
claims = json.loads(base64.urlsafe_b64decode(payload))
keys = ("sub", "aud", "job_workflow_ref", "repository_id")
print(json.dumps({k: claims.get(k) for k in keys}, indent=2))
'Run it from the main branch, from a feature branch, and from a job that targets an environment. Each run shows a different sub. Copy the value you want to allow into your trust policy. Do not retype it from memory.
Updating the trust policy
Once the template changes, every existing trust policy that matches the old sub format stops matching. The role fails closed: assumption is denied until you update the condition. Plan the change in this order:
- Run the inspection workflow under the new template and record the
subvalues for each job type you deploy from. - Update the trust policy so it accepts both the old and the new values. Use a list of strings in
StringEquals. - Switch the template in the repository or organization.
- Run a deployment and confirm that
aws sts get-caller-identitysucceeds. - Remove the old value from the trust policy.
A condition with two exact values looks like this. The second value is a placeholder that you replace with the string printed by the inspection workflow.
{
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": [
"repo:example-org/example-repo:ref:refs/heads/main",
"<new sub value printed by the inspection workflow>"
]
}
}
}StringEquals with a list means "equal to any one of these". It still uses exact matching, so it does not widen trust the way a wildcard does.
Making the subject harder to spoof
Names are the weakest part of the default subject. Consider this sequence:
- Your team renames
example-org/old-nametoexample-org/new-name. - Someone later creates a new repository called
example-org/old-name. - A trust policy that still says
repo:example-org/old-name:...now trusts the new repository.
GitHub blocks some of these cases through name-reservation behavior, but you should not rely on that alone. Numeric IDs do not change when a repository is renamed. Adding repository_id and repository_owner_id to the template puts those IDs in the subject, so a trust policy that matches them cannot be satisfied by a different repository that reuses an old name.
An organization-wide template that includes the IDs looks like this:
gh api -X PUT orgs/example-org/actions/oidc/customization/sub \
-F use_default=false \
-f 'include_claim_keys[]=repository_owner_id' \
-f 'include_claim_keys[]=repository_id' \
-f 'include_claim_keys[]=context'Run the inspection workflow afterward to see the resulting string. It contains the numeric IDs, which the default subject does not.
Trade-offs
| Choice | Benefit | Cost |
|---|---|---|
| Keep the default template | Matches documentation and examples. Every engineer reads it the same way. | Depends on names. Gives you fewer fields to match. |
| Template with `repo` and `context` | Same information as the default, but you set the order and shape yourself. | Little gain over the default. Existing policies need rewriting. |
| Template with numeric IDs | Resists rename and transfer attacks. | Subjects are harder for people to read. Reviewers cannot tell which repository a policy refers to without looking up the ID. |
| Template with `job_workflow_ref` in the subject | A policy can require one specific reusable workflow. | Each reusable workflow path and ref changes the subject. Policies multiply. |
For most teams the best default is to keep GitHub's template and tighten the trust policy with exact values and an aud check. Customize the sub when you have a concrete reason, such as repository renames in a large organization or a requirement to pin a shared deployment workflow.
Common mistakes
- Changing the template without updating the roles. All deployments fail with an
AccessDeniedorNot authorized to perform sts:AssumeRoleWithWebIdentityerror. Check thesubprinted by the inspection workflow against the trust policy character by character. - Leaving out
context. If the template has nocontext(orrefandenvironment), a job on any branch, tag, pull request, or environment produces the samesub. A policy that matches it cannot tell them apart, so branch and environment restrictions stop working. - Using a wildcard to cope with the new format. If you cannot see why the match fails, do not switch to
StringLikewith*. Read the real value and match it exactly. - Forgetting repository overrides. A repository with
use_default=falseignores the organization template. Review repositories individually when you audit. - Assuming the same format for every event. Pull request jobs and jobs that target an environment produce different
contextvalues. Test each type you deploy from.
Exercise
Your organization sets a template of repository_id and context, and a role's trust policy still has the old condition "repo:example-org/example-repo:ref:refs/heads/main" with StringEquals. Deployments from main now fail. Describe a safe migration that restores access without using a wildcard, and say why adding "repo:example-org/*" would be a mistake.
Show answer
Run an inspection workflow from main under the new template and copy the exact sub it prints. Add that value to the StringEquals list next to the old one, confirm a deployment succeeds, then remove the old value. Exact matching keeps trust limited to the one repository and branch. The pattern "repo:example-org/*" would be a mistake because under the old format it would let every repository, branch, tag, and pull request in the organization assume the role, and it would not even match the new ID-based format.