Skip to content

Fix Zip Slip vulnerability in file extraction utility #5422

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
18 changes: 15 additions & 3 deletions src/main/java/net/rptools/lib/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -370,19 +370,31 @@ public static void unzip(ZipInputStream in, File destDir) throws IOException {
public static void unzipFile(File sourceFile, File destDir) throws IOException {
if (!sourceFile.exists()) throw new IOException("source file does not exist: " + sourceFile);

// Canonicalize destination directory path
File canonicalDestDir = destDir.getCanonicalFile();

try (ZipFile zipFile = new ZipFile(sourceFile)) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();

while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (entry.isDirectory()) continue;

File file = new File(destDir, entry.getName());
String path = file.getAbsolutePath();
// Create file object and validate path
File file = new File(canonicalDestDir, entry.getName());
File canonicalFile = file.getCanonicalFile();

// Prevent zip slip - check if file path is within destination directory
if (!canonicalFile.toPath().startsWith(canonicalDestDir.toPath())) {
throw new IOException("Entry is outside of the target directory: " + entry.getName());
}

// Create parent directories
file.getParentFile().mkdirs();

// Extract file content
try (InputStream is = zipFile.getInputStream(entry);
OutputStream os = new BufferedOutputStream(new FileOutputStream(path))) {
OutputStream os = new BufferedOutputStream(new FileOutputStream(canonicalFile))) {
IOUtils.copy(is, os);
}
}
Expand Down
Loading