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 142050f5e5 Enforce max entry size consistently for streaming ZIP 
entries (#1055)
142050f5e5 is described below

commit 142050f5e538eaa5e93183f82d627c59f94591e7
Author: jmestwa-coder <[email protected]>
AuthorDate: Tue May 5 21:39:34 2026 +0530

    Enforce max entry size consistently for streaming ZIP entries (#1055)
---
 .../poi/openxml4j/util/ZipArchiveFakeEntry.java    | 40 ++++++++++++-
 .../util/TestZipArchiveFakeEntryLimits.java        | 70 ++++++++++++++++++++++
 2 files changed, 108 insertions(+), 2 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 fc33f072fa..ccc0b54d3d 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
@@ -26,6 +26,7 @@ import org.apache.logging.log4j.Logger;
 import org.apache.poi.logging.PoiLogManager;
 import org.apache.poi.poifs.crypt.temp.EncryptedTempData;
 import org.apache.poi.util.IOUtils;
+import org.apache.poi.util.RecordFormatException;
 import org.apache.poi.util.TempFile;
 
 /**
@@ -69,19 +70,35 @@ public final class ZipArchiveFakeEntry extends 
ZipArchiveEntry implements Closea
         super(entry.getName());
 
         final long entrySize = entry.getSize();
+        final int maxEntrySize = getMaxEntrySize();
 
         final int threshold = 
ZipInputStreamZipEntrySource.getThresholdBytesForTempFiles();
         if (threshold >= 0 && (entrySize >= threshold || entrySize == -1)) {
+            if (entrySize >= 0) {
+                checkEntrySize(entrySize, maxEntrySize, entry.getName());
+            }
             if (ZipInputStreamZipEntrySource.shouldEncryptTempFiles()) {
                 encryptedTempData = new EncryptedTempData();
                 try (OutputStream os = encryptedTempData.getOutputStream()) {
-                    IOUtils.copy(inp, os);
+                    copyWithMaxEntrySize(inp, os, maxEntrySize, 
entry.getName());
+                } catch (IOException | RuntimeException e) {
+                    encryptedTempData.dispose();
+                    encryptedTempData = null;
+                    throw e;
                 }
             } else {
                 tempFile = TempFile.createTempFile("poi-zip-entry", ".tmp");
                 LOG.atInfo().log("Creating temp file {} for zip entry {} of 
size {} bytes",
                         tempFile.getAbsolutePath(), entry.getName(), 
entrySize);
-                IOUtils.copy(inp, tempFile);
+                try (OutputStream os = 
Files.newOutputStream(tempFile.toPath())) {
+                    copyWithMaxEntrySize(inp, os, maxEntrySize, 
entry.getName());
+                } catch (IOException | RuntimeException e) {
+                    if (tempFile.exists() && !tempFile.delete()) {
+                        LOG.atDebug().log("temp file was already deleted 
(probably due to previous call to close this resource)");
+                    }
+                    tempFile = null;
+                    throw e;
+                }
             }
         } else {
             if (entrySize < -1 || entrySize >= Integer.MAX_VALUE) {
@@ -94,6 +111,25 @@ public final class ZipArchiveFakeEntry extends 
ZipArchiveEntry implements Closea
         }
     }
 
+    private static void checkEntrySize(long entrySize, int maxEntrySize, 
String entryName) {
+        if (entrySize > maxEntrySize) {
+            throw new RecordFormatException("Zip entry " + entryName + " 
exceeds the max entry size of " + maxEntrySize + " bytes");
+        }
+    }
+
+    private static void copyWithMaxEntrySize(InputStream inp, OutputStream 
out, int maxEntrySize, String entryName) throws IOException {
+        byte[] buffer = new byte[4096];
+        int read;
+        long totalPayloadSize = 0;
+        while ((read = inp.read(buffer)) != -1) {
+            totalPayloadSize += read;
+            if (totalPayloadSize > maxEntrySize) {
+                throw new RecordFormatException("Zip entry " + entryName + " 
exceeds the max entry size of " + maxEntrySize + " bytes");
+            }
+            out.write(buffer, 0, read);
+        }
+    }
+
     /**
      * Returns zip entry.
      * @return input stream
diff --git 
a/poi-ooxml/src/test/java/org/apache/poi/openxml4j/util/TestZipArchiveFakeEntryLimits.java
 
b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/util/TestZipArchiveFakeEntryLimits.java
new file mode 100644
index 0000000000..ffb06ae98e
--- /dev/null
+++ 
b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/util/TestZipArchiveFakeEntryLimits.java
@@ -0,0 +1,70 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.openxml4j.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
+import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
+import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
+import org.apache.poi.util.RecordFormatException;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.parallel.Isolated;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+@Isolated
+class TestZipArchiveFakeEntryLimits {
+    @Test
+    void oversizedEntryIsRejectedInTempFileMode() throws IOException {
+        int origThreshold = 
ZipInputStreamZipEntrySource.getThresholdBytesForTempFiles();
+        int origMax = ZipArchiveFakeEntry.getMaxEntrySize();
+        try {
+            // Force temp-file spooling path
+            ZipInputStreamZipEntrySource.setThresholdBytesForTempFiles(0);
+            ZipArchiveFakeEntry.setMaxEntrySize(100);
+
+            // Create ZIP with unknown size (-1) and payload > maxEntrySize
+            byte[] zipBytes = createZipWithUnknownSize("test.txt", 200);
+            try (ZipArchiveInputStream zis = new ZipArchiveInputStream(new 
ByteArrayInputStream(zipBytes));
+                 ZipArchiveThresholdInputStream ztis = new 
ZipArchiveThresholdInputStream(zis)) {
+                
+                // Should throw RecordFormatException during 
ZipInputStreamZipEntrySource construction
+                assertThrows(RecordFormatException.class, () -> new 
ZipInputStreamZipEntrySource(ztis));
+            }
+        } finally {
+            // Restore static values
+            
ZipInputStreamZipEntrySource.setThresholdBytesForTempFiles(origThreshold);
+            ZipArchiveFakeEntry.setMaxEntrySize(origMax);
+        }
+    }
+
+    private static byte[] createZipWithUnknownSize(String name, int size) 
throws IOException {
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        try (ZipArchiveOutputStream zos = new ZipArchiveOutputStream(bos)) {
+            ZipArchiveEntry entry = new ZipArchiveEntry(name);
+            zos.putArchiveEntry(entry);
+            zos.write(new byte[size]);
+            zos.closeArchiveEntry();
+            zos.finish();
+        }
+        return bos.toByteArray();
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to