Skip to content

fix: handle empty files in --only-allowlisted scan#949

Open
ReGenDevOps wants to merge 1 commit intoYelp:masterfrom
ReGenDevOps:fix/empty-file-allowlisted-scan
Open

fix: handle empty files in --only-allowlisted scan#949
ReGenDevOps wants to merge 1 commit intoYelp:masterfrom
ReGenDevOps:fix/empty-file-allowlisted-scan

Conversation

@ReGenDevOps
Copy link
Copy Markdown

Problem

detect-secrets scan --only-allowlisted crashes with a ValueError when it encounters an empty file:

ValueError: not enough values to unpack (expected 2, got 0)

Traceback:

File "detect_secrets/core/scan.py", line 229, in _scan_for_allowlisted_secrets_in_lines
    line_numbers, lines = zip(*lines)
ValueError: not enough values to unpack (expected 2, got 0)

Root Cause

_scan_for_allowlisted_secrets_in_lines() calls zip(*lines) directly. When lines is an empty iterable (from an empty file or an empty diff chunk), zip() returns an empty tuple which cannot be unpacked into two variables.

Fix

Materialize the iterable into a list first and return early if empty. This is semantically correct — an empty file cannot contain any allowlisted secrets.

lines_list = list(lines)
if not lines_list:
    return

line_numbers, lines = zip(*lines_list)

Testing

  • Added test_handles_empty_file_for_allowlisted_scan to tests/core/scan_test.py
  • Verified on Python 3.14.2 with detect-secrets 1.5.0
  • Tested with both empty .py files and full project scans

Environment

  • Python 3.14.2
  • detect-secrets 1.5.0
  • macOS 15 (Apple Silicon)

_scan_for_allowlisted_secrets_in_lines() calls zip(*lines) which raises
ValueError when lines is an empty iterable (e.g. scanning an empty file
or an empty diff chunk).

Materialize the iterable to a list first and return early if empty.

Fixes: ValueError: not enough values to unpack (expected 2, got 0)
when running: detect-secrets scan --only-allowlisted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant