Sub topic 12
Using other claims such as job_workflow_ref
This chapter assumes you have read the earlier chapters on the sub and aud claims. You should know how a trust policy compares claim values with StringEquals and StringLike, and how a workflow requests a token with id-token: write.
The sub claim can say which repository, branch, or environment is asking for access. It cannot say which workflow file is running or which pull request event triggered the job. Other claims in the token can say those things. The most useful one for access control is job_workflow_ref.
What else is in the token
GitHub signs more claims than the two you have matched so far. These are the ones most useful for trust decisions.
| Claim | Example value | What it tells you |
|---|---|---|
| `repository` | `example-org/example-repo` | The repository that triggered the run |
| `repository_owner` | `example-org` | The organization or user that owns it |
| `ref` | `refs/heads/main` | The Git ref that triggered the run |
| `ref_protected` | `true` | Whether the ref is protected by branch or tag rules |
| `event_name` | `push` | The event type, such as `push` or `pull_request` |
| `workflow_ref` | `example-org/example-repo/.github/workflows/deploy.yml@refs/heads/main` | The top-level (caller) workflow file |
| `job_workflow_ref` | `example-org/shared-workflows/.github/workflows/deploy.yml@refs/heads/main` | The workflow file that contains the running job |
| `actor` | `octocat` | The user who started the run |
| `runner_environment` | `github-hosted` | Whether the job runs on a GitHub-hosted or self-hosted runner |
The difference between workflow_ref and job_workflow_ref matters. In a plain workflow they are the same. When a job calls a reusable workflow, workflow_ref still names the caller. job_workflow_ref names the reusable workflow that holds the job.
Why job_workflow_ref helps
Suppose a platform team publishes a reusable deploy workflow in example-org/shared-workflows. It builds, scans, and deploys in a fixed way. You want any team's repository to be able to deploy, but only through that workflow.
Matching on sub cannot enforce this. The sub claim names the calling repository. A developer there could write their own workflow that calls aws s3 sync directly, and the sub would look the same. job_workflow_ref closes that gap, because it names the workflow file whose job requested the token.
Inspecting the claims before you write a policy
Never guess claim values. Decode a real token from the workflow you want to trust, and copy values from it. This workflow prints a selection of claims. It prints the payload only, never the token.
name: show-claims
on:
workflow_dispatch:
permissions:
id-token: write
contents: read
jobs:
inspect:
runs-on: ubuntu-latest
steps:
- name: Print selected OIDC claims
run: |
curl -sS \
-H "Authorization: bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \
"$ACTIONS_ID_TOKEN_REQUEST_URL&audience=sts.amazonaws.com" \
| jq -r .value \
| jq -R 'split(".")[1] | gsub("-";"+") | gsub("_";"/") | @base64d | fromjson
| {sub, repository, ref, ref_protected, event_name, workflow_ref, job_workflow_ref}'Run it once from the caller and once from inside the reusable workflow. Compare workflow_ref and job_workflow_ref in the two outputs.
Getting the claim into a trust decision
Whether you can match a claim directly depends on the cloud provider.
- Some providers expose arbitrary token claims to policy conditions. Google Cloud workload identity federation, for example, lets you write an attribute condition such as
assertion.job_workflow_ref == '...'. - AWS IAM exposes only a fixed set of token claims as condition keys for the GitHub provider. In this tutorial's examples that set is
audandsub. Check the current AWS documentation for your provider before relying on any other key.
When the cloud only lets you match sub, you can still use job_workflow_ref. Change the sub claim template so that it includes the extra claim. The next chapter covers that mechanism in detail. The call below sets it for one repository.
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' \
-f 'include_claim_keys[]=job_workflow_ref'After this change, sub is built from repo, context, and job_workflow_ref, in that order. Run the inspection workflow again and copy the new sub value exactly. It will be longer than the default.
Here is a trust condition that accepts only jobs from the shared deploy workflow on its main branch. It assumes the template above and a run on the caller's main branch. Replace the sub value with the one you copied from your own token.
{
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
},
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:example-org/example-repo:ref:refs/heads/main:job_workflow_ref:example-org/shared-workflows/.github/workflows/deploy.yml@refs/heads/main"
}
}
}The aud check stays. Extra claims add to the earlier checks and never replace them.
Pinning the workflow ref
The @refs/heads/main suffix on job_workflow_ref is part of the value, so match it. A caller chooses which version of a reusable workflow to use, for example uses: example-org/shared-workflows/.github/workflows/deploy.yml@refs/heads/experiment. If your condition matches only the file path, any branch of the shared repository passes, including one a developer pushed an hour ago.
Pin the suffix to a protected branch or a tag you control. Protect that branch in shared-workflows so that changes need review. The trust policy is only as strong as the rules on the workflow it names.
Other claims worth knowing, and their limits
You can decide which of these deserve a place in a trust decision.
repository_ownerlimits trust to one organization. It is a reasonable coarse guard, but it does not replace matching the repository.ref_protectedtells you whether the ref has protection rules. A trust decision based on it depends on settings that repository admins can change, so treat it as a supplement.event_nameseparatespushfrompull_request. Use it to keep pull request runs away from deploy roles.actornames whoever started the run. Avoid using it as a gate. Anyone with write access can trigger runs, and the value changes with each person.runner_environmentcan require GitHub-hosted runners, which are created fresh for each job. This matters if your organization also runs self-hosted runners with longer-lived state.
Common errors
- Policy written from the documentation, not a real token. The claim order and spelling in a custom
submust match exactly. Decode a token and copy it. - Changing the template and forgetting existing roles. Once
use_default=falseis set, the oldsubformat no longer appears. Every trust policy for that repository stops matching until you update it. - Matching a caller instead of a callee.
workflow_refnames the caller. If you want to enforce a reusable workflow, you needjob_workflow_ref. - Trusting an unprotected ref. A pinned
@refs/heads/mainhelps only ifmainin the shared repository requires review. - Assuming the cloud supports the claim. If a condition key is not supported, the condition either fails every request or is rejected. Test with a real run.
Exercise
A platform team's reusable deploy workflow is example-org/shared-workflows/.github/workflows/deploy.yml. A trust policy for the deploy role accepts a sub that includes job_workflow_ref:example-org/shared-workflows/.github/workflows/deploy.yml@*. Name two ways someone could still obtain credentials that the team did not intend, and state the fix.
Show answer
First, any caller can use the workflow at any ref, such as a feature branch in shared-workflows that someone just pushed and that skips the scan step, because the trailing @* accepts every ref. Second, the pattern says nothing restrictive about the calling side beyond what the rest of the sub includes. If the repo and context parts are also wildcarded or missing, any repository can use the role. The fix is to pin the ref to a protected branch or tag, for example @refs/heads/main, to keep exact repo and ref or environment values in the sub, and to protect the branch in shared-workflows with required reviews.