From 77187e358382979667c92aa9a961651f41fdeda1 Mon Sep 17 00:00:00 2001 From: ilia Date: Mon, 13 Jul 2026 14:44:45 -0500 Subject: [PATCH] ci: add local pre-commit gitleaks hook --- .gitleaks.toml | 3 +++ scripts/git-hooks/install.sh | 46 ++++++++++++++++++++++++++++++++++++ scripts/git-hooks/pre-commit | 27 +++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 scripts/git-hooks/install.sh create mode 100644 scripts/git-hooks/pre-commit diff --git a/.gitleaks.toml b/.gitleaks.toml index 5928221..e2c2a5a 100644 --- a/.gitleaks.toml +++ b/.gitleaks.toml @@ -2,6 +2,9 @@ # Working tree is redacted to xoxe-REDACTED; allowlist covers pre-redaction commits. title = "slack-sieve" +[extend] +useDefault = true + [allowlist] description = "Test fixtures (tokens redacted going forward)" paths = [ diff --git a/scripts/git-hooks/install.sh b/scripts/git-hooks/install.sh new file mode 100644 index 0000000..0dff3fa --- /dev/null +++ b/scripts/git-hooks/install.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash +# One-time installer for the local pre-commit gitleaks hook. +# Run once per clone: bash scripts/git-hooks/install.sh +# +# Respects `core.hooksPath` if you've set one (local or global) — some setups +# point git at a hooks dir outside `.git/hooks/` (e.g. a machine-wide +# `~/.git-hooks/`), and installing to `.git/hooks/` in that case would be a +# silent no-op. If an existing hook is already at that path, this chains to +# it so nothing already relying on it breaks. +set -euo pipefail + +REPO_ROOT="$(git rev-parse --show-toplevel)" +cd "$REPO_ROOT" + +HOOKS_DIR="$(git config --get core.hooksPath || true)" +if [ -z "$HOOKS_DIR" ]; then + HOOKS_DIR=".git/hooks" +elif [[ "$HOOKS_DIR" != /* ]]; then + HOOKS_DIR="$REPO_ROOT/$HOOKS_DIR" +fi +mkdir -p "$HOOKS_DIR" + +TARGET="$HOOKS_DIR/pre-commit" +if [ -f "$TARGET" ] && ! grep -q "gitleaks" "$TARGET" 2>/dev/null; then + echo "⚠️ Existing pre-commit hook found at $TARGET that isn't ours — chaining instead of overwriting." + CHAINED="$HOOKS_DIR/pre-commit.d-gitleaks" + cp scripts/git-hooks/pre-commit "$CHAINED" + chmod +x "$CHAINED" + if ! grep -q "pre-commit.d-gitleaks" "$TARGET" 2>/dev/null; then + printf '\n# Added by levkinops ansible repo (scripts/git-hooks/install.sh)\n"%s"\n' "$CHAINED" >> "$TARGET" + fi +else + cp scripts/git-hooks/pre-commit "$TARGET" + chmod +x "$TARGET" +fi + +echo "✓ Installed pre-commit gitleaks hook → $TARGET" +if [ "$HOOKS_DIR" != ".git/hooks" ] && [ "$HOOKS_DIR" != "$REPO_ROOT/.git/hooks" ]; then + echo " (using core.hooksPath=$HOOKS_DIR — applies to every repo that shares this hooksPath)" +fi +if ! command -v gitleaks >/dev/null 2>&1; then + echo " Note: gitleaks isn't installed locally yet. Install it for the hook to actually run:" + echo " macOS: brew install gitleaks" + echo " Linux: see https://github.com/gitleaks/gitleaks#installing" + echo " Until then this hook is a no-op locally (CI still scans every push)." +fi diff --git a/scripts/git-hooks/pre-commit b/scripts/git-hooks/pre-commit new file mode 100644 index 0000000..14e6898 --- /dev/null +++ b/scripts/git-hooks/pre-commit @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +# Local pre-commit secret scan — mirrors the `secret-scanning` CI job (gitleaks) +# so leaked secrets are caught before they ever leave your machine, not just at +# CI time. Installed via `make install-git-hooks`. +# +# Uses the same .gitleaks.toml allowlist as CI. Only scans staged content. +set -euo pipefail + +REPO_ROOT="$(git rev-parse --show-toplevel)" +cd "$REPO_ROOT" + +if ! command -v gitleaks >/dev/null 2>&1; then + echo "⚠️ gitleaks not installed locally — skipping local secret scan." + echo " Install: brew install gitleaks (CI will still catch it either way)" + exit 0 +fi + +echo "🔐 Running gitleaks on staged changes..." +if ! gitleaks protect --staged --config .gitleaks.toml --no-banner --redact; then + echo "" + echo "❌ gitleaks found a potential secret in your staged changes." + echo " Fix it, or if it's a false positive, add an allowlist entry to .gitleaks.toml." + echo " Bypass (not recommended): git commit --no-verify" + exit 1 +fi + +echo "✓ gitleaks: no secrets found in staged changes"