This is an automated email from the ASF dual-hosted git repository.
pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git
The following commit(s) were added to refs/heads/trunk by this push:
new a6e29606b7 Avoid large eager allocation from untrusted zip entry size
in ZipArchiveFakeEntry (#1227)
a6e29606b7 is described below
commit a6e29606b7e3a4e657cd7cd326645c83e970fedb
Author: PJ Fanning <[email protected]>
AuthorDate: Mon Aug 31 16:40:04 2026 +0100
Avoid large eager allocation from untrusted zip entry size in
ZipArchiveFakeEntry (#1227)
* Avoid large eager allocation from untrusted zip entry size in
ZipArchiveFakeEntry
The in-memory branch sized its read buffer from ZipArchiveEntry.getSize(),
which comes from the (untrusted) zip local file header. A tiny entry could
declare a huge uncompressed size and force a ~getMaxEntrySize() (100MB by
default) eager allocation before any data was read, without the
inflate-ratio
guard ever firing.
Read the actual bytes present via IOUtils.toByteArrayWithMaxLength, growing
the
buffer as needed and bounded by getMaxEntrySize() (a stream longer than that
fails with RecordFormatException). This matches the existing behaviour of
the
unknown-size (entrySize == -1) branch.
Behaviour change: an entry whose declared size exceeds its actual byte
count now
reads the actual bytes instead of throwing EOFException. Test updated
accordingly.
Co-Authored-By: Claude Fable 5 <[email protected]>
* Update poi-integration expected exceptions for the eager-allocation fix
ZipArchiveFakeEntry now reads entry data via toByteArrayWithMaxLength rather
than using the declared (untrusted) entry size, so the truncated-zip fuzz
file
diagram/clusterfuzz-...-POIVisioFuzzer-6312416206454784.vsdx now surfaces
"java.io.IOException: Truncated ZIP file" during the entry read instead of
being read far enough to fail later with an IllegalStateException. Update
the
expected exceptions for the XDGF and OPC handlers (handle + extract) to
match.
Co-Authored-By: Claude Fable 5 <[email protected]>
---------
Co-authored-by: Claude Fable 5 <[email protected]>
---
.../poi/openxml4j/util/ZipArchiveFakeEntry.java | 10 ++++--
.../openxml4j/util/TestZipArchiveFakeEntry.java | 38 ++++++++++++++++++----
test-data/poi-integration-exceptions.csv | 4 +--
3 files changed, 40 insertions(+), 12 deletions(-)
diff --git
a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/util/ZipArchiveFakeEntry.java
b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/util/ZipArchiveFakeEntry.java
index 5ea9394c44..68e37012f1 100644
---
a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/util/ZipArchiveFakeEntry.java
+++
b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/util/ZipArchiveFakeEntry.java
@@ -101,9 +101,13 @@ public final class ZipArchiveFakeEntry extends
ZipArchiveEntry implements Closea
throw new IOException("ZIP entry size is too large or
invalid");
}
- // Grab the de-compressed contents for later
- data = (entrySize == -1) ? IOUtils.toByteArrayWithMaxLength(inp,
getMaxEntrySize()) :
- IOUtils.toByteArray(inp, entrySize, getMaxEntrySize(),
"ZipArchiveFakeEntry.setMaxEntrySize()");
+ // Grab the de-compressed contents for later.
+ // Note: we deliberately do not use the declared entrySize to size
the read buffer.
+ // entrySize comes from the (untrusted) zip local file header, so
a tiny entry can
+ // claim a huge uncompressed size and force a large eager
allocation before any data
+ // is read. Instead read what is actually present, growing as
needed and bounded by
+ // getMaxEntrySize() (a stream longer than that fails with a
RecordFormatException).
+ data = IOUtils.toByteArrayWithMaxLength(inp, getMaxEntrySize());
numberOfBytes = data.length;
}
}
diff --git
a/poi-ooxml/src/test/java/org/apache/poi/openxml4j/util/TestZipArchiveFakeEntry.java
b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/util/TestZipArchiveFakeEntry.java
index 1a8e41a90e..cb1dcbcdf1 100644
---
a/poi-ooxml/src/test/java/org/apache/poi/openxml4j/util/TestZipArchiveFakeEntry.java
+++
b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/util/TestZipArchiveFakeEntry.java
@@ -20,10 +20,10 @@ package org.apache.poi.openxml4j.util;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
import org.apache.poi.util.IOUtils;
+import org.apache.poi.util.RecordFormatException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Isolated;
-import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
@@ -61,8 +61,12 @@ public class TestZipArchiveFakeEntry {
}
@Test
- void testInvalidEntrySize() throws IOException {
- final long fakeLen = 123;
+ void testEntrySizeLargerThanData() throws IOException {
+ // A malicious zip local file header can declare a huge uncompressed
size while the entry
+ // actually contains only a few bytes. We must not trust that declared
size to size the
+ // read buffer (that would allow a tiny input to trigger a large eager
allocation).
+ // Instead the actual bytes present are read, bounded by
getMaxEntrySize().
+ final long fakeLen = 99_000_000;
final String fakeValue = "fakeValue";
ZipArchiveEntry entry = new ZipArchiveEntry("hack123") {
@Override
@@ -73,10 +77,30 @@ public class TestZipArchiveFakeEntry {
UnsynchronizedByteArrayOutputStream baos =
UnsynchronizedByteArrayOutputStream.builder().get();
final byte[] data = fakeValue.getBytes(StandardCharsets.UTF_8);
baos.write(data);
- // the data in the stream is less than the fakeLen in the entry
- // this is a regression test for the existing internal behaviour - if
we choose to change the
- // behaviour in the future, this test can be updated
- assertThrows(EOFException.class, () -> new ZipArchiveFakeEntry(entry,
baos.toInputStream()));
+ try (ZipArchiveFakeEntry zipArchiveFakeEntry = new
ZipArchiveFakeEntry(entry, baos.toInputStream())) {
+ assertEquals(data.length, zipArchiveFakeEntry.getSize());
+ UnsynchronizedByteArrayOutputStream baos2 =
UnsynchronizedByteArrayOutputStream.builder().get();
+ try (InputStream stream = zipArchiveFakeEntry.getInputStream()) {
+ assertEquals(data.length, IOUtils.copy(stream, baos2));
+ }
+ assertEquals(fakeValue,
baos2.toString(StandardCharsets.UTF_8.name()));
+ }
+ }
+
+ @Test
+ void testEntryDataLargerThanMaxEntrySize() throws IOException {
+ // Reading is bounded by getMaxEntrySize(): a stream longer than the
limit is rejected
+ // rather than buffered in full.
+ final int previous = ZipArchiveFakeEntry.getMaxEntrySize();
+ ZipArchiveFakeEntry.setMaxEntrySize(4);
+ try {
+ ZipArchiveEntry entry = new ZipArchiveEntry("hack123");
+ UnsynchronizedByteArrayOutputStream baos =
UnsynchronizedByteArrayOutputStream.builder().get();
+ baos.write("fakeValue".getBytes(StandardCharsets.UTF_8));
+ assertThrows(RecordFormatException.class, () -> new
ZipArchiveFakeEntry(entry, baos.toInputStream()));
+ } finally {
+ ZipArchiveFakeEntry.setMaxEntrySize(previous);
+ }
}
static void runTestSize(boolean unencryptedTempFileExpected,
diff --git a/test-data/poi-integration-exceptions.csv
b/test-data/poi-integration-exceptions.csv
index 9d002322b7..fbefa8173d 100644
--- a/test-data/poi-integration-exceptions.csv
+++ b/test-data/poi-integration-exceptions.csv
@@ -388,8 +388,8 @@
slideshow/crash-7b60e9fe792eaaf1bba8be90c2b62f057cfff142.emf,"handle,extract",HE
slideshow/clusterfuzz-testcase-minimized-POIHSLFFuzzer-6028723156746240.ppt,additional,HPSF,,org.apache.poi.util.RecordFormatException,Tried
to allocate an array of length,
slideshow/clusterfuzz-testcase-minimized-POIHSLFFuzzer-6028723156746240.ppt,handle,HSLF,,org.apache.poi.util.RecordFormatException,Tried
to allocate an array of length,
slideshow/clusterfuzz-testcase-minimized-POIHSLFFuzzer-6028723156746240.ppt,handle,HPSF,,org.opentest4j.AssertionFailedError,expected:
<true> but was: <false>,
-diagram/clusterfuzz-testcase-minimized-POIVisioFuzzer-6312416206454784.vsdx,"handle,extract",XDGF,,java.lang.IllegalStateException,Cannot
create a ReLineTo object without a cell-name,
-diagram/clusterfuzz-testcase-minimized-POIVisioFuzzer-6312416206454784.vsdx,extract,OPC,,java.lang.IllegalStateException,Cannot
create a ReLineTo object without a cell-name,
+diagram/clusterfuzz-testcase-minimized-POIVisioFuzzer-6312416206454784.vsdx,"handle,extract",XDGF,,java.io.IOException,Truncated
ZIP file,
+diagram/clusterfuzz-testcase-minimized-POIVisioFuzzer-6312416206454784.vsdx,"handle,extract",OPC,,java.io.IOException,Truncated
ZIP file,
slideshow/62d2ecd89aeb715b07072d4f8a8734f4dbeb5c10.ppt,extract,"HSLF,HPSF",,org.apache.poi.util.RecordFormatException,Could
not read EscherSpRecord as child of SpContainer,
slideshow/62d2ecd89aeb715b07072d4f8a8734f4dbeb5c10.ppt,handle,HSLF,,java.lang.IllegalStateException,Cannot
get anchor,
spreadsheet/cf9f845e73447b092477d0472402a5baea4b8c9f.xls,handle,HSSF,,java.lang.IllegalStateException,Cannot
serialize EscherAggregate with missing shape-object,
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]