diff --git a/src/main/java/org/apache/maven/buildcache/CacheUtils.java b/src/main/java/org/apache/maven/buildcache/CacheUtils.java index 109d7395..17d1f717 100644 --- a/src/main/java/org/apache/maven/buildcache/CacheUtils.java +++ b/src/main/java/org/apache/maven/buildcache/CacheUtils.java @@ -286,7 +286,7 @@ public static void unzip(Path zip, Path out, boolean preservePermissions, boolea ZipArchiveEntry entry = entries.nextElement(); Path file = out.resolve(entry.getName()); if (!file.normalize().startsWith(out.normalize())) { - throw new RuntimeException("Bad zip entry"); + throw new IOException("Bad zip entry"); } if (entry.isDirectory()) { Files.createDirectories(file); diff --git a/src/test/java/org/apache/maven/buildcache/CacheUtilsTest.java b/src/test/java/org/apache/maven/buildcache/CacheUtilsTest.java new file mode 100644 index 00000000..c238faa4 --- /dev/null +++ b/src/test/java/org/apache/maven/buildcache/CacheUtilsTest.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.buildcache; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class CacheUtilsTest { + + @Test + void rejectsZipEntryOutsideOutputDirectory(@TempDir Path tempDir) throws IOException { + Path zip = tempDir.resolve("bad.zip"); + Path output = tempDir.resolve("output"); + Path outsideFile = tempDir.resolve("outside.txt"); + + Files.createDirectories(output); + + try (ZipOutputStream zipOutput = new ZipOutputStream(Files.newOutputStream(zip))) { + ZipEntry entry = new ZipEntry("../outside.txt"); + zipOutput.putNextEntry(entry); + zipOutput.write("test".getBytes(StandardCharsets.UTF_8)); + zipOutput.closeEntry(); + } + + assertThrows(IOException.class, () -> CacheUtils.unzip(zip, output, false)); + + assertFalse(Files.exists(outsideFile)); + } +}