Skip to content

Enforce maxNestingDepth when reading IonValue - #911

Open
Dhanu-dynamic wants to merge 8 commits into
FasterXML:2.21from
Dhanu-dynamic:2.21/ion-ionvalue-nesting-depth
Open

Dhanu-dynamic wants to merge 8 commits into
FasterXML:2.21from
Dhanu-dynamic:2.21/ion-ionvalue-nesting-depth

Conversation

@Dhanu-dynamic

Copy link
Copy Markdown
Contributor

IonParser.getIonValue(), which getEmbeddedObject() falls through to and IonValueDeserializer calls on the first token, copied the value with IonWriter.writeValue(IonReader). ion-java copies the whole subtree in that one call, so nextToken() and the validateNestingDepth() check from #803 never run, and readValue(..., IonValue.class) or any IonValue-typed property ignores maxNestingDepth. With maxNestingDepth(10) a 20-deep list reads fine; with the default (1000) a 200,000-deep list (400 KB of text) is accepted. This is the caveat noted in the #358 status comment.

_copyValue() now walks containers one level at a time and validates depth on each step in, counting from the parser context so a property value is measured from the document root, the same as nextToken(). Scalars and null containers still go through writeValue(). The walk is iterative, so with the limit raised to Integer.MAX_VALUE the 200,000-deep document still reads without a StackOverflowError. Field names and annotations are kept; the at-limit test compares the result against IonSystem.singleValue().

Tests are in DeeplyNestedIonReadTest; the two over-limit ones fail without the IonParser change. mvn verify in ion: 290 tests, 0 failures. Based on 2.21; 3.1 has the same writeValue(_reader) call, so it should merge forward cleanly. I left out the release-notes entry since there is no PR number yet, and can add it.

IonParser.getIonValue() copied the value with IonWriter.writeValue(IonReader),
which copies the whole subtree without going through nextToken(), so the
depth check from FasterXML#803 did not apply to IonValue binding. Copy containers
one level at a time and validate depth on each step in (see FasterXML#358).
@cowtowncoder cowtowncoder added this to the 2.21.9 milestone Nov 20, 2026
cowtowncoder and others added 6 commits November 20, 2026 14:41
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Validate nesting depth before stepping into a container and step reader
back out on constraint violation, so `nextToken()` continues from the
following value. Report `IonException` from the copy loop via
`_reportCorruptContent()` as elsewhere.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Cover binary structs, s-expressions, annotated containers and a reader
already positioned inside a container; comments now cite FasterXML#911.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Any failure during copy now marks reader to be stepped back out by
`nextToken()`, instead of eagerly and only for constraint violations;
non-`IonException` runtime failures are reported as corrupt content.
`getEmbeddedObject()` without current value fails with parse exception
instead of `IndexOutOfBoundsException`. Release note no longer refers to
private method.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Removes pending step-out state: reader is stepped out as soon as copy
fails. Also avoid NPE from `getEmbeddedObject()` / `getBinaryValue()`
when reader has no current value, and only convert exception types
`ion-java` throws for invalid content.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@cowtowncoder cowtowncoder changed the title (ion) Enforce maxNestingDepth when reading IonValue Enforce maxNestingDepth when reading IonValue Nov 20, 2026
@cowtowncoder

Copy link
Copy Markdown
Member

Been changing this pr with Claude Code, but seems to go in circles; will try to stabilize.

Stepping reader out of the rejected value requires reading the rest of
it, so nesting depth violation would not fail fast. Parser is not usable
after failed copy.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants