You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UnzipTest.java 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright (C) 2012-present the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.pf4j.util;
  17. import org.junit.jupiter.api.Test;
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import java.nio.file.Files;
  22. import java.nio.file.Path;
  23. import java.util.zip.ZipEntry;
  24. import java.util.zip.ZipException;
  25. import java.util.zip.ZipOutputStream;
  26. import static org.junit.jupiter.api.Assertions.assertThrows;
  27. import static org.junit.jupiter.api.Assertions.assertTrue;
  28. public class UnzipTest {
  29. @Test
  30. public void zipSlip() throws IOException {
  31. File zipFile = createMaliciousZipFile();
  32. Path destination = Files.createTempDirectory("zipSlip");
  33. Unzip unzip = new Unzip();
  34. unzip.setSource(zipFile);
  35. unzip.setDestination(destination.toFile());
  36. Exception exception = assertThrows(ZipException.class, unzip::extract);
  37. assertTrue(exception.getMessage().contains("is trying to leave the target output directory"));
  38. }
  39. private File createMaliciousZipFile() throws IOException {
  40. File zipFile = File.createTempFile("malicious", ".zip");
  41. String maliciousFileName = "../malicious.sh";
  42. try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile))) {
  43. ZipEntry entry = new ZipEntry(maliciousFileName);
  44. zipOutputStream.putNextEntry(entry);
  45. zipOutputStream.write("Malicious content".getBytes());
  46. zipOutputStream.closeEntry();
  47. }
  48. return zipFile;
  49. }
  50. }