diff --git a/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java b/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java index 34718100b6f..ae819d681fa 100644 --- a/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java +++ b/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java @@ -74,12 +74,19 @@ * final InputStream is = Files.newInputStream(input.toPath()); * ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.ZIP, is); * ZipArchiveEntry entry = (ZipArchiveEntry) in.getNextEntry(); - * OutputStream out = Files.newOutputStream(dir.toPath().resolve(entry.getName())); + * // ArchiveEntry.resolveIn(Path) normalizes the entry name and throws ArchiveException + * // if the result would escape the target directory (Zip Slip, CWE-22). + * OutputStream out = Files.newOutputStream(entry.resolveIn(dir.toPath())); * IOUtils.copy(in, out); * out.close(); * in.close(); * * + *
Always resolve entry names through {@link ArchiveEntry#resolveIn(java.nio.file.Path)} rather than + * {@link java.nio.file.Path#resolve(java.nio.file.Path)}: entry names are attacker-controlled in untrusted archives and may + * contain {@code ..} segments that escape the target directory (Zip Slip, CWE-22). See also + * {@link org.apache.commons.compress.archivers.examples.Expander} for a ready-made safe extraction API.
+ * * @Immutable provided that the deprecated method setEntryEncoding is not used. * @ThreadSafe even if the deprecated method setEntryEncoding is used */