Skip to content

Commit 672315b

Browse files
vuln-fix: Zip Slip Vulnerability (#1)
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]> Bug-tracker: JLLeitschuh/security-research#16 Co-authored-by: Moderne <[email protected]>
1 parent 03f81b5 commit 672315b

File tree

1 file changed

+8
-1
lines changed
  • hradmin-system/src/main/java/com/wensenma/modules/mnt/util

1 file changed

+8
-1
lines changed

hradmin-system/src/main/java/com/wensenma/modules/mnt/util/ZipUtils.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ public static void unZipIt(String zipFilePath, String outputFolder) {
4646
ZipEntry ze = zis.getNextEntry();
4747
while (ze != null) {
4848
String fileName = ze.getName();
49-
File newFile = new File(outputFolder + File.separator + fileName);
49+
File newFile = new File(outputFolder,fileName);
50+
if (!newFile.toPath().normalize().startsWith(outputFolder)) {
51+
throw new IOException("Bad zip entry");
52+
}
5053
System.out.println("file unzip : " + newFile.getAbsoluteFile());
5154
//大部分网络上的源码,这里没有判断子目录
5255
if (ze.isDirectory()) {
@@ -83,6 +86,10 @@ public static void unzip(File source, String out) throws IOException {
8386

8487
File file = new File(out, entry.getName());
8588

89+
if (!file.toPath().normalize().startsWith(out)) {
90+
throw new IOException("Bad zip entry");
91+
}
92+
8693
if (entry.isDirectory()) {
8794
if (!file.mkdirs()) {
8895
System.out.println("was not successful.");

0 commit comments

Comments
 (0)