Sub topic 11
Handling pull request and tag subjects
This chapter assumes you can read a sub claim and write a trust policy with StringEquals and StringLike. Branch and environment subjects are covered earlier. Pull requests and tags have their own subject formats, and they are easy to trust by accident.
The four subject shapes
The sub claim depends on what triggered the job and whether the job names an environment.
| Trigger | Subject value |
|---|---|
| Push to a branch | `repo:example-org/example-repo:ref:refs/heads/main` |
| Push of a tag | `repo:example-org/example-repo:ref:refs/tags/v1.2.0` |
| `pull_request` event | `repo:example-org/example-repo:pull_request` |
| Job with an `environment:` key | `repo:example-org/example-repo:environment:production` |
The environment check comes first. A job that declares environment: production gets the environment subject even if a pull request triggered it. The branch or tag no longer appears in sub.
Pull requests
A pull request subject carries no branch name. A policy that matches refs/heads/main therefore rejects pull request jobs, which is usually what you want for a deploy role.
Some roles should be reachable from pull requests, such as a read-only role for terraform plan. Give that role its own trust policy with an exact match on the pull request subject:
{
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": "repo:example-org/example-repo:pull_request"
}
}
}Attach only read permissions to this role. Anyone who can open a pull request in the repository can run workflow code that assumes it. For pull requests from forks, GitHub does not grant id-token: write to the pull_request event, so those jobs cannot get a token at all.
Be careful with pull_request_target. It runs in the context of the base branch, so its subject looks like a branch push to main. A main-only trust policy does not exclude it. Do not check out and run untrusted pull request code in that event.
Tags
To allow releases from version tags, use StringLike with a narrow prefix:
{
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
},
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:example-org/example-repo:ref:refs/tags/v*"
}
}
}This accepts v1.2.0 and v2.0.0-rc1. It also accepts any tag that starts with v, such as vandalism. The trust policy only checks the tag name, not who created the tag. Anyone with write access can push a matching tag from any commit. Restrict who can create tags with a tag ruleset or protected tags, or have the tag-triggered job deploy through a protected environment.
Choosing between patterns
- Use an exact
pull_requestmatch only for read-only roles. - Use an exact
refs/heads/mainor environment match for roles that write to production. - Use
refs/tags/v*only when tag creation is restricted. - Keep a separate role per trust level. Do not widen one role's policy to cover several.
Exercise
A trust policy requires sub to equal repo:example-org/example-repo:ref:refs/heads/main. A workflow triggered by a pull request has a job with environment: staging. Can that job assume the role? What sub does it present?
Show answer
No. Because the job names an environment, its subject is repo:example-org/example-repo:environment:staging, which does not equal the branch subject. The pull request trigger does not change this, since the environment form replaces the branch, tag, and pull request forms.
Exercise
Your release role trusts repo:example-org/example-repo:ref:refs/tags/v* through StringLike. Name one risk and one control that reduces it.
Show answer
Anyone with write access can push a tag starting with v on any commit and get the role. A tag protection rule or ruleset that limits who can create v* tags reduces the risk. Routing the job through a protected environment also works.