diff --git a/core/src/main/java/org/codelibs/saml2/core/util/Util.java b/core/src/main/java/org/codelibs/saml2/core/util/Util.java index 7f2268f..671cdcd 100644 --- a/core/src/main/java/org/codelibs/saml2/core/util/Util.java +++ b/core/src/main/java/org/codelibs/saml2/core/util/Util.java @@ -360,6 +360,49 @@ public static Document convertStringToDocument(final String xmlStr) { } } + /** The deepest element nesting accepted in a parsed document. A SAML message is a few dozen levels deep at most. */ + private static final int MAX_XML_DEPTH = 100; + + /** + * Refuses a document whose elements are nested deeper than {@link #MAX_XML_DEPTH}. + * + *
The DOM is walked recursively later on ({@code Element#normalize}, {@code importNode}, the + * canonicalization of a signature), and the document is parsed before any signature or schema check, + * so a few hundred bytes of nested elements sent to an anonymous endpoint would end in a + * {@link StackOverflowError} that no {@code catch (Exception)} sees. The check walks the tree without + * recursion.
+ * + * @param doc the parsed document + * @throws XMLParsingException if the document is nested too deeply + */ + private static void checkNestingDepth(final Document doc) { + final Element root = doc.getDocumentElement(); + if (root == null) { + return; + } + Node node = root; + int depth = 1; + while (true) { + if (depth > MAX_XML_DEPTH && node.getNodeType() == Node.ELEMENT_NODE) { + throw new XMLParsingException("The document is nested more than " + MAX_XML_DEPTH + " levels deep.", null); + } + final Node child = node.getFirstChild(); + if (child != null) { + node = child; + depth++; + continue; + } + while (node != root && node.getNextSibling() == null) { + node = node.getParentNode(); + depth--; + } + if (node == root) { + return; + } + node = node.getNextSibling(); + } + } + /** * Parse an XML from input source to a Document object * @@ -400,6 +443,7 @@ public static Document parseXML(final InputSource inputSource) { final XMLErrorAccumulatorHandler errorAccumulator = new XMLErrorAccumulatorHandler(); builder.setErrorHandler(errorAccumulator); final Document doc = builder.parse(inputSource); + checkNestingDepth(doc); // Loop through the doc and tag every element with an ID attribute // as an XML ID node. diff --git a/core/src/test/java/org/codelibs/saml2/core/test/logout/LogoutRequestTest.java b/core/src/test/java/org/codelibs/saml2/core/test/logout/LogoutRequestTest.java index 4dc96f7..69add84 100644 --- a/core/src/test/java/org/codelibs/saml2/core/test/logout/LogoutRequestTest.java +++ b/core/src/test/java/org/codelibs/saml2/core/test/logout/LogoutRequestTest.java @@ -1216,4 +1216,30 @@ protected String postProcessXml(String logoutRequestXml, LogoutRequestParams par }; assertEquals("changed", logoutRequest.getLogoutRequestXml()); } + + /** + * Tests the constructor + * Case: a deflated request whose elements are nested thousands of levels deep, which used to + * overflow the stack in Element#normalize before any signature or schema check + */ + @Test + public void testDeeplyNestedRequestIsRefusedWithoutAnError() throws Exception { + Saml2Settings settings = new SettingsBuilder().fromFile("config/config.min.properties").build(); + settings.setStrict(true); + int depth = 20000; + StringBuilder xml = new StringBuilder("