Skip to content

Commit 5735060

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

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

pre_commit_hooks/detect_private_key.py

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

33
import argparse
4+
import gzip
45
from collections.abc import Sequence
56

67
BLACKLIST = [
@@ -29,6 +30,16 @@ def main(argv: Sequence[str] | None = None) -> int:
2930
content = f.read()
3031
if any(line in content for line in BLACKLIST):
3132
private_key_files.append(filename)
33+
continue
34+
try:
35+
if filename.endswith(('.gz', '.tgz')):
36+
with gzip.open(filename, 'rb') as f:
37+
content = f.read()
38+
if any(line in content for line in BLACKLIST):
39+
private_key_files.append(filename)
40+
continue
41+
except gzip.BadGzipFile:
42+
pass
3243

3344
if private_key_files:
3445
for private_key_file in private_key_files:

tests/detect_private_key_test.py

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

3+
import gzip
4+
35
import pytest
46

57
from pre_commit_hooks.detect_private_key import main
@@ -26,3 +28,16 @@ def test_main(input_s, expected_retval, tmpdir):
2628
path = tmpdir.join('file.txt')
2729
path.write_binary(input_s)
2830
assert main([str(path)]) == expected_retval
31+
32+
33+
@pytest.mark.parametrize(('input_s', 'expected_retval'), TESTS)
34+
def test_main_gzip(input_s, expected_retval, tmpdir):
35+
path = tmpdir.join('file.txt.gz')
36+
path.write_binary(gzip.compress(input_s))
37+
assert main([str(path)]) == expected_retval
38+
39+
40+
def test_main_gz_not_gzip(tmpdir):
41+
path = tmpdir.join('file.txt.gz')
42+
path.write_binary(b'not a sensitive value nor gzip')
43+
assert main([str(path)]) == 0

0 commit comments

Comments
 (0)