Skip to content

Commit 9fc1935

Browse files
uleinalajjl
authored andcommitted
Add support for server side hooks
Client side hooks are great, but rely on developers properly setting them up and not taking shortcuts. Server side hooks provide an opportunity to enforce security policies at a more global level. This commit adds an "update_hook" option which can be added as a serverside update hook. It scans the pushed commits for secrets.
1 parent d8da475 commit 9fc1935

1 file changed

Lines changed: 43 additions & 3 deletions

File tree

git-secrets

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ f,force --install overwrites hooks if the hook already exists
3838
l,literal --add and --add-allowed patterns are escaped so that they are literal
3939
a,allowed --add adds an allowed pattern instead of a prohibited pattern
4040
global Uses the --global git config
41+
update_hook* update hook (internal only)
4142
commit_msg_hook* commit-msg hook (internal only)
4243
pre_commit_hook* pre-commit hook (internal only)
4344
prepare_commit_msg_hook* prepare-commit-msg hook (internal only)"
@@ -61,11 +62,17 @@ load_patterns() {
6162
}
6263

6364
load_allowed() {
65+
local new_rev="$1"
6466
git config --get-all secrets.allowed
6567
local gitallowed="$(git rev-parse --show-toplevel)/.gitallowed"
6668
if [ -e "$gitallowed" ]; then
6769
cat $gitallowed | awk 'NF && $1!~/^#/'
6870
fi
71+
if [ -n "${new_rev}" ]; then
72+
git show ${new_rev}:.gitallowed 2>/dev/null | awk 'NF && $1!~/^#/'
73+
# If there is a new commit being pushed to the server, read the .gitallowed from the new commit
74+
# not the one that is already there
75+
fi
6976
}
7077

7178
# load patterns and combine them with |
@@ -106,6 +113,38 @@ scan_history() {
106113
process_output $? "${output}"
107114
}
108115

116+
117+
# Scans commits that have been pushed to server through update hook
118+
update_hook() {
119+
local old_rev=$2
120+
local new_rev=$3
121+
122+
local new_branch=0
123+
local to_scan=''
124+
125+
# Deal with weirdness on new branches. Thank you stack overflow! https://stackoverflow.com/a/19738143
126+
if [ "${old_rev}" = '0000000000000000000000000000000000000000' ]; then
127+
new_branch=1
128+
to_scan=$(git rev-list $new_rev --not --branches=*)
129+
fi
130+
131+
local combined_patterns=$(load_combined_patterns)
132+
133+
[ -z "${combined_patterns}" ] && return 0
134+
135+
# Looks for differences in commit range if not a new branch
136+
if [ $new_branch -eq 0 ]; then
137+
local to_scan=$(git log ${old_rev}..${new_rev} -G"${combined_patterns}" --pretty=%H)
138+
fi
139+
[ -z "${to_scan}" ] && return 0
140+
141+
# Scan through revisions with findings to normalize output
142+
output=$(GREP_OPTIONS= LC_ALL=C git grep -nwHEI "${combined_patterns}" $to_scan)
143+
process_output $? "${output}" "${new_rev}"
144+
}
145+
146+
147+
109148
# Performs a git-grep, taking into account patterns and options.
110149
# Note: this function returns 1 on success, 0 on error.
111150
git_grep() {
@@ -129,8 +168,8 @@ regular_grep() {
129168
# Takes into account allowed patterns, and if a bad match is found,
130169
# prints an error message and exits 1.
131170
process_output() {
132-
local status="$1" output="$2"
133-
local allowed=$(load_allowed)
171+
local status="$1" output="$2" new_rev="$3"
172+
local allowed=$(load_allowed $new_rev)
134173
case "$status" in
135174
0)
136175
[ -z "${allowed}" ] && echo "${output}" >&2 && return 1
@@ -179,6 +218,7 @@ pre_commit_hook() {
179218
scan_with_fn_or_die "scan" "${files[@]}"
180219
}
181220

221+
182222
# Determines if merging in a commit will introduce tainted history.
183223
prepare_commit_msg_hook() {
184224
case "$2,$3" in
@@ -324,7 +364,7 @@ case "${COMMAND}" in
324364
--add-provider) add_config "secrets.providers" "$@" ;;
325365
--register-aws) register_aws ;;
326366
--aws-provider) aws_provider "$1" ;;
327-
--commit_msg_hook|--pre_commit_hook|--prepare_commit_msg_hook)
367+
--commit_msg_hook|--pre_commit_hook|--prepare_commit_msg_hook|--update_hook)
328368
${COMMAND:2} "$@"
329369
;;
330370
--add)

0 commit comments

Comments
 (0)