Commit 3d656b9
Add safe builtins allowlist to prevent false positives (#206)
* Add safe builtins allowlist to prevent false positives
Previously, all imports from the builtins module were flagged as
LIKELY_OVERTLY_MALICIOUS, even safe functions like dict(), len(),
sorted(), and enumerate(). This caused false positives for legitimate
pickle files.
Add SAFE_BUILTINS frozenset containing type constructors and pure
functions that cannot be used for code execution or system access.
Modify both UnsafeImportsML and UnsafeImports analyzers to check
individual builtin names against this allowlist.
Dangerous builtins like eval, exec, getattr, __import__, and open
remain blocked as they can be used for arbitrary code execution.
Fixes #205
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* Fix ruff formatting for PR
- Format analysis.py and test_bypasses.py with ruff
- Remove mypy from pre-commit hooks (was already skipped in CI)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* Extract SAFE_BUILTINS and BUILTIN_MODULE_NAMES out of analysis module
- Add BUILTIN_MODULE_NAMES constant to eliminate repeated tuple
- Move SAFE_BUILTINS from UnsafeImportsML class to fickle.py
- Update both UnsafeImportsML and UnsafeImports to use shared constants
- Removes cross-class dependency (UnsafeImports no longer references
UnsafeImportsML.SAFE_BUILTINS)
Co-Authored-By: Claude <noreply@anthropic.com>
* Remove type from SAFE_BUILTINS allowlist
type() with 3 arguments dynamically creates classes, which could be
a building block in exploit chains (e.g., triggering __init_subclass__
or __set_name__ on imported base classes/descriptors). While not
directly exploitable in isolation, there's no legitimate reason for
a pickle to dynamically create classes, so we exclude it as a
defense-in-depth measure.
Co-Authored-By: Claude <noreply@anthropic.com>
* Make unsafe builtin tests verify both analyzers detect the issue
Update test_unsafe_builtins_still_flagged and test_unsafe_builtin_eval_still_flagged
to assert that both UnsafeImports and UnsafeImportsML flag dangerous builtins,
rather than checking if either one does.
Co-Authored-By: Claude <noreply@anthropic.com>
* Simplify UnsafeImports builtin check using all()
Co-Authored-By: Claude <noreply@anthropic.com>
* Format BUILTIN_MODULE_NAMES as single line
Co-Authored-By: Claude <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Thomas Chauchefoin <thomas.chauchefoin@trailofbits.com>1 parent 69ba1e5 commit 3d656b9
3 files changed
+168
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
11 | 18 | | |
12 | 19 | | |
13 | 20 | | |
| |||
267 | 274 | | |
268 | 275 | | |
269 | 276 | | |
270 | | - | |
271 | | - | |
272 | | - | |
273 | | - | |
274 | | - | |
275 | | - | |
276 | | - | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
277 | 298 | | |
278 | 299 | | |
279 | 300 | | |
| |||
348 | 369 | | |
349 | 370 | | |
350 | 371 | | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
351 | 376 | | |
352 | 377 | | |
353 | 378 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 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 | + | |
65 | 131 | | |
66 | 132 | | |
67 | 133 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
376 | 376 | | |
377 | 377 | | |
378 | 378 | | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
0 commit comments