diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-21 20:47:18 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-21 20:47:18 +0000 |
commit | ceb85610c77b7487b0b7d742415301922c6b13b6 (patch) | |
tree | 82456c5d0bc77961759812ddd85414435ba89127 /pre_commit_hooks/detect_private_key.py | |
parent | Initial commit. (diff) | |
download | pre-commit-hooks-ceb85610c77b7487b0b7d742415301922c6b13b6.tar.xz pre-commit-hooks-ceb85610c77b7487b0b7d742415301922c6b13b6.zip |
Adding upstream version 4.5.0+dfsg.upstream/4.5.0+dfsgupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'pre_commit_hooks/detect_private_key.py')
-rw-r--r-- | pre_commit_hooks/detect_private_key.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/pre_commit_hooks/detect_private_key.py b/pre_commit_hooks/detect_private_key.py new file mode 100644 index 0000000..cd51f90 --- /dev/null +++ b/pre_commit_hooks/detect_private_key.py @@ -0,0 +1,42 @@ +from __future__ import annotations + +import argparse +from typing import Sequence + +BLACKLIST = [ + b'BEGIN RSA PRIVATE KEY', + b'BEGIN DSA PRIVATE KEY', + b'BEGIN EC PRIVATE KEY', + b'BEGIN OPENSSH PRIVATE KEY', + b'BEGIN PRIVATE KEY', + b'PuTTY-User-Key-File-2', + b'BEGIN SSH2 ENCRYPTED PRIVATE KEY', + b'BEGIN PGP PRIVATE KEY BLOCK', + b'BEGIN ENCRYPTED PRIVATE KEY', + b'BEGIN OpenVPN Static key V1', +] + + +def main(argv: Sequence[str] | None = None) -> int: + parser = argparse.ArgumentParser() + parser.add_argument('filenames', nargs='*', help='Filenames to check') + args = parser.parse_args(argv) + + private_key_files = [] + + for filename in args.filenames: + with open(filename, 'rb') as f: + content = f.read() + if any(line in content for line in BLACKLIST): + private_key_files.append(filename) + + if private_key_files: + for private_key_file in private_key_files: + print(f'Private key found: {private_key_file}') + return 1 + else: + return 0 + + +if __name__ == '__main__': + raise SystemExit(main()) |