惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MyScale Blog
MyScale Blog
人人都是产品经理
人人都是产品经理
云风的 BLOG
云风的 BLOG
小众软件
小众软件
F
Fortinet All Blogs
爱范儿
爱范儿
WordPress大学
WordPress大学
N
Netflix TechBlog - Medium
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
C
Check Point Blog
博客园 - 聂微东
D
Docker
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
aimingoo的专栏
aimingoo的专栏
Vercel News
Vercel News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
A
About on SuperTechFans
博客园 - 【当耐特】
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog
宝玉的分享
宝玉的分享
Jina AI
Jina AI
H
Hackread – Cybersecurity News, Data Breaches, AI and More

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Keras Deserialization Safe Mode: Security Capabilities an...
Madhan Alaga · 2026-04-27 · via DEV Community

Overview

This article analyzes the security behavior of Keras safe mode during model deserialization, focusing on what it prevents and what it does not.


Introduction

In TensorFlow Keras, loading a model involves more than reading stored data.

It requires deserializing objects such as layers, optimizers, and loss functions from a configuration structure.

This process can execute Python code, which introduces potential security risks when loading untrusted models.

To reduce this risk, Keras provides a parameter:

from tensorflow.keras.utils import deserialize_keras_object

obj = deserialize_keras_object(config, safe_mode=True)

Enter fullscreen mode Exit fullscreen mode

The safe_mode parameter is designed to restrict unsafe behavior during deserialization.

However, its protection is limited to specific cases.


Keras Deserialization Overview

Keras represents objects using a configuration dictionary:

config = {
    "class_name": "Adam",
    "config": {"learning_rate": 0.001},
    "module": "keras.optimizers",
    "registered_name": None
}

Enter fullscreen mode Exit fullscreen mode

During deserialization:

obj = deserialize_keras_object(config, safe_mode=True)

Enter fullscreen mode Exit fullscreen mode

Keras performs:

  1. Class resolution
  2. Module import
  3. Object instantiation

Each of these steps can execute code depending on how the object is defined.


Security Capabilities of Safe Mode

1. Blocking Lambda Deserialization

The primary security feature of safe mode is the prevention of lambda function deserialization.

Example:

malicious = lambda x: __import__("os").system("echo hacked")

Enter fullscreen mode Exit fullscreen mode

Behavior:

deserialize_keras_object(config, safe_mode=True)   # Blocked
deserialize_keras_object(config, safe_mode=False)  # May execute

Enter fullscreen mode Exit fullscreen mode

Lambda functions are unsafe because:

  • They are anonymous and hard to inspect
  • They can execute arbitrary system commands

Safe mode blocks this attack vector.


2. Limiting Certain Dynamic Execution

By blocking lambda functions, safe mode reduces some dynamic execution paths that rely on inline function definitions.

However, this protection is limited.


Security Limitations of Safe Mode

1. Custom Objects

Custom objects are treated as trusted:

class MyLayer:
    def __init__(self):
        print("Executed")

obj = deserialize_keras_object(
    config,
    custom_objects={"MyLayer": MyLayer},
    safe_mode=True
)

Enter fullscreen mode Exit fullscreen mode

Result:

  • Code executes normally
  • No restriction from safe mode

2. Registered Objects

Objects registered using:

from keras.saving import register_keras_serializable

@register_keras_serializable(package="custom")
class MyLayer:
    pass

Enter fullscreen mode Exit fullscreen mode

These are trusted and executed without restriction.


3. Built-in Keras Classes

All built-in components are allowed:

config = {
    "class_name": "Dense",
    "config": {"units": 64},
    "module": "keras.layers"
}

Enter fullscreen mode Exit fullscreen mode

These are always executed normally.


4. Execution in from_config()

Keras reconstructs objects using methods like from_config():

class DangerousLayer:
    @classmethod
    def from_config(cls, config):
        import os
        os.system("echo executed")
        return cls()

Enter fullscreen mode Exit fullscreen mode

Safe mode does not restrict this execution.


5. Module Imports

Keras dynamically imports modules:

"module": "keras.optimizers"

Enter fullscreen mode Exit fullscreen mode

Safe mode does not restrict imports.


6. Custom Object Scope

from keras.saving import custom_object_scope

with custom_object_scope({"MyLayer": MyLayer}):
    obj = deserialize_keras_object(config, safe_mode=True)

Enter fullscreen mode Exit fullscreen mode

Everything inside is trusted.


7. Normal Python Code Execution

Safe mode blocks lambda functions but allows normal Python code:

class Malicious:
    def __init__(self):
        __import__("os").system("echo executed")

Enter fullscreen mode Exit fullscreen mode

This executes even with safe_mode=True.


8. Namespace Shadowing and Priority Inversion

Safe mode strictly blocks lambda execution, but it relies on the integrity of the Keras object registry.

A critical limitation exists due to priority inversion during deserialization.

When a registered_name is present in the configuration, Keras prioritizes resolving the object through the registry instead of using the standard built-in class.

Example:

config = {
    "class_name": "Dense",
    "registered_name": "ShadowLib>Dense",
    "config": {"units": 64}
}

Enter fullscreen mode Exit fullscreen mode

If a malicious class has been registered under the same identifier (e.g., ShadowLib>Dense), Keras will instantiate that class instead of the expected built-in layer.

This creates a namespace shadowing risk, where:

  • A trusted name (like Dense) is overridden
  • A malicious implementation is executed

Safe mode does not validate:

  • The origin of the registered object
  • Whether it shadows a built-in class

As a result, registry-based attacks remain possible even with safe_mode=True.


Summary

Behavior Safe Mode
Lambda execution Blocked
Custom objects Not blocked
Registered objects Not blocked
Built-in classes Not blocked
from_config execution Not blocked
Module imports Not blocked
Normal class code Not blocked
Namespace shadowing Not blocked

Conclusion

Keras safe mode provides protection against unsafe lambda deserialization.

However, it does not:

  • Prevent execution of custom or registered objects
  • Restrict logic inside class methods
  • Limit module imports
  • Protect against registry-based shadowing attacks

Therefore, safe mode is a partial safeguard, not a complete security solution.


Key Takeaway

Using:

safe_mode=True

Enter fullscreen mode Exit fullscreen mode

improves safety, but it does not guarantee secure model loading.