#!/bin/sh # SPDX-License-Identifier: GPL-3.0-or-later # # leak-check: refuse personal data in the files git would commit. # # Scans every staged file (the index, not the working tree), and the names # of staged files against the private list, for: # - the current user's home directory, e.g. /home/alice; # - email addresses (LICENSE is exempt); # - a private pattern list: one extended regular expression per line, # blank lines and lines starting with # ignored, matched ignoring case. # Its path is set per clone with: git config krino.leakpatterns FILE # Keep that file outside the repository so the list is never published. # # A line matching the regex in `git config krino.leakallow` is never # reported (for example a second address of your own). krino's own public # contact address is exempt from the address check without any such setting. # # Prints file:line for each hit, never the matched text, so a hit on a # private pattern does not echo the secret. Exits 0 when clean, 1 when # something matched, 2 when the private list is configured but missing. set -eu cd "$(git rev-parse --show-toplevel)" allow=$(git config --get krino.leakallow || true) # krino's own contact address, published on purpose in README and About. # Exempt from the address check only, so any clone's `make ci` is clean. public='contact@labunix\.xyz' exempt= hits=0 # check LABEL GREP-ARGS...: report staged lines that match, as file:line. # $exempt, when set, widens $allow for this one call. check() { label=$1 shift filter=$allow if [ -n "$exempt" ]; then filter=${filter:+$filter|}$exempt fi out=$(git grep --cached -n -I "$@" | { if [ -n "$filter" ]; then grep -v -E -i -e "$filter"; else cat; fi; } | cut -d: -f1,2 || true) if [ -n "$out" ]; then printf 'leak-check: %s:\n%s\n' "$label" "$out" | sed '2,$s/^/ /' >&2 hits=1 fi } home=${HOME%/} # a HOME of / becomes empty and is skipped if [ -n "$home" ]; then check "your home directory ($home)" -F -e "$home" -- . fi exempt=$public check "an email address" -E -e '[[:alnum:]._%+-]+@[[:alnum:].-]+\.[[:alpha:]]{2,}' -- . ':(exclude)LICENSE' exempt= list=$(git config --type=path --get krino.leakpatterns || true) if [ -z "$list" ]; then echo "leak-check: no private pattern list (git config krino.leakpatterns FILE); built-in checks only" >&2 elif [ ! -r "$list" ]; then echo "leak-check: private pattern list $list is configured but cannot be read" >&2 exit 2 else patterns=$(mktemp) trap 'rm -f "$patterns"' EXIT grep -v -e '^[[:space:]]*#' -e '^[[:space:]]*$' "$list" > "$patterns" || true if [ -s "$patterns" ]; then check "a private pattern from $list" -i -E -f "$patterns" -- . named=$(git ls-files --cached | grep -i -E -f "$patterns" || true) if [ -n "$named" ]; then printf 'leak-check: a file name matching a private pattern:\n%s\n' "$named" | sed '2,$s/^/ /' >&2 hits=1 fi fi fi if [ "$hits" -ne 0 ]; then echo "leak-check: remove the data above before committing; for a false positive, narrow the pattern or set krino.leakallow" >&2 exit 1 fi