Add graceful degradation and scan API for robust scanning#218
Merged
thomas-chauchefoin-tob merged 8 commits intomasterfrom Mar 21, 2026
Merged
Add graceful degradation and scan API for robust scanning#218thomas-chauchefoin-tob merged 8 commits intomasterfrom
thomas-chauchefoin-tob merged 8 commits intomasterfrom
Conversation
- Add ScanResult class with is_safe, severity, results, and errors - Add scan_file() for graceful single-file scanning - Add scan_archive() for scanning ZIP archives - Add RelaxedZipFile class that ignores CRC validation errors - Add _scan_bytes() helper for in-memory scanning - Export new functions from package This API provides picklescan-like graceful degradation, continuing to scan even when individual files fail to parse. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Replace RelaxedZipFile CRC=0 hack (caused BadZipFile on valid files) with _expected_crc=None on the returned file handle - Make ScanResult.is_safe a computed property instead of a stored bool; __bool__ now returns False when errors exist (incomplete scan != safe) - Escalate severity in _scan_bytes when check_safety() or outer parse exceptions occur in graceful mode - Deduplicate scan_file by delegating to _scan_bytes - Change graceful default to False for scan_file and scan_archive - Remove dead code: data-is-None branch, is_safe kwarg, read() override - Add 12 tests covering scan_file, scan_archive, ScanResult, RelaxedZipFile Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Move RelaxedZipFile from polyglot.py to loader.py to avoid requiring numpy/torch for scan_archive (RelaxedZipFile only needs stdlib zipfile) - Add RuntimeWarning when _expected_crc attribute is missing on future Python versions so CRC bypass degradation is not silent - Widen scan_archive outer exception handler to catch OSError (covers FileNotFoundError, PermissionError) in graceful mode, matching scan_file - Separate archive.read() try/except from _scan_bytes() call so the inner catch only handles I/O errors, not analysis-layer exceptions - Escalate _scan_bytes outer except Exception to LIKELY_UNSAFE (was SUSPICIOUS — for a security scanner, unanalyzable files should be treated as dangerous) - Include exception type in all error messages for debuggability - Add 8 new tests: archive nonexistent graceful/non-graceful, bad ZIP non-graceful raises, mixed good/bad archive members, all pickle extensions, is_safe boundary at POSSIBLY_UNSAFE, analysis error severity escalation, RelaxedZipFile CRC bypass regression Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Narrow file-open exception catch from Exception to OSError - Rename scan_archive to scan_zip_archive to clarify ZIP-only scope - Use PurePosixPath.suffix for extension parsing - Remove .pt/.pth from extension filter (these are ZIP containers, not raw pickles) - Update docstring to reference fickling.polyglot for other archive formats Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ScanResultclass withis_safe,severity,results, anderrorsattributesscan_file()function for graceful single-file scanningscan_archive()function for scanning ZIP archives with graceful error handlingRelaxedZipFileclass that ignores CRC validation errors (matches PyTorch behavior)API Example
Test plan
🤖 Generated with Claude Code