Sub topic 3
Pinning an action to a full-length commit SHA
This chapter assumes you can read a GitHub Actions workflow (jobs, steps, uses:) and know what a Git commit, tag, and branch are. It also assumes you know that a ref like v4 is a semantic version tag. The previous chapter showed that tags and branches can be moved. This chapter shows how to remove that risk for each action you use.
The core idea
A commit SHA is a 40-character hexadecimal identifier. Git computes it from the commit's content: the file tree, the parent commit, the author, the message, and the timestamps. If any of that changes, the SHA changes. A tag or branch is only a name that points at a commit, and whoever has write access to the repository can change where the name points.
When you write uses: owner/repo@<full SHA>, you stop depending on a name. The runner fetches exactly one commit. If someone later moves the v4 tag, your workflow still runs the code you reviewed.
Here is the same step written both ways.
steps:
# Mutable: the tag can be moved to a different commit
- uses: actions/checkout@v4
# Immutable: a full-length commit SHA identifies exactly one commit
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2The first form trusts the maintainer, and anyone who gains write access to that repository, to keep v4 pointing at harmless code. The second form trusts only the commit you looked at. The trailing comment is for humans. GitHub ignores it. A later chapter covers keeping comments accurate.
What happens at run time
The diagram compares what the runner does in each case.
With a tag, the lookup happens on every run, so a change in the tag takes effect in your next job without any change to your repository. With a SHA, there is nothing to look up. The reference is the content address.
The format rules
GitHub accepts only the full 40-character SHA for this purpose. The rules that cause the most confusion are these:
- Short SHAs are not allowed.
actions/checkout@11bd719is not accepted as a pin. A short SHA is ambiguous, because several commits can share a prefix. GitHub rejects it as an unresolvable reference. - Use the commit SHA, not the tag object SHA. An annotated tag is its own Git object with its own SHA. That SHA is not a commit. If you copy it into
uses:, the reference will not resolve. A lightweight tag points straight at the commit, so there is nothing extra to unwrap. - Use lowercase hex. Stick to the 40 characters that
gitprints. - Pin each
uses:line. There is no global switch in the workflow file that pins everything. Each step, and each reusable workflow call, carries its own reference.
You will see how to find the correct commit SHA for a release, and how to confirm it belongs to the repository you meant, in the next chapter. For now, take the value from the project's release page or from git ls-remote and check that it is 40 characters.
A complete pinned workflow
This workflow runs on pushes and pull requests. Every third-party action is pinned. The run: steps are your own commands, so they need no pin.
name: ci
on:
push:
branches: [main]
pull_request:
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Show the checked-out commit
run: git log -1 --format='%H %s'The permissions: contents: read line is not part of pinning, but the two work well together. Pinning limits which code runs. Narrow token permissions limit what that code can do if it turns out to be hostile.
Why bother: an example failure
Suppose your workflow uses some-org/lint-action@v2. The maintainer's account is compromised. The attacker force-moves v2 to a new commit that reads environment variables and sends secrets to a server they control. Your repository has not changed, so no pull request exists to review. The next time your workflow runs, the runner resolves v2 to the malicious commit and runs it with your GITHUB_TOKEN and any secrets you passed to the job.
This is not hypothetical. In March 2025 the widely used tj-actions/changed-files action had its tags repointed to a malicious commit, and workflows that referenced those tags printed their secrets into build logs. Workflows pinned to a known-good commit SHA were not affected by the tag change.
If you had pinned to the SHA of the version you reviewed, the move would have had no effect on your pipeline.
What a pin does and does not protect
A SHA pin protects against the name moving. It does not make the code safe. The table separates the two.
| Threat | Does a SHA pin help? | Column |
|---|---|---|
| Maintainer or attacker moves a tag or branch to new code | Yes. Your reference no longer follows the name. | |
| Maintainer deletes and recreates a release tag | Yes. Your SHA still names the original commit, as long as that commit still exists. | |
| The commit you pinned already contained malicious code | No. You pinned the problem. Review before pinning. | |
| The action downloads and runs code at run time (for example, `curl \ | bash` inside it) | No. The pin fixes the action's files, not what they fetch. |
| The action internally uses other actions by tag | No. See the later chapter on transitive dependencies. | |
| A SHA that is not in the repository you intended | Partly. See the later chapter on impostor commits. |
Keep this table in mind when someone says "we pin everything, so we are safe." Pinning removes one specific class of silent change.
Trade-offs you accept
Pinning has real costs, and you should decide with them in view.
- Readability drops.
@v4tells a reader the major version. A 40-character hash does not. The version comment restores this, but only if you maintain it. - Updates become your job. A tag like
v4quietly picks up bug fixes and security patches. A pin stays on the old commit until you change it, including when the old commit has a known vulnerability. You need an automated way to propose updates, which is what Dependabot is for in a later chapter. - Reviews get noisier. Every update is a diff of one hash to another. Reviewers need to open the comparison between the two commits to see what changed.
- Moving tags can be a feature. Some teams deliberately track
v4so they receive patches without a pull request. Pinning trades that convenience for control.
Most security guidance, including GitHub's own, treats SHA pinning as the correct default for third-party actions. Many teams make an exception for actions published by GitHub itself, which they trust more. That is a policy decision, not a technical rule.
Finding unpinned references
Before you change anything, find out where you stand. This command lists uses: lines under .github/workflows that do not end in a 40-character hex SHA. It skips local actions that start with ./.
grep -rEn 'uses:[[:space:]]*[^.[:space:]][^@[:space:]]*@' .github/workflows \
| grep -vE '@[0-9a-f]{40}([[:space:]]|$)'Treat the output as a rough list, not a verdict. It also lists Docker image references such as docker://alpine@sha256:..., which use a different pinning form, and it does not look inside the actions you call. For a thorough check, use the policy setting described in a later chapter.
A practical procedure
- Pick one
uses:reference that uses a tag, such asactions/checkout@v4. - Decide which exact release you want, for example
v4.2.2. A major tag likev4is not a release, so choose the latest release under it. - Resolve that release to its commit SHA and check that it comes from the right repository (next chapter).
- Replace the tag with the 40-character SHA and add the release as a trailing comment.
- Run the workflow and confirm it still passes.
- Repeat for each remaining third-party reference.
Do this in a small pull request so that the diff is easy to read. Each changed line should differ only in the reference and the comment.
Common mistakes
- Pinning to a branch SHA from memory. If you copy a SHA from the repository's main branch instead of a release, you may pin an unreleased commit. Always start from a release or tag.
- Pasting the tag object SHA. The workflow fails with an error that the action cannot be resolved. Use the commit the tag resolves to.
- Truncating the SHA. A 7- or 12-character prefix is rejected.
- Forgetting reusable workflows.
uses: org/repo/.github/workflows/build.yml@mainis also auses:reference and needs the same treatment. - Leaving a stale comment. If you change the SHA but not the comment, the comment misleads every later reader.
Exercise
A teammate changes uses: actions/checkout@v4 to uses: actions/checkout@11bd719, copying the first seven characters of the commit SHA, and says the step is now pinned. Is it pinned? Explain what to change, and name one risk that remains even after the fix.
Show answer
It is not pinned. GitHub requires the full 40-character commit SHA, and a short prefix is not accepted as a pin, so the reference will fail to resolve instead of fixing the commit. The fix is to use the full value, actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2. Even then, the pin does not cover the contents of that commit. If the commit contained malicious code, you are pinned to it. Transitive references inside the action, or code the action downloads at run time, are also not covered.