Sub topic 11
Handling pull request and tag subjects
This chapter assumes you know how a GitHub Actions workflow is triggered and how the sub claim is built for branches and environments from earlier chapters. It also assumes you can read an AWS IAM trust policy and know what a Git tag is. Here you will learn what the subject looks like when a job runs for a pull request or a tag, and how to write trust policies for those cases without opening the role too wide.
Why these two subjects need separate treatment
A branch subject such as repo:example-org/example-repo:ref:refs/heads/main names a ref that your team controls through branch protection. Pull requests and tags are different:
- Pull requests are created by many people, and the code they run is the code in the proposed change.
- Tags are cheap to create. Anyone with write access to the repository can push one, and by default a tag can point at any commit.
If you allow these subjects with a loose pattern, you may let untrusted or unreviewed code assume a role. You should decide on purpose which roles these subjects may reach.
The pull request subject
When a workflow runs on the pull_request event, GitHub sets the subject to:
repo:example-org/example-repo:pull_requestThe subject does not include the pull request number, the source branch, or the author. Every pull request in the repository produces the same value. A trust policy cannot tell a reviewed change from one that was opened five minutes ago.
This has a practical consequence. The workflow file used for a pull_request run is the one in the merge commit, which includes the pull request's own changes. A contributor with write access can edit the workflow in their branch, open a pull request, and have the edited workflow request a token with this subject. Whatever the role allows, that edited workflow can do.
So treat the pull request subject as low trust:
- Give it a separate role from your deploy role.
- Grant only what a pull request needs, such as read access to a state bucket for
terraform plan. - Never grant write or deploy permissions to this subject.
Pull requests from forks
For a pull request from a fork, GitHub gives the GITHUB_TOKEN read-only permissions and does not allow the job to request an OIDC token. The id-token: write permission is not available there, so the role cannot be assumed. This protects you by default. The failure mode is a workflow that works for team members and fails for outside contributors. That is expected, so skip the cloud step for forks instead of loosening the trust policy.
A read-only role for pull requests
This trust policy accepts only the pull request subject:
{
"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:example-org/example-repo:pull_request"
}
}
}
]
}Attach only a read-only permissions policy to this role, for example ReadOnlyAccess scoped to the resources the plan step needs. The trust policy says who may assume the role. The permissions policy says what they can do after that, so both need to be narrow.
`pull_request_target` is different
The pull_request_target event runs in the context of the base branch, not the merge commit. For that event the subject is built from the base ref. It can look like a branch subject, such as repo:example-org/example-repo:ref:refs/heads/main, not like :pull_request.
This is a known trap. A trust policy that allows main can therefore be satisfied by a job triggered by a pull request. If that workflow also checks out and runs the pull request's code, untrusted code runs with the main role. Avoid combining pull_request_target with a checkout of the head ref and cloud credentials. If you must use that event, keep the role read-only and do not run code from the pull request.
The tag subject
When a workflow is triggered by a tag push, the subject names the tag ref:
repo:example-org/example-repo:ref:refs/tags/v1.4.0Tags are a common release trigger, so you often want a role that only a release tag can assume. There are two ways to match it.
Exact tag
An exact match works for a single known tag, but release tags change every time, so it is rarely practical:
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": "repo:example-org/example-repo:ref:refs/tags/v1.4.0"
}Tag pattern
A pattern with StringLike covers all release tags:
{
"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*"
}
}
}The * in StringLike matches any run of characters, including / and .. This pattern matches v1.4.0, but it also matches v-test and vandal. Keep the prefix as specific as your naming allows. A pattern like ref:refs/tags/* accepts every tag in the repository.
Who can create a tag
The trust policy checks the name of the tag, not who created it or which commit it points to. Anyone who can push a tag matching v* can run a workflow that assumes the role. To make the tag subject meaningful:
- Restrict tag creation with a repository ruleset or tag protection so only maintainers can create
v*tags. - Pair the tag trigger with an environment. When a job declares
environment: production, the subject becomesrepo:example-org/example-repo:environment:production, whatever event started the run. The environment's approval and branch or tag rules then act as the gate.
Environment-based trust is usually stronger than tag-based trust for production, because the controls live in GitHub settings that reviewers can audit.
How the event changes the subject
The subject depends on the event and on whether the job uses an environment. This diagram shows the decision.
Because an environment overrides the event, a job in production never presents a branch or tag subject. A role trusting only environment:production ignores the branch and tag subjects.
A workflow that deploys on tags
This workflow runs for tags that match v*, and it assumes a release role. It declares no environment, so the subject is the tag subject.
name: release
on:
push:
tags: ["v*"]
permissions:
id-token: write
contents: read
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/example-release-role
aws-region: us-east-1
- run: aws sts get-caller-identityThe on.push.tags filter is a convenience and not a security control. The role's trust policy must enforce the tag pattern itself, because a modified workflow file on another branch could drop the filter.
Allowing several subjects in one role
A role sometimes needs more than one subject, for example main and release tags. StringLike accepts a list, and the condition passes if any value matches:
{
"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",
"repo:example-org/example-repo:ref:refs/tags/v*"
]
}
}
}Think before combining. A role that serves two subjects must have permissions that are safe for the weaker of the two. If tags are easier to create than merges to main, the combined role is only as safe as tag creation. Separate roles for separate trust levels are often clearer.
Common mistakes in this area
| Mistake | Effect | Fix |
|---|---|---|
| Trusting `repo:example-org/example-repo:*` | Pull requests, every branch, and every tag can assume the role | List the exact subjects you need |
| Giving the `pull_request` subject a deploy role | Any edited workflow in a pull request can deploy | Use a separate read-only role for pull requests |
| Allowing `refs/tags/*` | Every tag, including accidental ones, can release | Use a specific prefix such as `v*` and protect tag creation |
| Using `pull_request_target` with the `main` role | Pull request code can run as `main` | Avoid checking out head code, or keep the role read-only |
| Relying only on the workflow `on:` filter | A changed workflow file bypasses it | Enforce the subject in the trust policy |
Exercise
A trust policy allows only repo:example-org/example-repo:ref:refs/heads/main. A team member opens a pull request using the pull_request event and the job requests a token. Can the job assume the role, and why?
Show answer
No. For the pull_request event the subject is repo:example-org/example-repo:pull_request, which does not equal the main branch subject, so the StringEquals condition fails. The case to watch for is pull_request_target, where the subject is built from the base ref and can match the main subject.
Exercise
Write the Condition block for a role that only jobs triggered by tags starting with release- in example-org/example-repo may assume. Include the audience check.
Show answer
Use StringEquals for the audience and StringLike for the subject:
"Condition": { "StringEquals": { "token.actions.githubusercontent.com:aud": "sts.amazonaws.com" }, "StringLike": { "token.actions.githubusercontent.com:sub": "repo:example-org/example-repo:ref:refs/tags/release-" } }
Pair it with a ruleset that limits who can create `release-` tags.
Exercise
A job sets environment: production and is triggered by a push to main. Which subject does it present, and would a role that trusts only the main branch subject accept it?
Show answer
It presents repo:example-org/example-repo:environment:production, because the environment replaces the event-based form. A role trusting only the main branch subject rejects it. The role would need to trust the environment subject.