Repository navigation
Reject deeply nested JSON documents instead of overflowing the stack - #120887
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]>
|
Workflow [PR], commit [bf178a2] Summary: ✅
AI ReviewSummaryThis PR fixes the stack-overflow path for deeply nested JSON in the main Findings
Final Verdict
|
nihalzp
left a comment
There was a problem hiding this comment.
The fix looks good.
The PR body and change log entry seems to be from a different PR.
We can consider using input_format_json_max_depth instead of max_parser_depth. input_format_json_max_depth seems more appropiate.
max_parser_depth = 0 means unlimited, while input_format_json_max_depth = 0 means no nesting at all; something to think about.
`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]>
| /// checkStackSize is the backstop. | ||
| checkStackSize(); | ||
| if (depth > format_settings.json.max_depth) | ||
| throw Exception(ErrorCodes::TOO_DEEP_RECURSION, |
There was a problem hiding this comment.
These new TOO_DEEP_RECURSION throws escape SerializationJSON::deserializeObject instead of coming back through the existing false + error path. That changes row-format behavior in two ways:
deserializeObjectno longer rewraps the failure asINCORRECT_DATA, soIRowInputFormatwill not treat a too-deep row as skippable underinput_format_allow_errors_*(isParseError()does not includeTOO_DEEP_RECURSION).- In the
JSONcolumn path the exception also bypassesObjectJSONNode::insertResultToColumn'srestoreColumnObject(prev_size)cleanup, so the safe continuation path here is still the existingfalse/errorflow.
So a single pathological row in JSONEachRow still aborts the whole import instead of being handled like other malformed rows. Can we funnel this limit failure back through the normal parse-error path (or catch+restore locally and rethrow as INCORRECT_DATA)? The sibling throw in DynamicNode::elementToDataTypeImpl has the same issue.
Build profile diff (arm_release)Commit See the job log for details. |
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]>
Backport #120887 to 26.8: Reject deeply nested JSON documents instead of overflowing the stack
Backport #120887 to 26.9: Reject deeply nested JSON documents instead of overflowing the stack
…ts instead of overflowing the stack
Resolve the conflict of backporting #120887 ("Reject deeply nested JSON documents instead of overflowing the stack") to 26.7: the `String` node keeps the new depth-bounded rendering, but the buffer is created without `json_max_string_column_growth_step`, which 26.7 does not have. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
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]>
Cherry pick #120887 to 26.3: Reject deeply nested JSON documents instead of overflowing the stack
… of overflowing the stack
Cherry pick #120887 to 26.7: Reject deeply nested JSON documents instead of overflowing the stack
… of overflowing the stack
Backport #120887 to 26.7: Reject deeply nested JSON documents instead of overflowing the stack
Backport #120887 to 26.3: Reject deeply nested JSON documents instead of overflowing the stack
The JSON parsers build the document iteratively, so its nesting depth is bounded only by the input, but three places walked the parsed document recursively without any bound:
ObjectJSONNode::traverseAndInsert, when inserting into aJSONcolumn (which 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; andjsonElementToString, when writing a parsed document back as a string (JSONExtractRaw,JSONExtracttoString, shared data, error messages). A deeply nested document exhausted the thread stack in all three and killed the process. This was reachable withallow_simdjson = 0, where theRapidJSONparser parses iteratively and so survives any depth, passing it on to these consumers; the defaultSimdJSONparser rejects documents deeper than its own limit of about 1024.The first two are now bounded by
max_parser_depth, as JSON schema inference already is, and all three keep acheckStackSizebackstop. 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.As a result, with
allow_simdjson = 0a document nested deeper thanmax_parser_depth(1000 by default) is now rejected withTOO_DEEP_RECURSIONwhere it was previously parsed. The limit is tunable, and the default parser already rejected such documents.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a crash caused by a stack overflow when a deeply nested JSON document was parsed into a
JSONcolumn or passed to theJSONExtractfamily of functions. Such documents are now rejected with theTOO_DEEP_RECURSIONerror.Workflow [PR]
Sync PR [sync-upstream/pr/120887]
Version info
26.10.1.2484-master(included in26.10and later)26.9.20.19,26.8.29.5,26.7.37.9,26.3.59.10