Validate a deserialized module reference. This hook is called before a module is imported during deserialization. When Called ----------- - Before importing modules via importlib.import_module() - Before the module is stored or its contents are accessed
(self, module_name: str, *, is_local: bool, **kwargs)
| 314 | return None |
| 315 | |
| 316 | def validate_module(self, module_name: str, *, is_local: bool, **kwargs): |
| 317 | """Validate a deserialized module reference. |
| 318 | |
| 319 | This hook is called before a module is imported during deserialization. |
| 320 | |
| 321 | When Called |
| 322 | ----------- |
| 323 | - Before importing modules via importlib.import_module() |
| 324 | - Before the module is stored or its contents are accessed |
| 325 | |
| 326 | Security Use Cases |
| 327 | ------------------ |
| 328 | - Whitelist/blacklist modules by name or prefix |
| 329 | - Prevent imports of system modules (os, subprocess, sys, etc.) |
| 330 | - Audit module imports for security logging |
| 331 | |
| 332 | Args: |
| 333 | module_name (str): The name of the module to import (e.g., 'os.path'). |
| 334 | is_local (bool): True if the reference being resolved is local (defined |
| 335 | in __main__ or within a function/method scope), False |
| 336 | otherwise. |
| 337 | **kwargs: Reserved for future extensions. |
| 338 | |
| 339 | Raises: |
| 340 | Exception: Raise any exception to reject the module import. |
| 341 | |
| 342 | Example: |
| 343 | >>> class ModuleWhitelistChecker(DeserializationPolicy): |
| 344 | ... ALLOWED = {'builtins', 'datetime', 'decimal', 'collections'} |
| 345 | ... |
| 346 | ... def validate_module(self, module_name, is_local, **kwargs): |
| 347 | ... root = module_name.split('.')[0] |
| 348 | ... if root not in self.ALLOWED: |
| 349 | ... raise ValueError(f"Module {module_name} not whitelisted") |
| 350 | |
| 351 | Note: |
| 352 | `check_module` is an alias for this hook. |
| 353 | """ |
| 354 | return None |
| 355 | |
| 356 | # ============================================================================ |
| 357 | # Protocol Interception Hooks (Interceptors) |
no outgoing calls
no test coverage detected