Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary Gregory">Add and use ArchiveException.requireNonNull(T, String).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add and use CompressException.requireNonNull(Class, T, String).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add and use CpioArchiveEntry.setTimeMillis(long).</action>
<action type="add" issue="COMPRESS-732" dev="ggregory" due-to="Sami Sabir">Add a configurable maxEntries option to all archivers.</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 85 to 107 #707, #752.</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-lang3 from 3.18.0 to 3.22.0.</action>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
public abstract class AbstractArchiveBuilder<T, B extends AbstractArchiveBuilder<T, B>>
extends AbstractStreamBuilder<T, B> {

private int maxEntries = Integer.MAX_VALUE;

private int maxEntryNameLength = Short.MAX_VALUE;

/**
Expand All @@ -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.
*
Expand All @@ -51,6 +62,28 @@ public int getMaxEntryNameLength() {
return maxEntryNameLength;
}

/**
* Sets the maximum number of entries in an archive.
*
* <p>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.</p>
*
* <p>The default is {@link Integer#MAX_VALUE}.</p>
*
* @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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -97,6 +98,10 @@ public Iterator<E> unwrap() {

private final Charset charset;

private long entryCount;

private final int maxEntries;

private final int maxEntryNameLength;

/**
Expand All @@ -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();
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public CpioArchiveEntry getNextCPIOEntry() throws IOException {
skipRemainderOfLastBlock();
return null;
}
return entry;
return countEntry(entry);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Integer, SevenZArchiveEntry> fileMap = new LinkedHashMap<>();
BitSet isEmptyStream = null;
BitSet isEmptyFile = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ public TarArchiveEntry getNextEntry() throws IOException {
if (currEntry.isDirectory() && !currEntry.getName().endsWith("/")) {
currEntry.setName(currEntry.getName() + "/");
}
return currEntry;
return countEntry(currEntry);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ public ZipArchiveEntry getNextZipEntry() throws IOException {
current.inputStream = new Deflate64CompressorInputStream(in);
}
entriesRead++;
return current.entry;
return countEntry(current.entry);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<ZipArchiveEntry, NameAndComment> entriesWithoutUTF8Flag = populateFromCentralDirectory();
if (!builder.ignoreLocalFileHeader) {
Expand Down Expand Up @@ -1539,6 +1542,7 @@ private void readCentralDirectoryEntry(final Map<ZipArchiveEntry, NameAndComment
ze.setLocalHeaderOffset(ZipLong.getValue(cfhBuf, off) + firstLocalFileHeaderOffset);
// data offset will be filled later
entries.add(ze);
ArchiveUtils.checkEntryCount(entries.size(), maxEntries);

final byte[] cdExtraData = org.apache.commons.compress.utils.IOUtils.readRange(archive, extraLen);
if (cdExtraData.length < extraLen) {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/apache/commons/compress/utils/ArchiveUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ public class ArchiveUtils {

private static final int SOFT_MAX_ARRAY_LENGTH = Integer.MAX_VALUE - 8;

/**
* Checks that the number of entries read from an archive does not exceed the given maximum.
*
* @param count The number of entries read so far.
* @param maxEntries The maximum number of entries.
* @throws ArchiveException If {@code count} exceeds {@code maxEntries}.
* @since 1.29.0
*/
public static void checkEntryCount(final long count, final int maxEntries) throws ArchiveException {
if (count > 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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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<AbstractArchiveBuilder<? extends ArchiveFile<?>, ?>> 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<AbstractArchiveBuilder<? extends ArchiveInputStream<?>, ?>> 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<? extends ArchiveFile<?>, ?> 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<? extends ArchiveInputStream<?>, ?> 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));
}
}