diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 381cda98868..68dbc97f7a8 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -240,6 +240,7 @@ The type attribute can be add,update,fix,remove. Add and use ArchiveException.requireNonNull(T, String). Add and use CompressException.requireNonNull(Class, T, String). Add and use CpioArchiveEntry.setTimeMillis(long). + Add a configurable maxEntries option to all archivers. Bump org.apache.commons:commons-parent from 85 to 107 #707, #752. Bump org.apache.commons:commons-lang3 from 3.18.0 to 3.22.0. diff --git a/src/main/java/org/apache/commons/compress/archivers/AbstractArchiveBuilder.java b/src/main/java/org/apache/commons/compress/archivers/AbstractArchiveBuilder.java index 75c6664275d..ece5a5afaa8 100644 --- a/src/main/java/org/apache/commons/compress/archivers/AbstractArchiveBuilder.java +++ b/src/main/java/org/apache/commons/compress/archivers/AbstractArchiveBuilder.java @@ -33,6 +33,8 @@ public abstract class AbstractArchiveBuilder> extends AbstractStreamBuilder { + private int maxEntries = Integer.MAX_VALUE; + private int maxEntryNameLength = Short.MAX_VALUE; /** @@ -42,6 +44,15 @@ protected AbstractArchiveBuilder() { // empty } + /** + * Gets the maximum number of entries in an archive. + * + * @return The maximum number of entries in an archive. + */ + public int getMaxEntries() { + return maxEntries; + } + /** * Gets the maximum length of an archive entry name. * @@ -51,6 +62,28 @@ public int getMaxEntryNameLength() { return maxEntryNameLength; } + /** + * Sets the maximum number of entries in an archive. + * + *

Readers that parse the whole archive up front, such as {@code ZipFile}, + * {@code TarFile} and {@code SevenZFile}, enforce this limit while opening the + * archive. Streaming readers enforce it in {@link ArchiveInputStream#getNextEntry()}. + * Either way, an {@link ArchiveException} is thrown once the limit is exceeded.

+ * + *

The default is {@link Integer#MAX_VALUE}.

+ * + * @param maxEntries The maximum number of entries; must be positive. + * @return {@code this} instance. + * @throws IllegalArgumentException If {@code maxEntries} is not positive. + */ + public B setMaxEntries(final int maxEntries) { + if (maxEntries <= 0) { + throw new IllegalArgumentException("maxEntries must be positive"); + } + this.maxEntries = maxEntries; + return asThis(); + } + /** * Sets the maximum length, in bytes, of an archive entry name. * diff --git a/src/main/java/org/apache/commons/compress/archivers/ArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/ArchiveInputStream.java index f59f21a955c..56df774c659 100644 --- a/src/main/java/org/apache/commons/compress/archivers/ArchiveInputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/ArchiveInputStream.java @@ -25,6 +25,7 @@ import java.util.Iterator; import java.util.Objects; +import org.apache.commons.compress.utils.ArchiveUtils; import org.apache.commons.io.Charsets; import org.apache.commons.io.function.IOConsumer; import org.apache.commons.io.function.IOIterator; @@ -97,6 +98,10 @@ public Iterator unwrap() { private final Charset charset; + private long entryCount; + + private final int maxEntries; + private final int maxEntryNameLength; /** @@ -120,6 +125,7 @@ public ArchiveInputStream() { protected ArchiveInputStream(final AbstractArchiveBuilder builder) throws IOException { super(builder.getInputStream()); this.charset = builder.getCharset(); + this.maxEntries = builder.getMaxEntries(); this.maxEntryNameLength = builder.getMaxEntryNameLength(); } @@ -135,6 +141,7 @@ protected ArchiveInputStream(final AbstractArchiveBuilder builder) throws protected ArchiveInputStream(final InputStream inputStream, final String charsetName) { super(inputStream == null ? new NullInputStream() : inputStream); this.charset = Charsets.toCharset(charsetName); + this.maxEntries = Integer.MAX_VALUE; this.maxEntryNameLength = Short.MAX_VALUE; } @@ -173,6 +180,21 @@ protected void count(final long read) { } } + /** + * Counts an entry about to be returned by {@link #getNextEntry()}. + * + * @param entry The entry, or {@code null} at the end of the archive. + * @return The given entry. + * @throws ArchiveException If the archive holds more entries than the configured maximum. + * @since 1.29.0 + */ + protected E countEntry(final E entry) throws ArchiveException { + if (entry != null) { + ArchiveUtils.checkEntryCount(++entryCount, maxEntries); + } + return entry; + } + /** * Performs the given action for each element of the stream until all elements have been processed or the action throws an exception. Actions are performed * in the order of iteration. Exceptions thrown by the action are relayed to the caller. diff --git a/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java index 76af6eb253d..f1c94440b79 100644 --- a/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java @@ -376,7 +376,7 @@ public ArArchiveEntry getNextEntry() throws IOException { currentEntry = new ArArchiveEntry(name, len, currentEntry.getUserId(), currentEntry.getGroupId(), currentEntry.getMode(), currentEntry.getLastModified()); - return currentEntry; + return countEntry(currentEntry); } /** diff --git a/src/main/java/org/apache/commons/compress/archivers/arj/ArjArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/arj/ArjArchiveInputStream.java index 3841344aab0..868421210f7 100644 --- a/src/main/java/org/apache/commons/compress/archivers/arj/ArjArchiveInputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/arj/ArjArchiveInputStream.java @@ -299,7 +299,7 @@ public ArjArchiveEntry getNextEntry() throws IOException { .get(); // @formatter:on } - return new ArjArchiveEntry(currentLocalFileHeader); + return countEntry(new ArjArchiveEntry(currentLocalFileHeader)); } currentInputStream = null; return null; diff --git a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java index ff35629af3e..0327e3671d3 100644 --- a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java @@ -337,7 +337,7 @@ public CpioArchiveEntry getNextCPIOEntry() throws IOException { skipRemainderOfLastBlock(); return null; } - return entry; + return countEntry(entry); } @Override diff --git a/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java index 903384edd63..cc85424ee75 100644 --- a/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java @@ -213,7 +213,7 @@ public DumpArchiveInputStream(final InputStream is, final String encoding) throw private DumpArchiveEntry checkEntry(final DumpArchiveEntry entry) throws ArchiveException, MemoryLimitException { ArchiveUtils.checkEntryNameLength(entry.getName().length(), getMaxEntryNameLength(), "DUMP"); - return entry; + return countEntry(entry); } /** diff --git a/src/main/java/org/apache/commons/compress/archivers/lha/LhaArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/lha/LhaArchiveInputStream.java index 7f63cb0c091..44cd7a614fb 100644 --- a/src/main/java/org/apache/commons/compress/archivers/lha/LhaArchiveInputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/lha/LhaArchiveInputStream.java @@ -350,7 +350,7 @@ public LhaArchiveEntry getNextEntry() throws IOException { this.currentCompressedStream = null; this.currentDecompressedStream = null; } - this.currentEntry = readHeader(); + this.currentEntry = countEntry(readHeader()); return this.currentEntry; } diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java index 13815063100..0226fdc3e99 100644 --- a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java +++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java @@ -623,12 +623,15 @@ private static int toNonNegativeInt(final String description, final long value) private final boolean tryToRecoverBrokenArchives; + private final int maxEntries; + private final int maxEntryNameLength; private SevenZFile(final Builder builder) throws IOException { this.channel = builder.getChannel(SeekableByteChannel.class); try { this.fileName = builder.getName(); + this.maxEntries = builder.getMaxEntries(); this.maxEntryNameLength = builder.getMaxEntryNameLength(); this.maxMemoryLimitKiB = builder.maxMemoryLimitKiB; this.useDefaultNameForUnnamedEntries = builder.useDefaultNameForUnnamedEntries; @@ -1444,6 +1447,7 @@ private ByteBuffer readEncodedHeader(final ByteBuffer header, final Archive arch private void readFilesInfo(final ByteBuffer header, final Archive archive) throws IOException { final int numFilesInt = readUint64ToIntExact(header, "numFiles"); + ArchiveUtils.checkEntryCount(numFilesInt, maxEntries); final Map fileMap = new LinkedHashMap<>(); BitSet isEmptyStream = null; BitSet isEmptyFile = null; diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java index 7607d5ccf4a..6650cef245c 100644 --- a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java @@ -554,7 +554,7 @@ public TarArchiveEntry getNextEntry() throws IOException { if (currEntry.isDirectory() && !currEntry.getName().endsWith("/")) { currEntry.setName(currEntry.getName() + "/"); } - return currEntry; + return countEntry(currEntry); } /** diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarFile.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarFile.java index 487eeda2a67..ccb2aaf769c 100644 --- a/src/main/java/org/apache/commons/compress/archivers/tar/TarFile.java +++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarFile.java @@ -195,6 +195,7 @@ private TarFile(final Builder builder) throws IOException { TarArchiveEntry entry; while ((entry = getNextTarEntry()) != null) { entries.add(entry); + ArchiveUtils.checkEntryCount(entries.size(), builder.getMaxEntries()); } } catch (final IOException e) { throw IOUtils.closeQuietlySuppress(archive, e); diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java index 59750f36a26..e1ca129b14a 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java @@ -948,7 +948,7 @@ public ZipArchiveEntry getNextZipEntry() throws IOException { current.inputStream = new Deflate64CompressorInputStream(in); } entriesRead++; - return current.entry; + return countEntry(current.entry); } /** diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java index 5b94db97ab1..ad03fa89919 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java @@ -732,6 +732,8 @@ private static boolean tryToLocateSignature(final SeekableByteChannel channel, f private long firstLocalFileHeaderOffset; + private final int maxEntries; + private final int maxEntryNameLength; private ZipFile(final Builder builder) throws IOException { @@ -749,6 +751,7 @@ private ZipFile(final Builder builder) throws IOException { this.zipEncoding = ZipEncodingHelper.getZipEncoding(encoding); this.useUnicodeExtraFields = builder.useUnicodeExtraFields; this.zstdInputStreamFactory = builder.zstdInputStreamFactory; + this.maxEntries = builder.getMaxEntries(); this.maxEntryNameLength = builder.getMaxEntryNameLength(); final Map entriesWithoutUTF8Flag = populateFromCentralDirectory(); if (!builder.ignoreLocalFileHeader) { @@ -1539,6 +1542,7 @@ private void readCentralDirectoryEntry(final Map maxEntries) { + throw new ArchiveException("Archive holds more than %,d entries. Consider increasing the `maxEntries` limit.", maxEntries); + } + } + /** * Checks that the given entry name length is valid. * diff --git a/src/test/java/org/apache/commons/compress/archivers/MaxEntriesTest.java b/src/test/java/org/apache/commons/compress/archivers/MaxEntriesTest.java new file mode 100644 index 00000000000..ff18f7c4dfe --- /dev/null +++ b/src/test/java/org/apache/commons/compress/archivers/MaxEntriesTest.java @@ -0,0 +1,117 @@ +/* + * 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 + * + * https://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.commons.compress.archivers; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; +import java.util.stream.Stream; + +import org.apache.commons.compress.AbstractTest; +import org.apache.commons.compress.archivers.ar.ArArchiveInputStream; +import org.apache.commons.compress.archivers.arj.ArjArchiveInputStream; +import org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream; +import org.apache.commons.compress.archivers.dump.DumpArchiveInputStream; +import org.apache.commons.compress.archivers.jar.JarArchiveInputStream; +import org.apache.commons.compress.archivers.lha.LhaArchiveInputStream; +import org.apache.commons.compress.archivers.sevenz.SevenZFile; +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; +import org.apache.commons.compress.archivers.tar.TarFile; +import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream; +import org.apache.commons.compress.archivers.zip.ZipFile; +import org.junit.jupiter.api.function.Executable; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; + +/** + * Tests {@link AbstractArchiveBuilder#setMaxEntries(int)}. + */ +class MaxEntriesTest extends AbstractTest { + + static Stream, ?>> testArchiveFiles() throws IOException { + return Stream.of( + SevenZFile.builder().setURI(getURI("bla.7z")), + TarFile.builder().setURI(getURI("bla.tar")), + ZipFile.builder().setURI(getURI("bla.zip"))); + } + + static Stream, ?>> testArchiveInputStreams() throws IOException { + return Stream.of( + ArArchiveInputStream.builder().setURI(getURI("bla.ar")), + ArjArchiveInputStream.builder().setURI(getURI("bla.arj")), + CpioArchiveInputStream.builder().setURI(getURI("bla.cpio")), + DumpArchiveInputStream.builder().setURI(getURI("bla.dump")), + JarArchiveInputStream.builder().setURI(getURI("bla.jar")), + LhaArchiveInputStream.builder().setURI(getURI("test-macos-l0.lha")), + TarArchiveInputStream.builder().setURI(getURI("bla.tar")), + ZipArchiveInputStream.builder().setURI(getURI("bla.zip"))); + } + + private static void assertLimitExceeded(final int maxEntries, final Executable executable) { + final ArchiveException exception = assertThrows(ArchiveException.class, executable); + assertTrue(exception.getMessage().contains(String.format("more than %,d entries", maxEntries)), exception.getMessage()); + } + + private static int countEntries(final ArchiveFile archiveFile) throws IOException { + try (ArchiveFile archive = archiveFile) { + return archive.entries().size(); + } + } + + private static int countEntries(final ArchiveInputStream archiveInputStream) throws IOException { + try (ArchiveInputStream archive = archiveInputStream) { + int count = 0; + while (archive.getNextEntry() != null) { + count++; + } + return count; + } + } + + @ParameterizedTest + @MethodSource + void testArchiveFiles(final AbstractArchiveBuilder, ?> builder) throws IOException { + final int entryCount = countEntries(builder.get()); + assertEquals(entryCount, countEntries(builder.setMaxEntries(entryCount).get())); + assertLimitExceeded(entryCount - 1, builder.setMaxEntries(entryCount - 1)::get); + } + + @ParameterizedTest + @MethodSource + void testArchiveInputStreams(final AbstractArchiveBuilder, ?> builder) throws IOException { + final int entryCount = countEntries(builder.get()); + assertEquals(entryCount, countEntries(builder.setMaxEntries(entryCount).get())); + try (ArchiveInputStream archive = builder.setMaxEntries(entryCount - 1).get()) { + for (int i = 1; i < entryCount; i++) { + assertNotNull(archive.getNextEntry()); + } + assertLimitExceeded(entryCount - 1, archive::getNextEntry); + } + } + + @ParameterizedTest + @ValueSource(ints = { Integer.MIN_VALUE, -1, 0 }) + void testRejectsNonPositiveLimit(final int maxEntries) { + assertThrows(IllegalArgumentException.class, () -> TarArchiveInputStream.builder().setMaxEntries(maxEntries)); + } +}