Skip to content

fix: detect unsafe module references in H5 nested config objects#359

Open
scruge1 wants to merge 1 commit into
protectai:mainfrom
scruge1:fix/h5-nested-config-scan
Open

fix: detect unsafe module references in H5 nested config objects#359
scruge1 wants to merge 1 commit into
protectai:mainfrom
scruge1:fix/h5-nested-config-scan

Conversation

@scruge1

@scruge1 scruge1 commented Jun 24, 2026

Copy link
Copy Markdown

Summary

H5LambdaDetectScan only inspected top-level layers for class_name == "Lambda". Any module/class_name reference nested elsewhere in the H5 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 — a false negative — even though Keras resolves that module via importlib when the model is loaded with tf.keras.models.load_model(..., safe_mode=False).

This recurses the full model_config tree and flags every module reference outside the keras/tensorflow/tf_keras namespace 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 nested builtins.exec kernel_initializer is now flagged as an unsafe operator (previously 0 issues).
  • test_h5_benign_keras_module_not_flagged — a standard keras.initializers.GlorotUniform reference is not flagged (no false positive).

Both pass locally (Python 3.10, h5py 3.14, tensorflow 2.21).

Checklist

  • Tests added and passing locally
  • No exploit code included
  • DCO sign-off

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant