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

Fix zipslip for jar #448

Open
wants to merge 4 commits 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 @@ -5,6 +5,7 @@
import org.jetbrains.java.decompiler.util.InterpreterUtil;

import java.io.*;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.jar.Manifest;
Expand Down Expand Up @@ -82,11 +83,19 @@ public void saveClassFile(String path, String qualifiedName, String entryName, S

@Override
public void copyEntry(String source, String path, String archiveName, String entryName) {
String rootPath = this.root.toString();

try (ZipFile srcArchive = new ZipFile(new File(source))) {
ZipEntry entry = srcArchive.getEntry(entryName);
if (entry != null) {
try (InputStream in = srcArchive.getInputStream(entry)) {
InterpreterUtil.copyStream(in, new FileOutputStream(this.root.resolve(entryName).toFile()));
String cleanedPath = FileSystems.getDefault().getPath("/"+entryName).normalize().toString();
File joinedPath = java.nio.file.Paths.get(rootPath, cleanedPath).toFile();
if (!joinedPath.toString().startsWith(rootPath)) {
throw new IOException("unable to verify safe file path");
}

InterpreterUtil.copyStream(in, new FileOutputStream(joinedPath));
}
}
}
Expand Down
29 changes: 29 additions & 0 deletions test/org/jetbrains/java/decompiler/CommandLineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import static org.jetbrains.java.decompiler.DecompilerTestFixture.assertFilesEqual;
import static org.junit.jupiter.api.Assertions.assertFalse;

public class CommandLineTest {
private DecompilerTestFixture fixture;
Expand Down Expand Up @@ -48,6 +53,30 @@ public void testJarToDir() {
assertFilesEqual(fixture.getTestDataDir().resolve("bulk"), fixture.getTempDir().resolve("bulk_out"));
}

@Test
public void testMaliciousJarToDir() throws IOException {
String out = fixture.getTempDir().resolve("bulk_out").toAbsolutePath().toString();
String in = fixture.getTestDataDir().resolve("mal.jar").toAbsolutePath().toString();
// create malicious jar if doesn't exist already
if (!new File(in).exists())
{
String badFileName = new String("../../../../../../../../../../../tmp/test.txt");
try (ZipOutputStream maliciousOut = new ZipOutputStream(new FileOutputStream(fixture.getTestDataDir().resolve("mal.jar").toAbsolutePath().toString())))
{
ZipEntry entry = new ZipEntry(badFileName);
maliciousOut.putNextEntry(entry);
maliciousOut.write("womp womp".getBytes());
maliciousOut.closeEntry();
}
}

ConsoleDecompiler.main(new String[]{in, out});

TextBuffer.checkLeaks();
File f = new File("/tmp/test.txt");
assertFalse(f.exists());
}

@Test
public void testZipToJar() {
String out = fixture.getTempDir().resolve("bulk_out.jar").toAbsolutePath().toString();
Expand Down