Skip to content

Commit 8e0aa19

Browse files
authored
Add security checks to prevent directory traversal when decompressing (#538)
1 parent c1c9c98 commit 8e0aa19

File tree

2 files changed

+70
-4
lines changed

2 files changed

+70
-4
lines changed

pf4j/src/main/java/org/pf4j/util/Unzip.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515
*/
1616
package org.pf4j.util;
1717

18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
1821
import java.io.File;
1922
import java.io.FileInputStream;
20-
import java.io.FileNotFoundException;
2123
import java.io.FileOutputStream;
2224
import java.io.IOException;
2325
import java.util.zip.ZipEntry;
26+
import java.util.zip.ZipException;
2427
import java.util.zip.ZipInputStream;
2528

26-
import org.slf4j.Logger;
27-
import org.slf4j.LoggerFactory;
28-
2929
/**
3030
* This class extracts the content of the plugin zip into a directory.
3131
* It's a class for only the internal use.
@@ -75,11 +75,17 @@ public void extract() throws IOException {
7575
FileUtils.delete(destination.toPath());
7676
}
7777

78+
String destinationCanonicalPath = destination.getCanonicalPath();
7879
try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(source))) {
7980
ZipEntry zipEntry;
8081
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
8182
File file = new File(destination, zipEntry.getName());
8283

84+
String fileCanonicalPath = file.getCanonicalPath();
85+
if (!fileCanonicalPath.startsWith(destinationCanonicalPath)) {
86+
throw new ZipException("The file "+ zipEntry.getName() + " is trying to leave the target output directory of "+ destination);
87+
}
88+
8389
// create intermediary directories - sometimes zip don't add them
8490
File dir = new File(file.getParent());
8591

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (C) 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.pf4j.util;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import java.io.File;
21+
import java.io.FileOutputStream;
22+
import java.io.IOException;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
import java.util.zip.ZipEntry;
26+
import java.util.zip.ZipException;
27+
import java.util.zip.ZipOutputStream;
28+
29+
import static org.junit.jupiter.api.Assertions.assertThrows;
30+
import static org.junit.jupiter.api.Assertions.assertTrue;
31+
32+
public class UnzipTest {
33+
34+
@Test
35+
public void zipSlip() throws IOException {
36+
File zipFile = createMaliciousZipFile();
37+
Path destination = Files.createTempDirectory("zipSlip");
38+
39+
Unzip unzip = new Unzip();
40+
unzip.setSource(zipFile);
41+
unzip.setDestination(destination.toFile());
42+
43+
Exception exception = assertThrows(ZipException.class, unzip::extract);
44+
assertTrue(exception.getMessage().contains("is trying to leave the target output directory"));
45+
}
46+
47+
private File createMaliciousZipFile() throws IOException {
48+
File zipFile = File.createTempFile("malicious", ".zip");
49+
String maliciousFileName = "../malicious.sh";
50+
try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile))) {
51+
ZipEntry entry = new ZipEntry(maliciousFileName);
52+
zipOutputStream.putNextEntry(entry);
53+
zipOutputStream.write("Malicious content".getBytes());
54+
zipOutputStream.closeEntry();
55+
}
56+
57+
return zipFile;
58+
}
59+
60+
}

0 commit comments

Comments
 (0)