Skip to content
Tutorials Center
My learning

Topics

Lesson 64 of 78 ยท GitHub and Platforms

Git Supply-Chain Security

Protect your software supply chain with Git - protected branches, signed tags, secret scanning, pinned dependencies and update bots - with runnable server-side rule demos.

Your code does not only come from you. It also comes from the libraries you install, the build scripts you run, the CI workflows that publish releases, and every person with push access. An attacker who sneaks one change into any of those links can ship malware to all of your users. Protecting that chain is called supply-chain security, and Git is where most of the protection starts: it is the place where changes are proposed, reviewed, signed and tagged.

This lesson builds on Signing, Credentials and Security. There you learned how to sign a commit and why secrets in history are a problem. Here we look at the team and platform level: protecting important branches, trusting release tags, catching secrets before they leave your machine, and keeping dependencies current. Hosting settings (GitHub, GitLab) are shown as read-only text, but the sandbox can run a bare repository with real server-side rules, so you can watch protection being enforced.

Where the chain can break

Think of every step between a keyboard and a user's machine as a link, and ask how each one could be abused.

flowchart LR
    A["Author<br/>stolen account or fake identity"] --> B["Branch<br/>unreviewed push or force push"]
    B --> C["CI<br/>poisoned workflow or secret leak"]
    C --> D["Release tag<br/>moved or forged tag"]
    D --> E["Users<br/>install the release"]
    F["Dependencies<br/>outdated or malicious package"] --> C

Each box is a place to add a control: review and branch rules for the first two, scanning for CI, signed tags for releases, pinning and update bots for dependencies.

No single control is enough. The goal is layers, so that a mistake or a compromise at one link is caught by another.

Protected branches

A protected branch is a branch the server refuses to change in dangerous ways. The rules live on the server (GitHub, GitLab, Bitbucket, or your own Git host), because anything configured only on a developer's laptop can be skipped. GitHub offers classic branch protection rules and the newer rulesets, which can target many branches or tags by pattern and can be applied across an organization. Typical rules for main and release branches:

  • Require a pull request before merging, with one or more approvals, and dismiss approvals when new commits are pushed (Pull Requests and Code Review).
  • Require status checks (tests, linters, security scans) to pass, and require the branch to be up to date.
  • Block force pushes and block deletion, so history cannot be rewritten (Rewriting History Safely explains why force pushes are dangerous).
  • Require signed commits or a linear history if your policy needs them.
  • Require review from code owners for sensitive paths, using a CODEOWNERS file.
  • Do not allow bypass, including for administrators, or at least log every bypass.

A CODEOWNERS file maps paths to people or teams. It lives in .github/CODEOWNERS (or the repository root or docs/), and a rule says who must approve changes to that path:

TEXT
# Everything needs the platform team by default
*                       @acme/platform

# Workflows and release scripts need the security team too
/.github/workflows/     @acme/security
/scripts/release.sh     @acme/security

# Dependency manifests need a maintainer
package.json            @acme/maintainers
package-lock.json       @acme/maintainers

A read-only example: the last matching pattern wins, like in .gitignore.

Note the workflow line. A file under .github/workflows/ can run code with access to your secrets, so it deserves the strictest review of anything in the repository.

Seeing server-side protection in action

You can enforce the same ideas on any bare repository with plain Git settings and a hook. The demo below creates a "server" with receive.denyNonFastForwards and receive.denyDeletes (the Git equivalents of "block force pushes" and "block deletion") and then tries to break both.

Bash A server that refuses force pushes and deletions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Both attempts were refused by the server, and the remote history is untouched. A hosting service does the same with a setting instead of a config line, and adds the more social rules (approvals, status checks) on top.

A pre-receive hook as a gatekeeper

Some rules are policy rather than a Git setting. A pre-receive hook runs on the server before any ref is updated and can reject the push with a message. Hosting platforms let you do this through rulesets, but a plain hook shows the mechanism. This one allows changes to main only when each new commit's message mentions a ticket number:

Bash Reject pushes to main without a ticket id

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The server printed its own message, prefixed by remote:, and refused the update. Because the hook runs on the server, a developer cannot skip it with --no-verify, unlike a local pre-commit hook (Git Hooks).

Signed tags for releases

Branches move; release tags should not. People download "version 2.4.0" and assume it is what the maintainers published. Two things can go wrong: someone moves the tag to a different commit, or someone creates a tag pretending to be you. A signed annotated tag protects against both, because the signature covers the tagged commit and the tag message, and only the key holder can make it.

sequenceDiagram
    participant M as Maintainer
    participant R as Repository
    participant U as User or CI
    M->>R: git tag -s v2.4.0
    M->>R: git push origin v2.4.0
    U->>R: git fetch --tags
    U->>U: git tag -v v2.4.0
    Note over U: Good signature from a trusted key?<br/>Only then build or deploy

Signing happens once; verifying happens everywhere the release is consumed.

Bash Create and verify a signed release tag

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

After the tag was replaced, verification failed: the new tag has no signature. In a real release process the check is automated. A deployment script runs git tag -v (or git verify-tag) against a list of trusted maintainer keys and stops if it fails. Also note the last two lines: an annotated tag is a real object of type tag, but a lightweight tag is only a name that points straight at a commit (commit), so it cannot carry a signature.

Platforms add their own layer. GitHub shows Verified next to tags and commits signed with a key registered to the account, and a ruleset can restrict tag creation and deletion so that v* tags can only be made by release managers and never moved. Treat tags as immutable: if a release is bad, publish a new version instead of moving the old tag (Tags and Releases).

Catch secrets before they leave your laptop

A leaked API key in a public repository is exploited within minutes by bots that scan new commits. Removing it in the next commit does not help, because the secret stays in history (Signing, Credentials and Security shows how). The best defence is three layers:

  1. Never put secrets in the repository. Read them from environment variables or a secret manager, and keep .env files in .gitignore.
  2. Scan locally with a pre-commit hook or a scanner such as gitleaks or trufflehog, so the commit never happens.
  3. Turn on push protection on the server. GitHub's secret scanning recognizes hundreds of token formats and blocks the push; for tokens it finds anyway, it alerts you and often the provider revokes them automatically. GitLab has an equivalent.

A scanner is mostly pattern matching plus entropy checks. A minimal local version fits in a hook:

Bash A tiny pre-commit secret scanner

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The first commit passed, the second was blocked, and only the fake value's first characters were printed, so that the scanner's own output does not leak the secret into logs. Remember that local hooks can be skipped, which is why the server-side push protection is the layer you can rely on. If a secret does get pushed, treat it as compromised: revoke and rotate it first, and only then clean the history if you need to.

Dependencies and update bots

Most code in a modern project is somebody else's. Two Git-adjacent habits matter most.

Pin what you build. A lockfile (package-lock.json, poetry.lock, Cargo.lock, go.sum) records the exact versions and checksums that were tested. Commit it, and install from it in CI (npm ci, pip install --require-hashes, cargo build --locked). Pin CI actions too: a tag like actions/checkout@v4 can be moved, but a full commit hash cannot, so uses: actions/checkout@<40-character-sha> is the safest form (GitHub Actions Basics). The same idea applies to submodules, which record an exact commit (Submodules and Subtrees).

Bash Why a commit hash is a safer pin than a tag

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The tag silently changed meaning, while the hash still points to the code that was reviewed. Because a commit hash covers the whole content and history, it cannot be faked.

Keep dependencies updated. Old versions collect known vulnerabilities. Update bots such as Dependabot (GitHub) and Renovate (many platforms) open pull requests when a new version appears, so updates arrive as small, reviewable changes instead of a risky yearly upgrade. They are configured by a file in the repository. A read-only example for Dependabot:

YAML
# .github/dependabot.yml
version: 2
updates:
  - package-ecosystem: "npm"
    directory: "/"
    schedule:
      interval: "weekly"
    open-pull-requests-limit: 5
  - package-ecosystem: "github-actions"
    directory: "/"
    schedule:
      interval: "weekly"

A bot is only helpful if your pipeline is. Let CI run the tests on every bot pull request, require review before merging, and be wary of auto-merging anything that is not a patch update. Separate security alerts (a known vulnerable version, with a suggested fix) from routine version updates, and act on the former quickly.

A practical checklist

For a repository that matters, work through this list. Each item maps to something you have met in this track.

  • Branch protection or a ruleset on main and release branches: pull requests, reviews, status checks, no force push, no deletion.
  • CODEOWNERS for workflows, release scripts and dependency manifests.
  • Two-factor authentication for every account with write access; short-lived or fine-grained tokens; no shared accounts.
  • Signed commits where policy needs them, and signed annotated tags for every release, verified by whoever consumes them.
  • Tag rules that stop v* tags from being moved or deleted.
  • Secret scanning with push protection on, plus a local hook for early feedback.
  • Lockfiles committed, CI actions pinned to commit hashes, dependencies updated by a bot with review.
  • Minimal permissions for CI tokens, and no secrets exposed to workflows triggered by untrusted forks (Forks and Upstreams).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why should branch protection live on the server rather than only in a local hook?

    Anything configured only on a laptop can be skipped or removed. A server-side rule (protected branch, ruleset or pre-receive hook) applies to every push.
  2. 2. Which Git server settings correspond to 'block force pushes' and 'block branch deletion'?

    denyNonFastForwards rejects pushes that rewrite history, and denyDeletes rejects deleting a ref. Hosting services expose the same ideas as protection rules.
  3. 3. Why is a signed annotated tag better for releases than a lightweight tag?

    A lightweight tag is only a name for a commit. An annotated tag is an object that can be signed, and git tag -v verifies it.
  4. 4. What is the safest way to pin a third-party CI action or a library from Git?

    Branches and tags can be moved to different code. A full commit hash identifies exact content and cannot be changed without changing the hash.
  5. 5. A real API key was pushed to a public repository. What is the right first step?

    Bots copy leaked keys within minutes, and history keeps the old commit anyway. Assume the key is compromised and replace it first.

Summary

  • Supply-chain security means adding controls at every link: authors, branches, CI, release tags and dependencies.
  • Protect important branches on the server: required pull requests and reviews, status checks, no force pushes, no deletion, CODEOWNERS for sensitive paths.
  • Server-side rules (receive.denyNonFastForwards, receive.denyDeletes, pre-receive hooks, rulesets) cannot be skipped like local hooks.
  • Sign annotated release tags, verify them with git tag -v, and never move a published tag.
  • Keep secrets out of the repository, scan locally, and enable push protection; if one leaks, revoke and rotate it first.
  • Commit lockfiles, pin CI actions and dependencies to exact hashes, and let Dependabot or Renovate open reviewable update pull requests.

Next: Fixing Common Git Mistakes