This is an automated email from the ASF dual-hosted git repository.

coheigea pushed a commit to branch coheigea/aegis
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit 2f53771daab8211ef201ba697330749ee8fbd989
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Mon Nov 16 08:20:17 2026 +0000

    Limit the size of SOAP encoded arrays read by Aegis
---
 .../org/apache/cxf/aegis/AbstractAegisIoImpl.java  |  4 ++
 .../cxf/aegis/type/encoded/SoapArrayType.java      | 64 ++++++++++++++++++--
 .../cxf/aegis/type/encoded/SoapArrayTypeTest.java  | 68 ++++++++++++++++++++++
 3 files changed, 131 insertions(+), 5 deletions(-)

diff --git 
a/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/AbstractAegisIoImpl.java
 
b/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/AbstractAegisIoImpl.java
index bb975f765d9..8a9e8274445 100644
--- 
a/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/AbstractAegisIoImpl.java
+++ 
b/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/AbstractAegisIoImpl.java
@@ -26,6 +26,7 @@ import javax.xml.validation.Schema;
 
 import org.apache.cxf.databinding.DataReader;
 import org.apache.cxf.interceptor.Fault;
+import org.apache.cxf.message.Message;
 
 /**
  * Common implementation of the Aegis data readers and writer.
@@ -58,6 +59,9 @@ public abstract class AbstractAegisIoImpl  {
     public void setProperty(String prop, Object value) {
         if (DataReader.FAULT.equals(prop)) {
             context.setFault((Fault)value);
+        } else if (Message.class.getName().equals(prop)) {
+            // make the message available to types that read configuration 
from it
+            context.setProperty(prop, value);
         }
     }
 
diff --git 
a/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/encoded/SoapArrayType.java
 
b/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/encoded/SoapArrayType.java
index cf1ccb41a58..be2f9f9ebe3 100644
--- 
a/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/encoded/SoapArrayType.java
+++ 
b/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/encoded/SoapArrayType.java
@@ -39,15 +39,29 @@ import org.apache.cxf.aegis.xml.MessageReader;
 import org.apache.cxf.aegis.xml.MessageWriter;
 import org.apache.cxf.binding.soap.Soap11;
 import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.common.util.SystemPropertyAction;
 import org.apache.cxf.helpers.CastUtils;
+import org.apache.cxf.message.Message;
 import org.apache.ws.commons.schema.XmlSchema;
 
 import static 
org.apache.cxf.aegis.type.encoded.SoapEncodingUtil.readAttributeValue;
 
 public class SoapArrayType extends AegisType {
+    /**
+     * The maximum total number of elements of the SOAP encoded arrays read 
from a message. Each array is
+     * created at the size declared in its arrayType attribute, however many 
elements are actually sent,
+     * so this limits the memory a message can make the service allocate. The 
default is 1000000, and a
+     * negative value means no limit. It can be set as a message (endpoint or 
client) property, or as a
+     * system property.
+     */
+    public static final String MAX_ARRAY_ELEMENTS = 
"org.apache.cxf.aegis.encoded.maxArrayElements";
+
     private static final Logger LOG = 
LogUtils.getL7dLogger(SoapArrayType.class);
     private static final String SOAP_ENCODING_NS_1_1 = 
Soap11.getInstance().getSoapEncodingStyle();
     private static final QName SOAP_ARRAY_POSITION = new 
QName(SOAP_ENCODING_NS_1_1, "position");
+    private static final int DEFAULT_MAX_ARRAY_ELEMENTS =
+        SystemPropertyAction.getInteger(MAX_ARRAY_ELEMENTS, 1000000);
+    private static final String ARRAY_ELEMENT_COUNT = 
SoapArrayType.class.getName() + ".elementCount";
 
     private QName componentName;
 
@@ -70,10 +84,7 @@ public class SoapArrayType extends AegisType {
             }
 
             // calculate max size
-            int maxSize = 1;
-            for (int dimension : arrayTypeInfo.getDimensions()) {
-                maxSize *= dimension;
-            }
+            int maxSize = reserveArrayElements(context, arrayTypeInfo);
 
             // verify offset doesn't exceed maximum size
             if (arrayTypeInfo.getOffset() >= maxSize) {
@@ -118,6 +129,46 @@ public class SoapArrayType extends AegisType {
         }
     }
 
+    /**
+     * Calculate the number of elements of the array from its declared 
dimensions, and check it against the
+     * limit on the total number of array elements in the message, before 
anything of that size is allocated.
+     */
+    private int reserveArrayElements(Context context, ArrayTypeInfo 
arrayTypeInfo) {
+        long size = 1;
+        for (int dimension : arrayTypeInfo.getDimensions()) {
+            size *= dimension;
+            if (size > Integer.MAX_VALUE) {
+                throw new DatabindingException("The size of the array " + 
arrayTypeInfo + " in "
+                        + getSchemaType() + " is too large");
+            }
+        }
+
+        Long count = context.getProperty(ARRAY_ELEMENT_COUNT, Long.class);
+        long total = size + (count == null ? 0 : count);
+        int maxElements = getMaxArrayElements(context);
+        if (maxElements >= 0 && total > maxElements) {
+            throw new DatabindingException("The size of the array " + 
arrayTypeInfo + " in " + getSchemaType()
+                    + " exceeds the maximum number of array elements in a 
message of " + maxElements);
+        }
+        context.setProperty(ARRAY_ELEMENT_COUNT, total);
+        return (int)size;
+    }
+
+    private static int getMaxArrayElements(Context context) {
+        Message message = context.getProperty(Message.class.getName(), 
Message.class);
+        Object value = message == null ? null : 
message.getContextualProperty(MAX_ARRAY_ELEMENTS);
+        if (value instanceof Number) {
+            return ((Number)value).intValue();
+        } else if (value instanceof String) {
+            try {
+                return Integer.parseInt(((String)value).trim());
+            } catch (NumberFormatException ex) {
+                throw new DatabindingException("Invalid value for " + 
MAX_ARRAY_ELEMENTS + ": " + value);
+            }
+        }
+        return DEFAULT_MAX_ARRAY_ELEMENTS;
+    }
+
     protected List<Object> readCollection(MessageReader reader,
             Context context,
             ArrayTypeInfo arrayTypeInfo,
@@ -167,7 +218,7 @@ public class SoapArrayType extends AegisType {
                 values.add(value);
             } else {
                 int valuesPosition = readValuesPosition(position, 
arrayTypeInfo.getDimensions());
-                if (valuesPosition > maxSize) {
+                if (valuesPosition < 0 || valuesPosition >= maxSize) {
                     throw new DatabindingException("Array position " + 
valuesPosition + " in "
                             + getSchemaType() + " exceeds the maximum size of 
" + maxSize);
                 }
@@ -207,6 +258,9 @@ public class SoapArrayType extends AegisType {
                     int tokenId = i * 2;
 
                     index[i] = Integer.parseInt(tokens.get(tokenId));
+                    if (index[i] < 0 || index[i] >= dimensions.get(i)) {
+                        throw new IllegalStateException("Position " + index[i] 
+ " is outside the array");
+                    }
 
                     if (tokenId + 1 < tokens.size() && 
!",".equals(tokens.get(tokenId + 1))) {
                         throw new IllegalStateException(
diff --git 
a/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/encoded/SoapArrayTypeTest.java
 
b/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/encoded/SoapArrayTypeTest.java
index 52b3a86cc96..31b502de396 100644
--- 
a/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/encoded/SoapArrayTypeTest.java
+++ 
b/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/encoded/SoapArrayTypeTest.java
@@ -18,15 +18,20 @@
  */
 package org.apache.cxf.aegis.type.encoded;
 
+import java.io.ByteArrayInputStream;
 import java.math.BigDecimal;
+import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 
 import javax.xml.namespace.QName;
 
 import org.apache.cxf.aegis.Context;
+import org.apache.cxf.aegis.DatabindingException;
 import org.apache.cxf.aegis.type.AegisType;
 import org.apache.cxf.aegis.type.basic.BeanTypeInfo;
 import org.apache.cxf.aegis.xml.stax.ElementReader;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -34,6 +39,7 @@ import org.junit.Test;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
 
 public class SoapArrayTypeTest extends AbstractEncodedTest {
     private static final String[][][] ARRAY_2_3_4 = new String[][][] {
@@ -328,6 +334,68 @@ public class SoapArrayTypeTest extends AbstractEncodedTest 
{
         }
     }
 
+    @Test
+    public void testDeclaredSizeTooLarge() throws Exception {
+        // a huge declared size must be rejected before anything of that size 
is allocated
+        Context context = getContext();
+        assertThrows(DatabindingException.class, () -> readIntArray(context, 
"xsd:int[2000000000]", ""));
+    }
+
+    @Test
+    public void testDeclaredSizeOverflow() throws Exception {
+        // 65536 * 65536 overflows an int
+        Context context = getContext();
+        assertThrows(DatabindingException.class,
+            () -> 
createArrayType(int[][].class).readObject(reader("xsd:int[65536,65536]", ""), 
context));
+    }
+
+    @Test
+    public void testMaxArrayElementsPerMessage() throws Exception {
+        // the limit applies to the total of all the arrays read from a message
+        Context context = getContext();
+        assertEquals(600000, readIntArray(context, "xsd:int[600000]", 
"").length);
+        assertThrows(DatabindingException.class, () -> readIntArray(context, 
"xsd:int[600000]", ""));
+
+        // a new message starts again
+        assertEquals(600000, readIntArray(getContext(), "xsd:int[600000]", 
"").length);
+    }
+
+    @Test
+    public void testMaxArrayElementsFromMessage() throws Exception {
+        Message message = new MessageImpl();
+        message.put(SoapArrayType.MAX_ARRAY_ELEMENTS, "10");
+        Context context = getContext();
+        context.setProperty(Message.class.getName(), message);
+        assertArrayEquals(new int[] {1, 2, 0, 0, 0, 0, 0, 0, 0, 0},
+                          readIntArray(context, "xsd:int[10]", 
"<n>1</n><n>2</n>"));
+        assertThrows(DatabindingException.class, () -> readIntArray(context, 
"xsd:int[1]", "<n>1</n>"));
+
+        message.put(SoapArrayType.MAX_ARRAY_ELEMENTS, -1);
+        Context unlimited = getContext();
+        unlimited.setProperty(Message.class.getName(), message);
+        assertEquals(2000000, readIntArray(unlimited, "xsd:int[2000000]", 
"").length);
+    }
+
+    @Test
+    public void testSparseArrayPositionOutOfRange() throws Exception {
+        Context context = getContext();
+        assertThrows(DatabindingException.class, () -> readIntArray(context, 
"xsd:int[2]",
+            "<n soapenc:position='[-1]'>1</n>"));
+        assertThrows(DatabindingException.class, () -> readIntArray(context, 
"xsd:int[2]",
+            "<n soapenc:position='[2]'>1</n>"));
+    }
+
+    private int[] readIntArray(Context context, String arrayType, String 
elements) throws Exception {
+        return (int[]) 
createArrayType(int[].class).readObject(reader(arrayType, elements), context);
+    }
+
+    private static ElementReader reader(String arrayType, String elements) 
throws Exception {
+        String xml = "<b:numbers xmlns:b='urn:Bean' 
xmlns:xsd='http://www.w3.org/2001/XMLSchema'"
+            + " xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/' 
soapenc:arrayType='" + arrayType + "'>"
+            + elements + "</b:numbers>";
+        return new ElementReader(new 
ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
+    }
+
     private SoapArrayType createArrayType(Class<?> typeClass) {
         return createArrayType(typeClass, new QName("urn:Bean", "stuff"));
     }

Reply via email to