Sub topic 14
Untrusted code and pull_request_target
This chapter assumes you know the permissions key, the difference between workflow-level and job-level permissions, and how fork pull requests normally get a read-only token.
Why pull_request_target is different
A pull_request workflow from a fork runs the fork's version of the workflow. It gets a read-only token and no secrets. This is safe by design.
A pull_request_target workflow runs the workflow file from the base branch, in the context of the base repository. Even for a fork PR, it can have a token with write access and can read repository secrets. GitHub built it for tasks like labeling and commenting on PRs from forks, where write access is needed.
The workflow file is trusted, but the PR's content is not. The risk appears when the workflow mixes the two.
The dangerous pattern
This workflow checks out the PR head and runs its build. Anyone who can open a PR can change npm test or package.json scripts to run arbitrary commands with the privileged token:
name: pr-build
on: pull_request_target
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
- run: npm ci && npm testChecking out the PR code and then running any part of it is the core mistake. "Running" includes build tools, test runners, package install scripts, and local actions from the checked-out tree.
Safer patterns
Keep pull_request_target jobs small. Declare minimal permissions and never check out PR code. This labeler reads only metadata and its config from the base branch:
name: triage
on: pull_request_target
permissions: {}
jobs:
label:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- uses: actions/labeler@v5If you must build or test PR code, use these options:
- Run it under plain
pull_request, where the fork gets a read-only token and no secrets. - Split the work. Let the untrusted build run under
pull_requestand upload its results as an artifact. A second workflow triggered byworkflow_rundownloads the artifact and does the privileged step, such as posting a comment. Treat the artifact as untrusted data, and validate it before using it. - Require a maintainer to approve first, for example by running only after a trusted user adds a label.
Untrusted text is also input
PR titles, branch names, and bodies are controlled by the author. Expanding them inside a run: script lets an attacker inject shell commands. Pass them through an environment variable instead:
- env:
TITLE: ${{ github.event.pull_request.title }}
run: echo "$TITLE"The shell now treats the title as data, not as code.
Checklist
- Set
permissions: {}at workflow level and grant scopes per job. - Do not check out
github.event.pull_request.headin apull_request_targetjob unless nothing from it is executed. - Move untrusted builds to
pull_requestor to aworkflow_runsplit. - Pass event text through
env:, not directly into scripts.
Exercise
A pull_request_target workflow has permissions: contents: read only. It checks out the PR head and runs make test. A teammate says it is safe because the token is read-only. What is wrong with that reasoning?
Show answer
The PR's code runs in a privileged context. A malicious Makefile can read repository secrets available to the job and use the read-only token to fetch private content. It can also tamper with caches or artifacts. Reducing permissions limits the damage but does not remove the risk. The fix is to run the tests under pull_request, or to split the work with workflow_run.