Sub topic 9
Pwn requests: checking out PR code in pull_request_target
This chapter assumes you know the basics of workflow YAML, forks, and the difference between the pull_request and pull_request_target triggers. It also assumes you know what GITHUB_TOKEN permissions are.
Why this pattern is dangerous
A pwn request happens when a workflow triggered by pull_request_target checks out code from the pull request and then runs it. The trigger runs in the context of the base repository. That means the job can get a token with write access and the repository's secrets, even when the pull request comes from a stranger's fork.
Checking out the fork's code puts attacker-controlled files into that privileged job. Anything that executes them runs with those privileges.
A vulnerable workflow
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 at the fork's commit. The next step then runs npm ci and npm test. Both commands execute code the attacker wrote.
How the attack works
The attacker does not need to change your workflow file. For pull_request_target, GitHub uses the workflow definition from the base branch. The attacker only needs to change files that your steps run.
Common entry points include:
preinstallandpostinstallscripts inpackage.json- test files and test configuration
- build scripts,
Makefiletargets, andsetup.py - local actions referenced with
uses: ./
Any of these can read secrets from the environment or use the token to push code, edit releases, or comment on issues.
The safe version
Use pull_request for building and testing contributions. Fork code then runs with a read-only token and no repository secrets.
name: ci
on: pull_request
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm testHere actions/checkout fetches the merge commit by default, and nothing privileged is available to it.
Spotting the pattern
Review any workflow that uses pull_request_target. It is risky when both of these hold:
- A checkout step uses
github.event.pull_request.head.sha,head.ref, or the head repository. - A later step runs code from that checkout, such as a package install, a test command, a script, or a local action.
Checking out the head is not harmful on its own. Running what you checked out is the problem. Even steps that look passive, like npm ci, execute lifecycle scripts.
When you need pull_request_target
Some jobs, such as labeling or commenting on PRs, need write access but do not need the PR's code. Keep them on pull_request_target, and do not check out the head. If you must build the code, build it in an unprivileged pull_request workflow and hand results to a separate privileged workflow. The next chapters cover that design.
Exercise
A workflow uses on: pull_request_target, runs actions/checkout with ref: ${{ github.event.pull_request.head.sha }}, and then runs npm test. Why is it unsafe, and what is the simplest fix if the job only builds and tests the code?
Show answer
The job runs with the base repository's secrets and a write-capable token, and npm test executes code from the fork, so an attacker can read secrets or misuse the token. The simplest fix is to switch the trigger to pull_request and set permissions: contents: read, so fork code runs with a read-only token and no secrets.