CVE-2022-4772: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal') (CWE-22)
value: A vulnerability was found in Widoco and classified as critical. Affected by this issue is the function unZipIt of the file src/main/java/widoco/WidocoUtils.java. The manipulation leads to path traversal. It is possible to launch the attack on the local host. The name of the patch is f2279b76827f32190adfa9bd5229b7d5a147fa92. It is recommended to apply a patch to fix this issue. VDB-216914 is the identifier assigned to this vulnerability.
CWE description: The product uses external input to construct a pathname that is intended to identify a file or directory that is located underneath a restricted parent directory, but the product does not properly neutralize special elements within the pathname that can cause the pathname to resolve to a location that is outside of the restricted directory.
CVSS v3 base score: 7.8
Published: 2022-12-27T23:15Z
Repository: https://github.com/dgarijo/Widoco
Language: Java
Files changed: src/main/java/widoco/WidocoUtils.java
Commit date: 2022-07-30 01:40:45 +0000
Commit message: vuln-fix: Zip Slip Vulnerability

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 <Jonathan.Leitschuh@gmail.com>
Signed-off-by: Jonathan Leitschuh <Jonathan.Leitschuh@gmail.com>

Bug-tracker: https://github.com/JLLeitschuh/security-research/issues/16

Co-authored-by: Moderne <team@moderne.io>
Diff: diff --git a/src/main/java/widoco/WidocoUtils.java b/src/main/java/widoco/WidocoUtils.java
index 1053376..0e6bf82 100644
--- a/src/main/java/widoco/WidocoUtils.java
+++ b/src/main/java/widoco/WidocoUtils.java
@@ -255,11 +255,14 @@ public class WidocoUtils {
 			ZipInputStream zis = new ZipInputStream(CreateResources.class.getResourceAsStream(resourceName));
 			ZipEntry ze = zis.getNextEntry();
 
 			while (ze != null) {
 				String fileName = ze.getName();
-				File newFile = new File(outputFolder + File.separator + fileName);
+				File newFile = new File(outputFolder, fileName);
+				if (!newFile.toPath().normalize().startsWith(outputFolder)) {
+					throw new RuntimeException("Bad zip entry");
+				}
 				// System.out.println("file unzip : "+ newFile.getAbsoluteFile());
 				if (ze.isDirectory()) {
 					String temp = newFile.getAbsolutePath();
 					new File(temp).mkdirs();
 				} else {

Vulnerable code: 				File newFile = new File(outputFolder + File.separator + fileName);
Fixed code: 				File newFile = new File(outputFolder, fileName);
				if (!newFile.toPath().normalize().startsWith(outputFolder)) {
					throw new RuntimeException("Bad zip entry");
				}