How-Tos

How-To: Pin Your GitHub Actions to Commit SHAs So a Moved Tag Can’t Hijack Your CI

Leave a comment

When an attacker gets a maintainer’s token, one of the nastiest moves is re-pointing a tag like v4 at malicious code. Every workflow that says @v4 picks it up on the next run. Pinning to a full commit SHA stops that. Here’s how I do it.

1. Find unpinned actions

grep -rnE 'uses: [^ ]+@[^ ]+' .github/workflows/ | grep -vE '@[0-9a-f]{40}'

2. Look up the commit SHA behind a tag

# example: actions/checkout tag v4
gh api repos/actions/checkout/git/ref/tags/v4 --jq '.object.type + " " + .object.sha'
# if type is "tag" (annotated), dereference it to the commit:
gh api repos/actions/checkout/git/tags/<TAG_OBJECT_SHA> --jq .object.sha

3. Pin it, and keep the version as a comment

- uses: actions/checkout@<40-CHAR-COMMIT-SHA> # v4

4. Let Dependabot keep pins fresh

# .github/dependabot.yml
version: 2
updates:
  - package-ecosystem: "github-actions"
    directory: "/"
    schedule:
      interval: "weekly"

Dependabot understands SHA pins with a version comment and opens PRs to bump them, so you get updates you can review instead of updates that just happen.

My take

Tags are bookmarks anyone with write access can move. Commits aren’t. Pin the ones that touch your secrets first.

God Bless, and Use the Source!

References & Further Reading

Subscribe (RSS) Follow How-Tos via RSS

Leave a note

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.