Skip to content

Commit

Permalink
[util] Fix arbitrary file access during archive extraction
Browse files Browse the repository at this point in the history
Closes #98
  • Loading branch information
carlosame committed May 10, 2024
1 parent 42a10a1 commit 451d77f
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
Expand Down Expand Up @@ -215,7 +216,7 @@ public int compareTo(Triple o) {
}

private static void collectJars(File dir, Map<String, Jar> jars, Map<String, ClassFile> classFiles)
throws IOException {
throws IOException, SecurityException {
File[] files = dir.listFiles();
if (files != null) {
for (File file : files) {
Expand All @@ -227,11 +228,13 @@ private static void collectJars(File dir, Map<String, Jar> jars, Map<String, Cla
j.jarFile = new JarFile(file);
jars.put(j.name, j);

Path dirPath = dir.toPath();
Enumeration<JarEntry> entries = j.jarFile.entries();
while (entries.hasMoreElements()) {
ZipEntry ze = entries.nextElement();
String name = ze.getName();
if (name.endsWith(".class")) {
sanitizeName(dir, dirPath, name);
ClassFile cf = new ClassFile();
cf.name = name;
cf.jar = j;
Expand All @@ -246,6 +249,13 @@ private static void collectJars(File dir, Map<String, Jar> jars, Map<String, Cla
}
}

private static void sanitizeName(File dir, Path dirPath, String name) throws SecurityException {
File file = new File(dir, name);
if (!file.toPath().normalize().startsWith(dirPath)) {
throw new SecurityException("Possibly malicious zip entry.");
}
}

/**
* Returns the dependencies of the given class.
*
Expand Down

0 comments on commit 451d77f

Please sign in to comment.