fix Arbitrary file access during archive extraction ("Zip Slip") FileUtils() #230
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.
astra-cli/src/main/java/com/dtsx/astra/cli/utils/FileUtils.java
Line 112 in bab2edb
fix the issue need to ensure that the extracted file paths are validated to prevent directory traversal attacks. Specifically:
Path.normalize()
orFile.getCanonicalFile()
.AstraCliUtils.ASTRA_HOME
) usingPath.startsWith()
or equivalent.The changes will be made in the
extractTarArchiveInAstraCliHome
method. Specifically:outputFile
path, validate that it starts with the intended base directory.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.if a zip file contains a file entry
..\astra-cli-file
, and the zip file is extracted to the directory c:\output, then naively combining the paths would result in an output file path ofc:\output\..\astra-cli-file
, which would cause the file to be written toc:\astra-file
.Recommendation
Ensure that output paths constructed from zip archive entries are validated to prevent writing files to unexpected locations. The recommended way of writing an output file from a zip archive entry is to verify that the normalized full path of the output file starts with a prefix that matches the destination directory. Path normalization can be done with either
java.io.File.getCanonicalFile()
orjava.nio.file.Path.normalize()
. Prefix checking can be done withString.startsWith(..)
, but it is better to usejava.nio.file.Path.startsWith(..)
, as the latter works on complete path segments.POC
a file path taken from a zip archive item entry is combined with a destination directory. The result is used as the destination file path without verifying that the result is within the destination directory. If provided with a zip file containing an archive path like
..\datastax-file
, then this file would be written outside the destination directory.To fix this vulnerability, we need to verify that the normalized
file
still hasdestinationDir
as its prefix, and throw an exception if this is not the case.References
Zip Slip Vulnerability
Path Traversal
CWE-22