-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-pre-commit-secrets
More file actions
32 lines (27 loc) · 1.17 KB
/
Copy pathgit-pre-commit-secrets
File metadata and controls
32 lines (27 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env bash
#
# git pre-commit hook: block a commit that would add a hardcoded secret.
#
# Install:
# cp examples/git-pre-commit-secrets .git/hooks/pre-commit
# chmod +x .git/hooks/pre-commit
#
# It scans only the files you're actually committing (fast), using secret-scan.js.
# A real credential in a commit is public the moment it's pushed — this stops it first.
# Bypass once with `git commit --no-verify` if you have a verified false positive.
#
# Assumes secret-scan.js is at the repo root. Change SCRIPTS_DIR if it lives elsewhere.
set -euo pipefail
SCRIPTS_DIR="${SCRIPTS_DIR:-$(git rev-parse --show-toplevel)}"
# Staged, added/copied/modified files only (not deletions).
mapfile -t staged < <(git diff --cached --name-only --diff-filter=ACM)
if [ "${#staged[@]}" -eq 0 ]; then
exit 0
fi
echo "pre-commit: scanning ${#staged[@]} staged file(s) for secrets…"
if ! node "$SCRIPTS_DIR/secret-scan.js" "${staged[@]}"; then
echo "✗ Possible secret in staged changes — commit blocked."
echo " If it's real: remove it AND rotate the credential (deleting the line is not enough)."
echo " If it's a verified false positive: 'git commit --no-verify'."
exit 1
fi