|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Scan only the changes being introduced for secrets, not content that was already |
| 3 | +# scanned elsewhere. Run by the gitleaks hook in .pre-commit-config.yaml. |
| 4 | +# |
| 5 | +# - GITLEAKS_BASE set (CI): the commits in GITLEAKS_BASE..HEAD, plus the lines that |
| 6 | +# merge commits in that range introduced themselves (e.g. conflict resolutions), |
| 7 | +# not what they brought in from the merged branch. Set but empty: an error, so a |
| 8 | +# run without a base cannot pass without scanning anything. |
| 9 | +# - Merge commit in progress: what differs from the branch being merged in (this |
| 10 | +# branch's changes and the conflict resolutions), not everything merged in. |
| 11 | +# - Otherwise: the staged changes (the upstream hook's behavior). |
| 12 | +set -euo pipefail |
| 13 | + |
| 14 | +# Lines of a combined diff (git show --cc) that are in none of the merge's parents, |
| 15 | +# with the file headers. With N parents those lines start with N "+". |
| 16 | +merge_introduced_lines() { |
| 17 | + local merge=$1 parents |
| 18 | + parents=$(($(git rev-list --parents -n 1 "$merge" | wc -w) - 1)) |
| 19 | + git show --cc --format= "$merge" | awk -v n="$parents" ' |
| 20 | + BEGIN { plus = sprintf("%" n "s", ""); gsub(/ /, "+", plus) } |
| 21 | + /^diff --cc / { print; next } |
| 22 | + /^\+\+\+ (b\/|\/dev\/null)/ { next } |
| 23 | + substr($0, 1, n) == plus { print } |
| 24 | + ' |
| 25 | +} |
| 26 | + |
| 27 | +if [[ -n "${GITLEAKS_BASE+set}" ]]; then |
| 28 | + if [[ -z "${GITLEAKS_BASE}" ]]; then |
| 29 | + echo "gitleaks: GITLEAKS_BASE is set but empty, refusing to pass without scanning" >&2 |
| 30 | + exit 1 |
| 31 | + elif git rev-parse --quiet --verify "${GITLEAKS_BASE}^{commit}" >/dev/null; then |
| 32 | + range=("${GITLEAKS_BASE}..HEAD") |
| 33 | + else |
| 34 | + # e.g. first push of a branch (all zeros) or a base that was force-pushed away |
| 35 | + echo "gitleaks: base ${GITLEAKS_BASE} not found, scanning the last commit only" >&2 |
| 36 | + range=(-1 HEAD) |
| 37 | + fi |
| 38 | + |
| 39 | + status=0 |
| 40 | + echo "gitleaks: scanning commits ${range[*]}" |
| 41 | + gitleaks git --redact --verbose --log-opts="${range[*]}" || status=1 |
| 42 | + |
| 43 | + # git log -p (used by gitleaks git) shows no diff for merge commits |
| 44 | + for merge in $(git rev-list --merges "${range[@]}"); do |
| 45 | + echo "gitleaks: scanning the lines merge commit ${merge} introduced" |
| 46 | + merge_introduced_lines "$merge" | gitleaks stdin --redact --verbose || status=1 |
| 47 | + done |
| 48 | + exit "$status" |
| 49 | +fi |
| 50 | + |
| 51 | +if git rev-parse --quiet --verify MERGE_HEAD >/dev/null; then |
| 52 | + # Added lines only (with the file headers), removed lines are not introduced |
| 53 | + git diff --cached --unified=0 MERGE_HEAD | { grep -E '^(diff --git |\+)' || true; } | gitleaks stdin --redact --verbose |
| 54 | + exit |
| 55 | +fi |
| 56 | + |
| 57 | +exec gitleaks git --pre-commit --redact --staged --verbose |
0 commit comments