diff --git a/components/hostobjects/org.jaggeryjs.hostobjects.file/src/main/java/org/jaggeryjs/hostobjects/file/FileHostObject.java b/components/hostobjects/org.jaggeryjs.hostobjects.file/src/main/java/org/jaggeryjs/hostobjects/file/FileHostObject.java index e32eba43..5603ef15 100644 --- a/components/hostobjects/org.jaggeryjs.hostobjects.file/src/main/java/org/jaggeryjs/hostobjects/file/FileHostObject.java +++ b/components/hostobjects/org.jaggeryjs.hostobjects.file/src/main/java/org/jaggeryjs/hostobjects/file/FileHostObject.java @@ -411,8 +411,7 @@ public static boolean jsFunction_unZip(Context cx, Scriptable thisObj, Object[] while ((entry = zin.getNextEntry()) != null) { name = entry.getName(); String canonicalDirPath = outdir.getCanonicalPath(); - String canonicalEntryPath = new File(canonicalDirPath + entry.getName()).getCanonicalPath(); - if(!canonicalEntryPath.startsWith(canonicalDirPath)){ + if(!new File(canonicalDirPath + entry.getName()).getCanonicalFile().toPath().startsWith(canonicalDirPath)){ log.error("Invalid entry found in the Zip file: " + name); return false; } @@ -605,4 +604,4 @@ private static boolean mkdirs(File parentDirectory, String path) { File dir = new File(parentDirectory, path); return dir.exists() || dir.mkdirs(); } -} \ No newline at end of file +} diff --git a/components/jaggery-core/org.jaggeryjs.jaggery.app.mgt/src/main/java/org/jaggeryjs/jaggery/app/mgt/JaggeryDeploymentUtil.java b/components/jaggery-core/org.jaggeryjs.jaggery.app.mgt/src/main/java/org/jaggeryjs/jaggery/app/mgt/JaggeryDeploymentUtil.java index ef739a09..00120517 100644 --- a/components/jaggery-core/org.jaggeryjs.jaggery.app.mgt/src/main/java/org/jaggeryjs/jaggery/app/mgt/JaggeryDeploymentUtil.java +++ b/components/jaggery-core/org.jaggeryjs.jaggery.app.mgt/src/main/java/org/jaggeryjs/jaggery/app/mgt/JaggeryDeploymentUtil.java @@ -44,7 +44,7 @@ static void unZip(InputStream is, String destDir) { ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { if (entry.isDirectory()) { - File entryDir = new File(unzipDestinationDirectory.getAbsolutePath() + File.separator + entry.getName()); + File entryDir = new File(unzipDestinationDirectory.getAbsolutePath(), entry.getName()); boolean created = entryDir.mkdir(); if (!created) { log.error("Could not create DIR : " + unzipDestinationDirectory.getAbsolutePath() + @@ -56,7 +56,11 @@ static void unZip(InputStream is, String destDir) { // write the files to the disk FileOutputStream fos = null; try { - fos = new FileOutputStream(unzipDestinationDirectory.getAbsolutePath() + File.separator + entry.getName()); + final File zipEntryFile = new File(unzipDestinationDirectory.getAbsolutePath(), entry.getName()); + if (!zipEntryFile.toPath().normalize().startsWith(unzipDestinationDirectory.getAbsolutePath())) { + throw new IOException("Bad zip entry"); + } + fos = new FileOutputStream(zipEntryFile); dest = new BufferedOutputStream(fos, BYTE_BUFFER_SIZE); while ((count = zis.read(data, 0, BYTE_BUFFER_SIZE)) != -1) {