Add optional module allowlist to PickleLoader deserialization#1648
Open
elijahbenizzy wants to merge 1 commit into
Open
Add optional module allowlist to PickleLoader deserialization#1648elijahbenizzy wants to merge 1 commit into
elijahbenizzy wants to merge 1 commit into
Conversation
PickleLoader has historically called pickle.load on caller-supplied bytes without restriction, mirroring pandas.read_pickle / joblib.load. That puts the safety burden entirely on the caller, which is brittle once pickle files are shared across teams or produced by upstream systems. Introduce an optional allowlist of (module, qualname) pairs, either per-instance via PickleLoader(allowlist=...) or process-wide via set_pickle_loader_allowlist(...). When configured, a RestrictedUnpickler overrides find_class to reject anything off the allowlist, raising UnpicklingError before any global is resolved. When no allowlist is set, behavior is unchanged for backward compatibility but a warning is now emitted on every load to signal the unrestricted mode. Tests cover: the legacy unrestricted-with-warning path, allowlist permitting a legitimate roundtrip, per-instance allowlist blocking a malicious __reduce__ payload, module-level allowlist applying to fresh instances, and per-instance overriding the module-level default. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Adds an optional allowlist to
PickleLoader.load_datainhamilton/io/default_data_loaders.py. Without an allowlist: backward-compatible behavior, but emits a one-timeUserWarningper load to surface that unrestricted pickle deserialization is in use. With an allowlist: a_RestrictedUnpickler(pickle.Unpickler)subclass overridesfind_class(module, name)to validate against the allowlist and raisesUnpicklingErrorbefore any__reduce__payload can resolve.API
Two ways to set the allowlist, in resolution order (highest priority first):
PickleLoader(allowlist=[("myapp.models", "MyClass"), ...])set_pickle_loader_allowlist([...])UserWarningAllowlist entries are
(module, qualname)tuples — per-class granularity rather than prefix-string. The motivation: pickle attacks routinely reach for specific symbols within otherwise-trusted modules (builtins.eval,os.system,subprocess.Popen), so denying by full(module, qualname)pair is more useful than module-prefix matching.Trust model (now in the docstring)
PickleLoaderreads pickle bytes from a caller-supplied path. Pickle deserialization is unsafe when those bytes originate from a source the application doesn't fully control — files passed between teams, intermediate caches written by upstream pipelines, shared persistence backends. The allowlist closes the__reduce__arbitrary-code-execution primitive while keeping the historical contract ofPickleLoaderavailable for callers who explicitly opt out of restriction.Tests
5 new tests in
tests/io/test_default_adapters.py:__reduce__payload (sentinel side effect never fires)set_pickle_loader_allowlistapplies to freshPickleLoaderinstancesFull
tests/io/runs 36/36 green.