Sub topic 6
Untrusted code, fork pull requests, and public repositories
This chapter assumes you can read a workflow file, know how runs-on labels route jobs, and understand why a reused runner keeps state between jobs. Here we look at who can make your runner execute code, and what that means for public repositories.
A runner executes whatever the workflow says
A workflow job is a set of shell commands. When it lands on a self-hosted runner, those commands run on your machine, inside your network, as the runner's user. Anyone who can change what a job runs can run code there.
A fork pull request is the clearest case. A stranger copies your public repository, edits a workflow file or a build script such as Makefile or package.json, and opens a pull request. If that pull request triggers a job on your runner, their code runs on your host.
Which triggers are risky
| Trigger | Whose workflow file runs | Secrets and token | Risk on a self-hosted runner |
|---|---|---|---|
| `pull_request` from a fork | The merged PR version | No secrets, read-only token | Attacker code still runs on your host |
| `pull_request_target` | The base branch version | Secrets and a write-capable token | Severe if the job checks out PR code |
| `push` to a protected branch | Your branch | Normal access | Lower, since only trusted people can push |
Read-only tokens on fork pull requests protect your repository data. They do nothing to protect the machine, because the job's commands run there regardless.
pull_request_target is the most common mistake. It is meant for tasks such as labeling a pull request, so it runs with the privileges of the base repository. If the job then checks out and builds the pull request's code, untrusted code runs with those privileges.
name: unsafe-example
on: pull_request_target
jobs:
test:
runs-on: [self-hosted, linux]
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
- run: make testDo not copy this workflow. It checks out the attacker's commit and runs the attacker's Makefile on a self-hosted runner with secrets available.
What the attacker gains
On a persistent runner, one malicious job can do lasting damage:
- Read environment variables, cached files, and credentials left by earlier jobs.
- Scan or call internal services that are not reachable from the internet.
- Leave a background process or modified tool behind, so later trusted jobs run compromised.
This is why GitHub's guidance is to avoid self-hosted runners for public repositories. Anyone can fork a public repository, so anyone can attempt this.
Controls that reduce the risk
Apply these in layers. No single one is enough.
- Use GitHub-hosted runners for any workflow triggered by untrusted input. Each job gets a fresh VM that is discarded.
- Keep self-hosted runners for private repositories, and restrict them with runner groups, covered later in this tutorial.
- In repository settings under Actions, require approval before workflows run for outside contributors.
- Never combine
pull_request_targetwith a checkout of pull request code on a self-hosted runner. - Set minimal
permissionsin every workflow. - Use ephemeral runners, so a compromised job cannot affect the next one. This limits persistence, but it does not stop the first job from using the network or any credentials it can reach.
For private repositories where forks are allowed, you can also gate the job on the pull request's origin:
name: test
on: pull_request
permissions:
contents: read
jobs:
test:
if: github.event.pull_request.head.repo.full_name == github.repository
runs-on: [self-hosted, linux, x64, ephemeral]
steps:
- uses: actions/checkout@v4
- run: make testThe if condition skips the job when the pull request comes from a fork. It is a useful backstop, but it does not replace runner group restrictions.
Exercise
A public repository has a self-hosted runner. A workflow uses pull_request_target, checks out the pull request head commit, and runs make test. Name two things an attacker can do, and give one change that removes the main danger.
Show answer
The attacker can edit the Makefile so make test reads secrets available to the job and sends them out, and can reach internal network services from the runner host. The main fix is to stop running pull request code under pull_request_target on a self-hosted runner. Use the pull_request trigger on a GitHub-hosted runner instead.