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 97216676ef HMEF: reject named MAPI property names shorter than 2 bytes 
(#1549)
97216676ef is described below

commit 97216676ef1fa318d0eef75ea62bed0c2ccde8b1
Author: PJ Fanning <[email protected]>
AuthorDate: Mon Nov 9 07:30:34 2026 +0100

    HMEF: reject named MAPI property names shorter than 2 bytes (#1549)
    
    A custom-named MAPI property with a name length below 2 made
    (mplen/2)-1 negative, so StringUtil threw an unchecked
    ArrayIndexOutOfBoundsException or IllegalArgumentException.
    Throw an IOException instead.
    
    Fixes #1546
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../apache/poi/hmef/attribute/MAPIAttribute.java   |  4 +++
 .../poi/hmef/attribute/TestMAPIAttributes.java     | 36 ++++++++++++++++++++++
 2 files changed, 40 insertions(+)

diff --git 
a/poi-scratchpad/src/main/java/org/apache/poi/hmef/attribute/MAPIAttribute.java 
b/poi-scratchpad/src/main/java/org/apache/poi/hmef/attribute/MAPIAttribute.java
index eac941d29c..a59b8aa3b0 100644
--- 
a/poi-scratchpad/src/main/java/org/apache/poi/hmef/attribute/MAPIAttribute.java
+++ 
b/poi-scratchpad/src/main/java/org/apache/poi/hmef/attribute/MAPIAttribute.java
@@ -166,6 +166,10 @@ public class MAPIAttribute {
                } else {
                   // Custom name was stored
                   int mplen = LittleEndian.readInt(inp);
+                  if (mplen < 2) {
+                     // Must hold at least the 2-byte UTF-16 null terminator
+                     throw new IOException("Invalid named MAPI property name 
length " + mplen + ", must be at least 2");
+                  }
                   byte[] mpdata = IOUtils.safelyAllocate(mplen, 
MAX_RECORD_LENGTH, "MAPIAttribute.setMaxRecordLength()");
                   if (IOUtils.readFully(inp, mpdata) < 0) {
                      throw new IOException("Not enough data to read " + mplen 
+ " bytes for attribute name");
diff --git 
a/poi-scratchpad/src/test/java/org/apache/poi/hmef/attribute/TestMAPIAttributes.java
 
b/poi-scratchpad/src/test/java/org/apache/poi/hmef/attribute/TestMAPIAttributes.java
index 2ece5a0a3a..7599ce5e2b 100644
--- 
a/poi-scratchpad/src/test/java/org/apache/poi/hmef/attribute/TestMAPIAttributes.java
+++ 
b/poi-scratchpad/src/test/java/org/apache/poi/hmef/attribute/TestMAPIAttributes.java
@@ -19,8 +19,10 @@ package org.apache.poi.hmef.attribute;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.io.ByteArrayInputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
@@ -33,6 +35,8 @@ import org.apache.poi.util.LittleEndian;
 import org.apache.poi.util.LocaleUtil;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
 
 public final class TestMAPIAttributes {
     private static final POIDataSamples _samples = 
POIDataSamples.getHMEFInstance();
@@ -179,4 +183,36 @@ public final class TestMAPIAttributes {
         assertEquals("quick.doc", 
quick.getAttachments().get(0).getLongFilename());
         assertEquals(".doc", quick.getAttachments().get(0).getExtension());
     }
+
+    /**
+     * A named property whose custom name length is too short to hold
+     *  the UTF-16 null terminator is rejected (GitHub #1546)
+     */
+    @ParameterizedTest
+    @ValueSource(ints = {-1, 0, 1})
+    void testNamedPropertyNameTooShort(int mplen) {
+        // One Boolean named property with a custom name of the given length
+        byte[] mapiData = new byte[]{
+                // One attribute
+                0x01, 0x00, 0x00, 0x00,
+                // Boolean, id 0x8000
+                0x0B, 0x00, 0x00, (byte) 0x80,
+                // GUID
+                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+                // Custom name
+                0x01, 0x00, 0x00, 0x00,
+                // Name length
+                (byte) mplen, (byte) (mplen >> 8), (byte) (mplen >> 16), 
(byte) (mplen >> 24),
+                // Name data, padding and value
+                0x41, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00
+        };
+        byte[] data = new byte[8 + mapiData.length + 2];
+        // Id 36867 (MAPI properties), type 6
+        LittleEndian.putUShort(data, 0, 0x9003);
+        LittleEndian.putUShort(data, 2, 0x0006);
+        LittleEndian.putInt(data, 4, mapiData.length);
+        System.arraycopy(mapiData, 0, data, 8, mapiData.length);
+
+        assertThrows(IOException.class, () -> TNEFAttribute.create(new 
ByteArrayInputStream(data)));
+    }
 }


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

Reply via email to