Repository navigation
feat: restrict pickle loading to builtins with SafeUnpickler #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,36 @@ | |
| from typing import Any | ||
|
|
||
|
|
||
| class UnsafePickleError(pickle.UnpicklingError): | ||
| """Raised when unpickling requests a global outside the builtins allowlist.""" | ||
|
|
||
|
|
||
| class SafeUnpickler(pickle.Unpickler): | ||
| """Allow only builtins container and scalar types during unpickling.""" | ||
|
|
||
| ALLOWED_CLASSES = { | ||
| "builtins": { | ||
| "dict", | ||
| "list", | ||
| "tuple", | ||
| "set", | ||
| "str", | ||
| "int", | ||
| "float", | ||
| "bool", | ||
| "bytes", | ||
| }, | ||
| } | ||
|
|
||
| def find_class(self, module: str, name: str): | ||
| if module in self.ALLOWED_CLASSES and name in self.ALLOWED_CLASSES[module]: | ||
| return super().find_class(module, name) | ||
| raise UnsafePickleError( | ||
| f"Unsafe pickle: global '{module}.{name}' is not allowed. " | ||
| "Only basic Python types are permitted for security." | ||
| ) | ||
|
|
||
|
|
||
| def load_pickle_to_dict(pickle_file: Path) -> dict: | ||
| """ | ||
| 从指定路径加载 pickle 文件,并确保其内容为 dict 类型。 | ||
|
|
@@ -23,14 +53,19 @@ def load_pickle_to_dict(pickle_file: Path) -> dict: | |
| Raises: | ||
| FileNotFoundError: 文件不存在 | ||
| ValueError: 文件内容不是 dict 类型 | ||
| pickle.UnpicklingError: 反序列化失败(如文件损坏或非 pickle 格式) | ||
| pickle.UnpicklingError: 反序列化失败(文件损坏或非 pickle 格式) | ||
| UnsafePickleError: 请求了 allowlist 之外的全局对象 | ||
| """ | ||
| if not pickle_file.is_file(): | ||
| raise FileNotFoundError(f"Cannot found pickle file: {pickle_file}") | ||
|
|
||
| try: | ||
| with open(pickle_file, "rb") as f: | ||
| data = pickle.load(f) | ||
| data = SafeUnpickler(f).load() | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [minor]
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Chained the original |
||
| except UnsafePickleError: | ||
| raise | ||
| except pickle.UnpicklingError as exc: | ||
| raise pickle.UnpicklingError(f"Cannot load pickle file: {pickle_file}: {exc}") from exc | ||
| except Exception: | ||
| # Preserve the established exception context and corrupt-pickle behavior. | ||
| raise pickle.UnpicklingError(f"Cannot load pickle file: {pickle_file}") # noqa: B904 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,36 @@ | ||
| import io | ||
| import pickle | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| from pt_snap_cli.snapshot.util.file_util import ( | ||
| SafeUnpickler, | ||
| UnsafePickleError, | ||
| check_dir_valid, | ||
| check_file_valid, | ||
| load_pickle_to_dict, | ||
| save_dict_to_pickle, | ||
| ) | ||
| from tests.snapshot.helpers import FIXTURE_DIR | ||
|
|
||
| UNSAFE_PICKLE_PAYLOAD = {"executed": False} | ||
|
|
||
|
|
||
| def unsafe_pickle_payload() -> dict: | ||
| UNSAFE_PICKLE_PAYLOAD["executed"] = True | ||
| return {"pwned": True} | ||
|
|
||
|
|
||
| class UnsafeReducePayload: | ||
| def __reduce__(self) -> tuple[object, tuple[()]]: | ||
| return (unsafe_pickle_payload, ()) | ||
|
|
||
|
|
||
| def write_unsafe_reduce_pickle(path: Path) -> Path: | ||
| UNSAFE_PICKLE_PAYLOAD["executed"] = False | ||
| path.write_bytes(pickle.dumps({"payload": UnsafeReducePayload()})) | ||
| return path | ||
|
|
||
|
|
||
| def test_save_and_load_pickle_round_trip(tmp_path: Path): | ||
|
|
@@ -35,6 +57,53 @@ def test_load_pickle_to_dict_rejects_missing_file(tmp_path: Path): | |
| load_pickle_to_dict(tmp_path / "missing.pkl") | ||
|
|
||
|
|
||
| def test_load_pickle_to_dict_rejects_unsafe_global_without_running_payload(tmp_path: Path): | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [minor] This test covers rejection of a non-
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||
| target = write_unsafe_reduce_pickle(tmp_path / "evil.pkl") | ||
|
|
||
| with pytest.raises( | ||
| UnsafePickleError, | ||
| match=r"Unsafe pickle: global 'tests\.snapshot\.test_file_util\.unsafe_pickle_payload'", | ||
| ): | ||
| load_pickle_to_dict(target) | ||
|
|
||
| assert UNSAFE_PICKLE_PAYLOAD["executed"] is False | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("name", ("eval", "exec", "open", "NoneType")) | ||
| def test_safe_unpickler_rejects_disallowed_builtin_names(name: str): | ||
| unpickler = SafeUnpickler(io.BytesIO(b"")) | ||
|
|
||
| with pytest.raises(UnsafePickleError, match=rf"Unsafe pickle: global 'builtins\.{name}'"): | ||
| unpickler.find_class("builtins", name) | ||
|
|
||
|
|
||
| def test_load_pickle_to_dict_rejects_corrupt_stream(tmp_path: Path): | ||
| 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") as caught: | ||
| load_pickle_to_dict(target) | ||
|
|
||
| message = str(caught.value) | ||
| assert str(target) in message | ||
| assert "Unsafe pickle" not in message | ||
| assert not isinstance(caught.value, UnsafePickleError) | ||
| assert isinstance(caught.value.__cause__, pickle.UnpicklingError) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "fixture_name", | ||
| ( | ||
| "snapshot_with_empty_cache.pkl", | ||
| "snapshot_expandable.pkl", | ||
| "snapshot_with_multi_devices.pkl", | ||
| ), | ||
| ) | ||
| def test_load_pickle_to_dict_accepts_fixture_snapshots(fixture_name: str): | ||
| data = load_pickle_to_dict(FIXTURE_DIR / fixture_name) | ||
| assert isinstance(data, dict) | ||
|
|
||
|
|
||
| def test_save_dict_to_pickle_rejects_non_dict(tmp_path: Path): | ||
| with pytest.raises(TypeError): | ||
| save_dict_to_pickle([1, 2, 3], tmp_path / "bad.pkl") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit] The allowlist set is duplicated here and in
ALLOWED_CLASSESinfile_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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No change. The PROVENANCE line stays a point-in-time record of the original allowlist;
ALLOWED_CLASSESremains the live source of truth.