Commit 818a0ec
Add graceful degradation and scan API for robust scanning (#218)
* Add graceful degradation and scan API for robust scanning
- 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>
* Fix scan API: broken RelaxedZipFile, unsafe ScanResult, missing tests
- 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>
* Address review: move RelaxedZipFile, fix error handling, add tests
- 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>
* Fix Ruff linting failure
* Minor improvements to scan API
- 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>
* Fix test_scans_all_pickle_extensions
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Thomas Chauchefoin <thomas.chauchefoin@trailofbits.com>1 parent e5e34bb commit 818a0ec
3 files changed
+478
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
| 3 | + | |
2 | 4 | | |
| 5 | + | |
3 | 6 | | |
4 | | - | |
| 7 | + | |
5 | 8 | | |
6 | | - | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
7 | 36 | | |
8 | 37 | | |
9 | 38 | | |
| |||
75 | 104 | | |
76 | 105 | | |
77 | 106 | | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
0 commit comments