fix: detect unsafe module references in H5 nested config objects#359
Open
scruge1 wants to merge 1 commit into
Open
fix: detect unsafe module references in H5 nested config objects#359scruge1 wants to merge 1 commit into
scruge1 wants to merge 1 commit into
Conversation
H5LambdaDetectScan only inspected top-level layers for class_name == "Lambda". Any module/class_name reference nested elsewhere in the model_config tree (initializers, regularizers, constraints, dtype policies, custom layers) was ignored, so a non-Keras module such as builtins.exec hidden in a kernel_initializer was reported as 0 issues even though Keras resolves it via importlib on load (load_model(..., safe_mode=False)). Recurse the full model_config tree and flag every module reference outside the keras/tensorflow/tf_keras namespace as an unsafe operator. Mirrors the equivalent .keras-format fix (protectai#340) for the H5 path. Adds tests/test_h5_nested_config.py: malicious nested builtins.exec is detected; a benign keras.initializers reference is not flagged. Signed-off-by: scruge1 <scruge1@proton.me>
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.
Summary
H5LambdaDetectScanonly inspected top-level layers forclass_name == "Lambda". Anymodule/class_namereference nested elsewhere in the H5model_configtree (initializers, regularizers, constraints, dtype policies, custom layers) was ignored, so a non-Keras module such asbuiltins.exechidden in akernel_initializerwas reported as 0 issues — a false negative — even though Keras resolves that module viaimportlibwhen the model is loaded withtf.keras.models.load_model(..., safe_mode=False).This recurses the full
model_configtree and flags every module reference outside thekeras/tensorflow/tf_kerasnamespace as an unsafe operator. It mirrors the equivalent.keras-format fix (#340) for the H5 path; the two scanners use independent operator-extraction methods, so the H5 path needed its own patch.Why
The file is scanned (not skipped) yet returns no issues for a payload that executes on load — a true false negative in the model-file scanner.
Testing
Adds
tests/test_h5_nested_config.py:test_h5_nested_unsafe_module_detected— a nestedbuiltins.execkernel_initializeris now flagged as an unsafe operator (previously 0 issues).test_h5_benign_keras_module_not_flagged— a standardkeras.initializers.GlorotUniformreference is not flagged (no false positive).Both pass locally (Python 3.10, h5py 3.14, tensorflow 2.21).
Checklist