Skip to content

Commit c861d84

Browse files
noreplyclaude
andcommitted
fork-secret-scan: fail when gitleaks logs an error and still exits 0
gitleaks 8.30.1 logs `ERR error="stderr is not empty"` when git writes to stderr while it reads the log, may drop the rest of the diffs, and exits 0. The scan now fails (2) on any ERR line in gitleaks' log, before the commit count. Test: a git wrapper that writes a stray stderr line; with a 3,000-file commit the real gitleaks misses a key in the last file and passes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c3ee354 commit c861d84

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

‎.github/scripts/fork-secret-scan.sh‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,15 @@ scanned="$(sed -E 's/\x1b\[[0-9;]*m//g' "$log" | sed -nE 's/.* ([0-9]+) commits
110110
echo "gitleaks scanned ${scanned:-no} commit(s); $expected commit(s) in range have changes it reads"
111111
summary "### Secret scan" ""
112112
if [[ -n "$warning" ]]; then summary "Warning: $warning." ""; fi
113+
# gitleaks 8.30.1 logs git's stderr as ERR (and `ERR error="stderr is not empty"`), stops reading
114+
# diffs once git exits, and still exits 0; the count above misses that when the dropped files all
115+
# belong to a commit it already counted. Any ERR line with exit 0 fails the job.
116+
errors="$(sed -E 's/\x1b\[[0-9;]*m//g' "$log" | grep -E '^([^[:space:]]+[[:space:]]+)?ERR[[:space:]]' || true)"
117+
if [[ $status -eq 0 && -n "$errors" ]]; then
118+
echo "::error::Secret scan: gitleaks logged an error and still passed, so it may not have read every change: $(head -n 1 <<< "$errors")" >&2
119+
summary "Failed: gitleaks logged an error while scanning $what (see the job log), so the scan may be incomplete."
120+
exit 2
121+
fi
113122
if [[ $status -eq 0 && ( -z "$scanned" || "$scanned" -lt "$expected" ) ]]; then
114123
echo "::error::Secret scan: gitleaks scanned ${scanned:-no} commit(s), but $expected commit(s) in range have changes (a git error stopped it?)" >&2
115124
summary "Failed: gitleaks scanned ${scanned:-no} of the $expected commit(s) with changes in $what, so the rest were not checked."

‎.github/scripts/fork-secret-scan.test.sh‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ if ! git log -p -U0 $opts > "$TMPDIR/fake-gitleaks.log" 2> "$TMPDIR/fake-gitleak
3838
echo "no leaks found"
3939
exit 0
4040
fi
41+
# Like gitleaks 8.30.1: any stderr line from git is logged as an error, and the scan goes on.
42+
if [[ -s "$TMPDIR/fake-gitleaks.err" ]]; then
43+
echo "ERR [git] $(head -n 1 "$TMPDIR/fake-gitleaks.err")"
44+
echo 'ERR error="stderr is not empty"'
45+
fi
4146
# A commit counts when a file it does not delete has a hunk, as in gitleaks.
4247
scanned="$(awk '/^commit / { c = $2 } /^diff --git / { d = 0 } /^deleted file mode / { d = 1 }
4348
/^@@ / && !d && !(c in s) { s[c] = 1; n++ } END { print n + 0 }' "$TMPDIR/fake-gitleaks.log")"
@@ -187,6 +192,39 @@ BASE_SHA="$base" HEAD_SHA="$leaky" \
187192
check "pull request: contents that cannot be fetched after the clone fail" 2 "cannot read the commits"
188193
mv "$root/origin-gone.git" "$root/origin.git"
189194

195+
# git writes a stray line to stderr while gitleaks reads the log (in gitleaks 8.30.1 that is
196+
# `ERR error="stderr is not empty"`, possibly with diffs dropped, and still exit 0): the scan fails.
197+
mkdir -p "$root/straygit"
198+
cat > "$root/straygit/git" <<STRAY
199+
#!/usr/bin/env bash
200+
"$(command -v git)" "\$@"
201+
status=\$?
202+
[[ " \$* " != *" log "* ]] || echo "warning: a stray line on stderr" >&2
203+
exit \$status
204+
STRAY
205+
chmod +x "$root/straygit/git"
206+
clone="$root/clone"
207+
PATH="$root/straygit:$PATH" BASE_SHA="$base" HEAD_SHA="$clean" \
208+
check "pull request: gitleaks logging a git error (stderr not empty) and passing fails" 2 "gitleaks logged an error|gitleaks scanned"
209+
BASE_SHA="$base" HEAD_SHA="$clean" check "pull request: without the stray line the same scan passes" 0
210+
# The case the count misses (gitleaks 8.30.1): one commit adds 3,000 files and a key in the last
211+
# one; the stray line stops gitleaks before that file, with the commit already counted. The real
212+
# gitleaks then exits 0, so the scan must fail on its ERR lines (2); the stand-in reads every file
213+
# and finds the key (1).
214+
cd "$root/work"
215+
git checkout -q -b wide "$base"
216+
mkdir -p wide && for n in $(seq -w 1 3000); do echo "line $n" > "wide/f$n.txt"; done
217+
printf '%s\n' "aws_access_key_id=$key" > wide/zzz.txt
218+
date=$((date + 60))
219+
git add -A && GIT_AUTHOR_DATE="@$date" GIT_COMMITTER_DATE="@$date" git commit -qm "PR W: 3,000 files, a key last"
220+
git push -q origin wide
221+
wide=$(git rev-parse wide)
222+
git -C "$root/clone" fetch -q origin wide
223+
cd "$root"
224+
wide_status=1; [[ -z "${GITLEAKS_BIN:-}" ]] || wide_status=2
225+
PATH="$root/straygit:$PATH" BASE_SHA="$base" HEAD_SHA="$wide" \
226+
check "pull request: a stray git stderr line that hides a key in a wide commit fails" "$wide_status"
227+
190228
if [[ "$failures" -ne 0 ]]; then
191229
echo "$failures check(s) failed"
192230
exit 1

0 commit comments

Comments
 (0)