Subscribe

Securing GitHub Actions workflows

Pin actions to full commit SHAs, start every workflow with permissions: {}, keep pull_request_target away from PR code, pass untrusted input through env instead of ${{ }}, and let Dependabot bump the pins. Checked with actionlint and zizmor.

Securing GitHub Actions workflows

A GitHub Actions workflow runs code from strangers with your repository token in reach. The actions you uses: are someone else's code. A pull request title is someone else's text. And every job gets a GITHUB_TOKEN that can do more than most jobs need. None of this is exotic, the big supply chain incidents of the last couple of years went through exactly these doors.

In this blog we will take two small workflows for shorty, the Go URL shortener from this series, and fix them one problem at a time: pin actions to commit SHAs, cut permissions: down to nothing by default, take pull_request_target and script injection apart, and let Dependabot keep the pins fresh. Level is advanced, in the sense that you should already write workflows. If not, start with GitHub Actions from zero and the CI workflow for Go.

I checked everything with actionlint 1.7.12 and zizmor 1.32.0. The action versions are the current ones as of writing: actions/checkout v7.0.1 and actions/setup-go v7.1.0.

The workflows we start with

This is the CI file. It looks harmless, and it's what most repos have:

name: ci

on:
  push:
    branches: [main]
  pull_request:

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-go@v7
        with:
          go-version-file: go.mod
      - run: go vet ./...
      - run: go test -race ./...

And this one checks that PR titles look like fix: something. I've seen this exact shape in many repos:

name: pr-title

on:
  pull_request_target:
    types: [opened, edited, synchronize]

jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
        with:
          ref: ${{ github.event.pull_request.head.sha }}
      - name: Check the title
        run: |
          echo "Checking: ${{ github.event.pull_request.title }}"
          if ! echo "${{ github.event.pull_request.title }}" | grep -Eq '^(feat|fix|chore|docs)(\(.+\))?: '; then
            echo "Title must look like 'fix: something'"
            exit 1
          fi
      - run: go run ./scripts/label-pr
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

The second file is actually bad in three separate ways. Lets first see what the tools say.

What the linters found

actionlint is the one most people already have. On the two files it found one thing:

pr-title.yml:15:34: "github.event.pull_request.title" is potentially untrusted. avoid using it
directly in inline scripts. instead, pass it through an environment variable. [expression]

Good catch, but only one. zizmor is a linter written only for Actions security, and it is easy to try in a throwaway venv:

python3 -m venv /tmp/zz && /tmp/zz/bin/pip install zizmor
/tmp/zz/bin/zizmor --offline .github/workflows/

It reported 16 findings, 6 of them suppressed as low confidence. The ones it shows by default, grouped:

   1 error[dangerous-triggers]: use of fundamentally insecure workflow trigger
   2 error[template-injection]: code injection via template expansion
   3 error[unpinned-uses]: unpinned action reference
   2 help[artipacked]: credential persistence through GitHub Actions artifacts
   2 warning[excessive-permissions]: overly broad permissions

--offline skips the audits that need the GitHub API. Without it you give zizmor a token and it also checks things like known vulnerable action versions. Now the fixes, in the order I'd do them.

Pin actions to a full commit SHA

actions/checkout@v7 is a tag. A tag is a pointer the action's owner (or whoever steals their account) can move to any commit, any time. Your workflow will then run that new code with your token. A full 40 character commit SHA can't be moved.

To find the SHA for a tag:

git ls-remote --tags https://github.com/actions/checkout.git | grep 'refs/tags/v7.0.1'
3d3c42e5aac5ba805825da76410c181273ba90b1    refs/tags/v7.0.1

There is one trap here, I fell into it while writing this post itself. Some projects use annotated tags. For those, ls-remote prints two lines, and the first SHA is the tag object, not the commit:

d583c34f0599d37dbac4a198b9c83201be380893    refs/tags/v9.3.0
ba0d7d2ec06a0ea1cb5fa41b2e4a3ab91d21278a    refs/tags/v9.3.0^{}

That is golangci/golangci-lint-action. The line ending in ^{} is the commit, and that's the one you pin. I nearly copied the first one.

Write the version as a comment after the SHA. Dependabot reads that comment and updates both together, and humans can still read the file:

      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

If you want this enforced, GitHub has had a setting since August 2025: in the repo or org under Settings, Actions, General there is "Require actions to be pinned to a full-length commit SHA". With it on, a tag-pinned action fails in "Set up job". GitHub also added workflow lockfiles in October 2026 (.github/workflows/actions.lock, made by the gh actions-lock extension), which keep tags in the YAML and pin commits in a separate file. I haven't moved to them yet and didn't try them for this post.

Give the token nothing by default

Every job gets a GITHUB_TOKEN. What it can do depends on a repo setting that old repos often have on "read and write". So I don't trust the default. I put permissions: {} at the top of every workflow, which turns everything off, and each job asks for what it needs:

name: ci

on:
  push:
    branches: [main]
  pull_request:

# Nothing by default. Each job asks for what it needs.
permissions: {}

concurrency:
  group: ci-${{ github.ref }}
  cancel-in-progress: true

jobs:
  test:
    runs-on: ubuntu-latest
    timeout-minutes: 15
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - uses: actions/setup-go@d8cbb8045d7f43706b9177cceb36c1f91a6644d5 # v7.1.0
        with:
          go-version-file: go.mod
      - run: go vet ./...
      - run: go test -race ./...

contents: read is enough to clone a private repo. Tests don't push anything.

persist-credentials: false is the fix for zizmor's artipacked finding. By default checkout writes the token into .git/config so later git push works. Then a later step that uploads the workspace as an artifact uploads the token too. This job never pushes, so the token doesn't need to stay on disk.

timeout-minutes isn't strictly security. But the default is 360 minutes, and a hung test burning six hours of runner time is its own kind of problem.

Script injection

Look at this line again:

          echo "Checking: ${{ github.event.pull_request.title }}"

See, the problem here is that ${{ }} is not a shell variable. GitHub pastes the text into the script before bash sees it. So the PR title becomes code. To show it without a real repo I did same paste by hand with a Python one-liner and a title of fix: typo"; echo "this line came from the PR title. The script bash got was:

echo "Checking: fix: typo"; echo "this line came from the PR title"

and it printed two lines:

Checking: fix: typo
this line came from the PR title

So the title ran as code. Swap that second echo for curl ... | sh and an attacker runs whatever they like, with whatever token and secrets the job has.

The fix is boring only. Put the value in an environment variable and use it as "$TITLE". Then bash treats it as data:

      - name: Check the title
        env:
          TITLE: ${{ github.event.pull_request.title }}
        run: |
          printf 'Checking: %s\n' "$TITLE"
          if ! printf '%s' "$TITLE" | grep -Eq '^(feat|fix|chore|docs)(\(.+\))?: '; then
            echo "::error::Title must look like 'fix: something'"
            exit 1
          fi

With the same title this prints Checking: fix: typo"; echo "this line came from the PR title. One line, quotes and all. Titles, branch names, commit messages, issue bodies and review comments are all attacker text. Treat every github.event.* string that a user can type the same way.

pull_request_target

pull_request from a fork runs with a read-only token and no secrets. That's safe, and it's why fork PRs can't do much. pull_request_target runs in the context of the base repo: write token, secrets, the lot. It exists for jobs like labelling a fork PR, which need write access but never need the PR's code.

The bad workflow above checks out head.sha (the fork's code) and then runs go run ./scripts/label-pr from it. So anyone who opens a PR can change scripts/label-pr and run it with a write token. That's the classic "pwn request".

GitHub has been closing this from its side. Since 8 December 2025, pull_request_target always takes the workflow file from the default branch. actions/checkout v7 (June 2026) refuses to check out a fork PR's head or merge commit in a pull_request_target job unless you set allow-unsafe-pr-checkout. On 20 July 2026 that was backported to the older supported majors, but it only reaches workflows on a floating tag like @v4. If you pinned an old SHA you don't get it. That is the price of pinning, and the reason Dependabot is the next section. And from 2 December 2026, public repos block pull_request_target runs completely unless an Actions event policy allows the trigger. Private repos aren't affected by that one.

My fix had two parts. The title check never needed write access, so it moved to plain pull_request with permissions: {} and no checkout at all. Labelling really does need write access on fork PRs, so it stays on pull_request_target, but with only pull-requests: write and no checkout:

name: pr-label

# Needs to write labels on fork PRs, so it runs on pull_request_target.
# It never checks out or runs code from the PR.
on:
  pull_request_target: # zizmor: ignore[dangerous-triggers] no PR code is checked out or run
    types: [opened]

permissions: {}

jobs:
  label:
    runs-on: ubuntu-latest
    timeout-minutes: 5
    permissions:
      pull-requests: write
    steps:
      - name: Add the needs-review label
        env:
          GH_TOKEN: ${{ github.token }}
          PR_URL: ${{ github.event.pull_request.html_url }}
        run: gh pr edit "$PR_URL" --add-label needs-review

zizmor still flagged the trigger itself as dangerous-triggers even with no checkout. Which is fair, basically it can't know my intent. I added the ignore comment with a reason on the same line, so the next person reading the file sees why. On a public repo this workflow also needs the event policy to allow it after 2 December.

Dependabot for actions

Pinning without updates means you slowly fall behind on security fixes. Dependabot understands SHA pins with a version comment and bumps both. This goes in .github/dependabot.yml:

version: 2
updates:
  - package-ecosystem: "github-actions"
    directory: "/"
    schedule:
      interval: "weekly"
    cooldown:
      default-days: 7
    groups:
      actions:
        patterns:
          - "*"
    commit-message:
      prefix: "chore"

directory: "/" makes it look in .github/workflows. The group puts all action bumps into one weekly PR rather than five. cooldown waits 7 days after a release before proposing it. The idea is that a hijacked release usually gets noticed and pulled within days, and I'm happy to be a week late for that. For GitHub Actions only default-days works, the per semver settings don't apply. I validated the file against the schemastore schema for dependabot.yml, since there's no way to run Dependabot locally.

The final check

With all of the above, both tools are quiet:

$ actionlint .github/workflows/*.yml
$ zizmor --offline .github/
No findings to report. Good job! (1 ignored, 6 suppressed)

I ran this on my Mac against the files only. I didn't push them to a GitHub repo for this post, so the policy settings and Dependabot PRs are from GitHub's docs and changelog, not from my own runs. If you already have gitleaks in your PR checks, zizmor fits in the same job, and that's what I'd add next.