Repository navigation
bug: load study.pkl.gz / exp_args.pkl with a restricted unpickler (sfind-862fb47f) #65
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
Open
devin-ai-integration
wants to merge
2
commits into
main
Choose a base branch
from
devin/sfind-862fb47f-study-safe-unpickle
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| """Restricted unpickling for study and experiment files. | ||
|
|
||
| `pickle.load` executes arbitrary code embedded in the file. Study directories can come from | ||
| shared drives or collaborators, so files such as `study.pkl.gz` and `exp_args.pkl` are loaded | ||
| through `RestrictedUnpickler`, which only resolves: | ||
|
|
||
| - dataclasses, Enums and pydantic models defined in trusted packages (`agentlab`, `browsergym` | ||
| and any extra package listed in the `AGENTLAB_TRUSTED_PICKLE_MODULES` environment variable, | ||
| comma separated, e.g. for agent args defined in your own package); | ||
| - an explicit allowlist of standard-library, numpy, pandas, pyarrow and omegaconf data types. | ||
|
|
||
| Any other global (functions such as `os.system`, arbitrary classes, ...) raises | ||
| `UnsafePickleError` before it can be called. | ||
| """ | ||
|
|
||
| import dataclasses | ||
| import enum | ||
| import io | ||
| import os | ||
| import pickle | ||
|
|
||
| TRUSTED_PACKAGES = ("agentlab", "browsergym", "bgym") | ||
| TRUSTED_PACKAGES_ENV_VAR = "AGENTLAB_TRUSTED_PICKLE_MODULES" | ||
|
|
||
| _STDLIB_GLOBALS = { | ||
| ("builtins", name) | ||
| for name in ( | ||
| "object", | ||
| "set", | ||
| "frozenset", | ||
| "slice", | ||
| "range", | ||
| "complex", | ||
| "bytes", | ||
| "bytearray", | ||
| "list", | ||
| "dict", | ||
| "tuple", | ||
| "str", | ||
| "int", | ||
| "float", | ||
| "bool", | ||
| ) | ||
| } | { | ||
| ("copyreg", "_reconstructor"), | ||
| ("collections", "OrderedDict"), | ||
| ("collections", "defaultdict"), | ||
| ("collections", "deque"), | ||
| ("collections", "Counter"), | ||
| ("datetime", "datetime"), | ||
| ("datetime", "date"), | ||
| ("datetime", "time"), | ||
| ("datetime", "timedelta"), | ||
| ("datetime", "timezone"), | ||
| ("decimal", "Decimal"), | ||
| ("fractions", "Fraction"), | ||
| ("uuid", "UUID"), | ||
| ("pathlib", "Path"), | ||
| ("pathlib", "PosixPath"), | ||
| ("pathlib", "WindowsPath"), | ||
| ("pathlib", "PurePath"), | ||
| ("pathlib", "PurePosixPath"), | ||
| ("pathlib", "PureWindowsPath"), | ||
| } | ||
|
|
||
| _NUMPY_GLOBALS = { | ||
| (module, name) | ||
| for module in ("numpy.core.multiarray", "numpy._core.multiarray") | ||
| for name in ("_reconstruct", "scalar") | ||
| } | { | ||
| ("numpy", "ndarray"), | ||
| ("numpy", "dtype"), | ||
| ("numpy.core.numeric", "_frombuffer"), | ||
| ("numpy._core.numeric", "_frombuffer"), | ||
| } | ||
|
|
||
| _PANDAS_GLOBALS = { | ||
| ("pandas", "DataFrame"), | ||
| ("pandas", "Series"), | ||
| ("pandas", "Index"), | ||
| ("pandas", "RangeIndex"), | ||
| ("pandas", "Timestamp"), | ||
| ("pandas", "Timedelta"), | ||
| ("pandas", "StringDtype"), | ||
| ("pandas", "CategoricalDtype"), | ||
| ("pandas", "Categorical"), | ||
| ("pandas.core.dtypes.dtypes", "CategoricalDtype"), | ||
| ("pandas.core.arrays.categorical", "Categorical"), | ||
| ("pandas.core.arrays.categorical", "_reconstruct"), | ||
| ("pandas.core.frame", "DataFrame"), | ||
| ("pandas.core.series", "Series"), | ||
| ("pandas.core.indexes.base", "Index"), | ||
| ("pandas.core.indexes.base", "_new_Index"), | ||
| ("pandas.core.indexes.range", "RangeIndex"), | ||
| ("pandas.core.internals.managers", "BlockManager"), | ||
| ("pandas.core.internals.managers", "SingleBlockManager"), | ||
| ("pandas._libs.internals", "_unpickle_block"), | ||
| ("pandas.core.internals.blocks", "new_block"), | ||
| ("pandas.arrays", "ArrowStringArray"), | ||
| ("pandas.arrays", "StringArray"), | ||
| ("pandas.core.arrays.string_", "StringDtype"), | ||
| ("pandas.core.arrays.string_", "StringArray"), | ||
| ("pandas.core.arrays.string_arrow", "ArrowStringArray"), | ||
| ("pandas.core.arrays.string_arrow", "ArrowStringArrayNumpySemantics"), | ||
| ("pyarrow.lib", "_restore_array"), | ||
| ("pyarrow.lib", "type_for_alias"), | ||
| ("pyarrow.lib", "py_buffer"), | ||
| } | ||
|
|
||
| _OMEGACONF_GLOBALS = { | ||
| ("omegaconf.dictconfig", "DictConfig"), | ||
| ("omegaconf.listconfig", "ListConfig"), | ||
| ("omegaconf.base", "ContainerMetadata"), | ||
| ("omegaconf.base", "Metadata"), | ||
| ("omegaconf.nodes", "AnyNode"), | ||
| ("omegaconf.nodes", "StringNode"), | ||
| ("omegaconf.nodes", "IntegerNode"), | ||
| ("omegaconf.nodes", "FloatNode"), | ||
| ("omegaconf.nodes", "BooleanNode"), | ||
| ("omegaconf.nodes", "BytesNode"), | ||
| ("omegaconf.nodes", "PathNode"), | ||
| ("omegaconf.nodes", "EnumNode"), | ||
| ("typing", "Any"), | ||
| } | ||
|
|
||
| SAFE_GLOBALS = frozenset(_STDLIB_GLOBALS | _NUMPY_GLOBALS | _PANDAS_GLOBALS | _OMEGACONF_GLOBALS) | ||
|
|
||
|
|
||
| class UnsafePickleError(pickle.UnpicklingError): | ||
| """Raised when a pickle references a global that is not allowed.""" | ||
|
|
||
|
|
||
| def _trusted_packages() -> tuple[str, ...]: | ||
| extra = os.environ.get(TRUSTED_PACKAGES_ENV_VAR, "") | ||
| return TRUSTED_PACKAGES + tuple(p.strip() for p in extra.split(",") if p.strip()) | ||
|
|
||
|
|
||
| def _in_trusted_package(module: str) -> bool: | ||
| return any(module == pkg or module.startswith(pkg + ".") for pkg in _trusted_packages()) | ||
|
|
||
|
|
||
| def _is_data_type(obj) -> bool: | ||
| if not isinstance(obj, type): | ||
| return False | ||
| if dataclasses.is_dataclass(obj) or issubclass(obj, enum.Enum): | ||
| return True | ||
| try: | ||
| from pydantic import BaseModel | ||
| except ImportError: # pragma: no cover | ||
| return False | ||
| return issubclass(obj, BaseModel) | ||
|
|
||
|
|
||
| class RestrictedUnpickler(pickle.Unpickler): | ||
| """Unpickler that only resolves allowlisted data types (see module docstring).""" | ||
|
|
||
| def find_class(self, module: str, name: str): | ||
| if (module, name) in SAFE_GLOBALS: | ||
| return super().find_class(module, name) | ||
| if _in_trusted_package(module) and "." not in name: | ||
| obj = super().find_class(module, name) | ||
| if _is_data_type(obj) and _in_trusted_package(getattr(obj, "__module__", "")): | ||
| return obj | ||
| raise UnsafePickleError( | ||
| f"Refusing to unpickle global '{module}.{name}': only dataclasses, Enums and pydantic " | ||
| f"models from {', '.join(_trusted_packages())} and allowlisted data types are " | ||
| f"permitted. If this file comes from a trusted source and defines classes in your " | ||
| f"own package, add that package to the {TRUSTED_PACKAGES_ENV_VAR} environment variable." | ||
| ) | ||
|
|
||
|
|
||
| def load(file): | ||
| """Safe replacement for `pickle.load` on study/experiment files.""" | ||
| return RestrictedUnpickler(file).load() | ||
|
|
||
|
|
||
| def loads(data: bytes): | ||
| """Safe replacement for `pickle.loads` on study/experiment files.""" | ||
| return load(io.BytesIO(data)) | ||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.