What a 12-line pre-commit hook catches, and what it doesn't
Disclosure, at the top: this page shows one ad banner, and a sponsored page appears before the full kit if you use the Continue link.
A pre-commit hook is the cheapest place to stop a credential from leaving a laptop. This one is 12 lines of POSIX shell, no dependencies, and it was tested against 22 cases before publishing. Here is exactly what it does, and where it stops.
#!/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 0Install: 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
- file
prod.env: blocked - file
.env.local: blocked - file
.envrc: blocked - file
notes.env.md: blocked - file
.env.example: blocked, a false positive - file
my notes.env: blocked a1.txtcontainingAPI_KEY = "abcdefghijklmnop": blockeda2.txtcontainingSECRET=abcdefghijklmnop: blockeda5.ymlcontainingtoken: ghp_abcdefghijklmnopqrst: blockeda6.jscontainingapiKey = "12345678901234567890": blockeds.pycontainingpassword = "Tr0ub4dor&3xyz1234": blockedfp2.pycontainingpassword = input_password_value: blocked, a false positive- file
environment.md: passed a3.txtcontainingpassword = "shortpw12": passed, by designa4.jscontainingpassword = process.env.PASSWORD: passed, by designa7.pycontainingpassword = getPassword(): passed, by designn.jsoncontaining{"api_key": "abcdefghijklmnop"}: passed, a known misso.txtcontainingsk-abcdefghijklmnopqrstuvwx: passed, a known missp.rbcontainingsecret_key_base = "abcdefghijklmnop": passed, a known missd1.txtcontainingpassword = abc.defghijklmnop: passed, a known missd2.txtcontainingpassword = abcdefghij(klmnop: passed, a known miss- 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