Skip to content

Commit 675886a

Browse files
noreplyclaude
andcommitted
fork-tests: fix round 2 (secret scan reads every commit offline, Lint)
Secret scan (.github/scripts/fork-secret-scan.sh, tests: fork-secret-scan.test.sh) - The checkout is blob-less, so gitleaks fetched file contents on demand. gitleaks 8.30.1 stops at the first git error (a failed content fetch), logs "0 commits scanned" and exits 0, and the summary said "No secrets found". The script now runs gitleaks' own `git log -p -U0 <opts>` first: it fetches every blob the scan reads and fails the job (exit 2) when a fetch fails. gitleaks then runs with GIT_NO_LAZY_FETCH=1. - The job also fails when gitleaks scanned fewer commits than that git log has commits with a hunk in a file they do not delete (what gitleaks counts; a pure rename or a clean merge has none, so it can be below rev-list --count). - New tests: contents unreachable after the clone, gitleaks stopping early, and a base or head commit that cannot be fetched. Lint (vp check) - Formatting of fork-ci-tests.cjs, fork-ci-tests.test.cjs, fork-tests-drift.test.cjs. - The header comments of fork-ci-tests.cjs and fork-tests-drift.cjs cut to 4 lines (t3code/no-low-value-comments). Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6d6e0b5 commit 675886a

7 files changed

Lines changed: 117 additions & 33 deletions

File tree

‎.github/scripts/fork-ci-tests.cjs‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
// Fork only: every test file under `.github/scripts` (`*.test.*`, subdirectories
2-
// included) must be run somewhere, checked against `.github/fork-ci-tests.txt`.
3-
// A plain entry is run here, by its extension (RUNNER). An entry
4-
// `<test> run by <file>` is run by that file, which must name the test's path; it
5-
// cannot be one of upstream's workflows, which are disabled in the fork. A
6-
// present test missing from the list fails, and so does a test with no runner
7-
// for its extension. A listed test that is missing fails once it has been in the
8-
// tested commit's history (deleted while still listed) and is a note before that.
1+
// Fork only: every `*.test.*` under .github/scripts must be listed in .github/fork-ci-tests.txt.
2+
// A plain entry runs here by its extension (RUNNER); `<test> run by <file>` is run by that file,
3+
// which must name the test and cannot be a (disabled) upstream workflow. Unlisted or runner-less
4+
// tests fail; a listed test that is gone fails once the tested history had it, a note before.
95
const childProcess = require("node:child_process");
106
const fs = require("node:fs");
117
const path = require("node:path");
@@ -41,7 +37,8 @@ function plan(entries, present, ever, read = () => null) {
4137
for (const { file, runBy } of entries) {
4238
if (!runBy || !present.includes(file)) continue;
4339
let why = null;
44-
if (UPSTREAM_WORKFLOW.test(runBy)) why = `${runBy} is an upstream workflow, disabled in the fork`;
40+
if (UPSTREAM_WORKFLOW.test(runBy))
41+
why = `${runBy} is an upstream workflow, disabled in the fork`;
4542
else {
4643
const text = read(runBy);
4744
if (text === null) why = `${runBy} does not exist`;
@@ -68,7 +65,10 @@ function readList(text) {
6865
.filter(Boolean)
6966
.map((line) => {
7067
const match = line.match(/^(\S+)(?:\s+run by\s+(\S+))?$/);
71-
if (!match) throw new Error(`${LIST}: cannot read "${line}" (expected "<test>" or "<test> run by <file>")`);
68+
if (!match)
69+
throw new Error(
70+
`${LIST}: cannot read "${line}" (expected "<test>" or "<test> run by <file>")`,
71+
);
7272
return { file: match[1], runBy: match[2] ?? null };
7373
});
7474
}
@@ -141,12 +141,14 @@ function main() {
141141
);
142142
for (const { file, why } of result.notRun)
143143
console.log(`::error file=${file}::${file} is not run: ${why}`);
144-
for (const { file, runBy } of result.elsewhere) rows.push(`| \`${file}\` | run by \`${runBy}\` | |`);
144+
for (const { file, runBy } of result.elsewhere)
145+
rows.push(`| \`${file}\` | run by \`${runBy}\` | |`);
145146
for (const file of result.pending)
146147
rows.push(`| \`${file}\` | not present: its PR has not merged | |`);
147148
for (const file of result.unlisted) rows.push(`| \`${file}\` | **not in ${LIST}** | |`);
148149
for (const file of result.deleted) rows.push(`| \`${file}\` | **deleted while listed** | |`);
149-
for (const file of result.noRunner) rows.push(`| \`${file}\` | **no runner for its extension** | |`);
150+
for (const file of result.noRunner)
151+
rows.push(`| \`${file}\` | **no runner for its extension** | |`);
150152
for (const { file, why } of result.notRun) rows.push(`| \`${file}\` | **not run: ${why}** | |`);
151153

152154
const summary = process.env.GITHUB_STEP_SUMMARY;

‎.github/scripts/fork-ci-tests.test.cjs‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ test("a listed test never in history is pending, not a failure", () => {
3535
});
3636

3737
test("a listed test deleted from history fails", () => {
38-
const result = plan(here("a.test.sh", "gone.test.sh"), ["a.test.sh"], (f) => f === "gone.test.sh");
38+
const result = plan(
39+
here("a.test.sh", "gone.test.sh"),
40+
["a.test.sh"],
41+
(f) => f === "gone.test.sh",
42+
);
3943
assert.deepEqual(result.deleted, ["gone.test.sh"]);
4044
assert.deepEqual(result.pending, []);
4145
});
@@ -83,7 +87,11 @@ test("finds every *.test.* file, subdirectories included", () => {
8387
fs.mkdirSync(path.dirname(path.join(root, file)), { recursive: true });
8488
fs.writeFileSync(path.join(root, file), "");
8589
}
86-
assert.deepEqual(findTests("s", root), ["s/a.test.cjs", "s/sub/c.test.mjs", "s/sub/deep/d.test.ts"]);
90+
assert.deepEqual(findTests("s", root), [
91+
"s/a.test.cjs",
92+
"s/sub/c.test.mjs",
93+
"s/sub/deep/d.test.ts",
94+
]);
8795
} finally {
8896
fs.rmSync(root, { recursive: true, force: true });
8997
}

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@
2323
# Merge commits are diffed with --remerge-diff: only what the merge itself changed beyond an
2424
# automatic merge of its parents (a conflict resolution, or a file added in the merge). Without it
2525
# git log shows merges with no diff at all, so gitleaks would never read them.
26+
#
27+
# Offline, and counted: the checkout is a blob-less clone, so file contents are fetched on demand.
28+
# gitleaks 8.30.1 stops reading at the first git error (a blob fetch that fails: network, GitHub
29+
# 5xx), logs "0 commits scanned" and still exits 0. So the script runs gitleaks' own git log first,
30+
# which fetches every blob the scan reads and fails the job if a fetch fails, then runs gitleaks with
31+
# GIT_NO_LAZY_FETCH=1, and fails when gitleaks scanned fewer commits than that git log holds.
32+
# gitleaks counts a commit when a file it neither deletes nor turns binary has a hunk: a pure
33+
# rename, a mode change, an empty new file or a merge with nothing beyond the automatic merge has
34+
# none, so the count can be below `git rev-list --count`.
2635
set -euo pipefail
2736

2837
have() { git cat-file -e "$1^{commit}" 2>/dev/null; }
@@ -75,14 +84,39 @@ esac
7584

7685
echo "Scanning $what ($range)"
7786
if [[ -n "$warning" ]]; then echo "::warning title=Secret scan::$warning"; fi
87+
88+
# gitleaks runs `git log -p -U0 <log-opts>`. The same command here fetches what it reads; counts the
89+
# commits gitleaks will scan (see the header); and fails the job (pipefail) when a fetch fails.
90+
# shellcheck disable=SC2086
91+
if ! expected="$(git log -p -U0 $opts | awk '
92+
/^commit / { sha = $2; next }
93+
/^diff --git / { deleted = 0; next }
94+
/^deleted file mode / { deleted = 1; next }
95+
/^@@ / { if (!deleted && sha != "" && !(sha in seen)) { seen[sha] = 1; n++ } }
96+
END { print n + 0 }')"; then
97+
echo "::error::Secret scan: cannot read the commits to scan (a file content fetch failed?)" >&2
98+
summary "### Secret scan" "" "Failed: the contents of $what could not be read, so nothing was scanned."
99+
exit 2
100+
fi
101+
102+
log="$(mktemp)"
103+
trap 'rm -f "$log"' EXIT
78104
set +e
79-
"$GITLEAKS" git --no-banner --redact --verbose --log-opts="$opts" .
105+
GIT_NO_LAZY_FETCH=1 "$GITLEAKS" git --no-banner --no-color --redact --verbose --log-opts="$opts" . > "$log" 2>&1
80106
status=$?
81107
set -e
108+
cat "$log"
109+
scanned="$(sed -E 's/\x1b\[[0-9;]*m//g' "$log" | sed -nE 's/.* ([0-9]+) commits scanned.*/\1/p' | tail -n 1)"
110+
echo "gitleaks scanned ${scanned:-no} commit(s); $expected commit(s) in range have changes it reads"
82111
summary "### Secret scan" ""
83112
if [[ -n "$warning" ]]; then summary "Warning: $warning." ""; fi
113+
if [[ $status -eq 0 && ( -z "$scanned" || "$scanned" -lt "$expected" ) ]]; then
114+
echo "::error::Secret scan: gitleaks scanned ${scanned:-no} commit(s), but $expected commit(s) in range have changes (a git error stopped it?)" >&2
115+
summary "Failed: gitleaks scanned ${scanned:-no} of the $expected commit(s) with changes in $what, so the rest were not checked."
116+
exit 2
117+
fi
84118
if [[ $status -eq 0 ]]; then
85-
summary "No secrets found in $what."
119+
summary "No secrets found in $what ($scanned commit(s) with changes scanned)."
86120
else
87121
summary "gitleaks found possible secrets in $what (values redacted; see the job log). Rotate any real secret: it is already public."
88122
fi

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

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,24 @@ else
2424
export TMPDIR="$root"
2525
cat > "$gitleaks" <<'FAKE'
2626
#!/usr/bin/env bash
27-
# Stand-in: `gitleaks git ... --log-opts=OPTS .` fails when the diffs of `git log OPTS` add a key id.
28-
set -euo pipefail
27+
# Stand-in: `gitleaks git ... --log-opts=OPTS .` fails when the diffs of `git log -p -U0 OPTS` add a
28+
# key id, and logs how many commits it scanned. Like gitleaks 8.30.1, a git error (a file content
29+
# fetch that fails) stops it with "0 commits scanned" and exit 0.
30+
set -uo pipefail
2931
opts=""
3032
for arg in "$@"; do [[ "$arg" == --log-opts=* ]] && opts="${arg#--log-opts=}"; done
3133
# Into a file first: `grep -q` stops reading early, and pipefail would read git's SIGPIPE as no match.
3234
# shellcheck disable=SC2086
33-
git log -p -U0 $opts > "$TMPDIR/fake-gitleaks.log"
35+
if ! git log -p -U0 $opts > "$TMPDIR/fake-gitleaks.log" 2> "$TMPDIR/fake-gitleaks.err"; then
36+
echo "ERR [git] $(head -n 1 "$TMPDIR/fake-gitleaks.err")"
37+
echo "INF 0 commits scanned."
38+
echo "no leaks found"
39+
exit 0
40+
fi
41+
# A commit counts when a file it does not delete has a hunk, as in gitleaks.
42+
scanned="$(awk '/^commit / { c = $2 } /^diff --git / { d = 0 } /^deleted file mode / { d = 1 }
43+
/^@@ / && !d && !(c in s) { s[c] = 1; n++ } END { print n + 0 }' "$TMPDIR/fake-gitleaks.log")"
44+
echo "INF $scanned commits scanned."
3445
if grep -qE '^\+.*(AKIA)[A-Z2-7]{16}' "$TMPDIR/fake-gitleaks.log"; then echo "leaks found: 1"; exit 1; fi
3546
echo "no leaks found"
3647
FAKE
@@ -143,6 +154,39 @@ BEFORE="1111111111111111111111111111111111111111" AFTER="$leaky" \
143154

144155
EVENT_NAME=workflow_dispatch check "an unknown event fails" 2
145156

157+
export EVENT_NAME=pull_request BEFORE="" AFTER=""
158+
BASE_SHA="1111111111111111111111111111111111111111" HEAD_SHA="$leaky" \
159+
check "pull request: a base commit that cannot be fetched fails" 2 "cannot fetch commit 1111"
160+
BASE_SHA="$base" HEAD_SHA="2222222222222222222222222222222222222222" \
161+
check "pull request: a head commit that cannot be fetched fails" 2 "cannot fetch commit 2222"
162+
163+
# gitleaks stopping early (a git error part-way) without failing: it scans only the newest commit.
164+
real_gitleaks="$gitleaks"
165+
gitleaks="$root/early-stop-gitleaks"
166+
cat > "$gitleaks" <<STOP
167+
#!/usr/bin/env bash
168+
args=()
169+
for arg in "\$@"; do
170+
if [[ "\$arg" == --log-opts=* ]]; then arg="--log-opts=--max-count=1 \${arg#--log-opts=}"; fi
171+
args+=("\$arg")
172+
done
173+
"$real_gitleaks" "\${args[@]}" || true
174+
exit 0
175+
STOP
176+
chmod +x "$gitleaks"
177+
BASE_SHA="$base" HEAD_SHA="$leaky" \
178+
check "pull request: gitleaks scanning fewer commits than the range has fails" 2 "scanned 1 commit"
179+
gitleaks="$real_gitleaks"
180+
181+
# The file contents cannot be fetched part-way: a fresh blob-less clone (no contents yet) whose
182+
# origin is gone afterwards. gitleaks would log "0 commits scanned" and exit 0.
183+
clone="$root/clone-offline"
184+
git clone -q --filter=blob:none --no-checkout --single-branch --branch leaky "file://$root/origin.git" "$clone"
185+
mv "$root/origin.git" "$root/origin-gone.git"
186+
BASE_SHA="$base" HEAD_SHA="$leaky" \
187+
check "pull request: contents that cannot be fetched after the clone fail" 2 "cannot read the commits"
188+
mv "$root/origin-gone.git" "$root/origin.git"
189+
146190
if [[ "$failures" -ne 0 ]]; then
147191
echo "$failures check(s) failed"
148192
exit 1

‎.github/scripts/fork-tests-drift.cjs‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
// Fork only: fails when upstream's ci.yml or release.yml test jobs change since
2-
// the copy in .github/workflows/fork-tests.yml last followed them. Compares a
3-
// digest of every job and step, and of the top-level keys that change how every
4-
// job runs (TOP_LEVEL), with .github/fork-tests-upstream.json; comments are ignored.
5-
//
6-
// Limit: the check compares upstream with its own record, not with
7-
// fork-tests.yml. Recording without porting passes, so --record prints what
8-
// changed, and the commit and PR that update the record must say, for each line,
9-
// what was ported or why it does not apply.
10-
// node .github/scripts/fork-tests-drift.cjs check
11-
// node .github/scripts/fork-tests-drift.cjs --record after porting: print the changes, save the record
1+
// Fork only: fails when upstream's ci.yml or release.yml test jobs or TOP_LEVEL keys changed since
2+
// .github/fork-tests-upstream.json recorded them (comments ignored). It compares upstream with its
3+
// record, not with fork-tests.yml: `--record` prints the changes and saves them, and its commit and
4+
// PR must say per change what was ported or why it does not apply. Run: `node <this> [--record]`.
125
const crypto = require("node:crypto");
136
const fs = require("node:fs");
147
const path = require("node:path");

‎.github/scripts/fork-tests-drift.test.cjs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ test("top-level env, defaults, concurrency and permissions count, wherever they
7777
assert.deepEqual(compare(wrap(withPerms), wrap(withPerms.replace(": read", ": write"))), [
7878
"ci.yml: top-level `permissions:` changed",
7979
]);
80-
assert.deepEqual(compare(wrap(withPerms), wrap(base)), ["ci.yml: top-level `permissions:` removed"]);
80+
assert.deepEqual(compare(wrap(withPerms), wrap(base)), [
81+
"ci.yml: top-level `permissions:` removed",
82+
]);
8183
// `on:` and `name:` are not digested.
8284
assert.deepEqual(compare(wrap(base), wrap(base.replace("on: push", "on: pull_request"))), []);
8385
});

‎.github/workflows/fork-tests.yml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,9 @@ jobs:
195195
runs-on: ubuntu-24.04
196196
timeout-minutes: 15
197197
steps:
198-
# Full history without file contents; gitleaks then fetches the contents
199-
# of only the scanned commits.
198+
# Full history without file contents. The scan script fetches the contents
199+
# of only the scanned commits (a failed fetch fails the job), then runs
200+
# gitleaks offline.
200201
- name: Checkout
201202
uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6
202203
with:

0 commit comments

Comments
 (0)