Summary
Keras versions prior to 3.14.0 contain a path traversal flaw in their tar and zip extraction helpers that validate member paths against the process working directory instead of the intended extraction target.
In environments where the working directory is set to /, attackers who can supply crafted archives may write files outside the extraction directory, potentially overwriting configurations, injecting code, or corrupting ML assets.
Affected context
This vulnerability appears in Keras deployments that rely on keras.src.utils.file_utils for extracting tar or zip archives.
It is most risky when:
- The process current working directory is
/(common in Docker images, CI/CD jobs, and Jupyter notebooks) - Archives are obtained from external or semi-trusted sources (model artifacts, datasets, or pre-packaged pipelines)
- Extraction paths are assumed to be confined to an application-controlled directory
Python 3.11 environments that lack the filter="data" safeguard are fully dependent on the flawed path validation logic.
Exploit preconditions
- Attack surface: Archive ingestion endpoints or workflows that accept user-supplied or remotely hosted tar/zip files (e.g., datasets, pre-trained models, pipeline bundles).
- Required access: Ability to influence or control the contents of tar/zip archives processed by Keras utilities.
- Trigger point: Invocation of
filter_safe_tarinfos()orfilter_safe_zipinfos()during extraction to a target directory. - Why exploitation is possible: Path checks compare archive member paths against the current working directory rather than the actual extraction root. When the CWD is
/, traversal sequences such as../are effectively evaluated against the filesystem root, allowing crafted entries to resolve outside the intended directory. Additionally, the zip filter bug can cause anAttributeErrorthat interrupts extraction in an inconsistent state.
Why this happens
The underlying issue is a logic error in how Keras validates archive member paths before extraction, corresponding to CWE-22: Improper Limitation of a Pathname to a Restricted Directory.
Instead of normalizing and enforcing that each member's resolved path stays within the configured extraction directory, the code compares paths against the process current working directory, which may not match the extraction root and can be / in many containerized or notebook environments.
This design assumes the CWD defines a safe boundary, which is not guaranteed and breaks the expected sandboxing behavior.
In Python 3.11 installations, absence of the filter="data" security parameter in the standard library means the application fully trusts this flawed validation logic.
Potential impact
- Attackers may write arbitrary files outside the intended extraction directory.
- Configuration files, environment files, or secrets stored on the filesystem could be overwritten.
- Malicious Python or shell scripts could be dropped into locations that are later executed by the application or other system components.
- Machine learning datasets and model artifacts may be silently corrupted, affecting training outcomes and model integrity.
- CI/CD pipelines and experiment tracking directories could be tampered with, undermining reproducibility and auditability.
General mitigation guidance
Effective mitigation focuses on tightening path handling and reducing trust in external archives:
- Ensure archive extraction routines resolve and normalize member paths against the actual intended extraction base directory, rejecting any entry whose final path escapes that directory.
- Treat all external tar/zip inputs as untrusted; restrict or gate their use through trusted channels and integrity checks (e.g., signatures, checksums).
- Run archive extraction under identities and sandboxes with minimal filesystem privileges, limiting the blast radius of any unexpected writes.
- Avoid setting the process current working directory to
/in container, CI/CD, or notebook environments when using file extraction utilities. - Where feasible, introduce additional validation steps that enforce explicit allowlists of expected paths or directories within archives.
How teams detect issues like this
Organizations can approach detection of this class of issue through multiple layers:
- Static analysis: Search for custom archive extraction code that:
- Normalizes member paths relative to the current working directory instead of a dedicated extraction root.
- Fails to check that
os.path.realpath()or equivalent remains under the target directory. - Code review: Flag patterns where tar/zip members are extracted without explicit bounding of their destination paths, especially in data ingestion, model loading, and pipeline utilities.
- Runtime monitoring: Log and alert on extraction processes writing into unexpected directories (e.g.,
/etc,/usr, or application configuration trees) when processing external archives. - Policy enforcement: Incorporate secure archive extraction guidelines into code review checklists and CI policies, and optionally employ automated tools that detect unsafe path handling around file operations.

