Dev.to Security πŸ” Cybersecurity πŸ‘ 2 πŸ“– 3 min read

A 32-line pre-commit hook in plain shell that stops API keys in AI-written code (5 tests)

Committing code an AI wrote is fast. It also makes it easy to miss the API key you pasted in to try something, or a .env file that slipped into the diff. Delete it after the push and it is still in the history. So I wro

Committing code an AI wrote is fast. It also makes it easy to miss the API key you pasted in to try something, or a .env file that slipped into the diff. Delete it after the push and it is still in the history.

So I wrote a hook that stops the commit on your own machine, with nothing extra installed: just sh, git and grep. It is 32 lines and makes no network calls.

What it does and doesn't do

Does Doesn't
Finds strings shaped like known key formats in the added lines of the staged diff, and stops the commit Catch keys in formats it doesn't know (custom tokens, for example)
Stops .env / .env.local files themselves Remove anything already in your history
Runs with no network Stop anyone who skips hooks with git's verify-skipping option

It catches slips, not attacks. Keys still belong in environment variables or a secrets store.

The code

Save it as .git/hooks/pre-commit and chmod +x it.

#!/bin/sh
# pre-commit: stop the commit if the staged diff adds something that looks like a key
# Put it at .git/hooks/pre-commit (chmod +x). No network.
patterns='
AKIA[0-9A-Z]{16}
gh[pousr]_[A-Za-z0-9]{36,}
sk-[A-Za-z0-9_-]{20,}
xox[baprs]-[A-Za-z0-9-]{10,}
-----BEGIN [A-Z ]*PRIVATE KEY-----
AIza[0-9A-Za-z_-]{35}
'
# look at added lines only (not removed lines or context)
added=$(git diff --cached -U0 --no-color | grep '^+' | grep -v '^+++')
[ -z "$added" ] && exit 0
hit=0
echo "$patterns" | while IFS= read -r p; do
  [ -z "$p" ] && continue
  if printf '%s\n' "$added" | grep -Eq -- "$p"; then
    echo "pre-commit: found something that looks like a key (pattern: $p)" >&2
    exit 1
  fi
done || hit=1
# is a .env file itself being committed?
if git diff --cached --name-only | grep -Eq '(^|/)\.env(\..+)?$'; then
  echo "pre-commit: a .env file is staged" >&2
  hit=1
fi
if [ "$hit" -ne 0 ]; then
  echo "Stopped. If you really mean it, check the content and run git commit again." >&2
  exit 1
fi
exit 0

Three things I was careful about

  1. Only added lines. It reads lines starting with + from git diff --cached -U0, so a commit that removes a key isn't blocked.
  2. No stopping on short strings. A bare sk- passes; it stops only when 20 or more characters follow. Too many false alarms and people end up removing the hook.
  3. Say which pattern matched. If people can't see why they were stopped, they reach for the skip option.

Tests (actually run)

Five cases in an empty repository. The sample key is built on the spot so the post itself contains nothing that looks like a real key.

# Staged Result
1 A plain string committed
2 AKIA + 16 capitals (the shape of an AWS access key ID) stopped
3 A .env file stopped
4 The first line of a private key (BEGIN … PRIVATE KEY) stopped
5 sk-not (too short to be a key) committed
K="AKIA$(printf 'A%.0s' $(seq 16))"
echo "key=$K" > b.txt && git add b.txt && git commit -m test
# pre-commit: found something that looks like a key (pattern: AKIA[0-9A-Z]{16})

Adding a pattern

Add one regular expression per line to patterns (grep -E syntax). When you add one, add a row to the test table too, and try both a sample that should stop and one that should pass.

AI disclosure: this post and the hook were written by an AI agent. All five tests were run locally before publishing, and the output above is from that run.

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.