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 traversal bug on extraction of bad jar/zip files during jetty-start #8688

Merged
merged 1 commit into from
Oct 6, 2022
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
9 changes: 8 additions & 1 deletion jetty-start/src/main/java/org/eclipse/jetty/start/FS.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,14 @@ public static void extractZip(Path archive, Path destination) throws IOException
continue;
}

Path destFile = destination.resolve(entry.getName());
String entryName = entry.getName();

Check failure

Code scanning / CodeQL

Arbitrary file write during archive extraction ("Zip Slip")

Unsanitized archive entry, which may contain '..', is used in a [file system operation](1).
Path destFile = destination.resolve(entryName).normalize().toAbsolutePath();
// make sure extracted path does not escape the destination directory
if (!destFile.startsWith(destination))
{
throw new IOException(String.format("Malicious Archive %s found with bad entry \"%s\"",
archive, entryName));
}
if (!Files.exists(destFile))
{
FS.ensureDirectoryExists(destFile.getParent());
Expand Down
17 changes: 17 additions & 0 deletions jetty-start/src/test/java/org/eclipse/jetty/start/FSTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@
package org.eclipse.jetty.start;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

import org.eclipse.jetty.toolchain.test.MavenPaths;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class FSTest
Expand All @@ -45,6 +51,17 @@ public void testCanReadFile()
assertTrue(FS.canReadFile(pom.toPath()), "Can read file: " + pom);
}

@Test
public void testExtractEscaperZip(WorkDir workDir) throws IOException
{
Path archive = MavenPaths.findTestResourceFile("bad-libs/escaper.zip");
Path dest = workDir.getEmptyPathDir();
Path bad = Path.of("/tmp/evil.txt");
Files.deleteIfExists(bad);
assertThrows(IOException.class, () -> FS.extractZip(archive, dest));
assertFalse(Files.exists(bad), "The escaper prevention didn't work, you should not have a /tmp/evil.txt file, but you do.");
}

/**
* Utility method used by other test cases
*
Expand Down
Binary file added jetty-start/src/test/resources/bad-libs/escaper.zip
Binary file not shown.