Sub topic 7
pull_request vs pull_request_target
This chapter assumes you can read a workflow file and know what a fork is. It also assumes you know that GITHUB_TOKEN is a short-lived credential GitHub gives each workflow run. Here you compare the two triggers that start a workflow when a pull request opens, and you learn which one is safe for running contributed code.
Two triggers, two trust levels
Both triggers fire on pull request events. They differ in which version of the code and which privileges the run receives.
| Property | `pull_request` | `pull_request_target` |
|---|---|---|
| Workflow file used | The merge result of the PR (head merged into base) | The base branch, so the PR cannot change it |
| Default checkout | The PR merge commit | The base branch |
| Token for fork PRs | Read-only | Read/write, as configured by the base repository |
| Repository secrets for fork PRs | Not available | Available |
| Intended use | Build and test contributions | Label, comment on, or triage PRs |
The pull_request_target trigger exists so that a workflow can write to a pull request from a fork, for example to add a label. A read-only token cannot do that. GitHub therefore runs the workflow in the context of the base repository, with its secrets and a write-capable token.
Why the difference matters
A workflow file from the base branch is trusted code, because maintainers reviewed it. The files in a fork's pull request are not trusted. The danger comes from combining the two: privileged context plus untrusted content.
Under pull_request, fork code runs, but with almost nothing to steal. Under pull_request_target, the context is rich, so the workflow must never execute anything the contributor wrote. That includes install scripts, test files, build scripts, and anything run through npm ci && npm test.
The safe default for building and testing
To build and test a contribution, use pull_request and declare the minimum permissions:
name: ci
on: pull_request
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm testEven if the fork's package.json contains a malicious install script, it runs with a read-only token and no secrets.
The unsafe combination
This workflow uses pull_request_target and then checks out the fork's head commit:
name: ci
on: pull_request_target
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
- run: npm ci && npm testThe ref input tells actions/checkout to fetch the attacker's code. The next step then runs it inside a job that holds the base repository's secrets and a write token. This pattern is called a pwn request, and the next chapters cover it in detail.
Choosing a trigger
- If the job must build, test, or lint contributed code, use
pull_request. - If the job only needs to write to the PR (labels, comments) and never runs PR code,
pull_request_targetis acceptable. Keeppermissionsminimal. - If you need both, split the work into two workflows. Build unprivileged, then act on the result in a privileged one. A later chapter covers this design.
Exercise
A workflow uses on: pull_request_target, checks out ${{ github.event.pull_request.head.sha }}, and runs npm test. Why is this dangerous, and which single change to the trigger makes it safe for testing fork code?
Show answer
The job runs fork-controlled code (install scripts, tests) with the base repository's secrets and a write-capable token, so a contributor can steal secrets or modify the repository. Changing the trigger to pull_request makes the fork code run with a read-only token and no secrets. Adding permissions: contents: read tightens it further.