Skip to content

Cherry pick #120887 to 26.7: Reject deeply nested JSON documents instead of overflowing the stack - #126050

Merged
robot-ch-test-poll3 merged 10 commits into
backport/26.7/120887from
cherrypick/26.7/120887
Oct 21, 2026
Merged

robot-ch-test-poll3 merged 10 commits into
backport/26.7/120887from
cherrypick/26.7/120887

Conversation

@robot-ch-test-poll

Copy link
Copy Markdown
Contributor

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-pick locally.
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:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

Avogar and others added 9 commits September 18, 2026 17:35
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]>
`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
@robot-ch-test-poll robot-ch-test-poll added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-critical-bugfix labels Oct 15, 2026
@robot-ch-test-poll4

Copy link
Copy Markdown
Contributor

Dear @Avogar, @nihalzp, this cherry-pick PR has not been updated for 3d0h22m59s. Please resolve the conflicts to backport #120887, or close this PR if the backport is no longer needed. This PR will be automatically closed after 7 days of inactivity.

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]>
@robot-ch-test-poll3
robot-ch-test-poll3 merged commit cacab4c into backport/26.7/120887 Oct 21, 2026
1 check passed
@robot-ch-test-poll3
robot-ch-test-poll3 deleted the cherrypick/26.7/120887 branch October 21, 2026 18:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! pr-critical-bugfix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants