Skip to content

Add sensible defaults to StaxUtils's MAX_ELEMENT_COUNT and MAX_XML_CHARACTERS - #3463

Merged
reta merged 3 commits into
apache:mainfrom
reta:limit.stax
Sep 14, 2026
Merged

reta merged 3 commits into
apache:mainfrom
reta:limit.stax

Conversation

@reta

@reta reta commented Sep 13, 2026

Copy link
Copy Markdown
Member

Add sensible defaults to StaxUtils's MAX_ELEMENT_COUNT and MAX_XML_CHARACTERS

@reta
reta requested a review from coheigea September 13, 2026 15:41
Comment thread core/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
@coheigea

Copy link
Copy Markdown
Contributor

@reta Some comments from Claude:

1. maxElementCount default overflows (blocking)

getLong(MAX_ELEMENT_COUNT, Math.max(100 * MAX_CHILD_ELEMENTS_VAL, 50000));

MAX_CHILD_ELEMENTS_VAL is an int, so 100 * ... is int arithmetic — the widening to long only happens after Math.max(int,int) has returned. It overflows once maxChildElements > 21_474_836:

maxChildElements resulting MAX_ELEMENT_COUNT_VAL
50 000 (default) 5 000 000
30 000 000 50 000
Integer.MAX_VALUE 50 000 (100 * Integer.MAX_VALUE wraps to -100, so the floor wins)

That inverts the invariant the comment states — the total element count ends up well below the per-parent child count. It also hits precisely the users most likely to notice: getInteger/getLong coerce negatives back to the default, so there is no -1 escape hatch and raising maxChildElements is the only way to relax that limit today. Anyone doing so would silently pick up a 50 000 total-element cap on upgrade.

One character:

getLong(MAX_ELEMENT_COUNT, Math.max(100L * MAX_CHILD_ELEMENTS_VAL, 50000L));

2. Derive maxXMLCharacters from MAX_TEXT_LENGTH_VAL

Your rationale above is "2x of MAX_TEXT_LENGTH" and the new code comment says the two "should be aligned", but the code hardcodes 256Mb — so the alignment breaks as soon as anyone sets org.apache.cxf.stax.maxTextLength. Raise it to 512Mb and the document-wide character cap is smaller than a single permitted text segment: a contradictory config that worked fine before this change.

private static final long MAX_XML_CHARS_VAL =
        getLong(MAX_XML_CHARACTERS, 2L * MAX_TEXT_LENGTH_VAL); // 256Mb by default

Same default (2 x 128Mb), stays correct under reconfiguration, and makes the comment true rather than aspirational. If you'd rather keep the literal, at least 256L * 1024 * 1024 so a future bump doesn't overflow.

@reta

reta commented Sep 14, 2026

Copy link
Copy Markdown
Member Author

Thanks @coheigea

  1. maxElementCount default overflows (blocking)

I will widen it to long, thanks!

  1. Derive maxXMLCharacters from MAX_TEXT_LENGTH_VAL

The straight derivation does not make sense here to be fair. MAX_TEXT_LENGTH_VAL is really large by default, but we should not use multipliers there (ideally, I would think of MAX_TEXT_LENGTH_VAL as 8k and MAX_XML_CHARACTERS as 32Mb fe), so 256Mb is just reasonably large value that is not in conflict with MAX_TEXT_LENGTH_VAL default.

Comment thread core/src/main/java/org/apache/cxf/staxutils/StaxUtils.java Outdated
Comment thread core/src/main/java/org/apache/cxf/staxutils/StaxUtils.java Outdated
Comment thread core/src/main/java/org/apache/cxf/staxutils/StaxUtils.java Outdated
@reta
reta merged commit 39a6ed6 into apache:main Sep 14, 2026
5 checks passed
reta added a commit that referenced this pull request Sep 15, 2026
…ARACTERS (#3463)

* Add sensible defaults to StaxUtils's MAX_ELEMENT_COUNT and MAX_XML_CHARACTERS

* Apply suggestion from @reta

* Apply suggestion from @reta

(cherry picked from commit 39a6ed6)
reta added a commit that referenced this pull request Sep 15, 2026
…ARACTERS (#3463)

* Add sensible defaults to StaxUtils's MAX_ELEMENT_COUNT and MAX_XML_CHARACTERS

* Apply suggestion from @reta

* Apply suggestion from @reta

(cherry picked from commit 39a6ed6)
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.

2 participants