Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SECURITY] Fix Zip Slip Vulnerability #218

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -605,4 +604,4 @@ private static boolean mkdirs(File parentDirectory, String path) {
File dir = new File(parentDirectory, path);
return dir.exists() || dir.mkdirs();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() +
Expand All @@ -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) {
Expand Down