Repository navigation
feat: restrict pickle loading to builtins with SafeUnpickler - #102
Conversation
| try: | ||
| with open(pickle_file, "rb") as f: | ||
| data = pickle.load(f) | ||
| data = SafeUnpickler(f).load() |
There was a problem hiding this comment.
[minor] SafeUnpickler(f).load() rejections (e.g. Unsafe pickle: global 'os.system' is not allowed) are swallowed by the except Exception: wrapper at line 61 and re-raised as a generic UnpicklingError('Cannot load pickle file: ...') without the cause chain (# noqa: B904, the only such suppression in the repo). No caller regression: import/split map both corrupt-stream and allowlist rejections to the same error surface, and the new tests only assert the exception type. But the specific rejected module.name is lost from logs/forensics. Consider except pickle.UnpicklingError: or raise ... from exc so the allowlist-rejection message is preserved.
There was a problem hiding this comment.
Chained the original UnpicklingError with from exc and included {exc} in the wrapper. Allowlist rejections now surface as Cannot load pickle file: <path>: Unsafe pickle: global 'module.name' ..., with __cause__ set. Other non-UnpicklingError failures still use the generic wrapper.
| "float", | ||
| "bool", | ||
| "bytes", | ||
| "NoneType", |
There was a problem hiding this comment.
[nit] NoneType is a valid GLOBAL lookup target, but CPython pickles None via the NONE opcode, so this entry is defensive/unreachable in normal streams. Harmless to keep (denying it would be moot).
There was a problem hiding this comment.
Leaving NoneType on the allowlist. It matches the issue spec and is only a defensive GLOBAL target; CPython still encodes None with the NONE opcode.
There was a problem hiding this comment.
Follow-up: hasattr(builtins, "NoneType") is false, so this allowlist entry was dead. super().find_class("builtins", "NoneType") would raise AttributeError and get wrapped as a corrupt-stream error. Removed NoneType. A GLOBAL of builtins.NoneType is now UnsafePickleError. The original PROVENANCE line is unchanged (append-only); the 2026-08-20 entry records the correction.
| load_pickle_to_dict(tmp_path / "missing.pkl") | ||
|
|
||
|
|
||
| def test_load_pickle_to_dict_rejects_unsafe_global_without_running_payload(tmp_path: Path): |
There was a problem hiding this comment.
[minor] This test covers rejection of a non-builtins module global (tests.snapshot.test_file_util.unsafe_pickle_payload), but not the module == 'builtins' disallowed-name branch (e.g. builtins.eval, builtins.open). Suggest parametrizing a few disallowed builtins names to lock that branch.
There was a problem hiding this comment.
Added find_class coverage for builtins.eval, builtins.exec, and builtins.open. A pickled open object is _io.open, so a dump/load test would miss the module == 'builtins' disallowed-name branch.
| target = tmp_path / "corrupt.pkl" | ||
| target.write_bytes(b"\x80\x04not a valid pickle stream") | ||
|
|
||
| with pytest.raises(pickle.UnpicklingError, match="Cannot load pickle file"): |
There was a problem hiding this comment.
[minor] match="Cannot load pickle file" only pins the generic wrapper prefix, which any wrapped failure would produce — the test would pass even if a valid stream were incorrectly rejected. A stronger assertion (e.g. checking the underlying cause, or dropping match to pin the exact exception) would lock the corrupt-stream behavior.
There was a problem hiding this comment.
The corrupt-stream test now asserts the path is in the wrapper, Unsafe pickle is absent, and __cause__ is UnpicklingError.
| evidence are unchanged. | ||
| - On 2026-08-19, issue #99 replaced unrestricted `pickle.load()` in | ||
| `snapshot/util/file_util.py` with `SafeUnpickler`. Unpickling now allows only | ||
| `builtins` container and scalar types (`dict`/`list`/`tuple`/`set`/`str`/ |
There was a problem hiding this comment.
[nit] The allowlist set is duplicated here and in ALLOWED_CLASSES in file_util.py. PROVENANCE is append-only, so this entry is a point-in-time record — acceptable, but a future allowlist change adds a third source of truth alongside the docstring. No action needed beyond awareness.
There was a problem hiding this comment.
No change. The PROVENANCE line stays a point-in-time record of the original allowlist; ALLOWED_CLASSES remains the live source of truth.
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
d261c1a to
a9f209a
Compare
Summary
pickle.load()inload_pickle_to_dictwithSafeUnpickler, allowing onlybuiltinscontainer and scalar types.__reduce__payloads fail withUnpicklingError/ImportExecutionError/SplitErrorand do not execute.PROVENANCE.mdrecords the security-boundary change.Fixes #99
Test plan
test_load_pickle_to_dict_rejects_unsafe_global_without_running_payloadUnpicklingErrorwrapperChecklist
Snapshot Runtime Provenance
Snapshot provenance decision: updated
Made with Cursor