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 77df76b9c9 Limit the size of the vbaProject.bin entry read by
VBAMacroReader (#1565)
77df76b9c9 is described below
commit 77df76b9c990cadbb3c8ae72f05a03fa06194c7a
Author: PJ Fanning <[email protected]>
AuthorDate: Wed Nov 11 16:04:21 2026 +0100
Limit the size of the vbaProject.bin entry read by VBAMacroReader (#1565)
* Limit the size of the vbaProject.bin entry read by VBAMacroReader
VBAMacroReader read the vbaProject.bin entry of an OOXML file into memory
with no upper bound. Cap it at a configurable limit (default 100 MB),
adjustable with VBAMacroReader.setMaxVbaProjectSize, in line with the
other configurable size limits in POI.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
* Set @since to 6.0.1 for the vbaProject.bin size limit API
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---------
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../apache/poi/poifs/macros/VBAMacroReader.java | 32 +++++++++++++++++++++-
.../poi/poifs/macros/TestVBAMacroReader.java | 29 ++++++++++++++++++++
2 files changed, 60 insertions(+), 1 deletion(-)
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 eca36d5823..04679af30c 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
@@ -99,6 +99,36 @@ public class VBAMacroReader implements Closeable {
return MAX_STRING_LENGTH;
}
+ //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;
+
+ /**
+ * 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.
+ * 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
+ * @since 6.0.1
+ */
+ public static void setMaxVbaProjectSize(int maxVbaProjectSize) {
+ if (maxVbaProjectSize <= 0) {
+ throw new IllegalArgumentException("maxVbaProjectSize must be >
0");
+ }
+ MAX_VBA_PROJECT_SIZE = maxVbaProjectSize;
+ }
+
+ /**
+ * @return the maximum number of bytes that will be read for the {@code
vbaProject.bin} entry
+ * of an OOXML file
+ * @since 6.0.1
+ */
+ public static int getMaxVbaProjectSize() {
+ return MAX_VBA_PROJECT_SIZE;
+ }
+
protected static final String VBA_PROJECT_OOXML = "vbaProject.bin";
protected static final String VBA_PROJECT_POIFS = "VBA";
@@ -134,7 +164,7 @@ public class VBAMacroReader implements Closeable {
ZipEntry zipEntry;
while ((zipEntry = zis.getNextEntry()) != null) {
if (endsWithIgnoreCase(zipEntry.getName(), VBA_PROJECT_OOXML))
{
- byte[] bytes = IOUtils.toByteArray(zis);
+ byte[] bytes = IOUtils.toByteArrayWithMaxLength(zis,
MAX_VBA_PROJECT_SIZE);
String lowerName =
zipEntry.getName().toLowerCase(Locale.ROOT);
boolean canonical = CANONICAL_VBA_DIRS.stream()
.anyMatch(lowerName::startsWith);
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 d4fe57ef28..0caa33afa8 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
@@ -353,6 +353,35 @@ class TestVBAMacroReader {
}
}
+ @Test
+ void setMaxVbaProjectSizeIsRespected() throws IOException {
+ int prevLimit = VBAMacroReader.getMaxVbaProjectSize();
+ try {
+ File f =
POIDataSamples.getSpreadSheetInstance().getFile("SimpleMacro.xlsm");
+ int vbaSize = extractZipEntry(f, "xl/vbaProject.bin").length;
+
+ VBAMacroReader.setMaxVbaProjectSize(vbaSize - 1);
+ assertThrows(RecordFormatException.class, () -> new
VBAMacroReader(f),
+ "Expected RecordFormatException when vbaProject.bin
exceeds the limit");
+ try (InputStream is = new FileInputStream(f)) {
+ assertThrows(RecordFormatException.class, () -> new
VBAMacroReader(is),
+ "Expected RecordFormatException when vbaProject.bin
exceeds the limit");
+ }
+
+ VBAMacroReader.setMaxVbaProjectSize(vbaSize);
+ try (VBAMacroReader r = new VBAMacroReader(f)) {
+ assertFalse(r.readMacros().isEmpty(), "macros should be read
when within the limit");
+ }
+ } finally {
+ VBAMacroReader.setMaxVbaProjectSize(prevLimit);
+ }
+ }
+
+ @Test
+ void setMaxVbaProjectSizeRejectsNonPositive() {
+ assertThrows(IllegalArgumentException.class, () ->
VBAMacroReader.setMaxVbaProjectSize(0));
+ }
+
/**
* Verify that VBAMacroReader prefers the vbaProject.bin at a canonical
OPC path
* (xl/vbaProject.bin) over one at a non-canonical path
(docProps/vbaProject.bin),
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]