Sub topic 5
The subject claim: who is asking?
This chapter assumes you know basic GitHub Actions syntax, what a JWT is, and how an IAM trust policy grants access. The sub (subject) claim is the field a cloud provider most often uses to decide whether a workflow may assume a role.
What the subject claim does
Every claim in the token describes the run in some way. The sub claim is different because the cloud provider matches it directly in trust conditions. It packs the repository and the triggering context into one string. A trust policy can then ask one question: is this the repository and context I expect?
If you skip a sub condition, any repository that can send the right audience may be able to assume the role. The aud and iss checks alone do not tell the cloud which repository is calling.
Default formats
By default, GitHub builds the subject from the repository and the context that started the job. The format depends on the trigger and on whether the job uses an environment.
| Context | Subject format |
|---|---|
| Branch push | `repo:octo-org/octo-repo:ref:refs/heads/main` |
| Tag push | `repo:octo-org/octo-repo:ref:refs/tags/v1.0.0` |
| Pull request event | `repo:octo-org/octo-repo:pull_request` |
| Job that uses an environment | `repo:octo-org/octo-repo:environment:production` |
Two details cause frequent confusion:
- When a job references an environment, the subject uses the environment name. The branch is no longer in the subject. A policy that expects
ref:refs/heads/mainwill reject that job. - Pull request runs use
pull_requestwith no branch name. Their subject is the same for every pull request in the repository.
Reading the claim in context
The abbreviated token from the previous chapter contained this:
{
"iss": "https://token.actions.githubusercontent.com",
"aud": "sts.amazonaws.com",
"sub": "repo:octo-org/octo-repo:ref:refs/heads/main",
"repository": "octo-org/octo-repo",
"ref": "refs/heads/main",
"event_name": "push"
}The sub value repeats information that also appears in repository and ref. The subject exists because trust policies in most clouds can only match a small set of standard claims, and sub is always among them.
Matching the subject in a trust policy
The trust policy from the AWS example uses an exact match:
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": "repo:octo-org/octo-repo:ref:refs/heads/main"
}
}StringEquals accepts only that one subject. A push to main in octo-org/octo-repo passes. A push to any other branch fails, and so does any run from a different repository.
To allow several subjects, use StringLike with a wildcard, or list several values:
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:octo-org/octo-repo:*"
}This pattern lets every branch, tag, pull request, and environment in the repository assume the role. That is often wider than you intend, so choose the narrowest pattern that works.
The subject as a decision
The diagram shows how the context of a run produces its subject.
Common mistakes
- Using a wildcard such as
repo:octo-org/*lets every repository in the organization assume the role. - Matching a branch subject while the job also sets
environment:. The subject changes to the environment form. - Forgetting that pull requests share one subject, so a policy cannot tell one pull request from another.
Exercise
A job in octo-org/octo-repo runs on a push to main and declares environment: production. The role's trust policy requires sub to equal repo:octo-org/octo-repo:ref:refs/heads/main. Will the job assume the role, and why?
Show answer
No. Because the job uses an environment, the subject is repo:octo-org/octo-repo:environment:production, which does not match the branch-based value. Change the policy to the environment subject, or remove the environment from the job.