Repository navigation
Secret scanners miss a secret that a later commit removes: scan the pushed commits #1879
Replies: 1 comment
|
Thanks for the report. The gap is real: I reproduced it with gitleaks, where #1880 adds hooks {
["pre-push"] {
steps {
["gitleaks"] = (Builtins.gitleaks) { scan = "commits" }
}
}
}It scans every commit in the run's ref range, so a pre-push that sends a commit adding a credential fails even if a later commit removes it. Along with it:
Known gaps are listed in the PR: AI-assisted — Tool: Claude Code; model: anthropic/claude-sonnet-5-5; version: unavailable. |
Uh oh!
There was an error while loading. Please reload this page.
What I was trying to do
I want to run gitleaks from hk on
pre-pushand in CI. The push should fail when any pushed commit adds a credential.The builtins scan files (or the staged content), so this sequence passes:
git pushThe tip has no credential, so the scan passes, but the push sends commit A and the credential is in the remote history.
Proposal
I built this on my fork as a reference. Use any part of it, or none.
Branch: https://github.com/hituzi-no-sippo/hk/tree/feat/secret-scan-commits
Diff: main...hituzi-no-sippo:hk:feat/secret-scan-commits
1.
scan = "commits"for the four secret scannersbetterleaks, gitleaks, kingfisher, and trufflehog get a
scan = "commits"mode that scans every commit in the run's range, using{{from_ref}}/{{to_ref}}from #1830:gitleaks git --log-opts={{from_ref}}..{{to_ref}}betterleaks git --log-opts={{from_ref}}..{{to_ref}}trufflehog git file://. --since-commit {{from_ref}} --branch {{to_ref}} --failkingfisher scan . --since-commit {{from_ref}} --branch {{to_ref}}scanof each builtin does not change.--no-verification,--no-validate).typesfilter. A range that adds and then removes a file has no changed files, and hk would skip the step with "no files to run".2.
requires_ref_rangeon steps, needed for 1I did not set out to change steps, but 1 needed it. Without a range, these commands do the wrong thing quietly:
gitleaks git --log-opts="..HEAD"scans nothing and passes, and--since-commit ""makes trufflehog scan the whole history.My first version guarded each builtin with POSIX shell:
This is copied into four builtins, and it needs a POSIX shell, so it would not run where hk uses cmd.exe. So the branch adds a step field instead:
from_refis empty), hk fails the step without running its command:gitleaks: needs a ref range (pre-push, --from-ref, or --pr).hk testdoes the same for a step test withoutfrom_ref.{{from_ref}}is a separate argument that no shell parses.The branch also makes
to_refHEADwhen only--from-refis given, so argv can pass{{to_ref}}without the shell's${HK_TO_REF:-HEAD}. This is the same end thatfiles_between_refsalready uses.Other ways I considered
step_condition = "from_ref != ''":it skips the step instead of failing it, so a
scan = "commits"step placed inpre-commitby mistake does not run, and the hook passes with no message.it removes the copies but still needs a POSIX shell.
Notes on the implementation in my fork
Choices you may want to make differently:
--from-ref,{{to_ref}}andHK_TO_REFare nowHEAD. Before, they were empty, so this changes existing steps that use them.Not done:
hk check --planstill lists arequires_ref_rangestep as running when the run has no range.from_refdoes not resolve, for examplehk check --from-ref origin/mainin a clone that has not fetchedorigin/main, gitleaks and kingfisher print a git error but pass, and trufflehog passes with no error, even with a secret in the commits. betterleaks fails.to_ref, so the scanners get an invalid range. betterleaks fails and blocks the deletion. gitleaks, trufflehog, and kingfisher print a git error but pass.docs/pirate/is not updated.Tests
hk testcases shared by the four scanners: secret added then removed in the range, clean range,from_refonly, commits beforefrom_refand afterto_refignored, no range fails.pre-pushblocks a secret removed in a later commit, for each scanner.requires_ref_rangewithout and with a range,to_refdefaults toHEAD.test/builtin_tool_stubs: gitleaks 8.30.1, betterleaks 1.5.0, trufflehog 3.99.7, kingfisher 2.18.1.from_ref/to_reffields, whichhk testpasses to the step like a real run does.AI-assisted — Tool: Claude Code; model: anthropic/claude-opus-5-5; version: 2.1.328.
All reactions