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

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

commit bc0daa811eb7da06c22cec9bebc4d089802ddf29
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Tue Nov 17 14:41:27 2026 +0000

    Limit the nesting depth of values read by the CORBA binding
---
 .../binding/corba/runtime/CorbaObjectReader.java   | 34 +++++++++
 .../binding/corba/types/CorbaAnyEventProducer.java |  3 +-
 .../cxf/binding/corba/types/CorbaAnyHandler.java   | 13 ++++
 .../corba/runtime/CorbaObjectReaderTest.java       | 16 ++++
 .../corba/types/CorbaAnyEventProducerTest.java     | 89 ++++++++++++++++++++++
 5 files changed, 154 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..91e81d7d918 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,47 @@ 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.
+     */
+    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 +150,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..d11acb5a813
--- /dev/null
+++ 
b/rt/bindings/corba/src/test/java/org/apache/cxf/binding/corba/types/CorbaAnyEventProducerTest.java
@@ -0,0 +1,89 @@
+/**
+ * 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.wsdl.CorbaConstants;
+import org.omg.CORBA.Any;
+import org.omg.CORBA.ORB;
+import org.omg.CORBA.TCKind;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+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));
+    }
+
+    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;
+    }
+}

Reply via email to