This is an automated email from the ASF dual-hosted git repository.
coheigea pushed a commit to branch 3.6.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git
The following commit(s) were added to refs/heads/3.6.x-fixes by this push:
new f3c43fcc00d Limit the size of SOAP encoded arrays read by Aegis (#3721)
f3c43fcc00d is described below
commit f3c43fcc00d24c32b3c8381c70222780cc52be2e
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Mon Nov 16 17:04:01 2026 +0000
Limit the size of SOAP encoded arrays read by Aegis (#3721)
* Limit the size of SOAP encoded arrays read by Aegis
* Applying review comments
(cherry picked from commit 1f94c64f7a4417bfd8679ca7695d50a8f66121e0)
---
.../org/apache/cxf/aegis/AbstractAegisIoImpl.java | 4 ++
.../cxf/aegis/type/encoded/SoapArrayType.java | 80 +++++++++++++++++++--
.../cxf/aegis/type/encoded/SoapArrayTypeTest.java | 82 ++++++++++++++++++++++
3 files changed, 161 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..d1f14d16390 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,38 @@ 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 number of elements of a SOAP encoded array, as declared in
its arrayType attribute. Each
+ * array is created at its declared size, however many elements are
actually sent. 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";
+ /**
+ * The maximum total number of elements of all the SOAP encoded arrays
read from a message, as declared
+ * in their arrayType attributes. Together with {@link
#MAX_ARRAY_ELEMENTS}, this limits the memory a
+ * message can make the service allocate, however many arrays it contains.
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_TOTAL_ARRAY_ELEMENTS =
"org.apache.cxf.aegis.encoded.maxTotalArrayElements";
+
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 int DEFAULT_MAX_TOTAL_ARRAY_ELEMENTS =
+ SystemPropertyAction.getInteger(MAX_TOTAL_ARRAY_ELEMENTS, 1000000);
+ private static final String ARRAY_ELEMENT_COUNT =
SoapArrayType.class.getName() + ".elementCount";
private QName componentName;
@@ -70,10 +93,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 +138,53 @@ public class SoapArrayType extends AegisType {
}
}
+ /**
+ * Calculate the number of elements of the array from its declared
dimensions, and check it against the
+ * limits on the size of an array and 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");
+ }
+ }
+
+ int maxElements = getLimit(context, MAX_ARRAY_ELEMENTS,
DEFAULT_MAX_ARRAY_ELEMENTS);
+ if (maxElements >= 0 && size > maxElements) {
+ throw new DatabindingException("The size of the array " +
arrayTypeInfo + " in " + getSchemaType()
+ + " exceeds the maximum array size of " + maxElements);
+ }
+
+ Long count = context.getProperty(ARRAY_ELEMENT_COUNT, Long.class);
+ long total = size + (count == null ? 0 : count);
+ int maxTotalElements = getLimit(context, MAX_TOTAL_ARRAY_ELEMENTS,
DEFAULT_MAX_TOTAL_ARRAY_ELEMENTS);
+ if (maxTotalElements >= 0 && total > maxTotalElements) {
+ throw new DatabindingException("The size of the array " +
arrayTypeInfo + " in " + getSchemaType()
+ + " exceeds the maximum total number of array elements in
a message of " + maxTotalElements);
+ }
+ context.setProperty(ARRAY_ELEMENT_COUNT, total);
+ return (int)size;
+ }
+
+ private static int getLimit(Context context, String key, int defaultValue)
{
+ Message message = context.getProperty(Message.class.getName(),
Message.class);
+ Object value = message == null ? null :
message.getContextualProperty(key);
+ 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 " + key + ":
" + value);
+ }
+ }
+ return defaultValue;
+ }
+
protected List<Object> readCollection(MessageReader reader,
Context context,
ArrayTypeInfo arrayTypeInfo,
@@ -167,7 +234,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 +274,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..6ec949a8e9c 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,82 @@ 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 testMaxTotalArrayElementsPerMessage() throws Exception {
+ // the total limit applies to 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 {
+ // the array size limit applies to each array separately
+ 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>"));
+ assertEquals(10, readIntArray(context, "xsd:int[10]", "").length);
+ assertThrows(DatabindingException.class, () -> readIntArray(context,
"xsd:int[11]", ""));
+
+ message.put(SoapArrayType.MAX_ARRAY_ELEMENTS, -1);
+ message.put(SoapArrayType.MAX_TOTAL_ARRAY_ELEMENTS, -1);
+ Context unlimited = getContext();
+ unlimited.setProperty(Message.class.getName(), message);
+ assertEquals(2000000, readIntArray(unlimited, "xsd:int[2000000]",
"").length);
+ }
+
+ @Test
+ public void testMaxTotalArrayElementsFromMessage() throws Exception {
+ Message message = new MessageImpl();
+ message.put(SoapArrayType.MAX_TOTAL_ARRAY_ELEMENTS, 15);
+ Context context = getContext();
+ context.setProperty(Message.class.getName(), message);
+ assertEquals(10, readIntArray(context, "xsd:int[10]", "").length);
+ assertEquals(5, readIntArray(context, "xsd:int[5]", "").length);
+ assertThrows(DatabindingException.class, () -> readIntArray(context,
"xsd:int[1]", ""));
+ }
+
+ @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"));
}