Validate a deserialized class reference. This hook is called after a class reference has been deserialized (either by importing from a module or reconstructing a local class), but before it is used. It allows inspection or rejection of class references. When Called
(self, cls, *, is_local: bool, **kwargs)
| 195 | # ============================================================================ |
| 196 | |
| 197 | def validate_class(self, cls, *, is_local: bool, **kwargs): |
| 198 | """Validate a deserialized class reference. |
| 199 | |
| 200 | This hook is called after a class reference has been deserialized (either by |
| 201 | importing from a module or reconstructing a local class), but before it is used. |
| 202 | It allows inspection or rejection of class references. |
| 203 | |
| 204 | When Called |
| 205 | ----------- |
| 206 | - After importing global classes via importlib |
| 207 | - After reconstructing local classes from serialized code |
| 208 | - Before the class is stored or used in further deserialization |
| 209 | |
| 210 | Security Use Cases |
| 211 | ------------------ |
| 212 | - Block dangerous classes (subprocess.Popen, os.system, etc.) |
| 213 | - Validate that local classes match expected signatures |
| 214 | - Audit class imports for security logging |
| 215 | |
| 216 | Args: |
| 217 | cls (type): The deserialized class object. |
| 218 | is_local (bool): True if the class is a local class (defined in __main__ |
| 219 | or within a function/method scope), False if it's a global |
| 220 | class from an importable module. |
| 221 | **kwargs: Reserved for future extensions. |
| 222 | |
| 223 | Raises: |
| 224 | Exception: Raise any exception to reject the class and abort deserialization. |
| 225 | |
| 226 | Example: |
| 227 | >>> class ClassChecker(DeserializationPolicy): |
| 228 | ... def validate_class(self, cls, is_local, **kwargs): |
| 229 | ... # Block dangerous classes |
| 230 | ... if cls.__module__ == 'subprocess': |
| 231 | ... raise ValueError("subprocess classes not allowed") |
| 232 | |
| 233 | Note: |
| 234 | `check_class` is an alias for this hook. |
| 235 | """ |
| 236 | return None |
| 237 | |
| 238 | def validate_function(self, func, is_local: bool, **kwargs): |
| 239 | """Validate a deserialized function reference. |
no outgoing calls
no test coverage detected