From 25722a9cb2b6b0db7eaea1977a90d6776f97289b Mon Sep 17 00:00:00 2001 From: Inf1mum Date: Mon, 16 Nov 2026 22:23:25 +0300 Subject: [PATCH] Fix unsafe Zip-Slip example in ArchiveStreamFactory Javadoc What: Replace the decompression example in ArchiveStreamFactory Javadoc that resolved archive entry names with dir.toPath().resolve(entry.getName()) with the safe ArchiveEntry.resolveIn(dir.toPath()) API, and add a short paragraph explaining why entry names must not be resolved directly. Why: The documented example taught a pattern that is vulnerable to Zip Slip path traversal (CWE-22): entry names in untrusted archives may contain '..' segments that escape the target directory. Fixes COMPRESS-725. https://issues.apache.org/jira/browse/COMPRESS-725 How: Use entry.resolveIn(dir.toPath()) in the example, which normalizes the resolved path and throws ArchiveException when it would escape the parent directory (available since 1.26.0). Document the pitfall and point readers at Expander for a ready-made safe extraction API. Signed-off-by: Inf1mum --- .../commons/compress/archivers/ArchiveStreamFactory.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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 */