KKAIROS
Practical guideIllustrative3 min read

What a 12-line pre-commit hook catches, and what it doesn't

A pre-commit hook is the cheapest place to stop a credential from leaving a laptop, and this one is 12 lines of POSIX shell with no dependencies. It was tested against 22 cases before publishing. Here is exactly what it does, and where it stops.

Published:

Updated:

Disclosure: this site shows ads that support the free content. The article text has no monetized links.

Ad
#!/bin/sh
# .githooks/pre-commit
names=$(git diff --cached --name-only --diff-filter=ACM)
if printf '%s\n' "$names" | grep -qE '\.env|\.pem$|\.key$|id_rsa'; then
  printf '%s\n' "$names" | grep -E '\.env|\.pem$|\.key$|id_rsa' | sed 's/^/blocked file: /'
  exit 1
fi
if git diff --cached -U0 | grep -E '^\+' | grep -v '^+++' | grep -inE "(api[_-]?key|secret|passw(or)?d|token)[[:space:]]*[:=][[:space:]]*[\"']?[^[:space:]\"'.()]{12,}"; then
  echo "blocked: possible secret in staged diff"
  exit 1
fi
exit 0

Install: git config core.hooksPath .githooks, then chmod +x .githooks/pre-commit. If a hooks path is already set globally, check it with git config --show-origin --get core.hooksPath first.

Blocked in testing: prod.env, .env.local, .envrc, notes.env.md, .env.example, API_KEY = "..." in uppercase, an unquoted SECRET=... line, token: ghp_... in YAML, apiKey = "..." in camelCase, password = "Tr0ub4dor&3xyz1234", and a file named my notes.env.

Let through by design: password = "shortpw12" (10 characters), password = process.env.PASSWORD, password = getPassword(), and removing a leaked secret.

Missed (the 5 known misses in the list above were tested: JSON style, a bare sk-... key, secret_key_base, and a secret cut short at . or (; the rest are by design): JSON style {"api_key": "..."}, a bare sk-... key, secret_key_base = "...", secrets under 12 characters, secrets split across lines, base64 blobs, keys already in history, a DB_PASS= line, and filenames containing newlines. A real secret containing ., (, ) or a quote inside is cut short at that character, so a value like abc.defghijklmnop is missed.

Two false positives to expect: .env.example is blocked, because the name check matches any path containing .env; and a long variable name or long plain words after the key name are blocked too, for example password = input_password_value. .env.example is the one most people do commit, so rename it or narrow the pattern.

The 22 cases

  1. file prod.env: blocked
  2. file .env.local: blocked
  3. file .envrc: blocked
  4. file notes.env.md: blocked
  5. file .env.example: blocked, a false positive
  6. file my notes.env: blocked
  7. a1.txt containing API_KEY = "abcdefghijklmnop": blocked
  8. a2.txt containing SECRET=abcdefghijklmnop: blocked
  9. a5.yml containing token: ghp_abcdefghijklmnopqrst: blocked
  10. a6.js containing apiKey = "12345678901234567890": blocked
  11. s.py containing password = "Tr0ub4dor&3xyz1234": blocked
  12. fp2.py containing password = input_password_value: blocked, a false positive
  13. file environment.md: passed
  14. a3.txt containing password = "shortpw12": passed, by design
  15. a4.js containing password = process.env.PASSWORD: passed, by design
  16. a7.py containing password = getPassword(): passed, by design
  17. n.json containing {"api_key": "abcdefghijklmnop"}: passed, a known miss
  18. o.txt containing sk-abcdefghijklmnopqrstuvwx: passed, a known miss
  19. p.rb containing secret_key_base = "abcdefghijklmnop": passed, a known miss
  20. d1.txt containing password = abc.defghijklmnop: passed, a known miss
  21. d2.txt containing password = abcdefghij(klmnop: passed, a known miss
  22. cleanup commit removing a committed secret ("leak.py"): passed, removing a secret is not blocked

For anything past this, gitleaks or trufflehog in CI, and a git log -p sweep for keys already in history.

Full kit: Secrets Safety Kit