Skip to content

Scan compressed pickle artifacts - #68

Open
massy-o wants to merge 1 commit into
mmaitre314:mainfrom
massy-o:fix-compressed-pickle-scanning
Open

Scan compressed pickle artifacts#68
massy-o wants to merge 1 commit into
mmaitre314:mainfrom
massy-o:fix-compressed-pickle-scanning

Conversation

@massy-o

@massy-o massy-o commented May 14, 2026

Copy link
Copy Markdown

Summary

  • recognize compound compressed pickle extensions such as .joblib.gz during file, directory, archive, and Hugging Face model scans
  • decompress gzip, bzip2, lzma, and xz pickle artifacts before disassembling their opcodes
  • add regression coverage for compressed joblib files scanned directly, from directories, and inside zip archives

Testing

  • uv run --with-editable . --with pytest pytest tests/test_scanner.py::test_scan_compressed_joblib_file_path tests/test_scanner.py::test_scan_directory_path_includes_compressed_joblib tests/test_scanner.py::test_scan_zip_bytes_includes_compressed_joblib_member
  • uv run --with-editable . --with pytest --with numpy --with py7zr pytest tests/test_scanner.py
  • uv run python -m compileall src/picklescan tests/test_scanner.py

@massy-o massy-o left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self-review notes:

  • The decompression step is gated by explicit compound pickle extensions only, so arbitrary .gz files are not treated as pickles during directory/Hugging Face filtering.
  • zipfile.is_zipfile() is still checked before the pickle fallback, preserving existing .npz and zip archive behavior.
  • I covered the three routes that previously skipped or misparsed compressed joblib payloads: direct file path, directory traversal, and compressed members inside zip archives.
  • Full tests/test_scanner.py passes with the optional NumPy/7z dependencies installed.

Comment thread src/picklescan/scanner.py
".lzma": lzma.decompress,
".xz": lzma.decompress,
}
_compressed_pickle_file_extensions = {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self-review: compound extensions are generated from the existing pickle extension allowlist, so this expands coverage for compressed forms without making every compressed file type scan as pickle.

Comment thread src/picklescan/scanner.py
"""Disassemble a Pickle stream and report issues"""
_log.debug(f"scan_pickle_bytes({file_id})")

if file_ext in _compressed_pickle_file_extensions:

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self-review: decompression happens before pickle opcode parsing and decompression failures are surfaced as scan errors, avoiding the previous clean pass on gzip headers.

Comment thread src/picklescan/scanner.py
_log.debug("Scanning file %s in zip archive %s", file_name, file_id)
with zip.open(file_name, "r") as file:
result.merge(scan_pickle_bytes(file, f"{file_id}:{file_name}"))
result.merge(scan_pickle_bytes(file, f"{file_id}:{file_name}", file_ext=file_ext))

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self-review: this keeps archive member scanning on the same compound-extension path as direct files, which closes the equivalent bypass for .joblib.gz stored inside a zip.

Comment thread tests/test_scanner.py
)


def test_scan_compressed_joblib_file_path(tmp_path):

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self-review: these regression tests exercise the direct path, directory traversal, and zip-member path so the fix is covered where the scanner previously relied on single-suffix extension checks.

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