This is an automated email from the ASF dual-hosted git repository. apupier pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit 216054aa0ea0c3a103048e40a2f994a925a15344 Author: Aurélien Pupier <[email protected]> AuthorDate: Mon Nov 23 10:15:51 2026 +0100 CAMEL-27428: Fix flaky XsdJsonSecurityTest.nestingDepthIsLimited The JSON to XML conversion of JsonToXml recurses through about six frames per nesting level. On a thread with a small stack, 512 levels (the limit of the data format, checked per element) can exhaust the stack first. The StackOverflowError has no message, so the test failed with "Expecting throwable message: null to contain: The JSON is nested deeper than 512". The failure was reproduced without the fix with -Dcamel.surefire.fork.additional-vmargs="-Xint -Xss256k". - JsonToXml.write() turns a StackOverflowError into an IllegalArgumentException, so that deeply nested input from an untrusted sender gets a normal error instead of an Error escaping from the data format. - XsdJsonSecurityTest accepts either the depth limit message or the stack message for the JSON to XML direction, as it already does for the XML to JSON direction (the XML parser of the JDK may have a lower limit). Co-authored-by: IBM Bob 2.2.1 Signed-off-by: Aurélien Pupier <[email protected]> --- .../java/org/apache/camel/dataformat/xsdjson/JsonToXml.java | 10 +++++++++- .../apache/camel/dataformat/xsdjson/XsdJsonSecurityTest.java | 5 ++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/components/camel-xsd-json/src/main/java/org/apache/camel/dataformat/xsdjson/JsonToXml.java b/components/camel-xsd-json/src/main/java/org/apache/camel/dataformat/xsdjson/JsonToXml.java index aed6f267fe26..9dee53baacd4 100644 --- a/components/camel-xsd-json/src/main/java/org/apache/camel/dataformat/xsdjson/JsonToXml.java +++ b/components/camel-xsd-json/src/main/java/org/apache/camel/dataformat/xsdjson/JsonToXml.java @@ -69,7 +69,15 @@ final class JsonToXml { "The JSON is null, but the root element " + root.name + " is not nillable"); } } - writeElement(root, value, "/" + root.jsonName(), 0); + try { + writeElement(root, value, "/" + root.jsonName(), 0); + } catch (StackOverflowError e) { + // the depth limit is checked per element, but a small thread stack can be exhausted before that limit + throw new IllegalArgumentException( + "The JSON is nested too deep for the stack of the thread (the limit is " + XmlToJson.MAX_DEPTH + + " levels)", + e); + } } private void writeElement(Element element, Object value, String path, int depth) throws XMLStreamException { diff --git a/components/camel-xsd-json/src/test/java/org/apache/camel/dataformat/xsdjson/XsdJsonSecurityTest.java b/components/camel-xsd-json/src/test/java/org/apache/camel/dataformat/xsdjson/XsdJsonSecurityTest.java index 79594107238d..92d6869220ce 100644 --- a/components/camel-xsd-json/src/test/java/org/apache/camel/dataformat/xsdjson/XsdJsonSecurityTest.java +++ b/components/camel-xsd-json/src/test/java/org/apache/camel/dataformat/xsdjson/XsdJsonSecurityTest.java @@ -92,7 +92,10 @@ class XsdJsonSecurityTest extends XsdJsonTestSupport { current.put("node", List.of(node)); current = node; } - assertThatThrownBy(() -> toXml(df, tree)).hasMessageContaining("The JSON is nested deeper than 512"); + // the stack of the thread may be exhausted before the limit of the data format + assertThatThrownBy(() -> toXml(df, tree)) + .satisfies(e -> assertThat(e.getMessage()).containsAnyOf("The JSON is nested deeper than 512", + "The JSON is nested too deep for the stack")); } @Test
