Deny import paths that allow code execution when given as values - #959
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #959 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 27 27
Lines 8671 8762 +91
=========================================
+ Hits 8671 8762 +91 ☔ View full report in Codecov by Harness. |
|
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.



What does this PR do?
Motivation
Resolving a
class_pathimports a module and instantiates a class with config-suppliedinit_args, so a config effectively decides what code runs. That's fine for trusted configs but a risk when configs come from an untrusted source. This adds an import-path denylist that limits what a value can reach.What's new
set_parsing_settings:import_path_denylistandimport_path_allowlist. Entries are dot paths; an entry covers everything beneath it (osalso deniesos.system), the most specific entry wins (sofunctools.partialcan be allowed out of a deniedfunctools), and*denies everything so only the allowlist is importable.os,subprocess,pickle,importlib,functools,shutil,builtins.open/io/sqlite3/logging.FileHandler,urllib/ftplib/socket).class_path,Callable,type[...],types.ModuleType, and type expressions given via config/CLI/env. Paths from code (annotations, defaults) are never checked.some.module.os.system→posix.system), and, for objects with no import path of their own, the callable they reach — the bound function of afunctools.partialand the defining class of a callable instance — so a denied callable can't be smuggled past by binding or wrapping it under an allowed name.import_path_denylist/import_path_allowlist(an empty list included) opts into the failing behavior now; from v5.0.0 it always fails.Also fixed (related type handling)
Callable[..., SomeClass]and instance-factory protocols now accept only subclasses of the return type / functions that return it, instead of any callable-instance class or any function.__call__now yield a factory instead of instantiating the class.Docs & tests
DOCUMENTATION.rst, CHANGELOG entries, and an explicit note that a denylist is a mitigation, not a sandbox — only*plus a narrow allowlist gives a real bound.test_import_paths.pycovering settings validation, policy resolution,*-mode, the definition-site re-check (including partials/instances), the default file/network entries, and end-to-end parsing; plus deprecation-transition tests.Before submitting