fix: detect unsafe module references in .keras nested config objects#340
Open
scruge1 wants to merge 1 commit intoprotectai:mainfrom
Open
fix: detect unsafe module references in .keras nested config objects#340scruge1 wants to merge 1 commit intoprotectai:mainfrom
scruge1 wants to merge 1 commit intoprotectai:mainfrom
Conversation
KerasLambdaDetectScan._get_keras_operator_names previously only checked top-level layers for class_name == "Lambda". This missed dangerous module references embedded in nested config objects (initializers, regularizers, constraints, dtype policies) which Keras resolves via importlib on load. Added _extract_unsafe_modules() which recursively traverses the entire config.json tree and flags any module/class_name pair where the module is not in the Keras/TensorFlow namespace. These are reported as "UnsafeModule" with CRITICAL severity. Added tests verifying: - Safe Keras configs return no issues - builtins.exec in kernel_initializer is detected - subprocess.Popen in top-level layer module is detected - tensorflow.* modules are treated as safe
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
_get_keras_operator_namesnow recursively traverses the entire config.json tree, not just top-level layersmodule/class_namepair where the module is outside the Keras/TensorFlow namespaceUnsafeModulewith CRITICAL severityWhy
The previous scanner only checked
class_name == "Lambda"in top-level layers. Keras config.json allows arbitrarymodulereferences in nested objects (initializers, regularizers, constraints, dtype policies) which Keras resolves viaimportlibon load. A malicious.kerasfile embeddingbuiltins.execas akernel_initializerpassed modelscan with 0 issues.Testing
builtins.exec, top-levelsubprocess.Popen, andtensorflow.*safe modulesbuiltins.exec