Fix PyTorch v1.3+ hook bypass by hooking pickle.Unpickler class#174
Merged
thomas-chauchefoin-tob merged 2 commits intomasterfrom Nov 27, 2025
Merged
Fix PyTorch v1.3+ hook bypass by hooking pickle.Unpickler class#174thomas-chauchefoin-tob merged 2 commits intomasterfrom
thomas-chauchefoin-tob merged 2 commits intomasterfrom
Conversation
This commit fixes an issue where fickling hooks did not intercept pickle operations in PyTorch v1.3+ model files. The root cause was that the hook system only replaced pickle.load() and pickle.loads() functions, but PyTorch v1.3+ uses pickle.Unpickler class directly when loading models from the new zipfile format. Changes: - Added FicklingSafetyUnpickler class that delegates to fickling.load() for security analysis - Updated run_hook() to also hook pickle.Unpickler and _pickle.Unpickler - Updated activate_safe_ml_environment() to hook Unpickler classes with FicklingMLUnpickler (using a dynamically created subclass) - Updated remove_hook() to restore original Unpickler classes - Stored original Unpickler references for proper restoration The fix ensures that direct uses of pickle.Unpickler() by PyTorch v1.3+ (and any other library) are intercepted and routed through fickling's safety analysis, providing complete hook coverage. All existing tests pass, confirming backward compatibility. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Contributor
Author
thomas-chauchefoin-tob
approved these changes
Nov 27, 2025
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.
Fix PyTorch v1.3+ Hook Bypass by Hooking pickle.Unpickler Class
Summary
This PR fixes a critical issue where fickling's hook system did not intercept pickle operations in PyTorch v1.3+ model files. The hooks worked correctly on legacy PyTorch files but failed on modern PyTorch files that use the new
zipfile serialization format.
Problem
The fickling hook system (
run_hook()andalways_check_safety()) only replacedpickle.load()andpickle.loads()functions but did NOT replace thepickle.Unpicklerclass. PyTorch v1.3+ usespickle.Unpickler()directlywhen loading models from zipfile format, completely bypassing the function hooks.
Reproduction