-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-secrets-guard-hook.sh
executable file
·66 lines (49 loc) · 1.44 KB
/
install-secrets-guard-hook.sh
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/bash
set -e
precommit_hookfile="./.git/hooks/pre-commit"
echo -n "Write pre-commit hook to $precommit_hookfile (Y/N)? "
read response
if [ "$response" != "Y" ]; then
exit 1
fi
cat <<\FIN > "$precommit_hookfile"
#!/bin/bash
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=$(git hash-object -t tree /dev/null)
fi
# If you want to allow possible secrets set this variable to true.
allowpossiblesecrets=$(git config --type=bool hooks.allowpossiblesecrets)
# Redirect output to stderr.
exec 1>&2
UUIDregex='[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'
GitHubPATregex='ghp_[a-zA-Z0-9]{36}'
forbiddenRegexes=(\
"$UUIDregex"\
"$GitHubPATregex"\
)
forbiddenRegexDescriptions=(\
"UUID"\
"GitHub Personal Authentication Token"\
)
for i in "${!forbiddenRegexes[@]}"; do
diffRes=$(git diff --cached --diff-filter=AM -G "${forbiddenRegexes[i]}" -z $against)
diffNames=$(git diff --name-only --cached --diff-filter=AM -G "${forbiddenRegexes[i]}" -z $against)
if [ "$allowpossiblesecrets" != "true" ] && test -n "$diffRes"
then
cat <<EOF
Error: Attempt to make commit containing possible secrets.
The following files may contain a ${forbiddenRegexDescriptions[i]}:
$diffNames
If you know what you are doing you can disable this check using:
git config hooks.allowpossiblesecrets
EOF
exit 1
fi
done
exit 0
FIN
chmod +x $precommit_hookfile