Additional check when unpacking archives. Contributed by Jason Lowe and Akira Ajisaka.
This commit is contained in:
parent
0f0d29a8d1
commit
cedc28d4ab
|
@ -620,16 +620,21 @@ public class FileUtil {
|
||||||
public static void unZip(File inFile, File unzipDir) throws IOException {
|
public static void unZip(File inFile, File unzipDir) throws IOException {
|
||||||
Enumeration<? extends ZipEntry> entries;
|
Enumeration<? extends ZipEntry> entries;
|
||||||
ZipFile zipFile = new ZipFile(inFile);
|
ZipFile zipFile = new ZipFile(inFile);
|
||||||
|
String targetDirPath = unzipDir.getCanonicalPath() + File.separator;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
entries = zipFile.entries();
|
entries = zipFile.entries();
|
||||||
while (entries.hasMoreElements()) {
|
while (entries.hasMoreElements()) {
|
||||||
ZipEntry entry = entries.nextElement();
|
ZipEntry entry = entries.nextElement();
|
||||||
if (!entry.isDirectory()) {
|
if (!entry.isDirectory()) {
|
||||||
|
File file = new File(unzipDir, entry.getName());
|
||||||
|
if (!file.getCanonicalPath().startsWith(targetDirPath)) {
|
||||||
|
throw new IOException("expanding " + entry.getName()
|
||||||
|
+ " would create file outside of " + unzipDir);
|
||||||
|
}
|
||||||
InputStream in = zipFile.getInputStream(entry);
|
InputStream in = zipFile.getInputStream(entry);
|
||||||
try {
|
try {
|
||||||
File file = new File(unzipDir, entry.getName());
|
if (!file.getParentFile().mkdirs()) {
|
||||||
if (!file.getParentFile().mkdirs()) {
|
|
||||||
if (!file.getParentFile().isDirectory()) {
|
if (!file.getParentFile().isDirectory()) {
|
||||||
throw new IOException("Mkdirs failed to create " +
|
throw new IOException("Mkdirs failed to create " +
|
||||||
file.getParentFile().toString());
|
file.getParentFile().toString());
|
||||||
|
@ -738,6 +743,13 @@ public class FileUtil {
|
||||||
|
|
||||||
private static void unpackEntries(TarArchiveInputStream tis,
|
private static void unpackEntries(TarArchiveInputStream tis,
|
||||||
TarArchiveEntry entry, File outputDir) throws IOException {
|
TarArchiveEntry entry, File outputDir) throws IOException {
|
||||||
|
String targetDirPath = outputDir.getCanonicalPath() + File.separator;
|
||||||
|
File outputFile = new File(outputDir, entry.getName());
|
||||||
|
if (!outputFile.getCanonicalPath().startsWith(targetDirPath)) {
|
||||||
|
throw new IOException("expanding " + entry.getName()
|
||||||
|
+ " would create entry outside of " + outputDir);
|
||||||
|
}
|
||||||
|
|
||||||
if (entry.isDirectory()) {
|
if (entry.isDirectory()) {
|
||||||
File subDir = new File(outputDir, entry.getName());
|
File subDir = new File(outputDir, entry.getName());
|
||||||
if (!subDir.mkdirs() && !subDir.isDirectory()) {
|
if (!subDir.mkdirs() && !subDir.isDirectory()) {
|
||||||
|
@ -752,7 +764,6 @@ public class FileUtil {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
File outputFile = new File(outputDir, entry.getName());
|
|
||||||
if (!outputFile.getParentFile().exists()) {
|
if (!outputFile.getParentFile().exists()) {
|
||||||
if (!outputFile.getParentFile().mkdirs()) {
|
if (!outputFile.getParentFile().mkdirs()) {
|
||||||
throw new IOException("Mkdirs failed to create tar internal dir "
|
throw new IOException("Mkdirs failed to create tar internal dir "
|
||||||
|
|
|
@ -38,6 +38,7 @@ import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -740,10 +741,8 @@ public class TestFileUtil {
|
||||||
|
|
||||||
@Test (timeout = 30000)
|
@Test (timeout = 30000)
|
||||||
public void testUnZip() throws IOException {
|
public void testUnZip() throws IOException {
|
||||||
// make sa simple zip
|
|
||||||
setupDirs();
|
setupDirs();
|
||||||
|
// make a simple zip
|
||||||
// make a simple tar:
|
|
||||||
final File simpleZip = new File(del, FILE);
|
final File simpleZip = new File(del, FILE);
|
||||||
OutputStream os = new FileOutputStream(simpleZip);
|
OutputStream os = new FileOutputStream(simpleZip);
|
||||||
ZipOutputStream tos = new ZipOutputStream(os);
|
ZipOutputStream tos = new ZipOutputStream(os);
|
||||||
|
@ -760,7 +759,7 @@ public class TestFileUtil {
|
||||||
tos.close();
|
tos.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
// successfully untar it into an existing dir:
|
// successfully unzip it into an existing dir:
|
||||||
FileUtil.unZip(simpleZip, tmp);
|
FileUtil.unZip(simpleZip, tmp);
|
||||||
// check result:
|
// check result:
|
||||||
assertTrue(new File(tmp, "foo").exists());
|
assertTrue(new File(tmp, "foo").exists());
|
||||||
|
@ -775,8 +774,36 @@ public class TestFileUtil {
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
// okay
|
// okay
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test (timeout = 30000)
|
||||||
|
public void testUnZip2() throws IOException {
|
||||||
|
setupDirs();
|
||||||
|
// make a simple zip
|
||||||
|
final File simpleZip = new File(del, FILE);
|
||||||
|
OutputStream os = new FileOutputStream(simpleZip);
|
||||||
|
try (ZipOutputStream tos = new ZipOutputStream(os)) {
|
||||||
|
// Add an entry that contains invalid filename
|
||||||
|
ZipEntry ze = new ZipEntry("../foo");
|
||||||
|
byte[] data = "some-content".getBytes(StandardCharsets.UTF_8);
|
||||||
|
ze.setSize(data.length);
|
||||||
|
tos.putNextEntry(ze);
|
||||||
|
tos.write(data);
|
||||||
|
tos.closeEntry();
|
||||||
|
tos.flush();
|
||||||
|
tos.finish();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unzip it into an existing dir
|
||||||
|
try {
|
||||||
|
FileUtil.unZip(simpleZip, tmp);
|
||||||
|
Assert.fail("unZip should throw IOException.");
|
||||||
|
} catch (IOException e) {
|
||||||
|
GenericTestUtils.assertExceptionContains(
|
||||||
|
"would create file outside of", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test (timeout = 30000)
|
@Test (timeout = 30000)
|
||||||
/*
|
/*
|
||||||
* Test method copy(FileSystem srcFS, Path src, File dst, boolean deleteSource, Configuration conf)
|
* Test method copy(FileSystem srcFS, Path src, File dst, boolean deleteSource, Configuration conf)
|
||||||
|
|
Loading…
Reference in New Issue