This is an automated email from the ASF dual-hosted git repository. centic9 pushed a commit to branch trunk in repository https://gitbox.apache.org/repos/asf/poi.git
commit f10567f53f05827837c8eff2fec0c1942082f0af Author: Dominik Stadler <[email protected]> AuthorDate: Sat Oct 24 17:34:17 2026 +0200 ByteChunks: Limit allocation To avoid OOM on input with invalid size-fields --- .../poi/hsmf/datatypes/ByteChunkDeferred.java | 5 ++- .../hsmf/extractor/TestOutlookTextExtractor.java | 50 +++++++++++++++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hsmf/datatypes/ByteChunkDeferred.java b/poi-scratchpad/src/main/java/org/apache/poi/hsmf/datatypes/ByteChunkDeferred.java index ce977b83b1..4f819313ee 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hsmf/datatypes/ByteChunkDeferred.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hsmf/datatypes/ByteChunkDeferred.java @@ -32,6 +32,9 @@ import org.apache.poi.util.IOUtils; * lazy loads its binary data from the document (if linked with a node via {@link #readValue(DocumentNode)}). */ public class ByteChunkDeferred extends ByteChunk { + // maximum size that is allowed by default, can be extended via IOUtils.setByteArrayMaxOverride() + // can be set higher if normal documents exceed this size + private static final int MAX_CHUNK_SIZE = 10 * 1024 * 1024; private DocumentNode node; @@ -77,7 +80,7 @@ public class ByteChunkDeferred extends ByteChunk { } try (DocumentInputStream dis = createDocumentInputStream()) { - return IOUtils.toByteArray(dis, node.getSize()); + return IOUtils.toByteArray(dis, node.getSize(), MAX_CHUNK_SIZE); } catch (IOException e) { return null; } diff --git a/poi-scratchpad/src/test/java/org/apache/poi/hsmf/extractor/TestOutlookTextExtractor.java b/poi-scratchpad/src/test/java/org/apache/poi/hsmf/extractor/TestOutlookTextExtractor.java index b90cf7cb00..16023cc6dc 100644 --- a/poi-scratchpad/src/test/java/org/apache/poi/hsmf/extractor/TestOutlookTextExtractor.java +++ b/poi-scratchpad/src/test/java/org/apache/poi/hsmf/extractor/TestOutlookTextExtractor.java @@ -18,10 +18,12 @@ package org.apache.poi.hsmf.extractor; import static org.apache.poi.POITestCase.assertContains; -import static org.apache.poi.POITestCase.assertNotContains; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; +import java.io.ByteArrayInputStream; import java.io.FileInputStream; +import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Locale; @@ -29,9 +31,13 @@ import java.util.TimeZone; import org.apache.poi.POIDataSamples; import org.apache.poi.POITestCase; +import org.apache.poi.extractor.ExtractorFactory; +import org.apache.poi.extractor.POIOLE2TextExtractor; +import org.apache.poi.extractor.POITextExtractor; import org.apache.poi.hsmf.MAPIMessage; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.util.LocaleUtil; +import org.apache.poi.util.RecordFormatException; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -252,4 +258,46 @@ public final class TestOutlookTextExtractor { assertContains(text, "( MSG \u683c\u5f0f\u6e2c\u8a66 )"); } } + + @Test + void testOOM() throws IOException { + final String base64Bytes = "0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAABAAAAAQAAAAAAAAAAEAAAAgAAAAEAAAD+////AAAAAAAAAAD///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// [...] + + byte[] input = java.util.Base64.getDecoder().decode(base64Bytes); + try (POITextExtractor extractor = ExtractorFactory.createExtractor(new ByteArrayInputStream(input))) { + checkExtractor(extractor); + + fail("Should throw an exception because of too large allocations"); + } catch (RecordFormatException e) { + // expected here + } + } + + @SuppressWarnings("DuplicatedCode") + public static void checkExtractor(POITextExtractor extractor) throws IOException { + extractor.getDocument(); + extractor.getFilesystem(); + try { + extractor.getMetadataTextExtractor(); + } catch (IllegalStateException e) { + // can happen here + } + extractor.getText(); + + if (extractor instanceof POIOLE2TextExtractor ole2Extractor) { + ole2Extractor.getRoot(); + ole2Extractor.getSummaryInformation(); + ole2Extractor.getDocSummaryInformation(); + + POITextExtractor[] embedded = ExtractorFactory.getEmbeddedDocsTextExtractors(ole2Extractor); + for (POITextExtractor poiTextExtractor : embedded) { + poiTextExtractor.getText(); + poiTextExtractor.getDocument(); + poiTextExtractor.getFilesystem(); + POITextExtractor metaData = poiTextExtractor.getMetadataTextExtractor(); + metaData.getFilesystem(); + metaData.getText(); + } + } + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
