Add optional allowlist to pickle serde deserializer#818
Open
elijahbenizzy wants to merge 2 commits into
Open
Conversation
Introduces a configurable allowlist of (module, qualname) pairs for the pickle-based deserializer registered by register_type_to_pickle(). When an allowlist is configured, deserialization uses a restricted unpickler that refuses to import classes outside the allowlist and raises pickle.UnpicklingError instead. When no allowlist is configured, behavior remains backward-compatible and a runtime warning is emitted once per call site to encourage adoption. The allowlist can be supplied (a) per-call at deserialize time, (b) per-registration via register_type_to_pickle(cls, allowlist=...), or (c) process-wide via set_pickle_serde_allowlist([...]).
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 the pickle deserializer used by Burr's serde system (
burr/integrations/serde/pickle.py), mirroring the shape of #794's pydantic allowlist work.Without an allowlist: backward-compatible —
deserialize_picklecontinues to callpickle.loads(...)and emits a one-timeSecurityWarningper registration site so users see the noise.With an allowlist: a
_RestrictedUnpickler(pickle.Unpickler)subclass overridesfind_class(module, name)to validate against the allowlist and raisespickle.UnpicklingErrorfor anything not on it.API
Three ways to set the allowlist, in resolution order (highest priority first):
deserialize_pickle(value, allowlist=[...])register_type_to_pickle(cls, allowlist=[...])set_pickle_serde_allowlist([...])SecurityWarningAllowlist entries are
(module, qualname)tuples — e.g.("myapp.models", "User")— rather than prefix strings. Pickle attacks routinely reach for specific symbols within otherwise-trusted modules (e.g.builtins.eval,os.system,subprocess.Popen), so per-class granularity matters more than for pydantic where the registered name is already a class.Trust model (now in the docstring)
Pickle deserialization is unsafe when the bytes come from a source the application doesn't fully control — including persistence backends that have a separate access model (SQLite files, Redis, S3, filesystems). An attacker with write access to the backend can plant a malicious
__reduce__payload that triggers code execution when state is loaded. The allowlist closes that primitive.Tests
8 tests in
tests/integrations/serde/test_pickle.py(was 1):_is_allowedtruth table for allowlist matching__reduce__payload (calling a sentinel function via__reduce__) is blocked when an allowlist is set; sentinel never runs(module, qualname)is on the allowlistset_pickle_serde_allowlistapplies to fresh registrationsregister_type_to_pickle) overrides module defaultSecurityWarningexactly once per registration siteWider
tests/integrations/serde/+tests/core/test_serde.pyruns 24/24 green.