Skip to content

Commit 99e8fdb

Browse files
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 <[email protected]> Signed-off-by: Jonathan Leitschuh <[email protected]> Bug-tracker: JLLeitschuh/security-research#16 Co-authored-by: Moderne <[email protected]>
1 parent 5b1906e commit 99e8fdb

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/test/java/com/conveyal/datatools/manager/jobs/DeploymentGisExportJobTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ private File[] getFoldersFromZippedShapefile(File zipFile) throws IOException {
3333
while (e.hasMoreElements()) {
3434
ZipEntry entry = e.nextElement();
3535
File destinationPath = new File(destDir, entry.getName());
36+
if (!destinationPath.toPath().normalize().startsWith(destDir.toPath())) {
37+
throw new RuntimeException("Bad zip entry");
38+
}
3639
// Create parent directories
3740
destinationPath.getParentFile().mkdirs();
3841

@@ -120,4 +123,4 @@ public void canExportRoutes() throws IOException, SQLException {
120123
@Test
121124
public void canExportRoutesFromPatternStops() {}
122125

123-
}
126+
}

src/test/java/com/conveyal/datatools/manager/jobs/GisExportJobTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ public File[] getFilesFromZippedShapefile(File zipFile) throws IOException {
265265
ZipEntry zipEntry = zis.getNextEntry();
266266
while (zipEntry != null) {
267267
File newFile = new File(destDir, zipEntry.getName());
268+
if (!newFile.toPath().normalize().startsWith(destDir.toPath())) {
269+
throw new RuntimeException("Bad zip entry");
270+
}
268271
FileOutputStream fos = new FileOutputStream(newFile);
269272
int len;
270273
while ((len = zis.read(buffer)) > 0) {

0 commit comments

Comments
 (0)