Skip to content
Open
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,12 +5,12 @@
import picocli.CommandLine;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Enumeration;
import java.util.Properties;
import java.util.jar.JarEntry;
Expand Down Expand Up @@ -67,30 +67,34 @@ public static File explodeWar(File warFile) throws IOException {
// Get current working directory path
Path currentPath = FileSystems.getDefault().getPath("").toAbsolutePath();
//Create Temporary directory
Path path = Files.createTempDirectory(currentPath.toAbsolutePath(), "jenkinsfile-runner");
File destDir = path.toFile();
Path destPath = Files.createTempDirectory(currentPath.toAbsolutePath(), "jenkinsfile-runner");
File destDir = destPath.toFile();
// Canonicalize once so every entry can be checked against the real destination root,
// guarding against a maliciously crafted WAR escaping it via "../" or absolute paths
// (a.k.a. "Zip Slip", CWE-22).
Path destDirReal = destPath.toRealPath();

while (enu.hasMoreElements()) {
JarEntry je = enu.nextElement();
File file = new File(destDir, je.getName());
if (!file.exists()) {
file.getParentFile().mkdirs();
file = new File(destDir, je.getName());

Path resolved = destDirReal.resolve(je.getName()).normalize();
if (!resolved.startsWith(destDirReal)) {
throw new IOException(
"Entry " + je.getName() + " in " + warFile
+ " would be extracted outside of the target directory. Aborting.");
}

if (je.isDirectory()) {
Files.createDirectories(resolved);
continue;
}
InputStream is = jarfile.getInputStream(je);

try (FileOutputStream fo = new FileOutputStream(file)) {
while (is.available() > 0) {
fo.write(is.read());
}
fo.close();
is.close();

Files.createDirectories(resolved.getParent());
try (InputStream is = jarfile.getInputStream(je)) {
Files.copy(is, resolved, StandardCopyOption.REPLACE_EXISTING);
}
}
return destDir;
}
}
}
}