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 0ec98d2 commit 1958ce6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/main/java/fr/lebonq/minecraft/Fabric.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,12 @@ public void getZipFabric(String pMcVersion,File pVersionPath,File pMcJar,AppCont
ZipEntry vNext = vEntries.nextElement();
AppController.LOGGER.log(Level.INFO,"{}",vNext.getName());

File vNextFolder = new File(pVersionPath.getAbsolutePath().substring(0, pVersionPath.getAbsolutePath().lastIndexOf("\\")) + "/" + vNext.getName().substring(0, vNext.getName().lastIndexOf("/")));
File vNextFolder = new File(pVersionPath.getAbsolutePath().substring(0, pVersionPath.getAbsolutePath().lastIndexOf("\\")), vNext.getName().substring(0, vNext.getName().lastIndexOf("/")));
vNextFolder.mkdirs();
File vNextFile = new File(pVersionPath.getAbsolutePath().substring(0, pVersionPath.getAbsolutePath().lastIndexOf("\\")) + "/" + vNext.getName()); //On cree un fichier temporaire
File vNextFile = new File(pVersionPath.getAbsolutePath().substring(0, pVersionPath.getAbsolutePath().lastIndexOf("\\")), vNext.getName());
if(!vNextFile.toPath().normalize().startsWith(pVersionPath.getAbsolutePath().substring(0, pVersionPath.getAbsolutePath().lastIndexOf("\\")))) {
throw new RuntimeException("Bad zip entry");
} //On cree un fichier temporaire

if(!(vEntries.hasMoreElements())){
if(vNextFile.exists()){
Expand Down Expand Up @@ -101,4 +104,4 @@ public File getClientJar() {
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ public void extractNative(File pBin) {
if(vEntry.isDirectory()){//Si c'est un directory on recupere tout dedans avant d'aller plus loins
}
else{
File vFileOut = new File(pBin + "/" +vEntry.getName());
File vFileOut = new File(pBin, vEntry.getName());
if(!vFileOut.toPath().normalize().startsWith(pBin.toPath())) {
throw new RuntimeException("Bad zip entry");
}
vFileOut.deleteOnExit();
if(vEntry.getName().lastIndexOf("/") != -1){//Si == -1 alors c'est un fichier sans dossier
File vFileFolder = new File(pBin + "/" + vEntry.getName().substring(0, vEntry.getName().lastIndexOf("/")));//to create folder without file name
File vFileFolder = new File(pBin, vEntry.getName().substring(0, vEntry.getName().lastIndexOf("/")));//to create folder without file name
vFileFolder.mkdirs();
vFileFolder.deleteOnExit();//On supprimer le fichier a la du process launcher
}
Expand Down

0 comments on commit 1958ce6

Please sign in to comment.