Repository navigation
Cherry pick #120887 to 26.3: Reject deeply nested JSON documents instead of overflowing the stack - #126048
Merged
robot-ch-test-poll3 merged 10 commits intoOct 21, 2026
Conversation
The JSON parsers build the document iteratively, so its nesting depth is bounded only by the input, and three places walked that document recursively without any bound: - `ObjectJSONNode::traverseAndInsert`, when inserting into a `JSON` column; it also appended to the path at every level, making the memory spent on paths quadratic in the depth. - `DynamicNode::elementToDataTypeImpl`, when inferring the type of a value of a dynamic path from nested arrays. - `jsonElementToString`, when writing a parsed document back as a string (`JSONExtractRaw`, `JSONExtract` to `String`, shared data, error messages). A deeply nested document exhausted the thread stack there and killed the process. Bound the first two by `max_parser_depth`, as JSON schema inference already does, and keep a `checkStackSize` backstop in all three. The limit on type inference matters beyond its own recursion: the inferred type is as deep as the document, and the recursions over that type afterwards (building its name, its extract tree, its column, destroying it) have no guard of their own. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`input_format_json_max_depth` is the JSON-specific limit on the nesting depth of a parsed document, so use it for the two depth checks instead of the general `max_parser_depth`. Both default to 1000, so the default behaviour does not change. The value is taken literally, as the only other user of the setting does, rather than treating 0 as unlimited. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The test forces `allow_simdjson = 0` to exercise the `RapidJSON` parser, which the Fast test build does not include, so there the JSON functions raise `NOT_IMPLEMENTED` instead of the expected error. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=120887&sha=6606580d54fd6fb4965cfc42e1843b3334f00d52&name_0=PR&name_1=Fast%20test PR: #120887 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Throwing from the traversal escaped the `false` + `error` protocol that the JSON extract tree uses for malformed input: the failure was not rewrapped as `INCORRECT_DATA`, so `isParseError` did not hold for it and a single too-deep row aborted a whole import instead of being skipped like any other malformed row, and the partially filled column was not restored. Report the limit through `error` instead. `checkStackSize` still throws, being a last-resort guard. The test now keeps every document that survives parsing shallow: such a document is walked again by consumers whose recursion the setting does not bound, for example the conversion of a constant to an `AST` literal. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=120887&sha=902d3408260a1c543731539176e02a0eb9f26b7e&name_0=PR PR: #120887 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`jsonElementToString` walks a parsed document whose nesting depth comes from the input, and two of its uses could not afford an unbounded walk. An error message quotes the element it is about, so producing one could recurse as deep as the document and build a huge string; messages now render at most a fixed number of levels and elide the rest. A `String`, `LowCardinality(String)` or `FixedString` column takes the rendered text as its value. A value cannot be elided, so one nested deeper than `input_format_json_max_depth` is now rejected through the `false` + `error` path, like any other value that cannot be read. The `String` column renders directly into the column's characters, so the characters are truncated back when that fails. As a result, extracting a too-deep value as a `String` yields the default, which is what the `SimdJSON` parser already does, since it refuses such a document. Rendering raw text (`JSONExtractRaw`) stays unbounded, with `checkStackSize` as its guard. Each case in the test now has a single possible rejector: which of the limit and the backstop comes first depends on the build. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=120887&sha=94f1d66740a9ee2bcf481a6530da9f4e72471c39&name_0=PR PR: #120887 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`readability-static-definition-in-anonymous-namespace`. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=120887&sha=c514050ce308ccd6e965fe5315d4c9193511314d&name_0=PR&name_1=Build%20(arm_tidy%2C%201%2F4) PR: #120887 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`JSONExtractRaw` rendered an element without a depth bound while the extract tree bounds it by `input_format_json_max_depth`, so the two ways of asking for an element as text disagreed: `JSONExtract(x, 'String')` gave the default for a value nested too deep, while `JSONExtractString`, which forwards a non-string value to the raw renderer, raised `TOO_DEEP_RECURSION`. Bound the raw renderer the same way, so all of them treat such a value as one that cannot be extracted. The `SimdJSON` parser already behaves this way, since it refuses to parse a document that deep at all. `JSONExtractArrayRaw` and `JSONExtractKeysAndValuesRaw` keep one element per element of the input, with the default for one that cannot be rendered. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Reject deeply nested JSON documents instead of overflowing the stack
Contributor
Resolve the conflicts of backporting #120887 ("Reject deeply nested JSON documents instead of overflowing the stack") to 26.3: - The `String` node keeps the new depth-bounded rendering, but the buffer is created without `json_max_string_column_growth_step`, which 26.3 does not have. - The `Variant` node error keeps the 26.3 message text and quotes the element with the bounded `jsonElementToStringForError`. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Original pull-request #120887
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Before you resolve anything
Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job