Sub topic 7
Requiring SHA pinning with repository and organization policy
This chapter assumes you can read a workflow file, know that uses: can point to a tag, branch, or commit SHA, and have seen how SHA pinning works. It also assumes you have admin rights on a repository or organization, since only admins can change these settings.
Why a policy is needed
Pinning by hand depends on every contributor remembering to do it. One pull request that adds uses: some-org/some-action@v2 puts a mutable reference back in your pipeline. A policy moves the rule from code review into the platform, so GitHub rejects the unpinned reference for you.
What the setting does
GitHub Actions has a policy named Require actions to be pinned to a full-length commit SHA. When it is on, a workflow that references an action or reusable workflow by tag or branch fails. A full 40-character commit SHA is accepted.
The policy sits next to the setting that controls which actions may run. You can find it in these places:
- Repository: Settings, Actions, General, in the Actions permissions section.
- Organization: Settings, Actions, General, with the same option. The organization setting applies to its repositories.
- Enterprise: an enterprise owner can set it in the enterprise policies, and it flows down to organizations.
Policies set at a higher level cannot be loosened by a lower level. A repository admin cannot turn off a requirement set by the organization.
What passes and what fails
With the policy on, only the first step below is accepted.
name: ci
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
# Accepted: full-length commit SHA
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
# Rejected: tag reference
# - uses: actions/checkout@v4
# Rejected: branch reference
# - uses: actions/checkout@main
# Not affected: the action lives in your own repository
- uses: ./.github/actions/setup-toolsA local action such as ./.github/actions/setup-tools is part of your own checkout, so the policy does not apply to it. A short SHA such as 11bd719 is not accepted, because short SHAs can be ambiguous. The reference must be the full 40 characters.
How a run is checked
Rolling the policy out
Turning the policy on across an organization can break many workflows at once. A safer order is:
- Inventory the unpinned references. A code search for
uses:lines without a 40-character hex string is enough to start. - Pin them in a pull request per repository, adding a version comment to each.
- Make sure Dependabot is configured for the
github-actionsecosystem, so pins keep receiving updates. - Enable the policy on one low-risk repository and confirm the workflows still run.
- Enable it at the organization level.
The policy catches unpinned references that sit directly in your workflows. It cannot repair a reference inside someone else's action, so it also does not solve the problem of nested actions covered in a later chapter.
Limits to keep in mind
- The policy checks the form of the reference, not where the commit came from. A full SHA that belongs to a commit in a fork passes the check. The impostor-commit chapter covers this.
- The policy does not judge whether an action is trustworthy. To control which actions may run at all, use the allow list in the same settings page. The two controls work together: the allow list says which actions, and the pinning policy says how they must be referenced.
- A pinned action can still be malicious if you pinned a bad commit. Pinning gives you repeatability, and you still have to review what you pin.
Exercise
Your organization enables the SHA-pinning policy. A developer opens a pull request that adds uses: actions/setup-node@v4 to a workflow. What happens when the workflow runs, and what should the developer change?
Show answer
The run fails because @v4 is a tag, which is mutable, and the policy accepts only full-length commit SHAs (or local ./ paths). The developer should resolve the tag to its full 40-character commit SHA, for example with git ls-remote, replace @v4 with that SHA, and add the version as a trailing comment such as # v4.1.0.