Sub topic 14
Third-Party Actions and SHA Pinning
This chapter assumes you can read a workflow file and know the difference between pull_request and pull_request_target. It also assumes you know that GITHUB_TOKEN and repository secrets are available to jobs. Here we look at a different trust boundary: the code you pull in with uses:.
Why third-party actions matter
A step that says uses: owner/repo@ref downloads that repository at that ref and runs it inside your job. The action can read every secret you pass to it. It can also read the job's GITHUB_TOKEN, the checked-out files, and any environment variable set for that step. On a self-hosted runner, it can reach the machine itself.
So an action is code you did not write, running with your privileges. If its maintainer's account is compromised, or its tag is moved to malicious code, every workflow that references it runs the attacker's code on the next trigger.
Tags and branches can move
Most workflows reference actions by a tag or a branch:
- uses: actions/checkout@v4
- uses: some-org/some-action@mainA tag such as v4 is a pointer. The owner can move it to a different commit at any time, and so can anyone who gains write access to that repository. A branch such as main moves with every push. Neither gives you a fixed version of the code.
A full commit SHA is different. It is a hash of the commit contents, so it identifies exactly one version of the code. Nobody can change what that SHA points to.
Pin to a full commit SHA
Replace the tag with the 40-character SHA and keep the tag in a comment so people can see which version it is:
name: ci
on: pull_request
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- run: npm ci && npm testUse the full 40-character SHA. A short SHA is not accepted as a safe pin, and tags cannot be trusted. Look up the SHA on the action's releases page, and check that it belongs to the action's own repository, not to a fork.
Keeping pins up to date
A pinned SHA never changes, so it also never receives security fixes unless you update it. Tools such as Dependabot can open pull requests that bump SHA pins and update the version comment. Review each bump like any other dependency change. Look at the diff between the old and new commits before merging.
Reducing exposure further
- Prefer actions published by GitHub (
actions/*) or by organizations you already trust, and avoid small, unmaintained actions for tasks a few shell lines can do. - Give the job only the permissions it needs, as in the earlier chapter on
GITHUB_TOKEN. A compromised action then has less to abuse. - Do not pass secrets to steps that do not need them. Scope them with
env:on the single step that uses them. - Allow only approved actions. Repository and organization settings can restrict workflows to actions from your organization, GitHub, or an explicit list, and can require full-length SHA pinning.
Where it fits
| Risk | Control |
|---|---|
| Moved or rewritten tag | Pin to a full commit SHA |
| Outdated pinned action | Automated update pull requests with review |
| Over-privileged token | Explicit `permissions` block |
| Unapproved action added | Organization allow-list for actions |
Exercise
A workflow uses uses: acme/deploy@v2. Why is this riskier than pinning to a SHA, and what two things should you do after pinning?
Show answer
The tag v2 can be moved to a different commit, so the code that runs can change without any edit to your workflow. A full commit SHA always identifies the same code. After pinning, keep the version in a comment for readability, and set up automated update pull requests (for example Dependabot) so you still receive fixes, reviewing each diff before merging.