Summary
The Keras model loading mechanism for HDF5-backed .keras files can be abused to read arbitrary local files when external dataset references are resolved without proper restriction.
An attacker who supplies a crafted model file can cause the Keras process to disclose sensitive information from the filesystem on which the model is loaded.
Affected context
This vulnerability affects environments where:
- Keras versions 3.0.0 through 3.13.1 are used to load
.kerasmodel files that rely on HDF5 integration - Models may originate from untrusted or semi-trusted sources (e.g., user uploads, shared artifacts, external repositories)
- The loading process runs on systems that store sensitive data (e.g., configuration files, credentials, proprietary datasets)
The issue manifests when HDF5 external dataset references embedded in a model file are allowed to resolve to arbitrary file paths on the local filesystem.
Exploit preconditions
- Attack surface: Keras model loading APIs that accept
.kerasfiles using HDF5 under the hood. - Required access: Ability to supply or influence the model file that a target application loads (e.g., upload, file drop, repository contribution).
- Trigger point: Invocation of
load_modelor equivalent loading routine that processes HDF5 external dataset references. - Why exploitation is possible: The loader implicitly trusts and resolves HDF5 external dataset references without enforcing strict path whitelists, sandboxing, or origin checks, allowing crafted references to point to arbitrary local files.
Why this happens
The root cause is insufficient access control over filesystem resources referenced from within serialized model artifacts.
HDF5 supports external dataset references that can map dataset contents to paths on the underlying filesystem.
When Keras model loading resolves these references, it does so in a way that does not adequately restrict which paths may be accessed or how those paths relate to the intended model data directory.
This aligns with CWE-73: External Control of File Name or Path, where external input fully determines file paths used by the application, and with CWE-200: Exposure of Sensitive Information, as the resolved file contents may include confidential data.
Potential impact
- Reading of application configuration files that may contain API keys, database credentials, or tokens
- Disclosure of sensitive user data stored on the same system (e.g., datasets, logs, exports)
- Exposure of internal system information, such as environment variables, host configuration, or internal network details
- Facilitation of further compromise by leaking secrets that can be reused in other services or environments
General mitigation guidance
Mitigation focuses on treating model files as untrusted input and constraining filesystem access during model loading:
- Restrict model loading to trusted, verified sources and avoid loading arbitrary user-supplied
.kerasfiles in privileged environments. - Apply path normalization and whitelisting when resolving any external file references, limiting access to a specific, controlled directory tree.
- Run model loading in a sandboxed or containerized environment with minimal filesystem visibility and least-privilege permissions.
- Separate sensitive configuration and secrets from the filesystem context used by model loading processes to reduce the value of any exposed files.
- Incorporate defense-in-depth controls, such as mandatory access control (e.g., SELinux/AppArmor) or filesystem namespaces, to limit what model loading can access even if path handling is imperfect.
How teams detect issues like this
Detection typically involves both code-focused and operational controls:
- Static analysis & code review:
- Flag usages where model loading APIs consume files from user-controllable locations without validation.
- Look for logic that permits arbitrary HDF5 or
.kerasfiles to be processed in services exposed to untrusted clients. - Identify absence of path validation or confinement when external references in serialized artifacts are resolved.
- Runtime monitoring & logging:
- Log file access patterns during model loading and alert on attempts to read files outside expected model directories.
- Use host-based intrusion detection to watch for unexpected reads of sensitive paths by model-serving processes.
- Policy enforcement:
- Enforce internal policies that model-serving components must operate with restricted filesystem permissions and vetted artifact sources.
- Integrate automated checks in CI/CD to prevent deployment of services that load models directly from untrusted input.

