This is an automated email from the ASF dual-hosted git repository.
coheigea pushed a commit to branch 4.1.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git
The following commit(s) were added to refs/heads/4.1.x-fixes by this push:
new 9fe016cac94 Limit the nesting depth of values read by the CORBA
binding (#3733)
9fe016cac94 is described below
commit 9fe016cac94603f7e25d4ec303b90541e60bb42b
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Tue Nov 17 17:33:06 2026 +0000
Limit the nesting depth of values read by the CORBA binding (#3733)
* Limit the nesting depth of values read by the CORBA binding
* Addressing review comments
(cherry picked from commit 79386735a355b3e2f1d434c1061417768636d23e)
---
.../binding/corba/runtime/CorbaObjectReader.java | 36 +++++++
.../binding/corba/types/CorbaAnyEventProducer.java | 3 +-
.../cxf/binding/corba/types/CorbaAnyHandler.java | 13 +++
.../corba/runtime/CorbaObjectReaderTest.java | 16 +++
.../corba/types/CorbaAnyEventProducerTest.java | 114 +++++++++++++++++++++
5 files changed, 181 insertions(+), 1 deletion(-)
diff --git
a/rt/bindings/corba/src/main/java/org/apache/cxf/binding/corba/runtime/CorbaObjectReader.java
b/rt/bindings/corba/src/main/java/org/apache/cxf/binding/corba/runtime/CorbaObjectReader.java
index c461c02c696..1429c064045 100644
---
a/rt/bindings/corba/src/main/java/org/apache/cxf/binding/corba/runtime/CorbaObjectReader.java
+++
b/rt/bindings/corba/src/main/java/org/apache/cxf/binding/corba/runtime/CorbaObjectReader.java
@@ -52,6 +52,7 @@ import org.apache.cxf.binding.corba.wsdl.Enumerator;
import org.apache.cxf.binding.corba.wsdl.Union;
import org.apache.cxf.binding.corba.wsdl.Unionbranch;
import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.common.util.SystemPropertyAction;
import org.omg.CORBA.Any;
import org.omg.CORBA.NO_IMPLEMENT;
import org.omg.CORBA.TCKind;
@@ -61,16 +62,49 @@ import org.omg.CORBA.portable.InputStream;
public class CorbaObjectReader {
+ /**
+ * The maximum depth to which values can be nested in a CORBA message,
including values nested inside an
+ * Any. The default is 100, and a negative value means no limit. It is set
as a system property.
+ * Each struct, exception, union, sequence, array or Any counts as one
level, so a value of a recursive
+ * type, such as a struct containing a sequence of itself, uses two levels
for each level of recursion.
+ */
+ public static final String MAX_NESTING_DEPTH =
"org.apache.cxf.corba.maxNestingDepth";
+
private static final Logger LOG =
LogUtils.getL7dLogger(CorbaObjectReader.class);
private static final int OCTET_CHUNK_SIZE = 64 * 1024;
+ private static final int MAX_NESTING_DEPTH_VAL =
SystemPropertyAction.getInteger(MAX_NESTING_DEPTH, 100);
private InputStream stream;
+ private int depth;
public CorbaObjectReader(InputStream inStream) {
+ this(inStream, 0);
+ }
+
+ /**
+ * @param inStream the stream to read from
+ * @param depth the depth at which the data in the stream is nested, for
the contents of an Any
+ */
+ public CorbaObjectReader(InputStream inStream, int depth) {
stream = inStream;
+ this.depth = depth;
}
public void read(CorbaObjectHandler obj) throws CorbaBindingException {
+ // the nesting depth is controlled by the sender, so limit it rather
than recursing without bound
+ if (MAX_NESTING_DEPTH_VAL >= 0 && depth >= MAX_NESTING_DEPTH_VAL) {
+ throw new CorbaBindingException("The maximum nesting depth of " +
MAX_NESTING_DEPTH_VAL
+ + " has been exceeded");
+ }
+ depth++;
+ try {
+ readValue(obj);
+ } finally {
+ depth--;
+ }
+ }
+
+ private void readValue(CorbaObjectHandler obj) throws
CorbaBindingException {
switch (obj.getTypeCode().kind().value()) {
case TCKind._tk_boolean:
((CorbaPrimitiveHandler)obj).setValue(this.readBoolean());
@@ -118,6 +152,8 @@ public class CorbaObjectReader {
break;
case TCKind._tk_any:
((CorbaAnyHandler)obj).setValue(this.readAny());
+ // the contents of the Any are read later, starting from this depth
+ ((CorbaAnyHandler)obj).setNestingDepth(depth);
break;
// Now for the complex types
diff --git
a/rt/bindings/corba/src/main/java/org/apache/cxf/binding/corba/types/CorbaAnyEventProducer.java
b/rt/bindings/corba/src/main/java/org/apache/cxf/binding/corba/types/CorbaAnyEventProducer.java
index 6f33b19566f..8ddca3328ec 100644
---
a/rt/bindings/corba/src/main/java/org/apache/cxf/binding/corba/types/CorbaAnyEventProducer.java
+++
b/rt/bindings/corba/src/main/java/org/apache/cxf/binding/corba/types/CorbaAnyEventProducer.java
@@ -121,7 +121,8 @@ public class CorbaAnyEventProducer extends
AbstractStartEndEventProducer {
}
InputStream is = a.create_input_stream();
- CorbaObjectReader reader = new CorbaObjectReader(is);
+ // carry on counting the nesting depth from the Any, so that nested
Anys can't recurse without bound
+ CorbaObjectReader reader = new CorbaObjectReader(is,
handler.getNestingDepth());
reader.read(result);
return result;
diff --git
a/rt/bindings/corba/src/main/java/org/apache/cxf/binding/corba/types/CorbaAnyHandler.java
b/rt/bindings/corba/src/main/java/org/apache/cxf/binding/corba/types/CorbaAnyHandler.java
index f0e18bbf882..6a5972f6e99 100644
---
a/rt/bindings/corba/src/main/java/org/apache/cxf/binding/corba/types/CorbaAnyHandler.java
+++
b/rt/bindings/corba/src/main/java/org/apache/cxf/binding/corba/types/CorbaAnyHandler.java
@@ -31,6 +31,7 @@ public class CorbaAnyHandler extends CorbaObjectHandler {
private Any value;
private CorbaObjectHandler containedType;
private CorbaTypeMap typeMap;
+ private int nestingDepth;
public CorbaAnyHandler(QName anyName,
QName anyIdlType,
@@ -68,4 +69,16 @@ public class CorbaAnyHandler extends CorbaObjectHandler {
public void setValue(Any v) throws CorbaBindingException {
value = v;
}
+
+ /**
+ * The depth at which this Any is nested in the message, so that the
nesting depth of its contents can
+ * be limited when they are read.
+ */
+ public int getNestingDepth() {
+ return nestingDepth;
+ }
+
+ public void setNestingDepth(int depth) {
+ nestingDepth = depth;
+ }
}
diff --git
a/rt/bindings/corba/src/test/java/org/apache/cxf/binding/corba/runtime/CorbaObjectReaderTest.java
b/rt/bindings/corba/src/test/java/org/apache/cxf/binding/corba/runtime/CorbaObjectReaderTest.java
index be42ecf7bc4..40950b9a6cb 100644
---
a/rt/bindings/corba/src/test/java/org/apache/cxf/binding/corba/runtime/CorbaObjectReaderTest.java
+++
b/rt/bindings/corba/src/test/java/org/apache/cxf/binding/corba/runtime/CorbaObjectReaderTest.java
@@ -650,4 +650,20 @@ public class CorbaObjectReaderTest {
TypeCode seqTC = orb.create_sequence_tc(bound,
orb.get_primitive_tc(TCKind.tk_octet));
return new CorbaOctetSequenceHandler(new QName("Seq"), seqIdlType,
seqTC, seqType);
}
+
+ @Test
+ public void testReadNestingDepth() {
+ OutputStream oStream = orb.create_output_stream();
+ oStream.write_long(1);
+ oStream.write_long(2);
+ InputStream iStream = oStream.create_input_stream();
+ QName longIdlType = new QName(CorbaConstants.NU_WSDL_CORBA, "long",
CorbaConstants.NP_WSDL_CORBA);
+
+ // a value at the maximum depth of 100 can be read, but not one nested
any deeper
+ new CorbaObjectReader(iStream, 99).read(new CorbaPrimitiveHandler(new
QName("item"), longIdlType,
+
orb.get_primitive_tc(TCKind.tk_long), null));
+ CorbaObjectReader reader = new CorbaObjectReader(iStream, 100);
+ assertThrows(CorbaBindingException.class, () -> reader.read(
+ new CorbaPrimitiveHandler(new QName("item"), longIdlType,
orb.get_primitive_tc(TCKind.tk_long), null)));
+ }
}
diff --git
a/rt/bindings/corba/src/test/java/org/apache/cxf/binding/corba/types/CorbaAnyEventProducerTest.java
b/rt/bindings/corba/src/test/java/org/apache/cxf/binding/corba/types/CorbaAnyEventProducerTest.java
new file mode 100644
index 00000000000..7f2888513ef
--- /dev/null
+++
b/rt/bindings/corba/src/test/java/org/apache/cxf/binding/corba/types/CorbaAnyEventProducerTest.java
@@ -0,0 +1,114 @@
+/**
+ * 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.cxf.binding.corba.types;
+
+import javax.xml.namespace.QName;
+
+import org.apache.cxf.binding.corba.CorbaBindingException;
+import org.apache.cxf.binding.corba.runtime.CorbaObjectReader;
+import org.apache.cxf.binding.corba.wsdl.CorbaConstants;
+import org.omg.CORBA.Any;
+import org.omg.CORBA.ORB;
+import org.omg.CORBA.TCKind;
+import org.omg.CORBA.portable.OutputStream;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThrows;
+
+public class CorbaAnyEventProducerTest {
+
+ private ORB orb;
+
+ @Before
+ public void setUp() throws Exception {
+ java.util.Properties props = System.getProperties();
+ props.put("yoko.orb.id", "CXF-CORBA-Binding");
+ orb = ORB.init(new String[0], props);
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ if (orb != null) {
+ try {
+ orb.destroy();
+ } catch (Exception ex) {
+ // ignore
+ }
+ }
+ }
+
+ @Test
+ public void testNestedAny() {
+ CorbaAnyEventProducer producer = new
CorbaAnyEventProducer(createAnyHandler(nestedAny(10)), null, orb);
+ assertNotNull(producer.getAttributes());
+ }
+
+ @Test
+ public void testNestedAnyTooDeep() {
+ // each nested Any is read when the producer for the Any containing it
is created, so the depth
+ // must be limited across them rather than recursing once per level
+ CorbaAnyHandler handler = createAnyHandler(nestedAny(150));
+ assertThrows(CorbaBindingException.class, () -> new
CorbaAnyEventProducer(handler, null, orb));
+ }
+
+ @Test
+ public void testNestedAnyFromMessage() {
+ // marshal the nested Any and read it back through the reader, as it
would arrive in a message
+ CorbaAnyHandler handler = readAnyFromMessage(nestedAny(10));
+ assertEquals(1, handler.getNestingDepth());
+ assertNotNull(new CorbaAnyEventProducer(handler, null,
orb).getAttributes());
+ }
+
+ @Test
+ public void testNestedAnyFromMessageTooDeep() {
+ CorbaAnyHandler handler = readAnyFromMessage(nestedAny(150));
+ assertThrows(CorbaBindingException.class, () -> new
CorbaAnyEventProducer(handler, null, orb));
+ }
+
+ private CorbaAnyHandler readAnyFromMessage(Any value) {
+ OutputStream out = orb.create_output_stream();
+ out.write_any(value);
+ CorbaAnyHandler handler = createAnyHandler(null);
+ new CorbaObjectReader(out.create_input_stream()).read(handler);
+ return handler;
+ }
+
+ private CorbaAnyHandler createAnyHandler(Any value) {
+ CorbaAnyHandler handler = new CorbaAnyHandler(new QName("any"),
CorbaConstants.NT_CORBA_ANY,
+
orb.get_primitive_tc(TCKind.tk_any), null);
+ handler.setValue(value);
+ return handler;
+ }
+
+ private Any nestedAny(int depth) {
+ Any any = orb.create_any();
+ any.insert_long(42);
+ for (int i = 1; i < depth; i++) {
+ Any outer = orb.create_any();
+ outer.insert_any(any);
+ any = outer;
+ }
+ return any;
+ }
+}