[repo] Hygiene: NUL-safe pre-commit hook, more key formats, 5,000,000-byte limit, QC bypass cases
This commit is contained in:
+82
-45
@@ -1,65 +1,102 @@
|
|||||||
#!/bin/sh
|
#!/usr/bin/env bash
|
||||||
# SPDX-License-Identifier: MPL-2.0
|
# SPDX-License-Identifier: MPL-2.0
|
||||||
#
|
#
|
||||||
# Ianitor pre-commit hook. Rejects staged files that must never be in git:
|
# Ianitor pre-commit hook. Rejects staged files that must never be in git:
|
||||||
# - binaries (anything git itself classifies as binary)
|
# - binaries: any NUL byte anywhere in the staged blob (git attributes do not matter)
|
||||||
# - private keys (PEM / OpenSSH / PKCS#8 / PGP headers, and key-store file names)
|
# - private keys: PEM/PKCS#8/OpenSSH/PGP blocks, SSH2 (RFC 4716) keys, PuTTY .ppk,
|
||||||
# - files over 5 MB
|
# age secret keys, JWK private keys, WireGuard PrivateKey lines, and key-store file names
|
||||||
# - build output and dependencies (target/, node_modules/, dist/)
|
# - 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
|
# Enable once per clone: git config core.hooksPath .githooks
|
||||||
# Test it: sh .githooks/test-pre-commit.sh
|
# Test it: bash .githooks/test-pre-commit.sh
|
||||||
# Plain POSIX sh: works in Git Bash on Windows and on Linux.
|
# 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),
|
# Patterns are written so that this file does not match them itself.
|
||||||
# so the loop can run in a pipeline subshell and still be counted here.
|
KEY_PATTERNS=(
|
||||||
failures=$(
|
'-----BEGIN [A-Z0-9 ]*PRIVATE KEY( BLOCK)?-----'
|
||||||
# numstat prints "-<TAB>-<TAB>path" for files git treats as binary.
|
'---- BEGIN SSH2 ENCRYPTED PRIVATE KEY[ ]----'
|
||||||
git -c core.quotePath=false diff --cached --numstat --diff-filter=ACMR | while IFS= read -r line; do
|
'---- BEGIN SSH2 PRIVATE KEY[ ]----'
|
||||||
added=${line%%" "*}
|
'PuTTY-User-Key-File-[0-9]+:'
|
||||||
rest=${line#*" "}
|
'AGE-SECRET-KEY-[1][0-9A-Z]+'
|
||||||
deleted=${rest%%" "*}
|
'^[[:space:]]*PrivateKey[[:space:]]*=[[:space:]]*[A-Za-z0-9+/]{42,43}=?[[:space:]]*$'
|
||||||
path=${rest#*" "}
|
)
|
||||||
if [ "$added" = "-" ] && [ "$deleted" = "-" ]; then
|
JWK_KTY='"kt[y]"[[:space:]]*:'
|
||||||
printf 'pre-commit: rejected %s: binary file\n' "$path" >&2
|
JWK_D='"d"[[:space:]]*:[[:space:]]*"'
|
||||||
echo FAIL
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
git -c core.quotePath=false diff --cached --name-only --diff-filter=ACMR | while IFS= read -r path; do
|
failed=0
|
||||||
case "/$path" in
|
reject() {
|
||||||
*/target/*|*/node_modules/*|*/dist/*)
|
# %q keeps odd characters in names visible and harmless.
|
||||||
printf 'pre-commit: rejected %s: build output or dependency directory\n' "$path" >&2
|
printf 'pre-commit: rejected %q: %s\n' "$1" "$2" >&2
|
||||||
echo FAIL ;;
|
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
|
esac
|
||||||
|
|
||||||
base=${path##*/}
|
base=${lower##*/}
|
||||||
case "$base" in
|
case "$base" in
|
||||||
*.pub) ;;
|
*.pub) ;;
|
||||||
*.key|*.p12|*.pfx|*.jks|*.keystore|id_rsa|id_dsa|id_ecdsa|id_ed25519|id_rsa.*|id_dsa.*|id_ecdsa.*|id_ed25519.*)
|
*.key|*.p12|*.pfx|*.jks|*.keystore|*.ppk|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
|
reject "$path" "private key file name" ;;
|
||||||
echo FAIL ;;
|
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# Size of the staged blob, not of the working-tree file.
|
# Submodule entries have no blob to inspect.
|
||||||
size=$(git cat-file -s ":$path" 2>/dev/null || echo 0)
|
[ "$mode" = 160000 ] && continue
|
||||||
if [ "$size" -gt "$LIMIT" ]; then
|
|
||||||
printf 'pre-commit: rejected %s: %s bytes is over the 5 MB limit\n' "$path" "$size" >&2
|
if ! git cat-file blob "$sha" >"$blob" 2>/dev/null; then
|
||||||
echo FAIL
|
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
|
fi
|
||||||
|
|
||||||
if git cat-file -p ":$path" 2>/dev/null | grep -E -q -e '-----BEGIN [A-Z0-9 ]*PRIVATE KEY( BLOCK)?-----'; then
|
textsize=$(tr -d '\000' <"$blob" | wc -c | tr -d '[:space:]')
|
||||||
printf 'pre-commit: rejected %s: contains a private key\n' "$path" >&2
|
if [ "$textsize" != "$size" ]; then
|
||||||
echo FAIL
|
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
|
fi
|
||||||
done
|
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"
|
||||||
|
|
||||||
case "$failures" in
|
if [ "$failed" -ne 0 ]; then
|
||||||
*FAIL*)
|
echo 'pre-commit: commit blocked. Unstage the files above (git rm --cached <file>).' >&2
|
||||||
printf 'pre-commit: commit blocked. Unstage the files above (git rm --cached <file>).\n' >&2
|
exit 1
|
||||||
exit 1 ;;
|
fi
|
||||||
esac
|
|
||||||
exit 0
|
exit 0
|
||||||
|
|||||||
+101
-14
@@ -1,9 +1,10 @@
|
|||||||
#!/bin/sh
|
#!/usr/bin/env bash
|
||||||
# SPDX-License-Identifier: MPL-2.0
|
# SPDX-License-Identifier: MPL-2.0
|
||||||
#
|
#
|
||||||
# Self-test for .githooks/pre-commit. Creates a throw-away repository, points it at the
|
# Self-test for .githooks/pre-commit. Creates a throw-away repository, points it at the
|
||||||
# hook and checks that every forbidden case is rejected and a normal file is accepted.
|
# hook and checks that every forbidden case is rejected and normal files are accepted.
|
||||||
# Usage: sh .githooks/test-pre-commit.sh (Git Bash on Windows or Linux)
|
# Includes every bypass QC found (odd file names, attributes, NULs late in a file, 5 MB
|
||||||
|
# boundary, other key formats). Usage: bash .githooks/test-pre-commit.sh (Git Bash or Linux)
|
||||||
|
|
||||||
set -u
|
set -u
|
||||||
HOOKS=$(cd "$(dirname "$0")" && pwd)
|
HOOKS=$(cd "$(dirname "$0")" && pwd)
|
||||||
@@ -16,28 +17,58 @@ git config user.name hook-test
|
|||||||
git config user.email hook-test@invalid
|
git config user.email hook-test@invalid
|
||||||
git config core.hooksPath "$HOOKS"
|
git config core.hooksPath "$HOOKS"
|
||||||
git config core.autocrlf false
|
git config core.autocrlf false
|
||||||
|
# Allow index entries Windows cannot check out ("quotes", tabs, backslashes) so the hook
|
||||||
|
# itself is what rejects them.
|
||||||
|
git config core.protectNTFS false
|
||||||
git commit -q --allow-empty -m base --no-gpg-sign
|
git commit -q --allow-empty -m base --no-gpg-sign
|
||||||
|
|
||||||
pass=0
|
pass=0
|
||||||
fail=0
|
fail=0
|
||||||
|
|
||||||
# expect <accept|reject> <description> <path> (file must already exist)
|
commit_and_check() {
|
||||||
expect() {
|
local want=$1 what=$2 got
|
||||||
want=$1; what=$2; path=$3
|
if git commit -q -m "$what" --no-gpg-sign >/dev/null 2>"$WORK/.err"; then
|
||||||
git add -f -- "$path"
|
got=accept
|
||||||
if git commit -q -m "$what" --no-gpg-sign >/dev/null 2>"$WORK/.err"; then got=accept; else got=reject; fi
|
elif grep -q 'pre-commit: rejected' "$WORK/.err"; then
|
||||||
|
got=reject
|
||||||
|
else
|
||||||
|
got="error ($(head -c 200 "$WORK/.err"))" # failed, but not because of the hook
|
||||||
|
fi
|
||||||
if [ "$got" = "$want" ]; then
|
if [ "$got" = "$want" ]; then
|
||||||
pass=$((pass + 1)); printf 'ok %-7s %s\n' "$want" "$what"
|
pass=$((pass + 1)); printf 'ok %-7s %s\n' "$want" "$what"
|
||||||
else
|
else
|
||||||
fail=$((fail + 1)); printf 'FAIL wanted %s, got %s: %s\n' "$want" "$got" "$what"; cat "$WORK/.err"
|
fail=$((fail + 1)); printf 'FAIL wanted %s, got %s: %s\n' "$want" "$got" "$what"; cat "$WORK/.err"
|
||||||
fi
|
fi
|
||||||
# Leave the index clean for the next case.
|
|
||||||
git reset -q --hard HEAD >/dev/null 2>&1
|
git reset -q --hard HEAD >/dev/null 2>&1
|
||||||
git clean -q -fdx >/dev/null 2>&1
|
git clean -q -fdx -e .err >/dev/null 2>&1
|
||||||
|
}
|
||||||
|
|
||||||
|
# expect <accept|reject> <description> <path> (file must exist in the work tree)
|
||||||
|
expect() {
|
||||||
|
git add -f -- "$3"
|
||||||
|
commit_and_check "$1" "$2"
|
||||||
|
}
|
||||||
|
|
||||||
|
# expect_staged <accept|reject> <description> <path> <content file>
|
||||||
|
# Stages content under a path that may be impossible to create on this file system
|
||||||
|
# (quotes, tabs or backslashes in names on Windows).
|
||||||
|
expect_staged() {
|
||||||
|
local sha
|
||||||
|
sha=$(git hash-object -w -- "$4")
|
||||||
|
if ! git update-index --add --cacheinfo "100644,$sha,$3"; then
|
||||||
|
fail=$((fail + 1)); printf 'FAIL could not stage the test path: %s
|
||||||
|
' "$2"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
commit_and_check "$1" "$2"
|
||||||
}
|
}
|
||||||
|
|
||||||
B='-----BEGIN'
|
B='-----BEGIN'
|
||||||
E='-----END'
|
E='-----END'
|
||||||
|
mkdir -p "$WORK/../ianitor-hook-src.$$" && SRC="$WORK/../ianitor-hook-src.$$"
|
||||||
|
trap 'rm -rf "$WORK" "$SRC"' EXIT INT TERM
|
||||||
|
printf '%s RSA PRIVATE KEY-----\nMIIBOgIBAAJBAK\n%s RSA PRIVATE KEY-----\n' "$B" "$E" > "$SRC/pem"
|
||||||
|
head -c 6291456 /dev/zero | tr '\000' 'a' > "$SRC/six-mb"
|
||||||
|
|
||||||
printf 'hello\n' > readme.txt
|
printf 'hello\n' > readme.txt
|
||||||
expect accept "plain text file" readme.txt
|
expect accept "plain text file" readme.txt
|
||||||
@@ -48,7 +79,15 @@ expect accept "source file" main.rs
|
|||||||
printf '\000\001\002\003binary\000' > blob.bin
|
printf '\000\001\002\003binary\000' > blob.bin
|
||||||
expect reject "binary file" blob.bin
|
expect reject "binary file" blob.bin
|
||||||
|
|
||||||
printf '%s RSA PRIVATE KEY-----\nMIIBOgIBAAJBAK\n%s RSA PRIVATE KEY-----\n' "$B" "$E" > notes.txt
|
{ head -c 10000 /dev/zero | tr '\000' 'a'; printf '\000tail'; } > late-nul.txt
|
||||||
|
expect reject "NUL byte after the first 8000 bytes" late-nul.txt
|
||||||
|
|
||||||
|
printf '*.dat diff\n' > .gitattributes
|
||||||
|
expect accept ".gitattributes marking *.dat as text" .gitattributes
|
||||||
|
printf 'abc\000def' > forced.dat
|
||||||
|
expect reject "binary file that .gitattributes calls text" forced.dat
|
||||||
|
|
||||||
|
cp "$SRC/pem" notes.txt
|
||||||
expect reject "PEM RSA private key in a .txt file" notes.txt
|
expect reject "PEM RSA private key in a .txt file" notes.txt
|
||||||
|
|
||||||
printf '%s PRIVATE KEY-----\nMIGHAgEAMBMG\n%s PRIVATE KEY-----\n' "$B" "$E" > pkcs8.pem
|
printf '%s PRIVATE KEY-----\nMIGHAgEAMBMG\n%s PRIVATE KEY-----\n' "$B" "$E" > pkcs8.pem
|
||||||
@@ -63,9 +102,33 @@ expect reject "EC private key" ec.txt
|
|||||||
printf '%s OPENSSH PRIVATE KEY-----\nb3BlbnNzaC1rZXktdjEAAAAA\n%s OPENSSH PRIVATE KEY-----\n' "$B" "$E" > deploy
|
printf '%s OPENSSH PRIVATE KEY-----\nb3BlbnNzaC1rZXktdjEAAAAA\n%s OPENSSH PRIVATE KEY-----\n' "$B" "$E" > deploy
|
||||||
expect reject "OpenSSH private key" deploy
|
expect reject "OpenSSH private key" deploy
|
||||||
|
|
||||||
|
printf -- '---- BEGIN SSH2 ENCRYPTED PRIVATE %s ----\nP2/56wAAA\n' KEY > ssh2.txt
|
||||||
|
expect reject "SSH2 (RFC 4716) encrypted private key" ssh2.txt
|
||||||
|
|
||||||
|
printf 'PuTTY-User-Key-File-%s: ssh-ed25519\nEncryption: none\n' 3 > putty.txt
|
||||||
|
expect reject "PuTTY private key content" putty.txt
|
||||||
|
|
||||||
|
printf 'not really a key\n' > me.ppk
|
||||||
|
expect reject "*.ppk file name" me.ppk
|
||||||
|
|
||||||
|
printf '# created: 2026\nAGE-SECRET-KEY-%s\n' 1QYZ8XRFK3W6J9P2M4N5L7KXQZJ5S8TVH3R2W6Y9 > identity.txt
|
||||||
|
expect reject "age secret key" identity.txt
|
||||||
|
|
||||||
|
printf '{"k%s":"EC","crv":"P-256","x":"f83O","y":"x_FE","d":"jpsQ"}\n' ty > key.json
|
||||||
|
expect reject "JWK private key" key.json
|
||||||
|
|
||||||
|
printf '{"k%s":"EC","crv":"P-256","x":"f83O","y":"x_FE"}\n' ty > pub.json
|
||||||
|
expect accept "JWK public key" pub.json
|
||||||
|
|
||||||
|
printf '[Interface]\nPrivate%s = %s\nAddress = 10.0.0.2/32\n' Key yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk= > wg0.conf
|
||||||
|
expect reject "WireGuard private key" wg0.conf
|
||||||
|
|
||||||
printf 'not really a key\n' > server.key
|
printf 'not really a key\n' > server.key
|
||||||
expect reject "*.key file name" server.key
|
expect reject "*.key file name" server.key
|
||||||
|
|
||||||
|
printf 'not really a key\n' > SERVER.KEY
|
||||||
|
expect reject "*.KEY file name in upper case" SERVER.KEY
|
||||||
|
|
||||||
printf 'not really a key\n' > id_ed25519
|
printf 'not really a key\n' > id_ed25519
|
||||||
expect reject "id_ed25519 file name" id_ed25519
|
expect reject "id_ed25519 file name" id_ed25519
|
||||||
|
|
||||||
@@ -75,20 +138,44 @@ expect accept "public key id_ed25519.pub" id_ed25519.pub
|
|||||||
printf '%s CERTIFICATE-----\nMIIBszCCAVmg\n%s CERTIFICATE-----\n' "$B" "$E" > cert.pem
|
printf '%s CERTIFICATE-----\nMIIBszCCAVmg\n%s CERTIFICATE-----\n' "$B" "$E" > cert.pem
|
||||||
expect accept "certificate (public) PEM" cert.pem
|
expect accept "certificate (public) PEM" cert.pem
|
||||||
|
|
||||||
head -c 6291456 /dev/zero | tr '\000' 'a' > big.txt
|
cp "$SRC/six-mb" big.txt
|
||||||
expect reject "6 MB text file" big.txt
|
expect reject "6 MB text file" big.txt
|
||||||
|
|
||||||
head -c 4194304 /dev/zero | tr '\000' 'a' > medium.txt
|
head -c 5000001 /dev/zero | tr '\000' 'a' > over.txt
|
||||||
expect accept "4 MB text file" medium.txt
|
expect reject "5,000,001 bytes" over.txt
|
||||||
|
|
||||||
|
head -c 5242880 /dev/zero | tr '\000' 'a' > mib.txt
|
||||||
|
expect reject "5 MiB (5,242,880 bytes)" mib.txt
|
||||||
|
|
||||||
|
head -c 5000000 /dev/zero | tr '\000' 'a' > exact.txt
|
||||||
|
expect accept "exactly 5,000,000 bytes" exact.txt
|
||||||
|
|
||||||
mkdir -p crates/x/target/debug && printf 'x\n' > crates/x/target/debug/out.txt
|
mkdir -p crates/x/target/debug && printf 'x\n' > crates/x/target/debug/out.txt
|
||||||
expect reject "file under target/" crates/x/target/debug/out.txt
|
expect reject "file under target/" crates/x/target/debug/out.txt
|
||||||
|
|
||||||
|
mkdir -p TARGET && printf 'x\n' > TARGET/out.txt
|
||||||
|
expect reject "file under TARGET/ (upper case)" TARGET/out.txt
|
||||||
|
|
||||||
mkdir -p apps/web/node_modules/pkg && printf 'x\n' > apps/web/node_modules/pkg/index.js
|
mkdir -p apps/web/node_modules/pkg && printf 'x\n' > apps/web/node_modules/pkg/index.js
|
||||||
expect reject "file under node_modules/" apps/web/node_modules/pkg/index.js
|
expect reject "file under node_modules/" apps/web/node_modules/pkg/index.js
|
||||||
|
|
||||||
mkdir -p apps/web/dist && printf 'x\n' > apps/web/dist/index.html
|
mkdir -p apps/web/dist && printf 'x\n' > apps/web/dist/index.html
|
||||||
expect reject "file under dist/" apps/web/dist/index.html
|
expect reject "file under dist/" apps/web/dist/index.html
|
||||||
|
|
||||||
|
mkdir -p apps/web/build && printf 'x\n' > apps/web/build/index.html
|
||||||
|
expect reject "file under build/" apps/web/build/index.html
|
||||||
|
|
||||||
|
mkdir -p apps/web/.svelte-kit && printf 'x\n' > apps/web/.svelte-kit/env.js
|
||||||
|
expect reject "file under .svelte-kit/" apps/web/.svelte-kit/env.js
|
||||||
|
|
||||||
|
# Names git would C-quote: only reachable through the index on Windows.
|
||||||
|
expect_staged reject "PEM key in a file named with a double quote" 'a"b.txt' "$SRC/pem"
|
||||||
|
expect_staged reject "PEM key in a file named with a tab" "$(printf 'tab\tx.txt')" "$SRC/pem"
|
||||||
|
expect_staged reject "6 MB file named with a backslash" 'big\file.txt' "$SRC/six-mb"
|
||||||
|
expect_staged reject "*.key named with a backslash" 'srv\x.key' "$SRC/pem"
|
||||||
|
expect_staged reject "PEM key in a file named with a newline" "$(printf 'new\nline.txt')" "$SRC/pem"
|
||||||
|
printf 'fine\n' > "$SRC/fine"
|
||||||
|
expect_staged accept "harmless file named with a double quote" 'say"hi".txt' "$SRC/fine"
|
||||||
|
|
||||||
printf '\nhook: %d passed, %d failed\n' "$pass" "$fail"
|
printf '\nhook: %d passed, %d failed\n' "$pass" "$fail"
|
||||||
[ "$fail" -eq 0 ]
|
[ "$fail" -eq 0 ]
|
||||||
|
|||||||
Reference in New Issue
Block a user