From 07c938e1b1c04df7f2969b5480cbd6fa54fd67c1 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Thu, 11 Jun 2015 13:28:10 -0700 Subject: [PATCH] Allow arbitrary binary to pass the private key hook check. Resolves #64 --- pre_commit_hooks/detect_private_key.py | 12 +++++------- tests/detect_private_key_test.py | 2 ++ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pre_commit_hooks/detect_private_key.py b/pre_commit_hooks/detect_private_key.py index 215ad56..782b680 100644 --- a/pre_commit_hooks/detect_private_key.py +++ b/pre_commit_hooks/detect_private_key.py @@ -1,7 +1,6 @@ from __future__ import print_function import argparse -import io import sys @@ -13,12 +12,11 @@ def detect_private_key(argv=None): private_key_files = [] for filename in args.filenames: - with io.open(filename, 'r') as f: - content = f.read() - if 'BEGIN RSA PRIVATE KEY' in content: - private_key_files.append(content) - if 'BEGIN DSA PRIVATE KEY' in content: - private_key_files.append(content) + content = open(filename, 'rb').read() + if b'BEGIN RSA PRIVATE KEY' in content: + private_key_files.append(content) + if b'BEGIN DSA PRIVATE KEY' in content: + private_key_files.append(content) if private_key_files: for private_key_file in private_key_files: diff --git a/tests/detect_private_key_test.py b/tests/detect_private_key_test.py index 6d2e627..c912624 100644 --- a/tests/detect_private_key_test.py +++ b/tests/detect_private_key_test.py @@ -10,6 +10,8 @@ TESTS = ( (b'-----BEGIN DSA PRIVATE KEY-----', 1), (b'ssh-rsa DATA', 0), (b'ssh-dsa DATA', 0), + # Some arbitrary binary data + (b'\xa2\xf1\x93\x12', 0), )