Fixes Arbitrary file access during archive extraction djl_api #3737
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
djl/api/src/main/java/ai/djl/util/ZipUtils.java
Lines 55 to 58 in fecfedb
Extracting files from a malicious zip file, or similar type of archive, is at risk of directory traversal attacks if filenames from the archive are not properly validated. Zip archives contain archive entries representing each file in the archive. These entries include a file path for the entry, but these file paths are not restricted and may contain unexpected special elements such as the directory traversal element (..). If these file paths are used to create a filesystem path, then a file operation may happen in an unexpected location. This can result in sensitive information being revealed or deleted, or an attacker being able to influence behavior by modifying unexpected files.
Fix the zipslip vulnerability, we must ensure that the output path constructed from the archive entry name resolves to a location that is strictly within the destination directory. The best way is to resolve the entry name to a path, canonicalize (or normalize) it, and check that it starts with the canonical destination directory path. If not, we should throw an exception and abort extraction of that entry.
fix should be applied within the
unzip
method inapi/src/main/java/ai/djl/util/ZipUtils.java
, specifically after the entry's path is resolved, but before any filesystem operation (directory creation, file write) occurs. We should usePath.normalize()
and compare withPath.startsWith()
, as recommended. also ensure that thevalidateArchiveEntry
method (if its logic is not shown or insufficient) is replaced or augmented with the proper check inline.Required changes:
file
(the output path) to ensure thatfile.normalize().startsWith(dest.normalize())
(or better, canonical paths).IOException
with a suitable message.The vulnerability improper path validation during zip extraction. critical points in
ZipUtil.java
where file paths were constructed using zip entry names without checking for directory traversal sequences ('../'). The patched version added canonical path checks to prevent escaping the target directory. The Unpacker.process method handles entry extraction and was missing these checks in vulnerable versions, making it the primary vulnerable function.References
Zip Slip Vulnerability
Path Traversal
CWE-22