Skip to content

Commit

Permalink
vuln-fix: Zip Slip Vulnerability
Browse files Browse the repository at this point in the history
This fixes a Zip-Slip vulnerability.

This change does one of two things. This change either

1. Inserts a guard to protect against Zip Slip.
OR
2. Replaces `dir.getCanonicalPath().startsWith(parent.getCanonicalPath())`, which is vulnerable to partial path traversal attacks, with the more secure `dir.getCanonicalFile().toPath().startsWith(parent.getCanonicalFile().toPath())`.

For number 2, consider `"/usr/outnot".startsWith("/usr/out")`.
The check is bypassed although `/outnot` is not under the `/out` directory.
It's important to understand that the terminating slash may be removed when using various `String` representations of the `File` object.
For example, on Linux, `println(new File("/var"))` will print `/var`, but `println(new File("/var", "/")` will print `/var/`;
however, `println(new File("/var", "/").getCanonicalPath())` will print `/var`.

Weakness: CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
Severity: High
CVSSS: 7.4
Detection: CodeQL (https://codeql.github.com/codeql-query-help/java/java-zipslip/) & OpenRewrite (https://public.moderne.io/recipes/org.openrewrite.java.security.ZipSlip)

Reported-by: Jonathan Leitschuh <[email protected]>
Signed-off-by: Jonathan Leitschuh <[email protected]>

Bug-tracker: JLLeitschuh/security-research#16


Co-authored-by: Moderne <[email protected]>
  • Loading branch information
JLLeitschuh and TeamModerne committed Jul 29, 2022
1 parent b62590b commit 3c714ca
Show file tree
Hide file tree
Showing 2 changed files with 329 additions and 321 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,142 +17,146 @@
*/

package com.dscalzi.zipextractor.core.provider;

import com.dscalzi.zipextractor.core.TaskInterruptedException;
import com.dscalzi.zipextractor.core.ZTask;
import com.dscalzi.zipextractor.core.managers.MessageManager;
import com.dscalzi.zipextractor.core.util.ICommandSender;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.AccessDeniedException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.regex.Pattern;
import java.util.zip.ZipException;

public class JarProvider implements TypeProvider {

// Shared pattern by JarProviders
public static final Pattern PATH_END = Pattern.compile("\\.jar$");
protected static final List<String> SUPPORTED = new ArrayList<>(Collections.singletonList("jar"));

@Override
public List<String> scanForExtractionConflicts(ICommandSender sender, File src, File dest, boolean silent) {

List<String> existing = new ArrayList<>();
final MessageManager mm = MessageManager.inst();
if(!silent)
mm.scanningForConflics(sender);
try (FileInputStream fis = new FileInputStream(src); JarInputStream jis = new JarInputStream(fis);) {
JarEntry je = jis.getNextJarEntry();

while (je != null) {
if (Thread.interrupted())
throw new TaskInterruptedException();

File newFile = new File(dest + File.separator + je.getName());
if (newFile.exists()) {
existing.add(je.getName());
}
je = jis.getNextJarEntry();
}

jis.closeEntry();
} catch (TaskInterruptedException e) {
mm.taskInterruption(sender, ZTask.EXTRACT);
} catch (IOException e) {
e.printStackTrace();
}

return existing;
}

@Override
public boolean canDetectPipedConflicts() {
return false;
}

@Override
public boolean extract(ICommandSender sender, File src, File dest, boolean log, boolean pipe) {
final MessageManager mm = MessageManager.inst();
byte[] buffer = new byte[1024];
mm.startingProcess(sender, ZTask.EXTRACT, src.getName());
try (FileInputStream fis = new FileInputStream(src); JarInputStream jis = new JarInputStream(fis);) {
JarEntry je = jis.getNextJarEntry();

while(je != null) {
if (Thread.interrupted())
throw new TaskInterruptedException();

File newFile = new File(dest + File.separator + je.getName());
if (log)
mm.info("Extracting : " + newFile.getAbsoluteFile());
File parent = newFile.getParentFile();
if (!parent.exists() && !parent.mkdirs()) {
throw new IllegalStateException("Couldn't create dir: " + parent);
}
if (je.isDirectory()) {
newFile.mkdir();
je = jis.getNextJarEntry();
continue;
}
try (FileOutputStream fos = new FileOutputStream(newFile)) {
int len;
while ((len = jis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
}
je = jis.getNextJarEntry();
}
jis.closeEntry();
if(!pipe)
mm.extractionComplete(sender, dest);
return true;
} catch (AccessDeniedException e) {
mm.fileAccessDenied(sender, ZTask.EXTRACT, e.getMessage());
return false;
} catch (ZipException e) {
mm.extractionFormatError(sender, src, "Jar");
return false;
} catch (TaskInterruptedException e) {
mm.taskInterruption(sender, ZTask.EXTRACT);
return false;
} catch (IOException e) {
e.printStackTrace();
mm.genericOperationError(sender, src, ZTask.EXTRACT);
return false;
}
}

@Override
public boolean validForExtraction(File src) {
return PATH_END.matcher(src.getAbsolutePath()).find();
}

@Override
public boolean srcValidForCompression(File src) {
return false; // Compression to Jars is not supported.
}

@Override
public boolean destValidForCompression(File dest) {
return false; // Compression to Jars is not supported.
}

@Override
public List<String> supportedExtractionTypes() {
return SUPPORTED;
}

@Override
public List<String> canCompressTo() {
return new ArrayList<>();
}

}

import com.dscalzi.zipextractor.core.TaskInterruptedException;
import com.dscalzi.zipextractor.core.ZTask;
import com.dscalzi.zipextractor.core.managers.MessageManager;
import com.dscalzi.zipextractor.core.util.ICommandSender;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.AccessDeniedException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.regex.Pattern;
import java.util.zip.ZipException;

public class JarProvider implements TypeProvider {

// Shared pattern by JarProviders
public static final Pattern PATH_END = Pattern.compile("\\.jar$");
protected static final List<String> SUPPORTED = new ArrayList<>(Collections.singletonList("jar"));

@Override
public List<String> scanForExtractionConflicts(ICommandSender sender, File src, File dest, boolean silent) {

List<String> existing = new ArrayList<>();
final MessageManager mm = MessageManager.inst();
if(!silent)
mm.scanningForConflics(sender);
try (FileInputStream fis = new FileInputStream(src); JarInputStream jis = new JarInputStream(fis);) {
JarEntry je = jis.getNextJarEntry();

while (je != null) {
if (Thread.interrupted())
throw new TaskInterruptedException();

File newFile = new File(dest + File.separator + je.getName());
if (newFile.exists()) {
existing.add(je.getName());
}
je = jis.getNextJarEntry();
}

jis.closeEntry();
} catch (TaskInterruptedException e) {
mm.taskInterruption(sender, ZTask.EXTRACT);
} catch (IOException e) {
e.printStackTrace();
}

return existing;
}

@Override
public boolean canDetectPipedConflicts() {
return false;
}

@Override
public boolean extract(ICommandSender sender, File src, File dest, boolean log, boolean pipe) {
final MessageManager mm = MessageManager.inst();
byte[] buffer = new byte[1024];
mm.startingProcess(sender, ZTask.EXTRACT, src.getName());
try (FileInputStream fis = new FileInputStream(src); JarInputStream jis = new JarInputStream(fis);) {
JarEntry je = jis.getNextJarEntry();

while(je != null) {
if (Thread.interrupted())
throw new TaskInterruptedException();

File newFile = new File(dest, je.getName());

if (!newFile.toPath().normalize().startsWith(dest.toPath())) {
throw new RuntimeException("Bad zip entry");
}
if (log)
mm.info("Extracting : " + newFile.getAbsoluteFile());
File parent = newFile.getParentFile();
if (!parent.exists() && !parent.mkdirs()) {
throw new IllegalStateException("Couldn't create dir: " + parent);
}
if (je.isDirectory()) {
newFile.mkdir();
je = jis.getNextJarEntry();
continue;
}
try (FileOutputStream fos = new FileOutputStream(newFile)) {
int len;
while ((len = jis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
}
je = jis.getNextJarEntry();
}
jis.closeEntry();
if(!pipe)
mm.extractionComplete(sender, dest);
return true;
} catch (AccessDeniedException e) {
mm.fileAccessDenied(sender, ZTask.EXTRACT, e.getMessage());
return false;
} catch (ZipException e) {
mm.extractionFormatError(sender, src, "Jar");
return false;
} catch (TaskInterruptedException e) {
mm.taskInterruption(sender, ZTask.EXTRACT);
return false;
} catch (IOException e) {
e.printStackTrace();
mm.genericOperationError(sender, src, ZTask.EXTRACT);
return false;
}
}

@Override
public boolean validForExtraction(File src) {
return PATH_END.matcher(src.getAbsolutePath()).find();
}

@Override
public boolean srcValidForCompression(File src) {
return false; // Compression to Jars is not supported.
}

@Override
public boolean destValidForCompression(File dest) {
return false; // Compression to Jars is not supported.
}

@Override
public List<String> supportedExtractionTypes() {
return SUPPORTED;
}

@Override
public List<String> canCompressTo() {
return new ArrayList<>();
}

}
Loading

0 comments on commit 3c714ca

Please sign in to comment.