Skip to content

Commit

Permalink
gracefully handles binary files (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Loo committed Nov 23, 2020
1 parent b06d965 commit 603fe0a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
10 changes: 7 additions & 3 deletions detect_secrets/core/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ def scan_file(filename: str) -> Generator[PotentialSecret, None, None]:
with open(filename) as f:
log.info(f'Checking file: {filename}')

lines = _get_transformed_file(f)
if not lines:
lines = f.readlines()
try:
lines = _get_transformed_file(f)
if not lines:
lines = f.readlines()
except UnicodeDecodeError:
# We flat out ignore binary files.
return

has_secret = False
for secret in _process_line_based_plugins(
Expand Down
1 change: 1 addition & 0 deletions detect_secrets/transformers/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def parse_file(self, file: IO) -> List[str]:
def _parse_file(file: IO, add_header: bool = False) -> List[str]:
"""
:raises: configparser.Error
:raises: UnicodeDecodeError
"""
lines: List[str] = []
for key, value, line_number in IniFileParser(file, add_header=add_header):
Expand Down
12 changes: 11 additions & 1 deletion tests/core/scan_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,23 @@ def test_handles_broken_yaml_gracefully():
textwrap.dedent("""
metadata:
name: {{ .values.name }}
""")[1:].encode(),
""")[1:].encode(),
)
f.seek(0)

assert not list(scan.scan_file(f.name))


def test_handles_binary_files_gracefully():
# NOTE: This suffix needs to be something that isn't in the known file types, as determined
# by `detect_secrets.util.filetype.determine_file_type`.
with tempfile.NamedTemporaryFile(suffix='.woff2') as f:
f.write(b'\x86')
f.seek(0)

assert not list(scan.scan_file(f.name))


@pytest.fixture(autouse=True)
def configure_plugins():
with transient_settings({
Expand Down

0 comments on commit 603fe0a

Please sign in to comment.