Sub topic 10
Safe designs: split unprivileged and privileged workflows
This chapter assumes you know the pull_request and pull_request_target triggers, the permissions key, and why a pwn request is dangerous. Here you will see the standard fix: separate the code that runs untrusted input from the code that holds privileges.
The core idea
A job that runs fork code should have no secrets and a read-only token. A job that has secrets or write access should never run fork code. If a task needs both, such as commenting test results on a pull request, split it into two workflows that share only a small, validated piece of data.
| Workflow | Trigger | Runs fork code? | Token and secrets |
|---|---|---|---|
| Unprivileged | `pull_request` | Yes | Read-only, no secrets |
| Privileged | `workflow_run` or `pull_request_target` | No | Write access, secrets available |
Step 1: build without privileges
This workflow runs the fork's code. It has contents: read and receives no repository secrets for fork pull requests. It saves the pull request number as an artifact.
name: ci
on: pull_request
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm test
- run: echo "${{ github.event.number }}" > pr-number.txt
- uses: actions/upload-artifact@v4
with:
name: pr-number
path: pr-number.txtgithub.event.number is an integer assigned by GitHub, so interpolating it is safe. Do not apply the same pattern to free-text fields like titles.
Step 2: report with privileges
The second workflow starts when ci finishes. A workflow_run workflow always runs from the default branch's version of the file, so a fork cannot change its definition. It never checks out or executes the pull request's code.
name: ci-report
on:
workflow_run:
workflows: [ci]
types: [completed]
permissions:
actions: read
pull-requests: write
jobs:
comment:
runs-on: ubuntu-latest
if: github.event.workflow_run.conclusion == 'success'
steps:
- uses: actions/download-artifact@v4
with:
name: pr-number
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ github.token }}
- env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
run: |
pr="$(cat pr-number.txt)"
if ! [[ "$pr" =~ ^[0-9]+$ ]]; then
echo "Unexpected artifact content" >&2
exit 1
fi
gh pr comment "$pr" --body "Tests passed."The artifact comes from untrusted code, so the job treats it as untrusted data. It checks that the content is digits only before using it, and it passes values through env instead of interpolating them into the script. A malicious build could write anything into pr-number.txt. The validation limits the damage to a comment on the wrong pull request number. The next chapter covers artifact trust in more detail.
Rules to keep the split safe
- Never check out
github.event.pull_request.head.*in a privileged job. If the job only labels or comments, it needs no checkout at all. - Set
permissionsexplicitly in every workflow. Grant write scopes only to the privileged job, not the whole workflow. - Keep the privileged side small. It should read, validate, and act, not build.
- Treat every file and string that crosses the boundary as attacker-controlled, even though your own workflow produced it.
Exercise
A privileged workflow_run job downloads an artifact named report from the fork's CI run and executes bash report/run.sh to post results. Why is this unsafe, and what should it do instead?
Show answer
The artifact was produced by fork-controlled code, so run.sh is attacker-controlled and executes with the privileged token and secrets. Instead, the CI job should write only plain data, such as a number or a short text file. The privileged job should validate that data (for example with a strict regular expression) and use it only as a value, passed through an environment variable, never as code to run.