fix: mark malformed Keras H5 configs inconclusive#917
fix: mark malformed Keras H5 configs inconclusive#917mldangelo-oai wants to merge 2 commits intomainfrom
Conversation
WalkthroughKeras H5 scanner now treats malformed or unparseable Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Performance BenchmarksCompared
|
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@modelaudit/scanners/keras_h5_scanner.py`:
- Around line 523-537: The code only validates layer_config type when
layer_class is in self.suspicious_layer_types, which lets malformed wrappers
(e.g., TimeDistributed with config as a string) bypass checks; move or duplicate
the dict-type validation for layer_config so every layer runs the check before
any further inspection: for each layer obtained in the scanner, call
layer_config = layer.get("config", {}), validate isinstance(layer_config, dict)
and on failure call self._mark_inconclusive_scan_result(result,
"keras_h5_layer_config_invalid_type") and result.add_check(...) (same payload
currently used), then set layer_config = {} and continue; keep subsequent logic
that inspects wrapped/inner layers (and uses self.suspicious_layer_types)
unchanged but rely on the earlier universal validation to prevent silent skips
(reference symbols: layer_config, layer.get, self.suspicious_layer_types,
_mark_inconclusive_scan_result, result.add_check, self.current_file_path).
In `@tests/scanners/test_keras_h5_scanner.py`:
- Around line 294-309: The helper _assert_inconclusive_keras_h5_scan currently
only asserts the check name and status; update it to also assert the specific
"fail-closed" message appears where expected (e.g., in result.checks'
message/text field and in metadata["scan_outcome_reasons"] or
audit_result.file_metadata[str(model_path)]), so that the test verifies the
operational fail-closed message alongside success/exit-code/cache behavior;
locate checks via KerasH5Scanner().scan(...) and audit_result from
scan_model_directory_or_file(...) and add assertions that the check with name ==
expected_check_name has the expected fail-closed message and that the same
message is present in the metadata scan_outcome_reasons.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 9678ece1-c35d-47d1-a591-8a2b54199d64
📒 Files selected for processing (3)
CHANGELOG.mdmodelaudit/scanners/keras_h5_scanner.pytests/scanners/test_keras_h5_scanner.py
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@modelaudit/scanners/keras_h5_scanner.py`:
- Around line 734-749: The nested-container branch only runs when "config" in
layer, so layers with a container class (self._MODEL_CONTAINER_CLASSES) but
missing "config" (e.g. {"class_name":"Functional"}) silently pass; update the
logic in the block that handles layer_class in self._MODEL_CONTAINER_CLASSES to
explicitly handle the case where "config" is absent by calling
self._mark_inconclusive_scan_result(result,
"keras_h5_nested_model_layers_missing") and adding the same
result.add_check(...) entry (use the same name "Nested Model Layers Presence
Validation", passed=False, rule_code="S902", severity=IssueSeverity.INFO,
location=self.current_file_path and details={"layer_class": layer_class,
"expected_key":"config.layers"}) so malformed nested containers are marked
inconclusive rather than scanning clean.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 521d20e8-8aeb-4d40-9e8c-06da88e9e6fe
📒 Files selected for processing (2)
modelaudit/scanners/keras_h5_scanner.pytests/scanners/test_keras_h5_scanner.py
| if layer_class in self._MODEL_CONTAINER_CLASSES and "config" in layer: | ||
| nested_config = layer.get("config") | ||
| if isinstance(nested_config, dict) and "layers" in nested_config: | ||
| self._scan_model_config(layer, result) | ||
| elif isinstance(nested_config, dict): | ||
| self._mark_inconclusive_scan_result(result, "keras_h5_nested_model_layers_missing") | ||
| result.add_check( | ||
| name="Nested Model Layers Presence Validation", | ||
| passed=False, | ||
| message=f"Nested model config for {layer_class} is missing required layers list", | ||
| rule_code="S902", | ||
| severity=IssueSeverity.INFO, | ||
| location=self.current_file_path, | ||
| details={"layer_class": layer_class, "expected_key": "config.layers"}, | ||
| ) | ||
|
|
There was a problem hiding this comment.
Nested container layers without config can still scan clean.
At Line 734, nested container validation only runs when "config" in layer. A malformed nested layer like {"class_name": "Functional"} skips this block and does not get marked inconclusive, which reopens a false-clean path.
🔧 Suggested fix
- if layer_class in self._MODEL_CONTAINER_CLASSES and "config" in layer:
- nested_config = layer.get("config")
- if isinstance(nested_config, dict) and "layers" in nested_config:
- self._scan_model_config(layer, result)
- elif isinstance(nested_config, dict):
+ if layer_class in self._MODEL_CONTAINER_CLASSES:
+ nested_config = layer.get("config")
+ if isinstance(nested_config, dict) and "layers" in nested_config:
+ self._scan_model_config(layer, result)
+ else:
self._mark_inconclusive_scan_result(result, "keras_h5_nested_model_layers_missing")
result.add_check(
name="Nested Model Layers Presence Validation",
passed=False,
message=f"Nested model config for {layer_class} is missing required layers list",
rule_code="S902",
severity=IssueSeverity.INFO,
location=self.current_file_path,
details={"layer_class": layer_class, "expected_key": "config.layers"},
)Based on learnings: "When a scan intentionally fails closed, make behavior operationally explicit and test the message plus success, exit-code, and cache semantics".
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@modelaudit/scanners/keras_h5_scanner.py` around lines 734 - 749, The
nested-container branch only runs when "config" in layer, so layers with a
container class (self._MODEL_CONTAINER_CLASSES) but missing "config" (e.g.
{"class_name":"Functional"}) silently pass; update the logic in the block that
handles layer_class in self._MODEL_CONTAINER_CLASSES to explicitly handle the
case where "config" is absent by calling
self._mark_inconclusive_scan_result(result,
"keras_h5_nested_model_layers_missing") and adding the same
result.add_check(...) entry (use the same name "Nested Model Layers Presence
Validation", passed=False, rule_code="S902", severity=IssueSeverity.INFO,
location=self.current_file_path and details={"layer_class": layer_class,
"expected_key":"config.layers"}) so malformed nested containers are marked
inconclusive rather than scanning clean.
Summary
Validation
Summary by CodeRabbit
Bug Fixes
Tests