Skip to content

fix(keras): prevent spoofed built-in registered_name from hiding non-allowlisted modules#736

Merged
mldangelo-oai merged 5 commits intomainfrom
codex/fix-keras-zip-scanner-to-detect-spoofed-objects
Mar 25, 2026
Merged

fix(keras): prevent spoofed built-in registered_name from hiding non-allowlisted modules#736
mldangelo-oai merged 5 commits intomainfrom
codex/fix-keras-zip-scanner-to-detect-spoofed-objects

Conversation

@mldangelo
Copy link
Copy Markdown
Member

@mldangelo mldangelo commented Mar 22, 2026

Motivation

  • Harden Keras .keras ZIP custom-object detection because spoofed built-in registered_name values such as "Add" could hide a non-allowlisted module reference and bypass warnings.
  • Ensure a single allowlisted module reference never suppresses a separate non-allowlisted module or fn_module reference.

Description

  • Enumerate all serialized layer module references via _iter_layer_module_references() and track both allowlisted and non-allowlisted refs.
  • Treat built-in or allowlisted registered objects as suppressible only when their module refs are entirely allowlisted or absent.
  • Add regression coverage for both a direct spoofed built-in module and a mixed safe/unsafe module-reference case.
  • Rebase the branch onto current main and resolve the CHANGELOG.md conflict under the active [Unreleased] section.

Testing

  • uv run pytest tests/scanners/test_keras_zip_scanner.py -k "registered_builtin_layer_does_not_false_positive or builtin_registered_name_with_non_allowlisted_module_is_flagged or builtin_registered_name_with_mixed_module_refs_is_flagged or allowlisted_module_layer_does_not_false_positive or allowlisted_module_does_not_suppress_unknown_custom_layer"
  • uv run ruff format modelaudit/ tests/
  • uv run ruff check --fix modelaudit/ tests/
  • uv run mypy modelaudit/
  • uv run pytest -n auto -m "not slow and not integration" --maxfail=1

Codex Task

Summary by CodeRabbit

  • Changelog

    • Added entries noting ZIP scan security improvements and suppression of duplicate custom-object warnings.
  • Bug Fixes

    • ZIP scanning more accurately detects non-allowlisted modules concealed alongside built-in layer names and suppresses duplicate warnings for allowlisted registered objects when module metadata is missing.
  • Tests

    • Added regression tests verifying correct flagging and suppression behavior for registered layer names and module metadata.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 22, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

Aggregates module references from Keras layer objects/configs, updates flagging to detect non-allowlisted module references even when registered_name matches built-ins or module metadata is missing, adds regression tests, and inserts two changelog "Fixed" entries.

Changes

Cohort / File(s) Summary
Changelog
CHANGELOG.md
Inserted two "Fixed" entries: (1) .keras ZIP scans retain spoofed built-in registered_name values when they mask non-allowlisted custom modules; (2) suppress duplicate custom-object warnings for allowlisted registered objects when module metadata is missing.
Scanner logic
modelaudit/scanners/keras_zip_scanner.py
Added _iter_layer_module_references to collect module references; introduced _layer_uses_non_allowlisted_module; tightened _is_known_safe_serialized_layer and revised _should_flag_registered_object to strip registered_name and flag layers when non-allowlisted refs exist even if registered_name appears built-in.
Tests
tests/scanners/test_keras_zip_scanner.py
Added regression tests covering: built-in registered_name with non-allowlisted module (ensure detection and preserved registered_name), mixed allowlisted+unsafe module refs in config, and allowlisted registered objects with no module metadata not producing false positives.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐰 I nibbled through layers, sniffed each name,
Where labels gleamed, some modules hid in shame.
I pulled the threads and found the sly disguise,
Kept true names safe and caught the surprised. 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and clearly summarizes the main security fix: preventing spoofed built-in registered_name values from masking non-allowlisted module references in Keras ZIP scanner detection.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/fix-keras-zip-scanner-to-detect-spoofed-objects

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_zip_scanner.py`:
- Around line 145-153: The current branch lets an identifier suppress warnings
if any allowlisted ref is present even when a non-allowlisted ref exists; change
the logic so suppression only occurs when there are zero non-allowlisted refs.
Concretely, in the block around normalized_registered_name ==
layer_class.strip() update the return condition to require that
_layer_uses_allowlisted_module(layer) is True,
_is_known_safe_allowlisted_registered_object(layer_class) is True, and
_layer_uses_non_allowlisted_module(layer) is False before suppressing; keep the
existing behavior of _is_known_safe_serialized_layer and otherwise flag when any
non-allowlisted module is used.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 92d2037d-7cf9-42b9-a53e-306a21d428ec

📥 Commits

Reviewing files that changed from the base of the PR and between a1019a8 and 980d940.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • modelaudit/scanners/keras_zip_scanner.py
  • tests/scanners/test_keras_zip_scanner.py

Harden Keras ZIP custom object detection so built-in registered_name
values do not hide non-allowlisted modules.

Co-Authored-By: Codex <noreply@openai.com>
@mldangelo mldangelo force-pushed the codex/fix-keras-zip-scanner-to-detect-spoofed-objects branch from 980d940 to 666358a Compare March 22, 2026 16:26
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 6c2c048f-de52-46d9-b122-6b52d4164c24

📥 Commits

Reviewing files that changed from the base of the PR and between 980d940 and 666358a.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • modelaudit/scanners/keras_zip_scanner.py
  • tests/scanners/test_keras_zip_scanner.py

mldangelo-oai and others added 2 commits March 23, 2026 11:02
Avoid emitting a duplicate Custom Object Detection warning for allowlisted registered objects when serialized module metadata is absent, while still flagging any non-allowlisted module reference.

Co-authored-by: Codex <noreply@openai.com>
Extend the allowlisted registered-object exception to serialized layers with absent module refs so the no-module path does not raise custom-layer or custom-object false positives.

Co-authored-by: Codex <noreply@openai.com>
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_zip_scanner.py`:
- Around line 121-143: The current logic allows a layer like {"class_name":
"Add", "module": "keras.src.ops.numpy", "config": {"module": "evil.module"}} to
bypass detection because _is_known_safe_serialized_layer() never enforces that
all module refs are allowlisted; only the _should_flag_registered_object() path
uses the non-allowlist check. Update _is_known_safe_serialized_layer() (and any
place that trusts _SAFE_ALLOWLISTED_REGISTERED_OBJECTS) to call the existing
helper that detects non-allowlisted refs—use
_layer_uses_non_allowlisted_module(layer) (or a new thin wrapper) to reject
layers that have any non-allowlisted module references before treating
class_name or registered-name as safe so mixed/unsafe module refs cannot
suppress custom-layer detection.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 31335720-8369-44a7-bf28-02f5129d0e83

📥 Commits

Reviewing files that changed from the base of the PR and between 666358a and cdb440e.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • modelaudit/scanners/keras_zip_scanner.py
  • tests/scanners/test_keras_zip_scanner.py

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
modelaudit/scanners/keras_zip_scanner.py (1)

149-156: ⚠️ Potential issue | 🟠 Major

Built-in class_name values bypass module allowlist validation.

The early return at lines 151-152 trusts is_known_safe_keras_layer_class(layer_class) without verifying module references. A crafted layer like {"class_name": "Add", "module": "evil.module", "config": {}} (no registered_name) will:

  1. Return True here because "Add" is a known-safe layer class
  2. Skip Custom Layer Class Detection
  3. Skip Custom Object Detection (no registered_name)
  4. Skip CVE-2025-1550 WARNING (not Lambda, not fn_module, not dangerous)

The module allowlist check added at lines 154-156 only protects the _SAFE_ALLOWLISTED_REGISTERED_OBJECTS branch, not built-in class names. Test coverage exists for scenarios with registered_name present, but the exact case without it (built-in class + evil module + no registered_name) is untested.

🔒 Suggested fix to validate modules for built-in class names
     def _is_known_safe_serialized_layer(self, layer: dict[str, Any]) -> bool:
         layer_class = layer.get("class_name")
-        if is_known_safe_keras_layer_class(layer_class):
-            return True
+        if is_known_safe_keras_layer_class(layer_class) or self._is_known_safe_allowlisted_registered_object(
+            layer_class
+        ):
+            return not self._layer_uses_non_allowlisted_module(layer)

-        return self._is_known_safe_allowlisted_registered_object(
-            layer_class
-        ) and not self._layer_uses_non_allowlisted_module(layer)
+        return False
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@modelaudit/scanners/keras_zip_scanner.py` around lines 149 - 156, The current
_is_known_safe_serialized_layer returns True for any class_name that
is_known_safe_keras_layer_class reports safe without validating the layer's
module; change _is_known_safe_serialized_layer so both branches validate the
module allowlist: when is_known_safe_keras_layer_class(layer_class) is True,
also require that not self._layer_uses_non_allowlisted_module(layer) before
returning True, and keep the existing combined check for the registered-object
branch (_is_known_safe_allowlisted_registered_object and not
_layer_uses_non_allowlisted_module). This ensures built-in class names with a
non-allowlisted "module" (e.g., {"class_name":"Add","module":"evil.module"}) are
treated as unsafe.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@tests/scanners/test_keras_zip_scanner.py`:
- Around line 913-942: Add a new test function mirroring
test_allowlisted_registered_object_without_module_does_not_false_positive_custom_object
that constructs a Keras config where a built-in class_name ("Add") is present
with a non-allowlisted "module" and no "registered_name", call
KerasZipScanner().scan on the zip produced by create_configured_keras_zip, and
assert that the scan returns at least one warning/critical issue (check
result.issues for IssueSeverity.WARNING or IssueSeverity.CRITICAL) to ensure
builtin class_name + evil module is flagged; place the test alongside the
existing tests and name it something like
test_builtin_class_with_non_allowlisted_module_and_no_registered_name_is_flagged
so it covers the bypass scenario.

---

Outside diff comments:
In `@modelaudit/scanners/keras_zip_scanner.py`:
- Around line 149-156: The current _is_known_safe_serialized_layer returns True
for any class_name that is_known_safe_keras_layer_class reports safe without
validating the layer's module; change _is_known_safe_serialized_layer so both
branches validate the module allowlist: when
is_known_safe_keras_layer_class(layer_class) is True, also require that not
self._layer_uses_non_allowlisted_module(layer) before returning True, and keep
the existing combined check for the registered-object branch
(_is_known_safe_allowlisted_registered_object and not
_layer_uses_non_allowlisted_module). This ensures built-in class names with a
non-allowlisted "module" (e.g., {"class_name":"Add","module":"evil.module"}) are
treated as unsafe.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 1caf056f-4dda-403d-b9dc-46f5c65f578e

📥 Commits

Reviewing files that changed from the base of the PR and between cdb440e and 82abd75.

📒 Files selected for processing (2)
  • modelaudit/scanners/keras_zip_scanner.py
  • tests/scanners/test_keras_zip_scanner.py

Treat built-in Keras class names as unsafe when any serialized module reference points outside the allowlist, and add regression coverage for the no-registered_name bypass.
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_zip_scanner.py`:
- Around line 151-155: The check that treats registered allowlisted objects as
safe currently passes layer_class into
_is_known_safe_allowlisted_registered_object; change the call sites in the Keras
ZIP scanner (the branch that also checks is_known_safe_keras_layer_class) to
pass the layer's registered_name (not class_name) when available, then ensure
_is_known_safe_allowlisted_registered_object uses registered_name for matching;
keep the fallback to class_name only if registered_name is missing. Also update
the related branch that calls _layer_uses_non_allowlisted_module to use the same
registered_name logic and add a regression test that constructs a layer with
class_name != registered_name (e.g.,
{"class_name":"Wrapper","registered_name":"NotEqual","config":{}}) asserting it
is treated as allowlisted and reported safe.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 423e31c4-807b-40e6-ab30-1671d2866bef

📥 Commits

Reviewing files that changed from the base of the PR and between 82abd75 and 37d08a6.

📒 Files selected for processing (2)
  • modelaudit/scanners/keras_zip_scanner.py
  • tests/scanners/test_keras_zip_scanner.py

Comment on lines +151 to +155
if is_known_safe_keras_layer_class(layer_class) or self._is_known_safe_allowlisted_registered_object(
layer_class
)
):
return not self._layer_uses_non_allowlisted_module(layer)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Match allowlisted registered objects against registered_name, not only class_name.

Line 151 and Line 167 still feed layer_class into _is_known_safe_allowlisted_registered_object(). A payload like {"class_name": "Wrapper", "registered_name": "NotEqual", "config": {}} will still be reported as a custom layer/object even though the registered object is allowlisted and there are no unsafe module refs. Please key this safe-object path off registered_name here and add a regression where class_name != registered_name.

🔧 Suggested direction
     def _is_known_safe_serialized_layer(self, layer: dict[str, Any]) -> bool:
         layer_class = layer.get("class_name")
+        registered_name = layer.get("registered_name")
         if is_known_safe_keras_layer_class(layer_class) or self._is_known_safe_allowlisted_registered_object(
-            layer_class
+            registered_name
+        ) or self._is_known_safe_allowlisted_registered_object(
+            layer_class
         ):
             return not self._layer_uses_non_allowlisted_module(layer)

         return False
...
         layer_class = layer.get("class_name")
         if isinstance(layer_class, str) and normalized_registered_name == layer_class.strip():
-            if self._is_known_safe_serialized_layer(layer) or self._is_known_safe_allowlisted_registered_object(
-                layer_class
-            ):
+            if self._is_known_safe_serialized_layer(layer) or self._is_known_safe_allowlisted_registered_object(
+                normalized_registered_name
+            ):
                 return has_non_allowlisted_module
             return True

Based on learnings: Preserve or strengthen security detections; test both benign and malicious samples when adding scanner/feature changes.

Also applies to: 167-170

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@modelaudit/scanners/keras_zip_scanner.py` around lines 151 - 155, The check
that treats registered allowlisted objects as safe currently passes layer_class
into _is_known_safe_allowlisted_registered_object; change the call sites in the
Keras ZIP scanner (the branch that also checks is_known_safe_keras_layer_class)
to pass the layer's registered_name (not class_name) when available, then ensure
_is_known_safe_allowlisted_registered_object uses registered_name for matching;
keep the fallback to class_name only if registered_name is missing. Also update
the related branch that calls _layer_uses_non_allowlisted_module to use the same
registered_name logic and add a regression test that constructs a layer with
class_name != registered_name (e.g.,
{"class_name":"Wrapper","registered_name":"NotEqual","config":{}}) asserting it
is treated as allowlisted and reported safe.

Copy link
Copy Markdown
Contributor

@mldangelo-oai mldangelo-oai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed locally after updating with main: spoofed built-in class bypass fixed, targeted Keras/MAR tests passed, full non-slow suite passed.

@mldangelo-oai mldangelo-oai enabled auto-merge (squash) March 25, 2026 19:03
@mldangelo-oai mldangelo-oai merged commit 6d8350e into main Mar 25, 2026
26 checks passed
@mldangelo-oai mldangelo-oai deleted the codex/fix-keras-zip-scanner-to-detect-spoofed-objects branch March 25, 2026 19:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants