This is an automated email from the ASF dual-hosted git repository. pjfanning pushed a commit to branch 6.0.x in repository https://gitbox.apache.org/repos/asf/poi.git
commit 008902140029dd8b38d5059edf7edb1bdafd7b5a Author: PJ Fanning <[email protected]> AuthorDate: Thu Nov 12 02:38:23 2026 +0100 Limit the size of decompressed VBA modules read by VBAMacroReader (#1566) Decompressed VBA modules were read into memory with no upper bound. Apply a size limit to them, sharing it with the vbaProject.bin limit added in #1565, which is renamed from setMaxVbaProjectSize to setMaxStreamSize (not yet released). The brute-force search for module source used when a module offset is wrong now decompresses only the start of each candidate to look for "Attribute", instead of decompressing the rest of the stream for every candidate. It returns the same result as before. Backport note: 6.0.x lacks #1497, so the brute-force search only runs when the dir stream is read before the module stream, and a chunk starting with a copy token is not recoverable. setMaxStreamSizeIsRespectedByBruteForceSearch is adapted to read the dir stream first and to use a bad signature byte. Co-authored-by: Claude Opus 5.5 <[email protected]> --- .../apache/poi/poifs/macros/VBAMacroReader.java | 102 ++++++++++------- .../poi/poifs/macros/TestVBAMacroReader.java | 127 +++++++++++++++++++-- 2 files changed, 182 insertions(+), 47 deletions(-) diff --git a/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroReader.java b/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroReader.java index eaa8f63d50..d14d8f78af 100644 --- a/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroReader.java +++ b/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroReader.java @@ -80,9 +80,11 @@ public class VBAMacroReader implements Closeable { private static int MAX_STRING_LENGTH = DEFAULT_MAX_STRING_LENGTH; /** - * Set the maximum number of bytes to be allocated for a single VBA entry or decompressed - * stream. Use this to adjust the default limit if you need to process very large VBA projects, - * or to tighten the limit on memory-constrained systems. + * Set the maximum number of bytes to be allocated for a single string read from the + * VBA project metadata (module names, references, ...). Use this to adjust the default limit + * if you need to process very large VBA projects, or to tighten the limit on memory-constrained + * systems. The size of the VBA project data and of each module is limited by + * {@link #setMaxStreamSize(int)} instead. * * @param maxStringLength the maximum number of bytes that may be allocated; must be > 0 * @since 5.4.1 @@ -92,7 +94,8 @@ public class VBAMacroReader implements Closeable { } /** - * @return the maximum number of bytes that may be allocated per VBA entry or decompressed stream + * @return the maximum number of bytes that may be allocated for a single string read from + * the VBA project metadata * @since 5.4.1 */ public static int getMaxStringLength() { @@ -100,33 +103,35 @@ public class VBAMacroReader implements Closeable { } //arbitrary limit on the size of the vbaProject.bin entry read from an OOXML file - private static final int DEFAULT_MAX_VBA_PROJECT_SIZE = 100_000_000; - private static int MAX_VBA_PROJECT_SIZE = DEFAULT_MAX_VBA_PROJECT_SIZE; + //and of each (decompressed) VBA module + private static final int DEFAULT_MAX_STREAM_SIZE = 100_000_000; + private static int MAX_STREAM_SIZE = DEFAULT_MAX_STREAM_SIZE; /** * Set the maximum number of bytes that will be read for the {@code vbaProject.bin} entry - * of an OOXML file (.xlsm, .docm, .pptm, ...). The entry is held in memory while it is parsed. + * of an OOXML file (.xlsm, .docm, .pptm, ...), and for each VBA module after decompression. + * This data is held in memory while it is parsed. * Use this to adjust the default limit if you need to process very large VBA projects, * or to tighten the limit on memory-constrained systems. * - * @param maxVbaProjectSize the maximum number of bytes that may be read; must be > 0 - * @throws IllegalArgumentException if {@code maxVbaProjectSize} is not positive + * @param maxStreamSize the maximum number of bytes that may be read; must be > 0 + * @throws IllegalArgumentException if {@code maxStreamSize} is not positive * @since 6.0.1 */ - public static void setMaxVbaProjectSize(int maxVbaProjectSize) { - if (maxVbaProjectSize <= 0) { - throw new IllegalArgumentException("maxVbaProjectSize must be > 0"); + public static void setMaxStreamSize(int maxStreamSize) { + if (maxStreamSize <= 0) { + throw new IllegalArgumentException("maxStreamSize must be > 0"); } - MAX_VBA_PROJECT_SIZE = maxVbaProjectSize; + MAX_STREAM_SIZE = maxStreamSize; } /** * @return the maximum number of bytes that will be read for the {@code vbaProject.bin} entry - * of an OOXML file + * of an OOXML file, and for each VBA module after decompression * @since 6.0.1 */ - public static int getMaxVbaProjectSize() { - return MAX_VBA_PROJECT_SIZE; + public static int getMaxStreamSize() { + return MAX_STREAM_SIZE; } protected static final String VBA_PROJECT_OOXML = "vbaProject.bin"; @@ -164,7 +169,7 @@ public class VBAMacroReader implements Closeable { ZipEntry zipEntry; while ((zipEntry = zis.getNextEntry()) != null) { if (endsWithIgnoreCase(zipEntry.getName(), VBA_PROJECT_OOXML)) { - byte[] bytes = IOUtils.toByteArrayWithMaxLength(zis, MAX_VBA_PROJECT_SIZE); + byte[] bytes = IOUtils.toByteArrayWithMaxLength(zis, MAX_STREAM_SIZE); String lowerName = zipEntry.getName().toLowerCase(Locale.ROOT); boolean canonical = CANONICAL_VBA_DIRS.stream() .anyMatch(lowerName::startsWith); @@ -235,7 +240,7 @@ public class VBAMacroReader implements Closeable { ModuleType moduleType; Charset charset; void read(InputStream in) throws IOException { - buf = IOUtils.toByteArray(in); + buf = IOUtils.toByteArrayWithMaxLength(in, MAX_STREAM_SIZE); } @Override public String getContent() { @@ -865,40 +870,57 @@ public class VBAMacroReader implements Closeable { */ private static byte[] findCompressedStreamWBruteForce(InputStream is) throws IOException { //buffer to memory for multiple tries - byte[] compressed = IOUtils.toByteArray(is); - byte[] decompressed = null; + byte[] compressed = IOUtils.toByteArrayWithMaxLength(is, MAX_STREAM_SIZE); + int lastCandidate = -1; for (int i = 0; i < compressed.length; i++) { if (compressed[i] == 0x01 && i < compressed.length-1) { int w = LittleEndian.getUShort(compressed, i+1); if (w <= 0 || (w & 0x7000) != 0x3000) { continue; } - decompressed = tryToDecompress(UnsynchronizedByteArrayInputStream.builder(). - setByteArray(compressed). - setOffset(i). - setLength(compressed.length - i). - get()); - if (decompressed != null) { - if (decompressed.length > 9) { - //this is a complete hack. The challenge is that there - //can be many 0 length or junk streams that are uncompressed - //look in the first 20 characters for "Attribute" - int firstX = Math.min(20, decompressed.length); - String start = new String(decompressed, 0, firstX, WIN_1252); - if (start.contains("Attribute")) { - return decompressed; - } + lastCandidate = i; + //this is a complete hack. The challenge is that there + //can be many 0 length or junk streams that are uncompressed + //look in the first 20 characters for "Attribute". + //Only the start is decompressed here, so that junk candidates + //do not each cost a full decompression of the rest of the stream. + if (startsWithAttribute(compressed, i)) { + byte[] decompressed = tryToDecompress(compressed, i); + if (decompressed != null) { + return decompressed; } } } } - return decompressed; + // no candidate starts with "Attribute": fall back to the last candidate + return lastCandidate < 0 ? null : tryToDecompress(compressed, lastCandidate); } - private static byte[] tryToDecompress(InputStream is) { - try (RLEDecompressingInputStream ris = new RLEDecompressingInputStream(is)) { - return IOUtils.toByteArray(ris); - } catch (IllegalArgumentException | IOException | IllegalStateException e){ + private static boolean startsWithAttribute(byte[] compressed, int offset) { + final int prefixLength = 20; + try (RLEDecompressingInputStream ris = new RLEDecompressingInputStream( + UnsynchronizedByteArrayInputStream.builder(). + setByteArray(compressed). + setOffset(offset). + setLength(compressed.length - offset). + get())) { + byte[] prefix = new byte[prefixLength]; + int read = IOUtils.readFully(ris, prefix); + return read > 9 && new String(prefix, 0, read, WIN_1252).contains("Attribute"); + } catch (IllegalArgumentException | IOException | IllegalStateException e) { + return false; + } + } + + private static byte[] tryToDecompress(byte[] compressed, int offset) { + try (RLEDecompressingInputStream ris = new RLEDecompressingInputStream( + UnsynchronizedByteArrayInputStream.builder(). + setByteArray(compressed). + setOffset(offset). + setLength(compressed.length - offset). + get())) { + return IOUtils.toByteArrayWithMaxLength(ris, MAX_STREAM_SIZE); + } catch (IllegalArgumentException | IOException | IllegalStateException e) { return null; } } diff --git a/poi/src/test/java/org/apache/poi/poifs/macros/TestVBAMacroReader.java b/poi/src/test/java/org/apache/poi/poifs/macros/TestVBAMacroReader.java index 1a49040414..aedd4a9f4c 100644 --- a/poi/src/test/java/org/apache/poi/poifs/macros/TestVBAMacroReader.java +++ b/poi/src/test/java/org/apache/poi/poifs/macros/TestVBAMacroReader.java @@ -354,13 +354,13 @@ class TestVBAMacroReader { } @Test - void setMaxVbaProjectSizeIsRespected() throws IOException { - int prevLimit = VBAMacroReader.getMaxVbaProjectSize(); + void setMaxStreamSizeIsRespectedByVbaProject() throws IOException { + int prevLimit = VBAMacroReader.getMaxStreamSize(); try { File f = POIDataSamples.getSpreadSheetInstance().getFile("SimpleMacro.xlsm"); int vbaSize = extractZipEntry(f, "xl/vbaProject.bin").length; - VBAMacroReader.setMaxVbaProjectSize(vbaSize - 1); + VBAMacroReader.setMaxStreamSize(vbaSize - 1); assertThrows(RecordFormatException.class, () -> new VBAMacroReader(f), "Expected RecordFormatException when vbaProject.bin exceeds the limit"); try (InputStream is = new FileInputStream(f)) { @@ -368,18 +368,18 @@ class TestVBAMacroReader { "Expected RecordFormatException when vbaProject.bin exceeds the limit"); } - VBAMacroReader.setMaxVbaProjectSize(vbaSize); + VBAMacroReader.setMaxStreamSize(vbaSize); try (VBAMacroReader r = new VBAMacroReader(f)) { assertFalse(r.readMacros().isEmpty(), "macros should be read when within the limit"); } } finally { - VBAMacroReader.setMaxVbaProjectSize(prevLimit); + VBAMacroReader.setMaxStreamSize(prevLimit); } } @Test - void setMaxVbaProjectSizeRejectsNonPositive() { - assertThrows(IllegalArgumentException.class, () -> VBAMacroReader.setMaxVbaProjectSize(0)); + void setMaxStreamSizeRejectsNonPositive() { + assertThrows(IllegalArgumentException.class, () -> VBAMacroReader.setMaxStreamSize(0)); } /** @@ -583,6 +583,119 @@ class TestVBAMacroReader { } } + @Test + void setMaxStreamSizeIsRespectedByModule() throws IOException { + final int chunks = 100; + // RLE header + chunks that each expand to 4096 bytes + final ByteArrayOutputStream moduleStream = new ByteArrayOutputStream(); + moduleStream.write(0x01); + moduleStream.writeBytes(expandingChunks(chunks)); + + int prevLimit = VBAMacroReader.getMaxStreamSize(); + try { + VBAMacroReader.setMaxStreamSize(chunks * 4096); + VBAMacroReader.ModuleMap modules = readModuleWithDir(moduleStream.toByteArray(), 0); + assertEquals(chunks * 4096, modules.get("Module1").buf.length); + + VBAMacroReader.setMaxStreamSize(chunks * 4096 - 1); + assertThrows(RecordFormatException.class, () -> readModuleWithDir(moduleStream.toByteArray(), 0)); + } finally { + VBAMacroReader.setMaxStreamSize(prevLimit); + } + } + + /** + * When the module offset is bad, the brute-force search for the module source + * must apply the stream size limit too + */ + @Test + void setMaxStreamSizeIsRespectedByBruteForceSearch() throws IOException { + final int chunks = 100; + final byte[] source = "Attribute VB_Name = \"Module1\"".getBytes(StandardCharsets.US_ASCII); + final ByteArrayOutputStream moduleStream = new ByteArrayOutputStream(); + // a byte that is not the RLE signature, so the offset 0 is bad + moduleStream.write(0x00); + // the real compressed source, followed by chunks that each expand to 4096 bytes + moduleStream.writeBytes(compressLiterals(source)); + moduleStream.writeBytes(expandingChunks(chunks)); + final int expectedSize = source.length + chunks * 4096; + + int prevLimit = VBAMacroReader.getMaxStreamSize(); + try { + VBAMacroReader.setMaxStreamSize(expectedSize); + VBAMacroReader.ModuleMap modules = readDirWithModule(moduleStream.toByteArray(), 0); + assertEquals(expectedSize, modules.get("Module1").buf.length); + + VBAMacroReader.setMaxStreamSize(expectedSize - 1); + assertThrows(RecordFormatException.class, () -> readDirWithModule(moduleStream.toByteArray(), 0)); + } finally { + VBAMacroReader.setMaxStreamSize(prevLimit); + } + } + + /** + * Reads the module stream "Module1" first, then a dir stream giving its offset + */ + private static VBAMacroReader.ModuleMap readModuleWithDir(byte[] moduleStream, int moduleOffset) throws IOException { + return readModuleAndDir(moduleStream, moduleOffset, false); + } + + /** + * Reads a dir stream giving the offset of the module stream "Module1" first, then the module stream + */ + private static VBAMacroReader.ModuleMap readDirWithModule(byte[] moduleStream, int moduleOffset) throws IOException { + return readModuleAndDir(moduleStream, moduleOffset, true); + } + + private static VBAMacroReader.ModuleMap readModuleAndDir(byte[] moduleStream, int moduleOffset, boolean dirFirst) throws IOException { + final ByteArrayOutputStream dir = new ByteArrayOutputStream(); + writeShort(dir, 0x1A); + writeInt(dir, 7); + dir.write("Module1".getBytes(StandardCharsets.US_ASCII)); + writeShort(dir, 0x32); + writeInt(dir, 14); + dir.write("Module1".getBytes(StandardCharsets.UTF_16LE)); + writeShort(dir, 0x31); + writeInt(dir, 4); + writeInt(dir, moduleOffset); + + try (POIFSFileSystem fs = new POIFSFileSystem(); + VBAMacroReader reader = new VBAMacroReader(fs)) { + DirectoryNode moduleDir = (DirectoryNode) fs.getRoot().createDirectory("first").createDirectory("VBA"); + moduleDir.createDocument("Module1", new ByteArrayInputStream(moduleStream)); + DirectoryNode dirDir = (DirectoryNode) fs.getRoot().createDirectory("second").createDirectory("VBA"); + dirDir.createDocument("dir", new ByteArrayInputStream(compressLiterals(dir.toByteArray()))); + + VBAMacroReader.ModuleMap modules = new VBAMacroReader.ModuleMap(); + if (dirFirst) { + reader.readMacros(dirDir, modules); + reader.readMacros(moduleDir, modules); + } else { + reader.readMacros(moduleDir, modules); + reader.readMacros(dirDir, modules); + } + return modules; + } + } + + /** + * RLE chunks (without the leading signature byte) that each hold one literal + * and one copy token, so that 6 bytes expand to 4096 + */ + private static byte[] expandingChunks(int count) { + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + for (int c = 0; c < count; c++) { + // compressed chunk of 6 bytes in total + writeShort(out, 0xB000 | (6 - 3)); + // flag: first token is a literal, second token is a copy token + out.write(0x02); + out.write('A'); + // copy token: offset 1, length 4095 (4 bits of offset, 12 bits of length - 3) + writeShort(out, 4095 - 3); + } + return out.toByteArray(); + } + /** * RLE-compresses {@code data} into a single chunk of literals only */ --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
