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 c26d11a6234 Apply the XML element limits to FastInfoset messages
(#3716)
c26d11a6234 is described below
commit c26d11a62344d0fc870f0c419f5b52a83df09eb4
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Fri Nov 13 14:28:56 2026 +0000
Apply the XML element limits to FastInfoset messages (#3716)
(cherry picked from commit bb62e2119912a407af8fb81af71682e134ec0249)
---
.../cxf/interceptor/FIStaxInInterceptor.java | 13 +-
.../java/org/apache/cxf/staxutils/StaxUtils.java | 11 +-
.../cxf/interceptor/FIStaxInInterceptorTest.java | 133 +++++++++++++++++++++
3 files changed, 153 insertions(+), 4 deletions(-)
diff --git
a/core/src/main/java/org/apache/cxf/interceptor/FIStaxInInterceptor.java
b/core/src/main/java/org/apache/cxf/interceptor/FIStaxInInterceptor.java
index 4337e519d6e..501eda68f3c 100644
--- a/core/src/main/java/org/apache/cxf/interceptor/FIStaxInInterceptor.java
+++ b/core/src/main/java/org/apache/cxf/interceptor/FIStaxInInterceptor.java
@@ -22,6 +22,7 @@ package org.apache.cxf.interceptor;
import java.io.InputStream;
+import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import com.sun.xml.fastinfoset.stax.StAXDocumentParser;
@@ -31,6 +32,7 @@ import org.apache.cxf.message.Message;
import org.apache.cxf.message.MessageUtils;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
+import org.apache.cxf.staxutils.StaxUtils;
/**
* Creates an XMLStreamReader from the InputStream on the Message.
@@ -51,12 +53,17 @@ public class FIStaxInInterceptor extends
AbstractPhaseInterceptor<Message> {
return
Boolean.TRUE.equals(message.containsKey(Message.REQUESTOR_ROLE));
}
- private XMLStreamReader getParser(InputStream in) {
+ private XMLStreamReader getParser(InputStream in, Message message) {
StAXDocumentParser parser = new StAXDocumentParser(in);
parser.setStringInterning(true);
parser.setForceStreamClose(true);
parser.setInputStream(in);
- return parser;
+ try {
+ // apply the same element limits as for XML
+ return StaxUtils.configureReader(parser, message);
+ } catch (XMLStreamException e) {
+ throw new Fault(e);
+ }
}
public void handleMessage(Message message) {
@@ -69,7 +76,7 @@ public class FIStaxInInterceptor extends
AbstractPhaseInterceptor<Message> {
if (ct != null && ct.indexOf("fastinfoset") != -1
&& message.getContent(InputStream.class) != null
&& message.getContent(XMLStreamReader.class) == null) {
- message.setContent(XMLStreamReader.class,
getParser(message.getContent(InputStream.class)));
+ message.setContent(XMLStreamReader.class,
getParser(message.getContent(InputStream.class), message));
//add the StaxInEndingInterceptor which will close the reader
message.getInterceptorChain().add(StaxInEndingInterceptor.INSTANCE);
diff --git a/core/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
b/core/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
index ce267af14b1..ff5adcd6609 100644
--- a/core/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
+++ b/core/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
@@ -2118,7 +2118,7 @@ public final class StaxUtils {
}
public static boolean isSecureReader(XMLStreamReader reader, Message
message) {
- if (reader instanceof DocumentDepthProperties) {
+ if (reader instanceof DepthRestrictingStreamReader) {
return true;
}
try {
@@ -2139,6 +2139,15 @@ public final class StaxUtils {
Integer messageMaxTextLength = PropertyUtils.getInteger(message,
MAX_TEXT_LENGTH);
Long messageMaxElementCount = PropertyUtils.getLong(message,
MAX_ELEMENT_COUNT);
Long messageMaxXMLCharacters = PropertyUtils.getLong(message,
MAX_XML_CHARACTERS);
+ if (!isSecureReader(xreader, message)) {
+ // A reader that doesn't support the Woodstox limits, such as a
FastInfoset parser, doesn't get
+ // the default limits from the input factory, so enforce the
element limits with a wrapper
+ long maxElementCount = messageMaxElementCount != null ?
messageMaxElementCount : MAX_ELEMENT_COUNT_VAL;
+ return new DepthRestrictingStreamReader(xreader, new
DocumentDepthProperties(
+ (int)Math.min(maxElementCount, Integer.MAX_VALUE),
+ messageMaxElementDepth != null ? messageMaxElementDepth :
MAX_ELEMENT_DEPTH_VAL,
+ messageMaxChildElements != null ? messageMaxChildElements :
MAX_CHILD_ELEMENTS_VAL));
+ }
return configureReader(xreader, messageMaxChildElements,
messageMaxElementDepth,
messageMaxAttributeCount,
messageMaxAttributeSize, messageMaxTextLength,
messageMaxElementCount,
messageMaxXMLCharacters);
diff --git
a/core/src/test/java/org/apache/cxf/interceptor/FIStaxInInterceptorTest.java
b/core/src/test/java/org/apache/cxf/interceptor/FIStaxInInterceptorTest.java
new file mode 100644
index 00000000000..ad56a1937d2
--- /dev/null
+++ b/core/src/test/java/org/apache/cxf/interceptor/FIStaxInInterceptorTest.java
@@ -0,0 +1,133 @@
+/**
+ * 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.interceptor;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+
+import javax.xml.stream.XMLStreamReader;
+
+import com.sun.xml.fastinfoset.stax.StAXDocumentSerializer;
+
+import org.apache.cxf.message.Exchange;
+import org.apache.cxf.message.ExchangeImpl;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
+import org.apache.cxf.phase.PhaseInterceptorChain;
+import org.apache.cxf.staxutils.StaxUtils;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThrows;
+
+public class FIStaxInInterceptorTest {
+
+ @Test
+ public void testReadDocument() throws Exception {
+ XMLStreamReader reader = parse(nested(10), null, null);
+ int elements = 0;
+ while (reader.hasNext()) {
+ if (reader.next() == XMLStreamReader.START_ELEMENT) {
+ elements++;
+ }
+ }
+ assertEquals(10, elements);
+ }
+
+ @Test
+ public void testDepthLimit() throws Exception {
+ // the default depth limit is 100
+ readAll(parse(nested(99), null, null));
+ assertThrows(RuntimeException.class, () -> readAll(parse(nested(100),
null, null)));
+ }
+
+ @Test
+ public void testDeepNestingRejected() throws Exception {
+ XMLStreamReader reader = parse(nested(100_000), null, null);
+ assertThrows(RuntimeException.class, () -> readAll(reader));
+ }
+
+ @Test
+ public void testChildElementLimit() throws Exception {
+ // the default limit on child elements is 50000
+ readAll(parse(children(49_999), null, null));
+ assertThrows(RuntimeException.class, () ->
readAll(parse(children(50_000), null, null)));
+ }
+
+ @Test
+ public void testLimitFromMessage() throws Exception {
+ readAll(parse(nested(150), StaxUtils.MAX_ELEMENT_DEPTH, "200"));
+ assertThrows(RuntimeException.class, () -> readAll(parse(nested(20),
StaxUtils.MAX_ELEMENT_DEPTH, "10")));
+ }
+
+ private static XMLStreamReader parse(byte[] fi, String key, String value) {
+ Message message = new MessageImpl();
+ Exchange exchange = new ExchangeImpl();
+ exchange.setInMessage(message);
+ message.setExchange(exchange);
+ message.setInterceptorChain(new PhaseInterceptorChain(new
java.util.TreeSet<>()));
+ message.put(Message.CONTENT_TYPE, "application/fastinfoset");
+ if (key != null) {
+ message.put(key, value);
+ }
+ message.setContent(InputStream.class, new ByteArrayInputStream(fi));
+ new FIStaxInInterceptor().handleMessage(message);
+ XMLStreamReader reader = message.getContent(XMLStreamReader.class);
+ assertNotNull(reader);
+ return reader;
+ }
+
+ private static void readAll(XMLStreamReader reader) throws Exception {
+ while (reader.hasNext()) {
+ reader.next();
+ }
+ }
+
+ private static byte[] nested(int depth) throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ StAXDocumentSerializer writer = new StAXDocumentSerializer(out);
+ writer.writeStartDocument();
+ for (int i = 0; i < depth; i++) {
+ writer.writeStartElement("a");
+ }
+ for (int i = 0; i < depth; i++) {
+ writer.writeEndElement();
+ }
+ writer.writeEndDocument();
+ writer.close();
+ return out.toByteArray();
+ }
+
+ private static byte[] children(int count) throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ StAXDocumentSerializer writer = new StAXDocumentSerializer(out);
+ writer.writeStartDocument();
+ writer.writeStartElement("root");
+ for (int i = 0; i < count; i++) {
+ writer.writeEmptyElement("a");
+ }
+ writer.writeEndElement();
+ writer.writeEndDocument();
+ writer.close();
+ return out.toByteArray();
+ }
+}