Sub topic 12
Injection through GITHUB_ENV and GITHUB_OUTPUT
This chapter assumes you know basic YAML, the structure of a workflow (events, jobs, steps), and basic shell scripting. It also assumes you have read the earlier chapters on ${{ }} expansion and on passing untrusted data through environment variables.
Two files that carry data between steps
A step cannot change the environment of the next step directly, because each run step starts a new shell. GitHub Actions solves this with two special files. Each one is named by a variable that the runner sets:
$GITHUB_ENV: lines written here asNAME=valuebecome environment variables in all later steps of the job.$GITHUB_OUTPUT: lines written here asname=valuebecome step outputs, which later steps read with${{ steps.<id>.outputs.name }}.
Both files are parsed line by line. A newline therefore ends one assignment and starts another. This is the root of the problem: if an attacker controls the value you write, and the value can contain a newline, the attacker can add extra assignments of their own.
Injecting variables through GITHUB_ENV
Consider a step that saves a pull request title for later use:
name: save-title
on:
pull_request_target:
types: [opened]
jobs:
save:
runs-on: ubuntu-latest
steps:
- env:
PR_TITLE: ${{ github.event.pull_request.title }}
run: echo "TITLE=$PR_TITLE" >> "$GITHUB_ENV"
- run: echo "Processing $TITLE"The title is passed safely through env, so there is no script injection in the first step. The weakness is in what it writes. If the value contains a line break, the text after the break becomes a new line in $GITHUB_ENV. A multi-line value such as hello followed by a second line NODE_OPTIONS=--require ./evil.js sets NODE_OPTIONS for every later step. Variables such as NODE_OPTIONS, LD_PRELOAD, BASH_ENV and PATH make a later step load or run attacker-chosen code. Pull request titles are normally single-line, but other values, such as a PR body or a commit message, can hold many lines.
Injecting outputs through GITHUB_OUTPUT
The same flaw applies to step outputs:
- id: meta
env:
PR_BODY: ${{ github.event.pull_request.body }}
run: echo "summary=$PR_BODY" >> "$GITHUB_OUTPUT"A body with a second line such as is_admin=true creates an extra output named is_admin. If a later step or an if: condition trusts that output, the attacker has changed the workflow's behavior.
Writing values safely
Use the multi-line syntax with a delimiter that the data cannot contain. A random delimiter works, because the attacker cannot guess it:
- env:
PR_BODY: ${{ github.event.pull_request.body }}
run: |
delimiter="EOF_$(openssl rand -hex 16)"
{
echo "BODY<<$delimiter"
echo "$PR_BODY"
echo "$delimiter"
} >> "$GITHUB_ENV"The value now sits between the heredoc markers, so newlines inside it are part of the value. Even so, this does not make the variable safe to use. The better rule is to avoid putting untrusted data in $GITHUB_ENV at all. Pass it through a step-scoped env: key, or write it to a file, so it never changes the environment of later steps.
Checklist
- Treat every value written to
$GITHUB_ENVor$GITHUB_OUTPUTas a potential carrier of extra lines. - Fixed variable names only. Never build a name from event data.
- Use a random delimiter for any multi-line value.
- Prefer step-scoped
env:over$GITHUB_ENVfor untrusted data.
Exercise
A step runs echo "branch=$BRANCH" >> "$GITHUB_OUTPUT", where BRANCH holds a value an attacker controls. What can the attacker do, and what is a safer way to handle the value?
Show answer
Branch names can contain characters that the attacker chooses, and any value that includes a newline lets the attacker add extra name=value lines, which creates outputs the workflow did not intend. Later steps or if: conditions that trust those outputs can be steered. A safer way is to avoid writing the untrusted value to an output, or to write it with a random heredoc delimiter, and to always read it later through an env: key instead of expanding it inside a script.