Fix FicklingContextManager incomplete cleanup and dead code (#241)#243
Open
mldangelo wants to merge 4 commits intotrailofbits:masterfrom
Open
Fix FicklingContextManager incomplete cleanup and dead code (#241)#243mldangelo wants to merge 4 commits intotrailofbits:masterfrom
mldangelo wants to merge 4 commits intotrailofbits:masterfrom
Conversation
…its#241) - Snapshot/restore pre-enter hook state on exit (via state stack), preserving any pre-existing hooks and supporting re-entrant usage - Remove dead wrapped_load lambda that was immediately overwritten - Add max_acceptable_severity parameter to run_hook() with duplicate- kwarg protection on all paths including Unpickler - Simplify context.py to a thin wrapper over hook.run_hook()
Cover hook lifecycle, exception safety, max_acceptable_severity enforcement, pre-existing hook preservation, duplicate-keyword safety on all paths, re-entrant/nested usage, and check_safety().
- Replace eval() with getattr-based _get_entry_point() helper - Move kwargs.pop to SafetyUnpicklerWithSeverity.__init__ to avoid per-load() dict copy
Author
|
@thomas-chauchefoin-tob can you please take a look at this PR too? Thanks! |
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.
FicklingContextManagerhad three issues noted in #241:__exit__only restoredpickle.load, leaving the other five entry points (_pickle.load,pickle.loads,_pickle.loads,pickle.Unpickler,_pickle.Unpickler) permanently hooked after the context exited.wrapped_loadin__enter__was dead code —hook.run_hook()immediately overwrote it, somax_acceptable_severitynever actually took effect.Entering a context while a pre-existing hook (from
run_hook()oractivate_safe_ml_environment()) was active would clobber that hook on exit.This PR fixes all three by:
__enter__and restoring it on__exit__(via a stack for re-entrant safety), so pre-existing hooks are preserved.max_acceptable_severityparameter torun_hook()inhook.py, which creates severity-aware wrappers when the threshold differs from the default. This includes theUnpicklerpath (via a subclass), following the same pattern asactivate_safe_ml_environment().context.pyto a thin wrapper that delegates entirely tohook.run_hook()/hook.restore_hooks().Also includes duplicate-keyword protection (
kwargs.pop) so callers that passmax_acceptable_severityexplicitly don't get aTypeErrorfrom the wrappers.Tests cover hook lifecycle, exception safety, severity enforcement, pre-existing hook preservation, duplicate-keyword handling, and re-entrant/nested usage.
Closes #241