|
| 1 | +import cc.openframeworks.OFZipUtil; |
| 2 | + |
| 3 | +import java.io.ByteArrayInputStream; |
| 4 | +import java.io.ByteArrayOutputStream; |
| 5 | +import java.io.File; |
| 6 | +import java.nio.charset.StandardCharsets; |
| 7 | +import java.nio.file.Files; |
| 8 | +import java.nio.file.Path; |
| 9 | +import java.util.zip.ZipEntry; |
| 10 | +import java.util.zip.ZipOutputStream; |
| 11 | + |
| 12 | +public class OFZipUtilTest |
| 13 | +{ |
| 14 | + private static byte[] zip(String name, String contents, boolean directory) throws Exception |
| 15 | + { |
| 16 | + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
| 17 | + try (ZipOutputStream zip = new ZipOutputStream(bytes)) |
| 18 | + { |
| 19 | + ZipEntry entry = new ZipEntry(name); |
| 20 | + zip.putNextEntry(entry); |
| 21 | + if (!directory) |
| 22 | + zip.write(contents.getBytes(StandardCharsets.UTF_8)); |
| 23 | + zip.closeEntry(); |
| 24 | + } |
| 25 | + return bytes.toByteArray(); |
| 26 | + } |
| 27 | + |
| 28 | + private static void extract(File output, String name, String contents) throws Exception |
| 29 | + { |
| 30 | + OFZipUtil.extract(new ByteArrayInputStream(zip(name, contents, false)), output); |
| 31 | + } |
| 32 | + |
| 33 | + private static void assertMissing(Path path, String message) |
| 34 | + { |
| 35 | + if (Files.exists(path)) |
| 36 | + throw new AssertionError(message + ": " + path); |
| 37 | + } |
| 38 | + |
| 39 | + public static void main(String[] args) throws Exception |
| 40 | + { |
| 41 | + Path parent = Files.createTempDirectory("ofziputil-test-"); |
| 42 | + File output = Files.createDirectory(parent.resolve("output")).toFile(); |
| 43 | + |
| 44 | + extract(output, "nested/good.txt", "good"); |
| 45 | + Path good = output.toPath().resolve("nested/good.txt"); |
| 46 | + if (!Files.isRegularFile(good) |
| 47 | + || !"good".equals(Files.readString(good, StandardCharsets.UTF_8))) |
| 48 | + throw new AssertionError("A safe nested entry was not extracted correctly"); |
| 49 | + |
| 50 | + Path escaped = parent.resolve("escaped.txt"); |
| 51 | + extract(output, "../escaped.txt", "bad"); |
| 52 | + assertMissing(escaped, "A parent traversal entry escaped the output directory"); |
| 53 | + |
| 54 | + extract(output, "nested/../../escaped.txt", "bad"); |
| 55 | + assertMissing(escaped, "A nested parent traversal entry escaped the output directory"); |
| 56 | + |
| 57 | + Path absolute = parent.resolve("absolute.txt"); |
| 58 | + extract(output, absolute.toString(), "bad"); |
| 59 | + assertMissing(absolute, "An absolute entry escaped the output directory"); |
| 60 | + |
| 61 | + Path escapedDirectory = parent.resolve("escaped-directory"); |
| 62 | + OFZipUtil.extract( |
| 63 | + new ByteArrayInputStream(zip("../escaped-directory/", "", true)), output); |
| 64 | + assertMissing(escapedDirectory, "A directory entry escaped the output directory"); |
| 65 | + |
| 66 | + Path outside = Files.createDirectory(parent.resolve("outside")); |
| 67 | + Files.createSymbolicLink(output.toPath().resolve("link"), outside); |
| 68 | + extract(output, "link/escaped.txt", "bad"); |
| 69 | + assertMissing(outside.resolve("escaped.txt"), |
| 70 | + "An entry escaped through a symbolic link in the output directory"); |
| 71 | + |
| 72 | + System.out.println("OFZipUtil security regression tests passed"); |
| 73 | + } |
| 74 | +} |
0 commit comments