Skip to content

Commit bf27513

Browse files
Find private keys within gzip-compresssed files
1 parent 5c514f8 commit bf27513

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

pre_commit_hooks/detect_private_key.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import annotations
22

33
import argparse
4+
import gzip
5+
46
from collections.abc import Sequence
57

68
BLACKLIST = [
@@ -29,6 +31,17 @@ def main(argv: Sequence[str] | None = None) -> int:
2931
content = f.read()
3032
if any(line in content for line in BLACKLIST):
3133
private_key_files.append(filename)
34+
continue
35+
try:
36+
if filename.endswith(".gz"):
37+
with gzip.open(filename, 'rb') as f:
38+
content = f.read()
39+
if any(line in content for line in BLACKLIST):
40+
private_key_files.append(filename)
41+
continue
42+
except gzip.BadGzipFile:
43+
pass
44+
3245

3346
if private_key_files:
3447
for private_key_file in private_key_files:

tests/detect_private_key_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import gzip
34
import pytest
45

56
from pre_commit_hooks.detect_private_key import main
@@ -26,3 +27,9 @@ def test_main(input_s, expected_retval, tmpdir):
2627
path = tmpdir.join('file.txt')
2728
path.write_binary(input_s)
2829
assert main([str(path)]) == expected_retval
30+
31+
@pytest.mark.parametrize(('input_s', 'expected_retval'), TESTS)
32+
def test_main_gzip(input_s, expected_retval, tmpdir):
33+
path = tmpdir.join('file.txt.gz')
34+
path.write_binary(gzip.compress(input_s))
35+
assert main([str(path)]) == expected_retval

0 commit comments

Comments
 (0)