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 da0c0a465e HEMF: limit the nesting depth of EMF+ region nodes (#1552)
da0c0a465e is described below

commit da0c0a465e703f6c046565cf13a0f14c433b73a8
Author: PJ Fanning <[email protected]>
AuthorDate: Mon Nov 9 07:32:35 2026 +0100

    HEMF: limit the nesting depth of EMF+ region nodes (#1552)
    
    * HEMF: limit the nesting depth of EMF+ region nodes
    
    Region nodes with child nodes were parsed recursively without a limit,
    so deeply nested nodes caused a StackOverflowError. Throw a
    RecordFormatException when nodes are nested more than 1000 levels deep.
    
    Fixes #1547
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    
    * HEMF: reject unknown EMF+ region node types
    
    An unknown node type made valueOf return null, which only an assert
    checked, so parsing threw a NullPointerException. Throw a
    RecordFormatException instead.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    
    ---------
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../poi/hemf/record/emfplus/HemfPlusRegion.java    |  31 +++++--
 .../hemf/record/emfplus/TestHemfPlusRegion.java    | 100 +++++++++++++++++++++
 2 files changed, 124 insertions(+), 7 deletions(-)

diff --git 
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusRegion.java
 
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusRegion.java
index 9ccfb5ff6a..0a908a8bd2 100644
--- 
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusRegion.java
+++ 
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusRegion.java
@@ -40,8 +40,12 @@ import 
org.apache.poi.hemf.record.emfplus.HemfPlusPath.EmfPlusPath;
 import org.apache.poi.util.GenericRecordUtil;
 import org.apache.poi.util.LittleEndianConsts;
 import org.apache.poi.util.LittleEndianInputStream;
+import org.apache.poi.util.RecordFormatException;
 
 public class HemfPlusRegion {
+    /** Maximum nesting depth of region nodes, to avoid a stack overflow on 
malformed data */
+    private static final int MAX_NODE_DEPTH = 1000;
+
     public enum EmfPlusRegionNodeDataType {
         /**
          * Specifies a region node with child nodes. A Boolean AND operation 
SHOULD be applied to the left and right
@@ -120,7 +124,7 @@ public class HemfPlusRegion {
             // An array of RegionNodeCount+1 EmfPlusRegionNode objects. 
Regions are specified as a binary tree of
             // region nodes, and each node MUST either be a terminal node or 
specify one or two child nodes.
             // RegionNode MUST contain at least one element.
-            size += readNode(leis, this::setRegionNode);
+            size += readNode(leis, this::setRegionNode, 0);
 
             return size;
         }
@@ -258,8 +262,12 @@ public class HemfPlusRegion {
 
         @Override
         public long init(LittleEndianInputStream leis) throws IOException {
-            long size = readNode(leis, this::setLeft);
-            size += readNode(leis, this::setRight);
+            return init(leis, 0);
+        }
+
+        private long init(LittleEndianInputStream leis, int depth) throws 
IOException {
+            long size = readNode(leis, this::setLeft, depth + 1);
+            size += readNode(leis, this::setRight, depth + 1);
             return size;
         }
 
@@ -325,14 +333,23 @@ public class HemfPlusRegion {
         }
     }
 
-    private static long readNode(LittleEndianInputStream leis, 
Consumer<EmfPlusRegionNodeData> con) throws IOException {
+    private static long readNode(LittleEndianInputStream leis, 
Consumer<EmfPlusRegionNodeData> con, int depth) throws IOException {
+        if (depth > MAX_NODE_DEPTH) {
+            throw new RecordFormatException("Region nodes are nested deeper 
than " + MAX_NODE_DEPTH + " levels");
+        }
         // A 32-bit unsigned integer that specifies the type of data in the 
RegionNodeData field.
         // This value MUST be defined in the RegionNodeDataType enumeration
-        EmfPlusRegionNodeDataType type = 
EmfPlusRegionNodeDataType.valueOf(leis.readInt());
-        assert(type != null);
+        int typeId = leis.readInt();
+        EmfPlusRegionNodeDataType type = 
EmfPlusRegionNodeDataType.valueOf(typeId);
+        if (type == null) {
+            throw new RecordFormatException("Unknown region node type 0x" + 
Integer.toHexString(typeId));
+        }
         EmfPlusRegionNodeData nd = type.constructor.get();
         con.accept(nd);
         nd.setNodeType(type);
-        return LittleEndianConsts.INT_SIZE + nd.init(leis);
+        long size = (nd instanceof EmfPlusRegionNode)
+            ? ((EmfPlusRegionNode) nd).init(leis, depth)
+            : nd.init(leis);
+        return LittleEndianConsts.INT_SIZE + size;
     }
 }
diff --git 
a/poi-scratchpad/src/test/java/org/apache/poi/hemf/record/emfplus/TestHemfPlusRegion.java
 
b/poi-scratchpad/src/test/java/org/apache/poi/hemf/record/emfplus/TestHemfPlusRegion.java
new file mode 100644
index 0000000000..3b74316251
--- /dev/null
+++ 
b/poi-scratchpad/src/test/java/org/apache/poi/hemf/record/emfplus/TestHemfPlusRegion.java
@@ -0,0 +1,100 @@
+/* ====================================================================
+   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.hemf.record.emfplus;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.Arrays;
+
+import org.apache.poi.hemf.record.emfplus.HemfPlusObject.EmfPlusObjectType;
+import org.apache.poi.hemf.record.emfplus.HemfPlusRegion.EmfPlusRegion;
+import org.apache.poi.hemf.record.emfplus.HemfPlusRegion.EmfPlusRegionNode;
+import 
org.apache.poi.hemf.record.emfplus.HemfPlusRegion.EmfPlusRegionNodeDataType;
+import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.LittleEndianInputStream;
+import org.apache.poi.util.RecordFormatException;
+import org.junit.jupiter.api.Test;
+
+class TestHemfPlusRegion {
+
+    @Test
+    void readsNestedNodes() throws IOException {
+        // AND(OR(EMPTY, INFINITE), EMPTY)
+        byte[] data = regionData(
+            EmfPlusRegionNodeDataType.AND.id,
+            EmfPlusRegionNodeDataType.OR.id,
+            EmfPlusRegionNodeDataType.EMPTY.id,
+            EmfPlusRegionNodeDataType.INFINITE.id,
+            EmfPlusRegionNodeDataType.EMPTY.id
+        );
+
+        EmfPlusRegion region = parse(data);
+        EmfPlusRegionNode root = assertInstanceOf(EmfPlusRegionNode.class, 
region.getRegionNode());
+        assertEquals(EmfPlusRegionNodeDataType.AND, root.getNodeType());
+        EmfPlusRegionNode left = assertInstanceOf(EmfPlusRegionNode.class, 
root.getLeft());
+        assertEquals(EmfPlusRegionNodeDataType.OR, left.getNodeType());
+    }
+
+    /**
+     * Deeply nested region nodes are rejected instead of overflowing the 
stack (GitHub #1547)
+     */
+    @Test
+    void rejectsDeeplyNestedNodes() {
+        int[] nodes = new int[30_000];
+        Arrays.fill(nodes, EmfPlusRegionNodeDataType.AND.id);
+        byte[] data = regionData(nodes);
+
+        RecordFormatException ex = assertThrows(RecordFormatException.class, 
() -> parse(data));
+        assertEquals("Region nodes are nested deeper than 1000 levels", 
ex.getMessage());
+    }
+
+    /**
+     * An unknown region node type is rejected instead of causing a 
NullPointerException
+     */
+    @Test
+    void rejectsUnknownNodeType() {
+        // AND with an unknown left child
+        byte[] data = regionData(EmfPlusRegionNodeDataType.AND.id, 0x12345678);
+
+        RecordFormatException ex = assertThrows(RecordFormatException.class, 
() -> parse(data));
+        assertEquals("Unknown region node type 0x12345678", ex.getMessage());
+    }
+
+    private static byte[] regionData(int... nodeTypes) {
+        byte[] data = new byte[8 + nodeTypes.length * 4];
+        // EmfPlusGraphicsVersion: metafile signature 0xDBC01, graphics 
version 1
+        LittleEndian.putInt(data, 0, 0xDBC01001);
+        LittleEndian.putInt(data, 4, nodeTypes.length - 1);
+        for (int i = 0; i < nodeTypes.length; i++) {
+            LittleEndian.putInt(data, 8 + i * 4, nodeTypes[i]);
+        }
+        return data;
+    }
+
+    private static EmfPlusRegion parse(byte[] data) throws IOException {
+        EmfPlusRegion region = new EmfPlusRegion();
+        try (LittleEndianInputStream leis = new LittleEndianInputStream(new 
ByteArrayInputStream(data))) {
+            region.init(leis, data.length, EmfPlusObjectType.REGION, 0);
+        }
+        return region;
+    }
+}


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

Reply via email to