Skip to content

Scan with -only-allowlisted flag raises a ValueError when checking empty files. #936

@Roman-Malinowski

Description

@Roman-Malinowski

When running detect-secrets scan --only-allowlisted on a directory containing empty files (for instance a tracked and empty __init__.py), a ValueError is raised:

Traceback (most recent call last):
  File ".venv/bin/detect-secrets", line 10, in <module>
    sys.exit(main())
             ~~~~^^
  File ".venv/lib/python3.13/site-packages/detect_secrets/main.py", line 30, in main
    handle_scan_action(args)
    ~~~~~~~~~~~~~~~~~~^^^^^^
  File ".venv/lib/python3.13/site-packages/detect_secrets/main.py", line 64, in handle_scan_action
    for secret in scan_for_allowlisted_secrets_in_file(filename):
                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File ".venv/lib/python3.13/site-packages/detect_secrets/core/scan.py", line 204, in scan_for_allowlisted_secrets_in_file
    yield from _scan_for_allowlisted_secrets_in_lines(enumerate(lines, start=1), filename)
  File ".venv/lib/python3.13/site-packages/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)

This is because the provided iterator lines is empty.
A simple way to fix this would be to handle the exception in scan_for_allowlisted_secrets_in_file with:

    try:
        for lines in _get_lines_from_file(filename):
            yield from _scan_for_allowlisted_secrets_in_lines(enumerate(lines, start=1), filename)
            break
    except IOError:
        log.warning(f'Unable to open file: {filename}')
        return
    except ValueError:
        log.warning(f"Unable to scan file: {filename}. Ignore if it is an empty file.")
        return

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions