Skip to content

Commit

Permalink
Validate zip file names before extracting (Zip Slip)
Browse files Browse the repository at this point in the history
  • Loading branch information
slachiewicz committed Sep 21, 2023
1 parent 860c7c5 commit c9408fa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ private String[] buildCompilerArguments( CompilerConfiguration config, String[]
{
dllDir.mkdir();
}
JarUtil.extract(dllDir, new File(element));
JarUtil.extract(dllDir.toPath(), new File(element));
for (String tmpfile : dllDir.list())
{
if ( tmpfile.endsWith(DLL_SUFFIX) )
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
package org.codehaus.plexus.compiler.csharp;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class JarUtil {
public static void extract( File destDir, File jarFile ) throws IOException
{
JarFile jar = new JarFile( jarFile );
Enumeration enumEntries = jar.entries();
while ( enumEntries.hasMoreElements() ) {
JarEntry file = ( JarEntry ) enumEntries.nextElement();
File f = new File( destDir + File.separator + file.getName() );
if ( file.isDirectory() )
{
f.mkdir();
continue;
}
try ( InputStream is = jar.getInputStream( file ); FileOutputStream fos = new FileOutputStream( f ) )
{
while ( is.available() > 0 )
{
fos.write( is.read() );
public static void extract(Path destDir, File jarFile) throws IOException {
Path toPath = destDir.normalize();
try (JarFile jar = new JarFile(jarFile)) {
Enumeration<JarEntry> enumEntries = jar.entries();
while (enumEntries.hasMoreElements()) {
JarEntry file = enumEntries.nextElement();
Path f = destDir.resolve(file.getName());
if (!f.startsWith(toPath)) {
throw new IOException("Bad zip entry");
}
if (file.isDirectory()) {
Files.createDirectories(f);
continue;
}
try (InputStream is = jar.getInputStream(file);
OutputStream fos = Files.newOutputStream(f)) {
while (is.available() > 0) {
fos.write(is.read());
}
}
}
}
Expand Down

0 comments on commit c9408fa

Please sign in to comment.