Sub topic 11
workflow_run and treating artifacts as untrusted
Prerequisites
This chapter assumes you have read the earlier chapters on ${{ }} expansion, pull_request versus pull_request_target, and GITHUB_TOKEN permissions. You should also be comfortable with workflow YAML and basic shell.
Why workflow_run exists
The previous chapter's safe design splits work into two parts. An unprivileged workflow builds and tests fork code. A privileged workflow then does something that needs a write token or secrets, such as commenting on the pull request or labeling it. The two workflows cannot share a job, so they need a way to hand data across.
The workflow_run trigger starts a workflow when another workflow finishes. The handoff data usually travels as an artifact, which is a file bundle that the first run uploads and the second run downloads.
The split only helps if the second workflow treats the first one's output as hostile. The first workflow ran attacker code, so every byte it produced was chosen by the attacker.
What workflow_run runs with
A workflow_run workflow differs from a pull_request workflow in four ways:
- It always runs the workflow file from the default branch, not from the pull request.
- It has access to repository secrets and can receive a write-capable token, even when the triggering run came from a fork.
- It is not tied to a pull request.
github.event.workflow_run.pull_requestsis empty for fork PRs, so you cannot rely on it to find the PR. - Its checkout defaults to the default branch, which is trusted. The triggering commit is available only through
head_sha.
In trust terms, workflow_run behaves like pull_request_target. It has the privileges of the base repository, plus inputs that the attacker influenced.
Event fields you still cannot trust
The workflow_run event carries data about the run that triggered it. Some of that data comes from the fork:
| Field | Attacker control |
|---|---|
| `github.event.workflow_run.head_branch` | Full. The fork owner picks the branch name. |
| `github.event.workflow_run.display_title` | Full. It usually mirrors the PR title or commit message. |
| `github.event.workflow_run.head_commit.message` | Full. |
| `github.event.workflow_run.head_commit.author.email` and `.name` | Full. |
| `github.event.workflow_run.head_repository.full_name` | Partial. The fork owner picks the repository name. |
| `github.event.workflow_run.head_sha` | None. It is a hex commit ID. |
| `github.event.workflow_run.id` | None. It is a numeric run ID. |
| `github.event.workflow_run.conclusion` | None. GitHub sets it. |
The rules from the injection chapters apply unchanged. Never place head_branch or display_title inside a run: script. Pass them through env: instead.
The vulnerable pattern
This workflow downloads an artifact from the CI run and uses its contents to configure later steps.
name: pr-comment
on:
workflow_run:
workflows: [ci]
types: [completed]
permissions:
actions: read
pull-requests: write
jobs:
comment:
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
name: pr-data
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ github.token }}
- name: Load PR number
run: echo "PR_NUMBER=$(cat pr_number.txt)" >> "$GITHUB_ENV"
- name: Post report
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
run: gh pr comment "$PR_NUMBER" --body-file report.mdThe pr_number.txt file was written by the fork's CI run, so the attacker controls it completely. The attacker can replace the file's contents with this:
1
NODE_OPTIONS=--require /tmp/evil.jsThe first step appends both lines to GITHUB_ENV. The second line defines a new environment variable for every later step, and that variable can change how later commands behave. This is the GITHUB_ENV injection described in a later chapter. Variables such as BASH_ENV, LD_PRELOAD, and PATH can hijack a later step the same way. That later step holds a write token, so the attacker's code runs with it.
An attacker can also mislead the workflow without running any code. The file could name a different PR number, so the bot comments on the wrong pull request. A report.md file could contain misleading Markdown or links, and the comment would appear to come from the trusted bot.
Artifacts have a further risk. If an artifact contains a script, a lockfile, or a config file, running it or feeding it to a build tool in the privileged job is the same mistake as the pwn request. Do not execute, source, or npm install anything from the artifact.
The safe pattern
The producer workflow stays unprivileged and writes only simple data.
name: ci
on: pull_request
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run tests
run: |
mkdir -p out
npm ci
npm test > out/report.md 2>&1
- name: Record PR number
if: ${{ !cancelled() }}
env:
PR_NUMBER: ${{ github.event.number }}
run: |
mkdir -p out
printf '%s\n' "$PR_NUMBER" > out/pr_number.txt
- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: pr-data
path: out/The consumer validates every value before using it.
name: pr-comment
on:
workflow_run:
workflows: [ci]
types: [completed]
permissions:
actions: read
pull-requests: write
jobs:
comment:
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
name: pr-data
path: pr-data
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ github.token }}
- name: Validate PR number
id: pr
working-directory: pr-data
run: |
pr_number=$(head -n 1 pr_number.txt)
if ! [[ "$pr_number" =~ ^[0-9]{1,9}$ ]]; then
echo "pr_number.txt is not a plain integer" >&2
exit 1
fi
echo "number=$pr_number" >> "$GITHUB_OUTPUT"
- name: Post report
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
PR_NUMBER: ${{ steps.pr.outputs.number }}
run: |
head -c 60000 pr-data/report.md > body.md
gh pr comment "$PR_NUMBER" --body-file body.mdThis version makes five changes:
- The artifact lands in its own directory, so its file names cannot overwrite workflow files.
- The PR number is parsed with a strict allow-list pattern. Anything other than digits fails the job. The check matters because
head -n 1stops a multi-line value from carrying extra lines. - The validated value goes to
GITHUB_OUTPUT, notGITHUB_ENV. A step output is scoped to one named key, so it cannot define arbitrary variables for later steps. - The report is passed with
--body-fileand truncated, so its content is never parsed as shell. It is still untrusted text. The bot posts it as-is, so a reader may believe it. - Nothing from the artifact is executed.
Prefer data formats you can validate. A single integer or a small JSON file parsed with a strict schema is much safer than free text. If you need structured data, parse it with a tool that does not execute content, such as jq, and check each field.
Looking up the right run and commit
When you need the PR a run belongs to, do not trust a PR number from the artifact if you can derive it from trusted data. For example, you can query the API for pull requests whose head matches github.event.workflow_run.head_sha. This uses the commit ID, which GitHub controls.
If the privileged workflow must check out code, check out the default branch. Check out head_sha only if you will treat the checkout as read-only data and run nothing from it.
Other details to watch
- Artifact source. Using
run-idties the download to the triggering run. Without it,download-artifactfetches from the current run, where your artifact does not exist. Do not accept a run ID from an artifact or from other attacker-controlled input. - Artifact names. Filter by exact name. If you download all artifacts, you also get any extra files a malicious run uploaded.
- Zip and path tricks. Official download actions guard against path traversal, but custom
unziportarcalls may not. Extract into a dedicated directory and avoid options that preserve absolute paths or symlinks. - Workflow chains. A
workflow_runworkflow can itself trigger anotherworkflow_run. GitHub limits this chain to three levels, but each hop carries the same trust problem. - Conclusion checks. Gate the job with
if: github.event.workflow_run.conclusion == 'success'when you only need results from passing runs. This is a correctness check and does not make the content trusted.
Review checklist
- Does the workflow run fork code, or only handle its outputs?
- Does the privileged job give any artifact content to a shell, an interpreter, a package manager, or
GITHUB_ENV? - Is each artifact value checked against an allow-list pattern, such as digits only?
- Are event strings like
head_branchpassed throughenv:and never expanded inside scripts? - Does the job request only the permissions it needs?
Exercise
A privileged workflow_run job downloads an artifact and runs source pr-data/vars.sh to load settings that the CI job wrote. Explain why this is dangerous, and describe a safer design.
Show answer
The CI job ran fork code, so the attacker controls vars.sh. Sourcing it executes the attacker's shell commands in a job that has a write token and possibly secrets. The safer design is to have CI write only a small data file, such as a single integer or a strict JSON document. The privileged job then reads it as data, validates each field against an allow-list pattern, and passes the validated values to later steps through step outputs and env: entries. It never sources or executes anything from the artifact.
Exercise
In the vulnerable consumer workflow, an attacker writes "1" on the first line and "BASHENV=/tmp/x.sh" on the second line of prnumber.txt. Which command appends the second line to the environment, and what single change in the safe version stops it?
Show answer
The command is echo "PR_NUMBER=$(cat pr_number.txt)" >> "$GITHUB_ENV". The command substitution keeps the embedded newline, so the second line becomes a separate KEY=value entry in GITHUBENV. The safe version reads only the first line with head -n 1, requires it to match ^[0-9]{1,9}$, and fails the job otherwise. It writes the result to GITHUBOUTPUT, so only the one validated key is set.