#!/usr/bin/env bash # Secret scan of the commits a push brings (scripts/git-hooks/pre-push). # # scripts/secret-scan.sh ... # # Scans every commit reachable from the given shas that no remote-tracking # branch already has (`git log ... --not --remotes`), with gitleaks and the # repo's .gitleaks.toml (default rules plus test fixtures allowlisted by path # and rule). Exit 1 when gitleaks finds a key shape, or when gitleaks itself # fails; values are redacted from the output. SECURITY-CHECKS-0.md, item 9. # # Without gitleaks on PATH it says so loudly and exits 0: a lane machine # without it must still push, and the coordinator installs it # (`brew install gitleaks`, or the release binary on Linux). set -uo pipefail ROOT="$(git rev-parse --show-toplevel)" # The config travels with this script, so the scan reads the same rules from any checkout. CONFIG="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/.gitleaks.toml" [ "$#" -gt 0 ] || exit 0 if ! command -v gitleaks >/dev/null 2>&1; then echo "[pre-push] WARNING: gitleaks is not installed on $(hostname -s), so this push was NOT scanned for secrets. Install it: brew install gitleaks" >&2 exit 0 fi # Nothing new to scan (everything is already on a remote): done. [ -n "$(git -C "$ROOT" rev-list -n 1 "$@" --not --remotes 2>/dev/null)" ] || exit 0 out="$(gitleaks git "$ROOT" --config "$CONFIG" --log-opts="$* --not --remotes" \ --redact=100 --no-banner --no-color --exit-code 1 2>&1)" status=$? if [ "$status" -ne 0 ]; then printf '%s\n' "$out" | grep -E 'RuleID|File|Line|Commit|leaks found|ERR|error' >&2 echo "[pre-push] push refused: gitleaks found a key shape in the commits above (exit $status). A real key is rotated, never pushed; an invented one in a test fixture is allowlisted by path and rule in .gitleaks.toml." >&2 exit 1 fi exit 0