Repository navigation
Add SafeDisk.with_types to allow chosen classes and module prefixes while keeping reads and writes restricted - #23
Merged
Conversation
Documentation build overview
7 files changed ·
|
…hile keeping reads and writes restricted.
avgvi
force-pushed
the
feat/safedisk-with-types
branch
from
October 18, 2026 19:14
56cf9b9 to
a12c6ab
Compare
avgvi
force-pushed
the
feat/separate-import-name
branch
from
October 18, 2026 19:14
b3f1248 to
6637c73
Compare
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.
Adds
SafeDisk.with_types(*types, modules=()), so caches that hold application objects (dataclasses, named tuples, enums, pydantic models) can use the restricted reader instead of falling back to the defaultDisk. Downstream libraries such as dspy currently maintain their own restricted unpickler for this.with_typesreturns aSafeDisksubclass whose allowlist adds the given classes, matched by identity, and every class defined in the named modules or below them. Calling it on the returned class adds to its allowlist.SafeUnpicklertakesnames,classesandmodules. A class is looked up attribute by attribute, every step must be a class, and the result must be one of the given classes or defined in a trusted module. A dotted name such asos.systemreached through a module that importsos, a class a trusted module only imports, a module, and dunder attributes are refused. Classes are looked up only in modules already insys.modules, so reading never imports a module.GLOBAL,INST,STACK_GLOBALwith the memo tracked) are checked against the same allowlist, so a value the reader would refuse raisesTypeErrornaming the class before anything is written. Pickles the scan cannot follow, such as copyreg extension codes, are checked with a trial restricted load.UNSAFE_MODULES): numpy restores a dtype's internal flags from the pickle without checking them, so a crafted array pickle can make an array read raw bytes as object pointers. The docs suggest storing arrays as bytes.Cache,FanoutCache,Deque,IndexorDjangoCacheusing one pickles as before; a class defined in a module on top of a derived class stays its own root.SafeDiskbehaves exactly as in 5.7.0.tests/test_safedisk_types.pywith model classes intests/safedisk_models/: round trips for protocols 0 to 5, refusals at write and read, a poisoned row, the dotted-name and module-import cases, the prefix boundary, pickling, every container, pydantic models, and the restricted-disk pattern dspy uses running on the fork. The CItestjob installs numpy and pydantic for them.Stacked on #22.
Verified: 452 tests pass on Python 3.12; blue, isort, flake8, mypy, doc8 and the Sphinx build with warnings as errors pass.