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 aa7dc402b2 cap metafile buffer presizes at 4 MiB instead of the max
record length (#1411)
aa7dc402b2 is described below
commit aa7dc402b25dcc6802cfe578469d602c71c1e29c
Author: PJ Fanning <[email protected]>
AuthorDate: Fri Oct 16 19:25:54 2026 +0100
cap metafile buffer presizes at 4 MiB instead of the max record length
(#1411)
PICT and HemfEmbeddedIterator presized their output buffers from untrusted
header sizes, capped only by the max record length (100 MB by default), so a
tiny record could force a 100 MB allocation up front. Cap the presize at
4 MiB; larger data is still supported, the buffer grows as data is read.
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../org/apache/poi/hemf/usermodel/HemfEmbeddedIterator.java | 8 +++++++-
.../src/main/java/org/apache/poi/hslf/blip/PICT.java | 10 ++++++++--
2 files changed, 15 insertions(+), 3 deletions(-)
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/usermodel/HemfEmbeddedIterator.java
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/usermodel/HemfEmbeddedIterator.java
index 4d50955279..0997952077 100644
---
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/usermodel/HemfEmbeddedIterator.java
+++
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/usermodel/HemfEmbeddedIterator.java
@@ -52,6 +52,12 @@ public class HemfEmbeddedIterator implements
Iterator<HwmfEmbedded> {
private static final int DEFAULT_MAX_RECORD_LENGTH = 100_000_000;
private static int MAX_RECORD_LENGTH = DEFAULT_MAX_RECORD_LENGTH;
+ /**
+ * Upper bound for presizing the buffer from the untrusted totalObjectSize
field.
+ * Larger images are still supported, the buffer just grows as the chunks
are read.
+ */
+ private static final int MAX_BUFFER_PRESIZE = 4 * 1024 * 1024;
+
private final Deque<Iterator<?>> iterStack = new ArrayDeque<>();
private Object current;
@@ -294,7 +300,7 @@ public class HemfEmbeddedIterator implements
Iterator<HwmfEmbedded> {
IOUtils.safelyAllocateCheck(totalSize, MAX_RECORD_LENGTH,
"HemfEmbeddedIterator.setMaxRecordLength()");
- try (UnsynchronizedByteArrayOutputStream bos =
UnsynchronizedByteArrayOutputStream.builder().setBufferSize(totalSize).get()) {
+ try (UnsynchronizedByteArrayOutputStream bos =
UnsynchronizedByteArrayOutputStream.builder().setBufferSize(Math.min(totalSize,
MAX_BUFFER_PRESIZE)).get()) {
boolean hasNext = false;
do {
EmfPlusImage img = epo.getObjectData();
diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/PICT.java
b/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/PICT.java
index 56aa3e8b8e..15112413ff 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/PICT.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/PICT.java
@@ -45,6 +45,12 @@ import org.apache.poi.util.Units;
public final class PICT extends Metafile {
private static final Logger LOG = PoiLogManager.getLogger(PICT.class);
+ /**
+ * Upper bound for presizing the inflate buffer from the untrusted wmfSize
header field.
+ * Larger metafiles are still supported, the buffer just grows as data is
inflated.
+ */
+ private static final int MAX_BUFFER_PRESIZE = 4 * 1024 * 1024;
+
/**
* Creates a new instance.
*
@@ -89,8 +95,8 @@ public final class PICT extends Metafile {
}
byte[] chunk = new byte[4096];
final int maxLength = getMaxRecordLength();
- // wmfSize is untrusted, so only use it as a hint up to the
inflate limit
- final int bufferSize = Math.min(header.getWmfSize(), maxLength);
+ // wmfSize is untrusted, so only use it as a hint, and don't
presize a huge buffer
+ final int bufferSize = Math.min(header.getWmfSize(),
Math.min(maxLength, MAX_BUFFER_PRESIZE));
try (UnsynchronizedByteArrayOutputStream out =
UnsynchronizedByteArrayOutputStream.builder().setBufferSize(bufferSize).get()) {
long totalInflated = 0;
try (InflaterInputStream inflater = new
InflaterInputStream(bis)) {
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]