Implement detect-private-key as builtin hook#893
Merged
j178 merged 2 commits intoj178:masterfrom Oct 16, 2025
Merged
Conversation
119353b to
3626d91
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #893 +/- ##
==========================================
+ Coverage 89.66% 89.80% +0.13%
==========================================
Files 61 62 +1
Lines 11429 11552 +123
==========================================
+ Hits 10248 10374 +126
+ Misses 1181 1178 -3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
db7c873 to
d91fc9f
Compare
34 tasks
detect-private-key as builtin hook
j178
approved these changes
Oct 15, 2025
d91fc9f to
b40a83a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
As discussed in #880 there is a priority to port more builtin hooks that are being used in projects with prek already, and
detect-private-keyis one of the more popular ones.This hook prevents the accidental upload of private keys, so it's obviously very important that it's correct.
The python hook is very simple: here, it has a blacklist of
and then it iterates over the blacklist, checking for substring matches against the entire file content:
This computes a mask over the blacklist, i.e. a bool for each trigger string:
the
line in contentis a substring match (sub-bytesmatch technically) returning a bool.It triggers like this
The first is
Trueso the any iterator will returnTrueDemo
Running this on the Apache Airflow repo (which uses it, and uses prek), pre-commit runs 40% faster (.08s vs .13s)
and then with the new feature branch of prek, it runs in 0.47s (over 3x slower)
Detect if private key is added to the repository.........................Passed - hook id: detect-private-key - duration: 0.47sI swapped to
memchr::memmem::find(I see the memchr dependency is already here) and it sped up to become faster than pre-commit (now 40% faster) 🎉Detect if private key is added to the repository.........................Passed - hook id: detect-private-key - duration: 0.05sMore importantly, if I write a new file (and
git addit) to the airflow repo, it does indeed get detected:louis 🌟 ~/tmp/airflow $ cat foo.md --- BEGIN RSA PRIVATE KEY --- hello