Sub topic 3
How ${{ }} is expanded before the shell runs
This chapter assumes you can read a workflow YAML file and know what events, jobs, and steps are. It also assumes basic shell knowledge, such as quoting and $VARIABLE expansion. The chapter explains the step that makes script injection possible.
Two different expansions
A workflow has two separate systems that substitute values.
- GitHub expressions look like
${{ github.actor }}. The Actions service and runner evaluate them. - Shell variables look like
$HOME. The shell evaluates them when it runs.
These happen at different times, and the expression always comes first. Before a run step starts, the runner finds every ${{ }} in the step. It evaluates each one and replaces it with the resulting text. The result is a finished script, which the runner writes to a temporary file. Only then does the shell start and read that file.
What the shell actually receives
Here is a workflow that greets the user who opened an issue.
name: greet
on:
issues:
types: [opened]
jobs:
greet:
runs-on: ubuntu-latest
steps:
- run: echo "New issue: ${{ github.event.issue.title }}"Suppose someone opens an issue titled Login page broken. The shell receives this script:
echo "New issue: Login page broken"Now suppose the title is x"; whoami; echo ". The runner substitutes it as plain text, with no escaping, so the shell receives this script:
echo "New issue: x"; whoami; echo ""The shell cannot tell which characters came from the workflow author and which came from the issue title. It sees one script. The quote in the title closed the string, and whoami became a second command.
Expansion is text substitution
The runner does not treat the value as data. It does not know it is inside a shell string, so it does not quote, escape, or validate anything. The same happens inside other script-like inputs, such as the script: input of actions/github-script. There the substituted text becomes JavaScript source instead of shell script.
The result of an expression depends on where it appears. In a run step, it becomes shell text. In an env: value, it becomes a string stored in an environment variable. That second place is safe, and a later chapter builds on it.
Comparing the two mechanisms
| Aspect | `${{ github.actor }}` | `$GITHUB_ACTOR` |
|---|---|---|
| Evaluated by | The runner | The shell |
| When | Before the shell starts | While the script runs |
| Result is | Part of the script text | A value the shell treats as data |
| Can change the script's structure | Yes | No |
The last row is the important one. A shell variable's value can contain quotes and semicolons without changing what the script does, unless the script itself re-parses it, as eval does. An expression's value can change the script before parsing even begins.
Exercise
A step contains run: echo "Branch: ${{ github.head_ref }}". A contributor names their branch a"; id; echo "b. Write the script text the shell receives, and say at which point the branch name became part of the script's structure.
Show answer
The shell receives echo "Branch: a"; id; echo "b". The branch name became part of the script's structure when the runner substituted the expression, before the shell started. The shell then parsed the result as three commands, and the id command runs.