Skip to main content

Pre-commit security checks that developers keep switched on

A secret that reaches a commit is already a problem. Even if the next commit deletes it, it lives in history, in every clone and in every fork, and the only real fix is to rotate it. A pre-commit check is the one control that runs before that happens, on the developer's own machine, while the change is still a few lines in a staging area.

It is also the control developers turn off fastest when it gets in the way. This post covers what belongs in a pre-commit hook, how to install it so the whole team gets it, how to keep it fast enough that nobody reaches for --no-verify, and what to move to CI instead.

What a pre-commit hook should check

The job of a pre-commit hook is narrow: stop the mistakes that are expensive to undo once committed, and do it in a couple of seconds. Two checks earn their place.

Secrets. API keys, private keys, database URLs with passwords, cloud credentials, tokens in .env files that were never meant to be staged. These are the reason pre-commit security exists. Once pushed, a secret has to be treated as exposed, and cleaning it out of history means rewriting shared branches.

New high-confidence findings in the change. An injection sink, a disabled TLS check, a route with no auth guard. The key word is new. A hook that reports the 400 findings already in the repository is a hook that gets uninstalled by lunchtime.

Everything else (full dependency audits, whole-repository scans, licence checks, SBOM generation) belongs further down the pipeline. More on that below.

Plain git hooks vs the pre-commit framework

Git runs .git/hooks/pre-commit before it creates a commit, and aborts if the script exits non-zero. The catch is that .git/hooks is not part of the repository, so a hook you write there exists only on your machine. Teams usually solve this in one of three ways.

Approach How it is shared Good for
Hook script in .git/hooks Each developer installs it A single tool, one language, small teams
core.hooksPath pointing at a tracked folder git config core.hooksPath .githooks once per clone Plain shell hooks kept in the repo
A hook manager (the pre-commit framework, husky) A config file in the repo, plus one install command Several tools, several languages

The pre-commit framework is the common choice for polyglot repositories. It reads .pre-commit-config.yaml, installs each hook in its own environment, and by default runs hooks against staged content only, stashing unstaged changes so the check sees exactly what will be committed. Hooks from remote repositories are pinned with rev, and the documentation recommends a tag rather than a branch name, since a branch can change under you. That is the same supply-chain lesson as pinning CI actions.

# .pre-commit-config.yaml
repos:
  - repo: local
    hooks:
      - id: vulkro-quick
        name: vulkro quick scan
        entry: vulkro scan quick .
        language: system
        pass_filenames: false

To skip one hook for one commit, the framework honours SKIP=hook-id git commit ..., which is better than --no-verify because it leaves every other hook running.

Keeping it fast

Speed decides whether a hook survives. A few rules that hold across tools:

  • Scan the change, not the repository. Staged files, or the diff against HEAD. A full scan belongs in CI.
  • Report only what is new. Compare against the last commit so existing debt never blocks a change.
  • Prefer high confidence. A pre-commit false positive costs a developer's flow. Tune for precision here and for recall in CI.
  • Stay offline. A hook that calls a network service is slow on a train and broken on a plane.
  • Make the bypass explicit and visible. People will skip hooks in an emergency. Give them a named way to do it rather than teaching everyone --no-verify.

If a check cannot run in a few seconds on a typical change, move it to pre-push or CI rather than making every commit wait.

Pre-commit or pre-push

pre-push runs when the developer pushes, not on every commit. It is the right home for checks that are too slow for each commit but should still run before code leaves the machine, such as a quick scan of the source tree. Many teams end up with a light pre-commit hook and a slightly heavier pre-push hook.

What belongs in CI instead

Client-side hooks are a convenience, not a control. git commit --no-verify skips the pre-commit hook, a fresh clone has no hooks until someone installs them, and web edits and bot commits never run them. Anything you need to be true of every merged change has to be enforced on the server side:

  • The full scan of the repository on every pull request, gated on new Critical and High findings.
  • Dependency and CVE checks against the lockfile.
  • Secrets scanning across git history, which catches what a skipped hook let through.
  • SBOMs, compliance evidence and anything an auditor will ask for.

Think of the hook as the fast feedback loop and CI as the gate. The hook catches most mistakes in seconds; CI catches the rest before merge.

How Vulkro does it

vulkro install-hook writes both hooks for you (install-hook docs):

vulkro install-hook                       # pre-commit and pre-push
vulkro install-hook --pre-push            # only the pre-push hook
VULKRO_SKIP_HOOKS=1 git commit -m "wip"   # skip the hooks once
  • pre-commit first runs vulkro ctx check --staged, which reports call sites left on an old signature, removed names still used and syntax errors. It warns by default; --block-on-ctx-check makes it block. Then it runs the new-findings gate against HEAD, so only findings your change introduces can stop the commit.
  • pre-push runs a quick, high-confidence scan of src.

The hook file gets a marked Vulkro block, so re-running the command is safe and it composes with an existing husky or hand-written hook instead of replacing it (--force replaces the whole file, --uninstall removes the block). The pre-commit step makes no outbound request, and VULKRO_OFFLINE=1 in the hook's environment makes both hooks fully network-silent. Your source and findings stay on the machine.

If you use the pre-commit framework instead, vulkro scan quick . is the preset meant for this spot: high confidence only, source files only, and changes since HEAD~1. In CI, the same scanner runs with the ci preset or vulkro gate --base origin/main.

FAQ

What should a pre-commit hook check for security?

Secrets first, because a committed secret has to be rotated. Then new, high-confidence findings in the staged change. Leave full-repository scans, dependency audits and compliance output to CI.

Can developers bypass pre-commit hooks?

Yes. git commit --no-verify skips them, and a new clone has none until installed. That is why every check that must hold for merged code also has to run in CI.

Is the pre-commit framework better than plain git hooks?

For repositories with several tools or languages, usually yes: the config is versioned, hooks are pinned and installed consistently. For a single check, a tracked hooks folder with core.hooksPath is simpler.

Start a 14-day trial at /start/ and run vulkro install-hook in your repository. Related: Secrets in git history and Diff-scoped security gates.