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 1fb1cdd3c8c9feff951e00e303624ce454fcce94 Author: Aurélien Pupier <[email protected]> AuthorDate: Mon Nov 23 10:59:51 2026 +0100 CAMEL-27428: Address review of the flaky XsdJsonSecurityTest fix - Add nestingOnASmallStackIsRefused: it runs the JSON to XML conversion on a thread with a 128k stack, so that the StackOverflowError branch is covered in every build, not only on a thread that happens to have a small stack. Without the fix it fails with a StackOverflowError under the default JVM settings. The classes are loaded with a shallow conversion first, so that the class loading does not overflow the small stack. - nestingDepthIsLimited asserts an IllegalArgumentException, so that an Error escaping from the data format is detected. - Clarify the message: "The JSON is nested too deep for the thread stack (the configured maximum is 512 levels)". Co-authored-by: IBM Bob 2.2.1 Signed-off-by: Aurélien Pupier <[email protected]> --- .../apache/camel/dataformat/xsdjson/JsonToXml.java | 2 +- .../dataformat/xsdjson/XsdJsonSecurityTest.java | 41 +++++++++++++++++++--- 2 files changed, 37 insertions(+), 6 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 9dee53baacd4..5b3da8c08830 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 @@ -74,7 +74,7 @@ final class JsonToXml { } 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 + "The JSON is nested too deep for the thread stack (the configured maximum is " + XmlToJson.MAX_DEPTH + " levels)", e); } 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 92d6869220ce..9b3ce7c7d0f6 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 @@ -21,6 +21,7 @@ import java.nio.file.Path; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -85,17 +86,47 @@ class XsdJsonSecurityTest extends XsdJsonTestSupport { assertThatThrownBy(() -> toJson(df, xml)) .satisfies(e -> assertThat(e.getMessage()).containsAnyOf("depth", "nested deeper than 512")); + Map<String, Object> tree = nestedTree(600); + // the stack of the thread may be exhausted before the limit of the data format + assertThatThrownBy(() -> toXml(df, tree)) + .isInstanceOf(IllegalArgumentException.class) + .satisfies(e -> assertThat(e.getMessage()).containsAnyOf("The JSON is nested deeper than 512", + "The JSON is nested too deep for the thread stack")); + } + + @Test + void nestingOnASmallStackIsRefused() throws Exception { + XsdJsonDataFormat df = dataFormat("classpath:xsdjson/shop.xsd", d -> d.setElement("tree")); + Map<String, Object> tree = nestedTree(600); + // load the classes first, so that the stack of the small thread is not exhausted by the class loading + toXml(df, nestedTree(2)); + + AtomicReference<Throwable> thrown = new AtomicReference<>(); + Thread thread = new Thread(null, () -> { + try { + toXml(df, tree); + } catch (Throwable e) { + thrown.set(e); + } + }, "small-stack", 128 * 1024); + thread.start(); + thread.join(); + + // an IllegalArgumentException, never a StackOverflowError, whichever limit is reached first + assertThat(thrown.get()).isInstanceOf(IllegalArgumentException.class); + assertThat(thrown.get().getMessage()).containsAnyOf("The JSON is nested deeper than 512", + "The JSON is nested too deep for the thread stack"); + } + + private static Map<String, Object> nestedTree(int depth) { Map<String, Object> tree = new LinkedHashMap<>(); Map<String, Object> current = tree; - for (int i = 0; i < 600; i++) { + for (int i = 0; i < depth; i++) { Map<String, Object> node = new LinkedHashMap<>(); current.put("node", List.of(node)); current = node; } - // 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")); + return tree; } @Test
