Skip to content

fix: refuse a document whose elements are nested too deeply - #18

Merged
marevol merged 1 commit into
fix/logout-message-log-and-replay-keyfrom
fix/limit-xml-nesting-depth
Nov 16, 2026
Merged

marevol merged 1 commit into
fix/logout-message-log-and-replay-keyfrom
fix/limit-xml-nesting-depth

Conversation

@marevol

@marevol marevol commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Stacked on #17 (the base is that branch; retarget to main once #17 is merged).

Summary

A SAML message is parsed before its signature or its schema is checked, and the DOM is then walked recursively: Element#normalize in the SamlResponse and LogoutRequest constructors, importNode, and the canonicalization of a signature. The JDK parser has no depth limit by default, and a document nested thousands of levels deep fits in a short deflated request, so the walk ended in a StackOverflowError. catch (Exception) does not see an Error, so it left the library.

Util.parseXML now walks the parsed tree without recursion and throws XMLParsingException for a document nested more than 100 levels deep. A SAML message is a few dozen levels deep at most. Util.loadXML answers such a document with null, as it does for any other document it cannot parse. The check is done on the parsed tree rather than through a parser property, so it does not depend on which XML parser the application puts on the class path.

Test

  • UtilsTest#testLoadXMLRefusesDeeplyNestedElements: depth 100 is accepted, 101 and 50,000 are refused, and 5,000 siblings are not counted as nesting.
  • UtilsTest#testParseXMLReportsDeeplyNestedElements: the refusal is an XMLParsingException.
  • LogoutRequestTest#testDeeplyNestedRequestIsRefusedWithoutAnError: a deflated request under 1,000 characters with 20,000 nested elements no longer overflows the stack; the request is invalid. Without the change this test fails with StackOverflowError.
  • The core module's 428 tests pass.

A SAML message is parsed before its signature or schema is checked, and
the DOM is then walked recursively (Element#normalize in the Response
and LogoutRequest constructors, importNode, the canonicalization of a
signature). A document nested thousands of levels deep fits in a short
deflated request, and it ended in a StackOverflowError that
catch (Exception) does not see, so it left the library.

Util.parseXML now walks the parsed tree without recursion and throws
XMLParsingException for a document nested more than 100 levels deep. A
SAML message is a few dozen levels deep at most. loadXML answers such a
document with null, as it does for any other document it cannot parse.
@marevol
marevol added this pull request to stack #21 November 16, 2026 14:18
@marevol marevol added this to the 3.1.3 milestone Nov 16, 2026
@marevol marevol self-assigned this Nov 16, 2026
@marevol
marevol merged commit d6a3325 into main Nov 16, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant