|
@@ -617,11 +617,16 @@ public class FileUtil {
|
|
throws IOException {
|
|
throws IOException {
|
|
try (ZipInputStream zip = new ZipInputStream(inputStream)) {
|
|
try (ZipInputStream zip = new ZipInputStream(inputStream)) {
|
|
int numOfFailedLastModifiedSet = 0;
|
|
int numOfFailedLastModifiedSet = 0;
|
|
|
|
+ String targetDirPath = toDir.getCanonicalPath() + File.separator;
|
|
for(ZipEntry entry = zip.getNextEntry();
|
|
for(ZipEntry entry = zip.getNextEntry();
|
|
entry != null;
|
|
entry != null;
|
|
entry = zip.getNextEntry()) {
|
|
entry = zip.getNextEntry()) {
|
|
if (!entry.isDirectory()) {
|
|
if (!entry.isDirectory()) {
|
|
File file = new File(toDir, entry.getName());
|
|
File file = new File(toDir, entry.getName());
|
|
|
|
+ if (!file.getCanonicalPath().startsWith(targetDirPath)) {
|
|
|
|
+ throw new IOException("expanding " + entry.getName()
|
|
|
|
+ + " would create file outside of " + toDir);
|
|
|
|
+ }
|
|
File parent = file.getParentFile();
|
|
File parent = file.getParentFile();
|
|
if (!parent.mkdirs() &&
|
|
if (!parent.mkdirs() &&
|
|
!parent.isDirectory()) {
|
|
!parent.isDirectory()) {
|
|
@@ -656,12 +661,17 @@ public class FileUtil {
|
|
|
|
|
|
try {
|
|
try {
|
|
entries = zipFile.entries();
|
|
entries = zipFile.entries();
|
|
|
|
+ String targetDirPath = unzipDir.getCanonicalPath() + File.separator;
|
|
while (entries.hasMoreElements()) {
|
|
while (entries.hasMoreElements()) {
|
|
ZipEntry entry = entries.nextElement();
|
|
ZipEntry entry = entries.nextElement();
|
|
if (!entry.isDirectory()) {
|
|
if (!entry.isDirectory()) {
|
|
InputStream in = zipFile.getInputStream(entry);
|
|
InputStream in = zipFile.getInputStream(entry);
|
|
try {
|
|
try {
|
|
File file = new File(unzipDir, entry.getName());
|
|
File file = new File(unzipDir, entry.getName());
|
|
|
|
+ if (!file.getCanonicalPath().startsWith(targetDirPath)) {
|
|
|
|
+ throw new IOException("expanding " + entry.getName()
|
|
|
|
+ + " would create file outside of " + unzipDir);
|
|
|
|
+ }
|
|
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 " +
|
|
@@ -944,6 +954,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()) {
|
|
@@ -966,7 +983,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 "
|