Hardening your workflows

Advanced · 17 min

Make a workflow a hard target: least-privilege tokens, SHA-pinned actions, no script injection from pull request text, and care with privileged triggers.

Before thisWorkflows, triggers, jobs and stepsRunners and the marketplaceSecrets, variables and contextsBranch protection and rulesetsCODEOWNERS and templates

OpenSSF · 17 min

Hardening your workflows

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by OpenSSFReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A workflow is code that runs with your repository's trust, so anyone who can change it, or influence what it runs, can use that trust.
  • Set permissions to the minimum at the top of every workflow, usually contents: read, and raise it per job only where needed.
  • Pin third-party actions to a full commit SHA, because a tag can be moved to different code after you reviewed it.
  • Never paste untrusted text such as a pull request title into a run script with a ${{ }} expression, since it is substituted into the script and can inject commands. Pass it through an environment variable instead.
  • Avoid pull_request_target and workflow_run with untrusted code, and protect .github/workflows with CODEOWNERS and branch rules.

A workflow is a privileged program

It is easy to treat a workflow file as configuration. It is not. It is a program that runs on a machine you pay for, with access to your repository, your secrets, and often your cloud account. Whoever can change that program, or feed it input it trusts, borrows all of that.

Most of Actions security is a short list of ways to stop that borrowing. The sources are GitHub’s “Secure use” guidance and hardening guide, which I read on 2026-11-23. I ran the injection example and checked the commit SHAs below, and nothing else on this page could be run, because it needs GitHub.

1. Give the token the least it needs

Every run gets a GITHUB_TOKEN, as you saw in the secrets lesson. Its default permissions depend on repository and organization settings, so do not rely on them. State what you need at the top of each workflow:

permissions:
  contents: read

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - run: npm test

  comment:
    needs: test
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write
    steps:
      - run: echo "this job may comment on pull requests"

Each scope takes read, write or none, and any scope you do not list becomes none. A job’s own permissions replace the workflow-level ones. If a stolen token can only read, a compromised step cannot push code.

2. Pin actions to a commit SHA

uses: actions/checkout@v7 trusts a tag. Tags can be moved or deleted. I looked at the real repository with git ls-remote https://github.com/actions/checkout refs/tags/v7 refs/tags/v7.0.1. Both tags pointed at 3d3c42e5aac5ba805825da76410c181273ba90b1 that day, and that is only a fact about that day: when 7.0.2 ships, v7 will move. For actions/setup-node, v7 and v7.1.0 were both 949feb2413d6458794dcd2491c4babbbce0c15c1.

Pin to the full SHA and keep the version in a comment:

steps:
  - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
  - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
    with:
      node-version: 24

A SHA identifies one exact snapshot, so a later change to the tag cannot change what runs. The hardening guide stresses that the SHA must come from the action’s own repository and not a fork. It also says to audit any third-party action you use. For GitHub’s own actions/* you might accept tags, which is a judgement about trust. For everything else, pin.

The guide also mentions workflow dependency lockfiles, which it says offer the strongest protection and supersede SHA pinning where you can adopt them. I could not find their reference page, so I cannot describe them. Check the current docs.

Pinned versions go stale, so add the github-actions ecosystem to Dependabot. It updates SHA pins and the version comment together.

3. Script injection

This is the one that surprises people. Look at this step:

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

${{ ... }} is not a shell variable. GitHub replaces it with the text before the shell starts, so the title becomes part of the script. A pull request whose title is fix"; echo INJECTED; echo " turns into a different script. I reproduced exactly that substitution in bash:

echo "Title is fix"; echo INJECTED; echo ""

Running it printed Title is fix, then INJECTED. An attacker would not print a word. They would send your secrets somewhere. The pull request title, branch names, issue text and commit messages are all attacker-controlled.

The fix is to put the value in an environment variable. The script’s text is then fixed, and the value is data:

# Safe
- name: Show PR title
  env:
    PR_TITLE: ${{ github.event.pull_request.title }}
  run: echo "Title is $PR_TITLE"

Run with the same hostile title, the script printed Title is fix"; echo INJECTED; echo ", with nothing executed. Always quote "$PR_TITLE". I also learned why my first draft of this example would not even load: run: echo "PR title: ..." has a colon followed by a space inside an unquoted YAML value, which YAML reads as a nested mapping. Running every snippet through a YAML parser caught it. Where possible, pass the value as an input to an action instead of using a script.

4. Be wary of privileged triggers

pull_request from a fork is safe by design: read-only token, no secrets. Two other triggers are not. The events reference says pull_request_target “runs in the context of the default branch of the base repository”, so it has the base repository’s secrets and write token, and it runs even for conflicting pull requests. Its warning is blunt: running untrusted code on that trigger may lead to vulnerabilities, including cache poisoning and access to secrets, and you should avoid it if you need to build or run code from the pull request. workflow_run is similar in its privileges.

The rule that follows: never check out and run code from a pull request in a workflow that has privileges or secrets. If you need to comment on fork pull requests, do the untrusted work in an unprivileged pull_request workflow, and pass results by artifact to a small privileged one that only reads data.

5. Handle secrets as if they will leak

  • Environments with required reviewers guard production secrets.
  • One value per secret, and mask derived values with ::add-mask::.
  • Prefer OIDC so there is nothing to leak.
  • Do not run self-hosted runners on public repositories, as the runners lesson explained.

6. Protect the workflows themselves

All of this assumes the workflow files say what you reviewed. Add them to CODEOWNERS:

/.github/workflows/  @ada

Combined with a rule that requires code-owner review, as in branch protection and rulesets, nobody can quietly weaken a check, add a trigger or remove a permission limit. Also turn on dependency review for pull requests that change workflows, and consider OpenSSF Scorecards, which the guide recommends for spotting risky practices.

A checklist

  1. permissions set at the top of every workflow, usually contents: read.
  2. Third-party actions pinned to SHAs, with Dependabot keeping them current.
  3. No ${{ }} of untrusted text inside run:. Use env:.
  4. No untrusted checkout in pull_request_target or workflow_run.
  5. Secrets scoped to environments, with OIDC for cloud access.
  6. Workflow files under CODEOWNERS.

What the video covers

OpenSSF’s “Securing Your GitHub Actions” by Jaroslav Lobacevski of GitHub is about 17 minutes and was published around 2025. Its description says that, given recent supply chain compromises of GitHub Actions, it covers how to secure them. I could not read a transcript or chapters, so I cannot say which compromises or techniques it covers, though SHA pinning and permissions are very likely. A conference talk, so it has less hands-on click path than the rest of this group. English is judged from the title, description and the speaker’s affiliation, not from audio. An 8-minute 2026 summary is linked under deeper as a lighter alternative from a small channel.

Where this fits

That completes the roadmap. You have taken a project from git init to a repository with protected branches, reviewed pull requests, CI that tests several Node versions, a deployment with no stored keys, and a workflow hardened against the usual attacks.

Check yourself

  1. 01Why pin an action to a commit SHA and not a tag like v7?

    A tag is a movable label. If its repository is compromised, the tag can be pointed at malicious code, and your next run executes it. A full commit SHA identifies one exact snapshot of the code.

  2. 02What is script injection in a workflow?

    Using a ${{ }} expression with attacker-controlled text, such as a pull request title, inside a run script. GitHub substitutes the text before the shell starts, so a crafted title becomes shell commands.

  3. 03What is the fix for script injection?

    Assign the expression to an environment variable in env: and refer to it as a shell variable in the script, quoted. The value stays data and is never parsed as code.

  4. 04Why is pull_request_target dangerous?

    It runs in the context of the base repository with access to secrets and a token that can write. If the workflow also checks out and runs code from the pull request, an outsider's code gets those privileges.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark