Sub topic 11
Avoiding script injection from inputs
This chapter assumes you can read a basic action.yml, know how composite steps declare shell, and know how inputs and env work. It covers one risk: untrusted text from an input turning into shell code.
Why composite actions are exposed
Your action does not control who calls it. A workflow in another repository can pass anything into your inputs, including values taken from issue titles, pull request branch names, or commit messages. Anyone who can open an issue or a pull request controls those values. Treat every input as untrusted, even if your own workflow only passes harmless strings today.
The risk comes from the order of processing. GitHub evaluates ${{ ... }} expressions first and pastes the result into the step as text. Only after that does the runner start the shell. The shell cannot tell which part of the script was yours and which part came from the input.
A vulnerable action
This action prints a greeting. It looks harmless.
name: Unsafe greet
description: Do not copy this action
inputs:
who-to-greet:
description: Name to greet
required: true
runs:
using: composite
steps:
- shell: bash
run: echo "Hello, ${{ inputs.who-to-greet }}"Suppose a caller passes this value:
x"; echo "injected" ; echo "After expression substitution, the shell receives:
echo "Hello, x"; echo "injected" ; echo ""The quote in the input closed your string, and the semicolons started new commands. A real attacker would replace echo "injected" with a command that reads environment variables or sends data to another server. The step runs with the job's permissions, so it can reach any secret the job exposes.
The fix: pass inputs through env
Move the expression out of the script and into an env entry. Then read the value as a shell variable.
name: Safe greet
description: Print a greeting without evaluating the input as code
inputs:
who-to-greet:
description: Name to greet
required: true
runs:
using: composite
steps:
- shell: bash
env:
WHO: ${{ inputs.who-to-greet }}
run: echo "Hello, ${WHO}"The script text is now fixed. The expression engine still pastes the value, but it pastes it into the env mapping, where it is stored as a string. The shell then expands ${WHO} after parsing, and expansion output is never parsed again as commands. The same hostile input now prints literally as Hello, x"; echo "injected" ; echo ".
Keep two habits:
- Always double-quote the variable (
"$WHO"). Unquoted expansion splits on spaces and expands wildcards. - Keep
${{ }}out of therun:body entirely for any value you did not write yourself.
The first example in this tutorial's action anatomy chapters already follows this pattern (env: WHO: ${{ inputs.who-to-greet }}). This is the reason.
Which expressions need this treatment
Not every expression is dangerous. The risk exists wherever the substituted text is parsed as code.
| Where the expression appears | Safe? | Reason |
|---|---|---|
| `run:` body | No, if the value is untrusted | Pasted into shell source |
| `actions/github-script` `script:` input | No, if the value is untrusted | Pasted into JavaScript source |
| `env:` value | Yes | Stored as a string |
| `with:` value for another action | Yes for injection into the workflow | The called action decides what to do with it |
| `if:` condition | Yes | Evaluated by the expression engine, not a shell |
| `github.action_path` in `run:` | Yes | The runner sets it, not the caller |
Be careful with the with: row. It protects your step from the shell, but the called action can still misuse the value. Read what an action does with its inputs before you feed it untrusted text.
Other attacker-controlled values include github.event.issue.title, github.event.pull_request.title, github.event.pull_request.head.ref, github.head_ref, and github.event.head_commit.message. The same rule applies to them if you ever use them inside your action.
Injection into outputs and the environment
Using env stops the shell from running the input. The input can still cause harm in other ways.
Multi-line values in GITHUB_OUTPUT
A step writes outputs by appending name=value lines to the file named by $GITHUB_OUTPUT. If the value contains a newline, the text after it becomes a new line in that file. An attacker could then set an output you never intended.
name: Normalize title
description: Expose a title as an output without allowing extra outputs
inputs:
title:
description: Title text, may contain newlines
required: true
outputs:
title:
description: The title, unchanged
value: ${{ steps.write.outputs.title }}
runs:
using: composite
steps:
- id: write
shell: bash
env:
TITLE: ${{ inputs.title }}
run: |
delimiter="EOF_$(openssl rand -hex 8)"
{
echo "title<<${delimiter}"
printf '%s\n' "$TITLE"
echo "${delimiter}"
} >> "$GITHUB_OUTPUT"The heredoc form name<<DELIMITER reads everything up to a line equal to the delimiter. A random delimiter means the input cannot contain it by accident or by design. Use printf '%s\n' instead of echo, because echo can treat a value like -n as an option.
The same reasoning applies to $GITHUB_ENV and $GITHUB_PATH. Writing an untrusted value into $GITHUB_ENV lets an attacker define variables such as LD_PRELOAD or NODE_OPTIONS for every later step. Avoid writing untrusted values there. If you must, use the delimiter form and validate the name and value first.
Argument injection
A value that starts with - can be read as an option by the command you call. If an input is a branch name and you run git checkout "$BRANCH", a value like --orphan changes the command's behavior. Put -- before untrusted arguments when the tool supports it, and validate the value first.
Workflow commands in logs
The runner scans log output for lines that begin with ::, such as ::add-mask:: or ::error::. Printing untrusted text can therefore trigger workflow commands. Print untrusted values with care, and avoid echoing multi-line input directly to the log.
Validate when the format is known
Quoting protects the shell. Validation protects your logic. If an input should be a version number, check it:
name: Validate version
description: Reject malformed version strings before using them
inputs:
version:
description: Semantic version such as 1.2.3
required: true
runs:
using: composite
steps:
- 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"
exit 1
fi
echo "Using version ${VERSION}"The error message here is a fixed string and does not include the input. That keeps the rejected value out of the log commands.
Prefer an allow-list (only these characters or values) over a deny-list (reject these bad characters). Deny-lists miss cases.
Review checklist
- Search your
action.ymlfiles for${{insiderun:blocks. Every hit that uses an input or event field should move toenv. - Confirm every
$VARIABLEis double-quoted. - Check every write to
$GITHUB_OUTPUT,$GITHUB_ENV, and$GITHUB_PATHfor newline handling. - Add
--before untrusted arguments to commands that accept options. - Validate inputs that have a known format.
Exercise
A composite step contains run: git checkout "${{ inputs.branch }}". Name two problems with it and rewrite it.
Show answer
The expression is pasted into the script text, so a value containing a quote and a semicolon can run extra commands (script injection). Even after quoting, a value starting with a dash can be read as an option (argument injection). A safer version is: env: BRANCH: ${{ inputs.branch }} with run: git checkout -- "$BRANCH". Validating the branch name against an allow-list pattern first is better still.
Exercise
Why does a step that writes echo "result=$VALUE" >> "$GITHUB_OUTPUT" stay unsafe even when VALUE comes from env?
Show answer
If VALUE contains a newline, the text after it becomes a new line in the output file. The caller could then define extra outputs or overwrite one. Use the name<<DELIMITER heredoc form with a random delimiter, and write the value with printf '%s\n'.
Exercise
Is if: ${{ inputs.run-tests == 'true' }} vulnerable to script injection? Explain.
Show answer
No. The expression engine evaluates the comparison itself and produces a boolean. No shell parses the input text, so it cannot run as code.