Skip to content

Extract files only within the destination directory #1428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/java/org/myrobotlab/io/Zip.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,23 @@ static public void unzip(String zipFile, String newPath) throws ZipException, IO
new File(newPath).mkdir();
Enumeration<?> zipFileEntries = zip.entries();

File canonicalNewPath = new File(newPath).getCanonicalFile();

// Process each entry
while (zipFileEntries.hasMoreElements()) {
// grab a zip file entry
ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
String currentEntry = entry.getName();
File destFile = new File(newPath, currentEntry);

// Canonicalize the destination file path
File canonicalDestFile = destFile.getCanonicalFile();

// Check if the canonical destination path starts with the canonical newPath
if (!canonicalDestFile.getPath().startsWith(canonicalNewPath.getPath())) {
throw new IOException("Attempt to write outside of the target directory: " + currentEntry);
}

// destFile = new File(newPath, destFile.getName());
File destinationParent = destFile.getParentFile();

Expand Down