[repo] Hygiene: NUL-safe pre-commit hook, more key formats, 5,000,000-byte limit, QC bypass cases
This commit is contained in:
+92
-55
@@ -1,65 +1,102 @@
|
||||
#!/bin/sh
|
||||
#!/usr/bin/env bash
|
||||
# SPDX-License-Identifier: MPL-2.0
|
||||
#
|
||||
# Ianitor pre-commit hook. Rejects staged files that must never be in git:
|
||||
# - binaries (anything git itself classifies as binary)
|
||||
# - private keys (PEM / OpenSSH / PKCS#8 / PGP headers, and key-store file names)
|
||||
# - files over 5 MB
|
||||
# - build output and dependencies (target/, node_modules/, dist/)
|
||||
# - binaries: any NUL byte anywhere in the staged blob (git attributes do not matter)
|
||||
# - private keys: PEM/PKCS#8/OpenSSH/PGP blocks, SSH2 (RFC 4716) keys, PuTTY .ppk,
|
||||
# age secret keys, JWK private keys, WireGuard PrivateKey lines, and key-store file names
|
||||
# - files over 5 MB (5,000,000 bytes, as CLAUDE.md says)
|
||||
# - build output and dependencies: target/, node_modules/, dist/, build/, .svelte-kit/
|
||||
# Name checks are case-insensitive. Paths are read NUL-separated, so quotes, tabs,
|
||||
# backslashes or newlines in file names cannot slip past. A staged file that cannot be read
|
||||
# blocks the commit.
|
||||
#
|
||||
# Enable once per clone: git config core.hooksPath .githooks
|
||||
# Test it: sh .githooks/test-pre-commit.sh
|
||||
# Plain POSIX sh: works in Git Bash on Windows and on Linux.
|
||||
# Test it: bash .githooks/test-pre-commit.sh
|
||||
# Needs bash 4+ (Git Bash on Windows, any Linux).
|
||||
|
||||
LIMIT=5242880 # 5 MiB
|
||||
set -u
|
||||
LIMIT=5000000
|
||||
|
||||
# Every check prints one "FAIL" line on stdout per problem (reasons go to stderr),
|
||||
# so the loop can run in a pipeline subshell and still be counted here.
|
||||
failures=$(
|
||||
# numstat prints "-<TAB>-<TAB>path" for files git treats as binary.
|
||||
git -c core.quotePath=false diff --cached --numstat --diff-filter=ACMR | while IFS= read -r line; do
|
||||
added=${line%%" "*}
|
||||
rest=${line#*" "}
|
||||
deleted=${rest%%" "*}
|
||||
path=${rest#*" "}
|
||||
if [ "$added" = "-" ] && [ "$deleted" = "-" ]; then
|
||||
printf 'pre-commit: rejected %s: binary file\n' "$path" >&2
|
||||
echo FAIL
|
||||
fi
|
||||
done
|
||||
|
||||
git -c core.quotePath=false diff --cached --name-only --diff-filter=ACMR | while IFS= read -r path; do
|
||||
case "/$path" in
|
||||
*/target/*|*/node_modules/*|*/dist/*)
|
||||
printf 'pre-commit: rejected %s: build output or dependency directory\n' "$path" >&2
|
||||
echo FAIL ;;
|
||||
esac
|
||||
|
||||
base=${path##*/}
|
||||
case "$base" in
|
||||
*.pub) ;;
|
||||
*.key|*.p12|*.pfx|*.jks|*.keystore|id_rsa|id_dsa|id_ecdsa|id_ed25519|id_rsa.*|id_dsa.*|id_ecdsa.*|id_ed25519.*)
|
||||
printf 'pre-commit: rejected %s: private key file name\n' "$path" >&2
|
||||
echo FAIL ;;
|
||||
esac
|
||||
|
||||
# Size of the staged blob, not of the working-tree file.
|
||||
size=$(git cat-file -s ":$path" 2>/dev/null || echo 0)
|
||||
if [ "$size" -gt "$LIMIT" ]; then
|
||||
printf 'pre-commit: rejected %s: %s bytes is over the 5 MB limit\n' "$path" "$size" >&2
|
||||
echo FAIL
|
||||
fi
|
||||
|
||||
if git cat-file -p ":$path" 2>/dev/null | grep -E -q -e '-----BEGIN [A-Z0-9 ]*PRIVATE KEY( BLOCK)?-----'; then
|
||||
printf 'pre-commit: rejected %s: contains a private key\n' "$path" >&2
|
||||
echo FAIL
|
||||
fi
|
||||
done
|
||||
# Patterns are written so that this file does not match them itself.
|
||||
KEY_PATTERNS=(
|
||||
'-----BEGIN [A-Z0-9 ]*PRIVATE KEY( BLOCK)?-----'
|
||||
'---- BEGIN SSH2 ENCRYPTED PRIVATE KEY[ ]----'
|
||||
'---- BEGIN SSH2 PRIVATE KEY[ ]----'
|
||||
'PuTTY-User-Key-File-[0-9]+:'
|
||||
'AGE-SECRET-KEY-[1][0-9A-Z]+'
|
||||
'^[[:space:]]*PrivateKey[[:space:]]*=[[:space:]]*[A-Za-z0-9+/]{42,43}=?[[:space:]]*$'
|
||||
)
|
||||
JWK_KTY='"kt[y]"[[:space:]]*:'
|
||||
JWK_D='"d"[[:space:]]*:[[:space:]]*"'
|
||||
|
||||
case "$failures" in
|
||||
*FAIL*)
|
||||
printf 'pre-commit: commit blocked. Unstage the files above (git rm --cached <file>).\n' >&2
|
||||
exit 1 ;;
|
||||
esac
|
||||
failed=0
|
||||
reject() {
|
||||
# %q keeps odd characters in names visible and harmless.
|
||||
printf 'pre-commit: rejected %q: %s\n' "$1" "$2" >&2
|
||||
failed=1
|
||||
}
|
||||
|
||||
list=$(mktemp 2>/dev/null || echo "${TMPDIR:-/tmp}/ianitor-pre-commit.$$")
|
||||
blob=$(mktemp 2>/dev/null || echo "${TMPDIR:-/tmp}/ianitor-pre-commit-blob.$$")
|
||||
trap 'rm -f "$list" "$blob"' EXIT
|
||||
|
||||
if ! git diff --cached --raw -z --no-abbrev --no-renames --diff-filter=ACMT >"$list"; then
|
||||
echo 'pre-commit: cannot list the staged files; commit blocked.' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# --raw -z gives ":<old mode> <new mode> <old sha> <new sha> <status>" NUL "<path>" NUL.
|
||||
while IFS= read -r -d '' meta && IFS= read -r -d '' path; do
|
||||
read -r _ mode _ sha _ <<<"${meta#:}"
|
||||
lower=${path,,}
|
||||
|
||||
case "/$lower" in
|
||||
*/target/*|*/node_modules/*|*/dist/*|*/build/*|*/.svelte-kit/*)
|
||||
reject "$path" "build output or dependency directory" ;;
|
||||
esac
|
||||
|
||||
base=${lower##*/}
|
||||
case "$base" in
|
||||
*.pub) ;;
|
||||
*.key|*.p12|*.pfx|*.jks|*.keystore|*.ppk|id_rsa|id_dsa|id_ecdsa|id_ed25519|id_rsa.*|id_dsa.*|id_ecdsa.*|id_ed25519.*)
|
||||
reject "$path" "private key file name" ;;
|
||||
esac
|
||||
|
||||
# Submodule entries have no blob to inspect.
|
||||
[ "$mode" = 160000 ] && continue
|
||||
|
||||
if ! git cat-file blob "$sha" >"$blob" 2>/dev/null; then
|
||||
reject "$path" "the staged content cannot be read"
|
||||
continue
|
||||
fi
|
||||
size=$(wc -c <"$blob" | tr -d '[:space:]')
|
||||
if ! [[ $size =~ ^[0-9]+$ ]]; then
|
||||
reject "$path" "the staged size cannot be determined"
|
||||
continue
|
||||
fi
|
||||
if (( size > LIMIT )); then
|
||||
reject "$path" "$size bytes is over the 5 MB limit (5,000,000 bytes)"
|
||||
fi
|
||||
|
||||
textsize=$(tr -d '\000' <"$blob" | wc -c | tr -d '[:space:]')
|
||||
if [ "$textsize" != "$size" ]; then
|
||||
reject "$path" "binary file (contains NUL bytes)"
|
||||
fi
|
||||
|
||||
for pattern in "${KEY_PATTERNS[@]}"; do
|
||||
if LC_ALL=C grep -a -E -q -e "$pattern" "$blob"; then
|
||||
reject "$path" "contains a private key"
|
||||
break
|
||||
fi
|
||||
done
|
||||
if LC_ALL=C grep -a -E -q -e "$JWK_KTY" "$blob" && LC_ALL=C grep -a -E -q -e "$JWK_D" "$blob"; then
|
||||
reject "$path" "contains a JWK private key"
|
||||
fi
|
||||
done <"$list"
|
||||
|
||||
if [ "$failed" -ne 0 ]; then
|
||||
echo 'pre-commit: commit blocked. Unstage the files above (git rm --cached <file>).' >&2
|
||||
exit 1
|
||||
fi
|
||||
exit 0
|
||||
|
||||
Reference in New Issue
Block a user