Sub topic 10
Safe designs: split unprivileged and privileged workflows
This chapter assumes you know basic workflow YAML, the difference between pull_request and pull_request_target, how GITHUB_TOKEN permissions work, and why ${{ }} expansion in a run step is dangerous. The earlier chapters showed what goes wrong. Here you will learn the design that avoids it.
The core idea
Some jobs need to run code you don't trust, such as a fork's npm test. Other jobs need privileges, such as writing a PR comment, adding a label, or reading a secret. The unsafe designs from earlier chapters put both in one job. The safe design keeps them in separate workflows and passes only data between them.
- The unprivileged workflow runs on
pull_request. Fork code runs there with a read-only token and no secrets. - The privileged workflow runs from the base repository's own code. It never checks out or executes fork code. It reads a small, validated result and performs one narrow action.
The boundary between the two is an artifact. Anyone who controls the unprivileged job controls every byte of that artifact, so the privileged side must treat it as untrusted input.
Why the split works
pull_request_target and workflow_run both run the workflow file from the base repository's default branch. A contributor cannot change that file by editing their fork. The privileged workflow is therefore only as dangerous as the data it reads and the commands it runs on that data.
The split is safe only if the privileged side keeps three rules:
- It does not check out the PR head or run its code.
- It does not build shell commands or script source from artifact contents.
- It requests only the permissions its one action needs.
| Property | Unprivileged workflow | Privileged workflow |
|---|---|---|
| Trigger | `pull_request` | `workflow_run` (or `pull_request_target` without checkout) |
| Code that runs | Fork's code | Base repository's code only |
| Token | Read-only for fork PRs | Scoped write, such as `pull-requests: write` |
| Secrets | None for fork PRs | Available, used only by trusted steps |
| Input trust | Anything in the PR | Artifact contents are attacker-controlled |
Step 1: the unprivileged workflow
This workflow does the risky part: installing dependencies and running tests from the fork. Its token is read-only, so even a malicious install script cannot write to the repository. It saves the PR number and result as plain text files.
name: ci
on: pull_request
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm test
- name: Save result
if: always()
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
JOB_STATUS: ${{ job.status }}
run: |
mkdir -p out
echo "$PR_NUMBER" > out/pr_number
echo "$JOB_STATUS" > out/status
- uses: actions/upload-artifact@v4
if: always()
with:
name: ci-result
path: out/Both values go through env, as in the earlier chapter on environment variables. The PR number is an integer that GitHub assigns, but the habit costs nothing. Also note that a malicious test run can overwrite out/ with anything. Code in the job can write to those files after your step runs, or replace them before upload. Do not treat the file contents as trustworthy.
Step 2: the privileged workflow
The privileged workflow triggers when ci completes. It downloads the artifact from that run, validates it, and posts a comment.
name: ci-report
on:
workflow_run:
workflows: [ci]
types: [completed]
permissions:
actions: read
pull-requests: write
jobs:
report:
runs-on: ubuntu-latest
if: github.event.workflow_run.event == 'pull_request'
steps:
- uses: actions/download-artifact@v4
with:
name: ci-result
path: out
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ github.token }}
- uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const pr = Number(fs.readFileSync('out/pr_number', 'utf8').trim());
const status = fs.readFileSync('out/status', 'utf8').trim();
if (!Number.isInteger(pr) || pr <= 0) {
throw new Error('Invalid PR number in artifact');
}
if (!['success', 'failure', 'cancelled'].includes(status)) {
throw new Error('Invalid status in artifact');
}
const { data: pull } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr,
});
if (pull.head.sha !== context.payload.workflow_run.head_sha) {
throw new Error('Artifact PR does not match the tested commit');
}
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr,
body: `CI finished with status: ${status}`,
});Walk through what keeps this safe:
- No checkout of PR code. The only repository content that runs is this workflow file itself.
- No expressions inside
script. The values come from files read at runtime withfs.readFileSync, not from${{ }}text pasted into the JavaScript. Onlyrun-iduses an expression, in a plainwithinput that is not executed as code. - Allow-list validation. The PR number must be a positive integer. The status must be one of three known strings. Free-form text from the artifact never reaches the comment body.
- Cross-check against trusted data. An attacker could write someone else's PR number into the artifact. Comparing the PR's current head SHA with
workflow_run.head_sha, which GitHub supplies, confirms the artifact describes the commit that was tested. - Narrow permissions.
actions: readallows the artifact download.pull-requests: writeallows the comment. Everything else isnone, because settingpermissionsremoves the defaults.
Why not just use `pull_request_target`?
You can do the same job in one pull_request_target workflow if it never checks out the PR head. A labeler that only reads PR metadata through the API is a good example. The split design is better when the useful work needs fork code to run, because a single workflow cannot both run that code and hold privileges.
Use this rule of thumb:
- If the job runs, builds, installs, or tests anything from the fork, use
pull_requestwith a read-only token. - If the job only calls the GitHub API using metadata, a privileged trigger with no checkout is acceptable. Handle metadata as untrusted and pass it through
env. - If the job needs both, split it and pass validated data through an artifact.
Failure modes in split designs
The split is easy to undo by accident. Watch for these errors.
Executing artifact contents. If the privileged job runs a script from the artifact, or sources a file from it, you have recreated the pwn request with an extra step. Treat artifacts as data only.
Interpolating artifact text. A line such as run: echo "${{ steps.read.outputs.title }}" where the output came from the artifact is script injection again. Read values inside the script runtime or pass them through env, and validate them first.
Writing artifact values to GITHUB_ENV. An artifact line like FOO=bar appended to GITHUB_ENV lets the attacker set variables such as NODE_OPTIONS or LD_PRELOAD for later steps. Never copy untrusted content into GITHUB_ENV or GITHUB_OUTPUT without strict validation.
Checking out the head in the privileged job. Adding actions/checkout with ref: ${{ github.event.workflow_run.head_sha }} to the report workflow gives the attacker's code the write token. If a privileged job really needs files from the PR, read them through the API as data instead.
Over-broad permissions. Using permissions: write-all, or leaving the repository default permissive, gives an exploited privileged job far more reach. Declare permissions per workflow, or per job when jobs differ.
Trusting the triggering event's name. workflow_run fires for any event that completes the named workflow, including push runs and runs on forks. The if: github.event.workflow_run.event == 'pull_request' check above scopes the job to the case you designed for. Add other checks, such as the head repository, if your logic depends on them.
Checklist for a safe split
- The unprivileged workflow uses
pull_request, setspermissions: contents: read, and uses no secrets. - The privileged workflow's only trusted code is its own file and pinned actions.
- Artifacts hold small, simple data: integers, enumerated strings, or JSON you parse and validate.
- Every value from an artifact is checked against an allow-list or against data from the GitHub API before use.
- No
${{ }}expression places artifact or event text insiderunorscriptsource. - The privileged workflow has the minimum
permissionsand no checkout of fork code.
Exercise
A teammate writes a privileged workflow_run job that downloads the ci-result artifact and then runs run: echo "Result: $(cat out/summary.txt)" >> "$GITHUB_STEP_SUMMARY". The summary.txt file is produced by the fork's test script. Is this safe? Name one concrete risk, and describe how you would change the design.
Show answer
It is not fully safe. The file is attacker-controlled, so the fork can put anything in it. The shell does not re-parse the output of $(cat ...) as code, so it is not direct command injection. The content is still untrusted and goes into the job summary unvalidated. The attacker can inject Markdown, misleading links, or fake results into a page that maintainers trust. A related mistake is writing file content to GITHUB_ENV or GITHUB_OUTPUT, which does allow variable injection. A safer design has the privileged job read only validated fields, such as an allow-listed status and a numeric count. It builds the summary text itself from those fields. If free text must be shown, escape it or place it in a code block, and never feed it into GITHUB_ENV, GITHUB_OUTPUT, or an expression inside run or script.
Exercise
Why does the ci-report workflow compare pull.head.sha with context.payload.workflow_run.head_sha before commenting?
Show answer
The PR number comes from an artifact that the fork's code can modify, so it could name a different PR. The workflow_run.head_sha value comes from GitHub, not the artifact. If the PR's current head commit equals the commit that was tested, the artifact refers to the right pull request. If the SHAs differ, the number is wrong or the PR has moved on, so the job stops.