Skip to content

fix: refuse an assertion whose decryption overflows the stack - #20

Merged
marevol merged 1 commit into
fix/bound-schema-errors-and-release-inflaterfrom
fix/limit-decrypted-xml-depth
Nov 16, 2026
Merged

marevol merged 1 commit into
fix/bound-schema-errors-and-release-inflaterfrom
fix/limit-decrypted-xml-depth

Conversation

@marevol

@marevol marevol commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

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

Summary

#18 limits the nesting depth of every document Util.parseXML reads. The plaintext of an EncryptedAssertion is a second way into the DOM: the XML security library puts it there when the element is decrypted, and anyone can encrypt for the certificate of a service provider.

  • A plaintext nested more than 100 levels deep (but not very deep) is already refused: the decrypted assertion is read back through parseXML in SamlResponse#decryptAssertion. This pull request adds the missing test for that.
  • A plaintext nested far deeper overflows the stack inside the decryption itself, before parseXML can look at it, and the StackOverflowError left the library through catch (Exception). Util.decryptElement and Util.decryptUsingHsm now catch StackOverflowError next to Exception. The element stays encrypted, and the response is refused like any other whose decryption failed (ValidationException: no Assertion element found).

Catching an Error is deliberate and limited to this one call. The depth cannot be known before the decryption, because it is the decrypted text that is deep.

Not covered: the EncryptedID of an assertion and of a LogoutRequest is decrypted through the same two methods, so it gets the catch, but its plaintext is not read back through parseXML. A plaintext nested deep enough to be unwanted but not deep enough to overflow the decryption is therefore not limited there. That was not tested.

Test

  • AuthnResponseTest#testEncryptedAssertionNestedTooDeeplyIsRefused: a helper encrypts an assertion for the service provider certificate. At depth 50 the response is accepted (control), at 300 the constructor throws XMLParsingException, and at 50,000 it throws a SAMLException instead of overflowing the stack. Without the catch the last case fails with StackOverflowError.
  • The core module's 431 tests and the toolkit module's 93 tests pass.

The plaintext of an EncryptedAssertion is put into the DOM by the XML
security library when the element is decrypted, and anyone can encrypt
for the certificate of a service provider. parseXML limits the nesting
depth of what it parses, so a plaintext nested more than 100 levels deep
is refused when the decrypted assertion is read back. A plaintext nested
far deeper overflowed the stack inside the decryption itself, and the
StackOverflowError left the library through catch (Exception).

Catch StackOverflowError next to Exception in decryptElement and
decryptUsingHsm. The element stays encrypted, and the response is
refused like any other whose decryption failed.
@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 f541736 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