Sub topic 9
Pwn requests: checking out PR code in pull_request_target
This chapter covers the most damaging pull_request_target mistake: checking out and running code from a pull request in a privileged job. It assumes you know basic YAML, the structure of a workflow (events, jobs, steps), what forks and pull requests are, and basic shell scripting. It also assumes you have read the earlier chapter that compares pull_request and pull_request_target.
What a pwn request is
A pwn request is a workflow that runs untrusted code from a pull request while holding privileges the author of that pull request should not have. The name comes from security researcher Jaroslav Lobačevski, who described the pattern in 2021.
Two facts combine to create it:
pull_request_targetruns in the context of the base repository. The job gets aGITHUB_TOKENthat can have write access, and it can read repository and organization secrets. This is true even when the pull request comes from a fork.- By default,
actions/checkoutin that trigger checks out the base branch, not the pull request. That default is safe. The workflow YAML comes from the base branch, so the attacker cannot edit it.
The mistake is overriding the default so the job checks out the pull request's commit, and then running something from that tree. The attacker now controls the code that runs inside a job that holds secrets.
The vulnerable pattern
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 testThis workflow looks like an ordinary CI job. It is exploitable because npm ci and npm test execute files that the fork's author wrote. The author does not need to touch the workflow file. Any of the following in the fork is enough:
- A
preinstall,installorpostinstallscript inpackage.json.npm ciruns these automatically. - A changed
testscript inpackage.json, or a modified test file. - A malicious dependency listed in
package.jsonor the lock file. - A
Makefile,setup.py,build.rs,conftest.py, Gradle script, or any other file a build tool runs.
An attacker only has to open a pull request. No maintainer approval is needed in many configurations, because pull_request_target workflows are not held back by the first-time-contributor approval prompt in the same way as fork pull_request runs. The workflow file is read from the base branch, and that branch is trusted.
What the attacker gets
Code running in this job can:
- Read any secret the workflow passes to a step, and any secret stored in files the job writes, such as
.npmrccreated from a token. - Use the
GITHUB_TOKENwith whatever permissions the job holds. If you did not setpermissions:, the default depends on the repository or organization setting, and may include write access to contents, issues and pull requests. - Read the token that
actions/checkoutpersisted in.git/config. By default, checkout stores the token there so latergitcommands can authenticate. Code in the same job can read that file. - Push commits or tags, create releases, change labels, or comment, depending on the token's scope.
- Poison the Actions cache, which later privileged runs may restore.
The attacker does not need to exfiltrate in a noisy way. A single request to a server they control, carrying $GITHUB_TOKEN or the contents of .git/config, is enough. Secrets are masked in logs, but masking only hides exact string matches in output. It does not stop network exfiltration, and an attacker can encode a value to defeat the log filter.
The diagram
The diagram shows which parts of the job the attacker controls and where privileges sit.
Variations that are still pwn requests
The ref: line is the obvious sign, but several variants are equally unsafe.
| Variant | Why it is unsafe |
|---|---|
| `ref: ${{ github.event.pull_request.head.ref }}` | Uses the branch name. The attacker controls the branch and can push new commits to it after the run starts. |
| `ref: refs/pull/${{ github.event.number }}/merge` | Checks out the merge commit, which includes the attacker's changes. |
| `repository: ${{ github.event.pull_request.head.repo.full_name }}` with a `ref` | Checks out the fork repository directly. |
| `gh pr checkout` or `git fetch` of the PR ref in a `run` step | Same result without using `actions/checkout`'s `ref:` input. |
| Checkout of base code, then `npm install` of a package path taken from the PR | The PR still decides what gets executed. |
| A local action (`uses: ./.github/actions/foo`) after checking out PR code | The action's `action.yml` and scripts come from the fork. |
Using the commit SHA (head.sha) rather than the branch name is better than using the branch name. A branch can change between the trigger and the checkout, so a reviewer could look at one commit and the job could run another. A SHA is fixed. But a SHA does not make the code trustworthy. It only makes the code stable.
Code does not have to be "run" to be dangerous
Teams sometimes reason that they only lint or build, never "execute" anything. Most build and lint tools run code from the repository:
- Linters and formatters load config files that can be JavaScript or Python.
- Test runners import test files.
- Package managers run lifecycle scripts.
- Static analysis tools may load plugins named in the config.
- A
Dockerfilebuild runs arbitraryRUNcommands.
Even read-only operations on PR content can be risky if they feed results into later steps, for example through $GITHUB_ENV (covered in a later chapter).
The fix: use the unprivileged trigger
For build and test jobs, use pull_request. Fork runs of pull_request get a read-only token and no repository secrets, so malicious code has nothing valuable to steal.
name: ci
on: pull_request
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm testThree things changed:
- The trigger is
pull_request. Checkout now defaults to the merge commit of the pull request, so theref:override is unnecessary. permissions: contents: readmakes the token read-only for every job, regardless of repository defaults.- No secrets are referenced. Code that does not need secrets should not be given them.
When you need privileges after untrusted code
Some tasks truly need write access: commenting test results on the pull request, labeling it, or publishing a preview. The safe design is to split the work:
- An unprivileged
pull_requestworkflow builds and tests the fork code, then uploads the results as an artifact. - A separate privileged workflow, triggered by
workflow_run, downloads the artifact and uses the token to comment or label. It treats the artifact as untrusted data and never executes it.
The next chapters cover this split and the care needed with artifacts.
If you must use pull_request_target and read files from the PR, limit the job to inspecting them as text and keep three rules:
- Run no scripts, builds, installers or tests from the PR tree.
- Set
persist-credentials: falseonactions/checkout, so the token is not left in.git/config. - Grant only the permissions that the job needs.
name: label-docs
on:
pull_request_target:
types: [opened, synchronize]
permissions:
contents: read
pull-requests: write
jobs:
label:
runs-on: ubuntu-latest
steps:
- uses: actions/labeler@v5The labeler action reads the list of changed file paths from the API. It does not execute anything from the fork. Checkout of the fork is not needed at all.
Reviewing a workflow for this flaw
Use this checklist on each workflow whose trigger is pull_request_target, and on workflow_run:
- Does any step check out the PR head, by
ref,repository,gh pr checkoutorgit fetch? - Does any later step run, import, build or install something from that checkout?
- Does the job have secrets or a write-capable token?
- Are
permissionsset explicitly to the minimum?
If the answers to 1 and 2 are both yes, you have a pwn request, whatever the answer to 3. Even a token with limited scope can be used to pivot, for example by commenting with malicious links or changing labels that trigger other workflows.
Exercise
A workflow uses on: pull_request_target. It runs actions/checkout@v4 with ref: ${{ github.event.pull_request.head.sha }}, then runs pip install -r requirements.txt && pytest. The job has permissions: contents: read and no secrets. A teammate says it is safe because of this. Is it? What should you change?
Show answer
It is still a pwn request. The fork's requirements.txt can name a malicious package, and pytest imports the fork's conftest.py and test files, so attacker code runs in the job. Read-only permissions limit the damage but do not remove it. The job can still read .git/config if credentials were persisted, use the network to attack internal services, and poison the Actions cache. Change the trigger to pull_request so the code runs with no secrets and a read-only fork token, and drop the ref: override because checkout then uses the merge commit by default. If you need to post results, do it in a separate workflow_run job that only reads an artifact as data.
Exercise
Why is checking out github.event.pull_request.head.sha better than head.ref, and why does it still not make a pull_request_target job safe?
Show answer
A SHA identifies one fixed commit, while a branch name can point to new commits after the workflow starts, so the code that runs may differ from the code a reviewer saw. A SHA only fixes which code runs. The code is still written by the fork's author, and it still runs in a job with the base repository's token and secrets.