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 #7

Merged
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 @@ -17,142 +17,146 @@
*/

package com.dscalzi.zipextractor.core.provider;

import com.dscalzi.zipextractor.core.TaskInterruptedException;
import com.dscalzi.zipextractor.core.ZTask;
import com.dscalzi.zipextractor.core.managers.MessageManager;
import com.dscalzi.zipextractor.core.util.ICommandSender;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.AccessDeniedException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.regex.Pattern;
import java.util.zip.ZipException;

public class JarProvider implements TypeProvider {

// Shared pattern by JarProviders
public static final Pattern PATH_END = Pattern.compile("\\.jar$");
protected static final List<String> SUPPORTED = new ArrayList<>(Collections.singletonList("jar"));

@Override
public List<String> scanForExtractionConflicts(ICommandSender sender, File src, File dest, boolean silent) {

List<String> existing = new ArrayList<>();
final MessageManager mm = MessageManager.inst();
if(!silent)
mm.scanningForConflics(sender);
try (FileInputStream fis = new FileInputStream(src); JarInputStream jis = new JarInputStream(fis);) {
JarEntry je = jis.getNextJarEntry();

while (je != null) {
if (Thread.interrupted())
throw new TaskInterruptedException();

File newFile = new File(dest + File.separator + je.getName());
if (newFile.exists()) {
existing.add(je.getName());
}
je = jis.getNextJarEntry();
}

jis.closeEntry();
} catch (TaskInterruptedException e) {
mm.taskInterruption(sender, ZTask.EXTRACT);
} catch (IOException e) {
e.printStackTrace();
}

return existing;
}

@Override
public boolean canDetectPipedConflicts() {
return false;
}

@Override
public boolean extract(ICommandSender sender, File src, File dest, boolean log, boolean pipe) {
final MessageManager mm = MessageManager.inst();
byte[] buffer = new byte[1024];
mm.startingProcess(sender, ZTask.EXTRACT, src.getName());
try (FileInputStream fis = new FileInputStream(src); JarInputStream jis = new JarInputStream(fis);) {
JarEntry je = jis.getNextJarEntry();

while(je != null) {
if (Thread.interrupted())
throw new TaskInterruptedException();

File newFile = new File(dest + File.separator + je.getName());
if (log)
mm.info("Extracting : " + newFile.getAbsoluteFile());
File parent = newFile.getParentFile();
if (!parent.exists() && !parent.mkdirs()) {
throw new IllegalStateException("Couldn't create dir: " + parent);
}
if (je.isDirectory()) {
newFile.mkdir();
je = jis.getNextJarEntry();
continue;
}
try (FileOutputStream fos = new FileOutputStream(newFile)) {
int len;
while ((len = jis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
}
je = jis.getNextJarEntry();
}
jis.closeEntry();
if(!pipe)
mm.extractionComplete(sender, dest);
return true;
} catch (AccessDeniedException e) {
mm.fileAccessDenied(sender, ZTask.EXTRACT, e.getMessage());
return false;
} catch (ZipException e) {
mm.extractionFormatError(sender, src, "Jar");
return false;
} catch (TaskInterruptedException e) {
mm.taskInterruption(sender, ZTask.EXTRACT);
return false;
} catch (IOException e) {
e.printStackTrace();
mm.genericOperationError(sender, src, ZTask.EXTRACT);
return false;
}
}

@Override
public boolean validForExtraction(File src) {
return PATH_END.matcher(src.getAbsolutePath()).find();
}

@Override
public boolean srcValidForCompression(File src) {
return false; // Compression to Jars is not supported.
}

@Override
public boolean destValidForCompression(File dest) {
return false; // Compression to Jars is not supported.
}

@Override
public List<String> supportedExtractionTypes() {
return SUPPORTED;
}

@Override
public List<String> canCompressTo() {
return new ArrayList<>();
}

}

import com.dscalzi.zipextractor.core.TaskInterruptedException;
import com.dscalzi.zipextractor.core.ZTask;
import com.dscalzi.zipextractor.core.managers.MessageManager;
import com.dscalzi.zipextractor.core.util.ICommandSender;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.AccessDeniedException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.regex.Pattern;
import java.util.zip.ZipException;

public class JarProvider implements TypeProvider {

// Shared pattern by JarProviders
public static final Pattern PATH_END = Pattern.compile("\\.jar$");
protected static final List<String> SUPPORTED = new ArrayList<>(Collections.singletonList("jar"));

@Override
public List<String> scanForExtractionConflicts(ICommandSender sender, File src, File dest, boolean silent) {

List<String> existing = new ArrayList<>();
final MessageManager mm = MessageManager.inst();
if(!silent)
mm.scanningForConflics(sender);
try (FileInputStream fis = new FileInputStream(src); JarInputStream jis = new JarInputStream(fis);) {
JarEntry je = jis.getNextJarEntry();

while (je != null) {
if (Thread.interrupted())
throw new TaskInterruptedException();

File newFile = new File(dest + File.separator + je.getName());
if (newFile.exists()) {
existing.add(je.getName());
}
je = jis.getNextJarEntry();
}

jis.closeEntry();
} catch (TaskInterruptedException e) {
mm.taskInterruption(sender, ZTask.EXTRACT);
} catch (IOException e) {
e.printStackTrace();
}

return existing;
}

@Override
public boolean canDetectPipedConflicts() {
return false;
}

@Override
public boolean extract(ICommandSender sender, File src, File dest, boolean log, boolean pipe) {
final MessageManager mm = MessageManager.inst();
byte[] buffer = new byte[1024];
mm.startingProcess(sender, ZTask.EXTRACT, src.getName());
try (FileInputStream fis = new FileInputStream(src); JarInputStream jis = new JarInputStream(fis);) {
JarEntry je = jis.getNextJarEntry();

while(je != null) {
if (Thread.interrupted())
throw new TaskInterruptedException();

File newFile = new File(dest, je.getName());

if (!newFile.toPath().normalize().startsWith(dest.toPath().normalize())) {
throw new RuntimeException("Bad zip entry");
}
if (log)
mm.info("Extracting : " + newFile.getAbsoluteFile());
File parent = newFile.getParentFile();
if (!parent.exists() && !parent.mkdirs()) {
throw new IllegalStateException("Couldn't create dir: " + parent);
}
if (je.isDirectory()) {
newFile.mkdir();
je = jis.getNextJarEntry();
continue;
}
try (FileOutputStream fos = new FileOutputStream(newFile)) {
int len;
while ((len = jis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
}
je = jis.getNextJarEntry();
}
jis.closeEntry();
if(!pipe)
mm.extractionComplete(sender, dest);
return true;
} catch (AccessDeniedException e) {
mm.fileAccessDenied(sender, ZTask.EXTRACT, e.getMessage());
return false;
} catch (ZipException e) {
mm.extractionFormatError(sender, src, "Jar");
return false;
} catch (TaskInterruptedException e) {
mm.taskInterruption(sender, ZTask.EXTRACT);
return false;
} catch (IOException e) {
e.printStackTrace();
mm.genericOperationError(sender, src, ZTask.EXTRACT);
return false;
}
}

@Override
public boolean validForExtraction(File src) {
return PATH_END.matcher(src.getAbsolutePath()).find();
}

@Override
public boolean srcValidForCompression(File src) {
return false; // Compression to Jars is not supported.
}

@Override
public boolean destValidForCompression(File dest) {
return false; // Compression to Jars is not supported.
}

@Override
public List<String> supportedExtractionTypes() {
return SUPPORTED;
}

@Override
public List<String> canCompressTo() {
return new ArrayList<>();
}

}
Loading