Sub topic 7
Requiring SHA pinning with repository and organization policy
This chapter assumes you can read a GitHub Actions workflow and know what uses: does. It also assumes you have seen a full-length commit SHA pin (covered earlier in this tutorial) and have admin access to a repository or organization to try the settings. Pinning by convention depends on every contributor remembering the rule. A policy makes GitHub check the rule for you.
Why convention is not enough
Pinning an action to a commit SHA protects you only where the pin exists. Consider how a single unpinned line can appear:
- A contributor copies a snippet from a README that says
uses: some-org/some-action@v2. - A bot or template generates a workflow with tag references.
- A reviewer approves a pull request without noticing that one of 40 changed lines is a tag reference.
Each of these puts a mutable reference back into a pipeline you believed was pinned. Reviewers miss this kind of change because a tag looks harmless. Enforcement moves the check from human attention to the platform.
The policy: require full-length SHA pinning
GitHub offers a setting named Require actions to be pinned to a full-length commit SHA. It sits with the other Actions permissions and can be turned on at three levels:
| Level | Where to find it | Effect |
|---|---|---|
| Repository | Settings, Actions, General, Actions permissions | Applies to workflows in that repository |
| Organization | Organization settings, Actions, General | Applies to repositories in the organization |
| Enterprise | Enterprise policies, Actions | Applies to organizations in the enterprise |
When the setting is on, a workflow that references an action or reusable workflow by tag or branch fails instead of running that code. The error names the offending reference, so the fix is to replace it with a SHA.
Settings at a higher level take precedence. If an organization turns the policy on, a repository admin cannot turn it off for that repository. This is the property that makes the organization-level setting useful: no single repository can opt out.
The names and menu locations of GitHub settings change over time. If a label differs from the one above, look for the option containing "pinned" and "commit SHA" in the Actions permissions section, and check the current GitHub documentation.
What the policy checks
The policy looks at the uses: value. A reference passes when the part after @ is a full 40-character commit SHA.
steps:
# Fails under the policy: tag reference
- uses: actions/checkout@v4
# Fails under the policy: branch reference
- uses: actions/checkout@main
# Fails under the policy: abbreviated SHA
- uses: actions/checkout@11bd719
# Passes: full-length SHA, version recorded in a comment
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2Abbreviated SHAs fail because a short prefix could in principle match more than one commit, and GitHub does not accept it as an identifier for uses:.
Local references such as uses: ./.github/actions/setup-tools are not affected. They point at files in the same checkout, so their content is already fixed by the commit that triggered the run.
Turning it on with the API
The same setting is available through the REST API, which helps when you manage many repositories. This example uses the GitHub CLI. Replace OWNER and REPO with real values.
gh api --method PUT /repos/OWNER/REPO/actions/permissions \
-F enabled=true \
-f allowed_actions=all \
-F sha_pinning_required=trueThe PUT call replaces the repository's Actions permissions, so include the values you want to keep. In this example, allowed_actions=all states that any action may be used as long as it is pinned. Read the current values first with a GET request to the same path, then send back what you want to keep along with the new field.
Organizations have an equivalent endpoint at /orgs/ORG/actions/permissions. It also requires you to say which repositories the permissions apply to, through enabled_repositories. Check the API reference for the exact parameters, because they are the part most likely to change.
Rolling out a policy without a day of broken builds
Switching the policy on in a large organization will break every workflow that still has a tag reference. A staged rollout avoids that.
- Inventory. Search your repositories for
uses:lines that do not end in a 40-character hexadecimal string. A regular expression such asuses: [^./][^@]+@(?![0-9a-f]{40}\b)finds candidates with a tool that supports lookahead, such asgrep -P. - Convert. Replace each reference with a SHA and a version comment. Tools such as pinact do this in bulk. Review the result, because an automated conversion is only as trustworthy as the SHA it resolved.
- Add Dependabot. Without it, pins go stale and people start disabling the policy to get updates. Use the
github-actionsecosystem configuration from the earlier chapter. - Enable on a pilot repository. Confirm that workflows pass and the error messages are understood.
- Enable at the organization level. Do this when most repositories are already converted. Expect a few failures in forgotten repositories.
Combining the policy with an allowlist
SHA pinning controls how you reference an action. The allowlist controls which actions you may reference at all. They are separate settings and work together.
Under Allow select actions and reusable workflows, you can permit:
- actions created by GitHub,
- actions from verified creators on the Marketplace,
- a list of patterns you write, such as
my-org/*oractions/cache@*.
An allowlist entry can name a specific SHA as well, which narrows approval to exactly one commit. This is stricter than the pinning policy alone: the pinning policy says "the reference must be a SHA", and the allowlist entry says "and it must be this SHA". Teams that review third-party actions before approving them can use this to record what they reviewed.
The cost is maintenance. Every Dependabot update to a new SHA is blocked until someone adds the new SHA to the list. For most teams, an allowlist at the level of repository owner, with the pinning policy on top, is the more workable balance.
What the policy does not do
The policy checks the form of the reference. It does not check the content behind it. Keep these limits in mind.
- It does not verify that the SHA is a good commit. A full SHA that points to malicious code passes the check. Resolving the SHA from the right repository and release is still your job, as described in the chapter on finding and verifying SHAs.
- It does not verify that the commit belongs to the repository you named. A SHA from a fork can resolve through the original repository's name. That caveat is covered in the chapter on impostor commits.
- It does not rewrite nested references. If a composite action you use contains its own
uses:with a tag, that inner reference is resolved by the action's author's choice. Depending on how GitHub evaluates the policy for nested references, the enforcement may or may not reach that inner reference. Do not assume it does. Test with a deliberately unpinned inner reference, and read the documentation for your GitHub version. - It does not cover everything that can run. Container references such as
docker://image:tagand scripts that download code at run time (curl | bash) are separate mechanisms. Pin images by digest and treat download scripts as dependencies in their own right.
Catching problems before the run fails
The platform policy fails a workflow at run time. That is late: the contributor has already pushed and is waiting on a red check. Pair the policy with a check that runs on the pull request and tells the author which line to change.
Static analysis tools for workflows can do this. For example, zizmor has an audit for unpinned uses: references, and pinact can run in a check mode. Either can run as a step in a lightweight workflow. Whichever you choose, the setup is the same:
- run it on every pull request that changes
.github/workflows/**, - make it a required status check,
- have it point at the exact file and line.
The tool and the platform policy overlap on purpose. The tool gives fast feedback to humans. The policy is the backstop that cannot be forgotten or bypassed by editing the tool's configuration.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Workflow fails right after enabling the policy | A `uses:` still has a tag, branch, or short SHA | Replace it with the full 40-character SHA |
| Dependabot pull requests are blocked by an allowlist | The new SHA is not on your list of allowed patterns | Allow the owner or action with a wildcard, or add the new SHA |
| Repository admin cannot change the setting | The organization or enterprise enforces it | Ask the organization owner; this is the intended behavior |
| A pinned workflow still ran unexpected code | A nested action inside a composite action uses a tag | Inspect the action's `action.yml`, and consider a lockfile (later chapters) |
| `git ls-remote` shows two lines for a tag | The tag is annotated; the second line (`^{}`) is the commit | Use the commit SHA, not the tag object SHA |
The last row deserves emphasis. Using the tag object SHA from an annotated tag in uses: can resolve incorrectly or fail. Always take the SHA from the ^{} line.
Exercise
Your organization enables the "require full-length commit SHA" policy. A teammate's workflow uses uses: some-org/build-action@3f2a9c1 (a 7-character SHA) and uses: ./.github/actions/lint. Which of these two references does the policy reject, and why? Then name one risk the policy leaves open even when every reference passes.
Show answer
The policy rejects some-org/build-action@3f2a9c1, because an abbreviated SHA is not a full-length 40-character commit SHA. The local reference ./.github/actions/lint passes, since it points to files in the same repository checkout and is fixed by the triggering commit. The policy still leaves open any risk in the content behind a valid SHA: for example, a full SHA that points to a malicious commit, a SHA from a fork resolved through the original repository name, or a tag-based reference nested inside a composite action.
Exercise
You want to roll out SHA pinning enforcement to 200 repositories. List the order of steps you would use, and say why enabling Dependabot comes before turning the policy on for the whole organization.
Show answer
Inventory unpinned references, convert them to full SHAs with version comments, add Dependabot for the github-actions ecosystem, enable the policy on a pilot repository and fix failures, then enable it for the organization. Dependabot comes first because pinned actions go stale without automated update pull requests. If updates are manual and painful, teams are tempted to weaken or disable the policy to get new versions.