Sub topic 11
workflow_run and treating artifacts as untrusted
This chapter assumes you know the pull_request and pull_request_target triggers, the default GITHUB_TOKEN permissions, and the split-workflow pattern from the previous chapter. Here you will see how the privileged half of that split can still be attacked through the data it receives.
Why the split needs a second look
Fork code is unsafe to run with secrets. The standard fix is to build and test it in an unprivileged pull_request workflow. A second workflow then runs with privileges and does something useful with the result, such as commenting on the PR or labeling it.
The second workflow uses the workflow_run trigger. It starts when the first workflow finishes. It always runs from the default branch of the base repository. Because of that, it gets a write-capable token and can read secrets, even when the triggering run came from a fork.
The weak point is what passes between the two workflows. The usual carrier is an artifact, which is a file bundle uploaded by one run and downloaded by another. The unprivileged workflow was driven by the attacker, so the attacker controls every byte of its artifact.
The two workflows
The first workflow runs fork code with no privileges and uploads a result file.
name: build
on: pull_request
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm test > result.txt
- uses: actions/upload-artifact@v4
with:
name: result
path: result.txtThe second workflow downloads that artifact in a privileged context.
name: comment
on:
workflow_run:
workflows: [build]
types: [completed]
permissions:
actions: read
pull-requests: write
jobs:
comment:
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
name: result
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ github.token }}
- run: echo "RESULT=$(cat result.txt)" >> "$GITHUB_ENV"The last step is the bug. The attacker wrote result.txt, and the privileged job copies its contents into GITHUB_ENV. A file containing a newline followed by NODE_OPTIONS=--require ./evil.js, or any other variable, can change how later steps behave. The same applies to any value that reaches GITHUB_OUTPUT or a run expression.
Rules for the privileged workflow
- Never check out or execute anything from the artifact. Treat a downloaded script or binary the way you treat fork code in a pwn request.
- Treat every field as data. Validate it before use. For example, require a PR number to match
^[0-9]+$. - Do not write artifact content into
GITHUB_ENVorGITHUB_PATH. - Pass values to shell steps through
env:, as in the earlier chapters, and quote them. - Do not trust
github.event.workflow_run.head_branchorhead_commit.message. The attacker also controls these. - Grant only the permissions the job needs.
A safer last step validates a PR number and uses it as data.
- env:
GH_TOKEN: ${{ github.token }}
run: |
pr="$(head -n 1 pr-number.txt)"
if ! [[ "$pr" =~ ^[0-9]+$ ]]; then
echo "Invalid PR number" >&2
exit 1
fi
gh pr comment "$pr" --repo "$GITHUB_REPOSITORY" --body "Build finished"The comment text is fixed in the workflow. The only artifact-derived value is a number that passed a strict check.
Exercise
A privileged workflowrun job reads pr-title.txt from a fork's artifact and runs `echo "Title: $(cat pr-title.txt)" >> "$GITHUBENV"`. Name two ways to make this safer.
Show answer
Do not write artifact text to GITHUB_ENV at all, since a newline lets the attacker set arbitrary variables. If the value is needed, pass only validated data (for example a numeric PR number checked with a regex) and give it to later steps through a step-level env: block, not through the environment file.