Skip to content

JSONObjectEachRow: reject data after the closing brace regardless of max_block_size - #121055

Open
qoega wants to merge 4 commits into
ClickHouse:masterfrom
qoega:json-fuzz-group6-jsonobjecteachrow-trailing
Open

qoega wants to merge 4 commits into
ClickHouse:masterfrom
qoega:json-fuzz-group6-jsonobjecteachrow-trailing

Conversation

@qoega

@qoega qoega commented Sep 19, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

JSONObjectEachRow input format now rejects trailing data after the closing brace regardless of max_block_size. Previously the trailing data was silently accepted when the whole payload fit in a single block, so validation depended on block-size configuration.

Documentation entry for user-facing changes

No documentation change needed.

Detailed description / Documentation

JSONObjectEachRowRowInputFormat only ran its "no trailing bytes after the closing }" check on chunk boundaries; when the payload fit into a single max_block_size block, that check never ran and garbage after the closing brace was accepted as valid input. Fix moves the trailing-data check to a place that runs regardless of block size. New test 05241.

Found by json_ast_sql_execution_fuzzer (differential oracle: same input with max_block_size = 1 vs default produced different acceptance).

Closes: #121028

cc @groeneai — fuzz-lab find (JSON AST fuzzer)

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/121055]

…max_block_size

`JSONObjectEachRowInputFormat::checkEndOfData` reported the end when it consumed
the closing `}` but left `allow_new_rows` set, and `readSuffix` was empty. With
`max_block_size = 1` the end was met as the first row of a block, the block came
back empty and the input was considered finished, so bytes after the object were
silently ignored; with a larger block the next read continued after the `}` and
failed on them. The end of the object now clears `allow_new_rows` and `readSuffix`
asserts the end of input.

Found by `json_ast_sql_execution_fuzzer` (differential oracle).

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@qoega qoega added the fuzz Problem found by one of the fuzzers label Sep 19, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [eb45cf5]

Summary: ❌

job_name test_name status info comment
Fast test FAIL
05241_json_object_each_row_trailing_data_any_block_size FAIL cidb
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/new_tests_check.py FAIL
Code Review DROPPED
Fast test (arm_darwin) DROPPED
Build (amd_debug) DROPPED
Build (amd_asan_ubsan) DROPPED
Build (amd_tsan) DROPPED
Build (amd_msan) DROPPED
Build (amd_binary) DROPPED
Build (arm_asan_ubsan) DROPPED

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 19, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). label Sep 19, 2026
@groeneai

Copy link
Copy Markdown
Collaborator

I had this staged identically, so I am not opening a competing PR. The three functional edits match hunk for hunk (allow_new_rows = false in the checkAndSkipObjectEnd arm, readSuffix with skipWhitespaceIfAny + assertEOF, override {} -> override;), and the post-image of JSONObjectEachRowRowInputFormat.h is the same blob in both trees, 908e65c43671. Two measurements from my run that this PR does not capture yet.

1. The predicate is divisibility, not "the payload fits in a single block". The trailing bytes were ignored exactly when the row count was an exact multiple of the effective block size, so this is reachable with no settings changes at all: a payload of 65409 rows plus a trailing xyz is accepted at the default max_block_size of 65409 (SELECT sum(y), count() over it returns 65409 rows and no error) and rejected with code 27 at 65408. Smaller illustration on master: 3 rows plus xyz is accepted at max_block_size = 3 and rejected at 2 and 4. max_block_size = 1 is just the case where every row count is a multiple. Probably worth a word in the changelog entry, which currently reads "when the whole payload fit in a single block".

2. There is a wrong-results case here, not only lax validation. With format_json_object_each_row_column_for_object_name set, '{"a":{"y":1}}, "b":{"y":2}' returns 1 row at max_block_size = 1 and 2 rows at 65536, with no error either way: the b row is silently dropped in the first, the closing brace is silently ignored in the second.

Six assertions from my version of this test that 05241 does not reach. All six are accepted on master today, and all six raise CANNOT_PARSE_INPUT_ASSERTION_FAILED with the change in this PR (measured on my tree, which carried the same src edit: 9 of my 16 assertions diverged pre-fix, 0 after):

-- wrong results: 1 row at bs=1, 2 rows at bs=65536, no error either way
SELECT * FROM format(JSONObjectEachRow, 'n String, y Int64', '{"a":{"y":1}}, "b":{"y":2}') SETTINGS max_block_size = 1, format_json_object_each_row_column_for_object_name = 'n'; -- { serverError CANNOT_PARSE_INPUT_ASSERTION_FAILED }
SELECT * FROM format(JSONObjectEachRow, 'n String, y Int64', '{"a":{"y":1}}, "b":{"y":2}') SETTINGS max_block_size = 65536, format_json_object_each_row_column_for_object_name = 'n'; -- { serverError CANNOT_PARSE_INPUT_ASSERTION_FAILED }
-- the count() fast path (countRows, a different function from readRow) carried the same divergence
SELECT count() FROM format(JSONObjectEachRow, 'y Int64', '{"a":{"y":1}}xyz') SETTINGS max_block_size = 1; -- { serverError CANNOT_PARSE_INPUT_ASSERTION_FAILED }
-- divisibility: accepted at 3, rejected at 2 and 4
SELECT * FROM format(JSONObjectEachRow, 'y Int64', '{"a":{"y":1},"b":{"y":2},"c":{"y":3}}xyz') SETTINGS max_block_size = 3; -- { serverError CANNOT_PARSE_INPUT_ASSERTION_FAILED }
-- accepted at every block size before this PR
SELECT * FROM format(JSONObjectEachRow, 'y Int64', '{}xyz') SETTINGS max_block_size = 65536; -- { serverError CANNOT_PARSE_INPUT_ASSERTION_FAILED }
SELECT * FROM format(JSONObjectEachRow, 'y Int64', '{"a":{"y":1}}}') SETTINGS max_block_size = 65536; -- { serverError CANNOT_PARSE_INPUT_ASSERTION_FAILED }

Take them into 05241 if they look useful, or tell me and I will send them as a test-only follow-up once this merges. Either way I would rather not add a duplicate test file.

Two scope notes, both measured, both outside this PR. JSONEachRow is already strict ('[{"y":1}]xyz' gives code 27 today), so after this the two formats agree, which was the point of the issue. The JSON and JSONStrings readers are lax at every block size, because JSONRowInputFormat::readSuffix does skipArrayEnd plus skipTheRestOfObject and never asserts EOF: '{"meta":[{"name":"y","type":"Int64"}],"data":[{"y":1}],"rows":1}xyz' is accepted at both max_block_size = 1 and 65536. That is a separate, block-size-independent laxity and I am leaving it alone unless you want it covered.

…on is allowed

A truncated payload without the outer `}` was accepted as a clean end of data;
the optional `;` that ends the data of an `INSERT` was rejected by the new end
check. Both in the row reader and the schema reader.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
if (in.eof() || JSONUtils::checkAndSkipObjectEnd(in))
if (in.eof())
throw Exception(ErrorCodes::CANNOT_PARSE_INPUT_ASSERTION_FAILED, "Unexpected end of data: expected '}}' that closes the object");
if (JSONUtils::checkAndSkipObjectEnd(in))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSONObjectEachRowSchemaReader still accepts trailing bytes after the outer }. IRowWithNamesSchemaReader::readSchema() stops as soon as this method returns eof = true; it never runs a suffix check like JSONObjectEachRowInputFormat::readSuffix(). That means schema-only entry points such as DESCRIBE format(JSONObjectEachRow, '{"x":{"y":1}}xyz') or DESCRIBE file(..., JSONObjectEachRow) still succeed on the same invalid payload that readRow now rejects.

Please validate the suffix on the schema-inference path as well, preserving the optional whitespace / ; contract, and add a regression that exercises a schema-only caller.

@qoega

qoega commented Sep 21, 2026

Copy link
Copy Markdown
Member Author

Review follow-up landed as 305e4e8fe26. Covers all three items:

  1. Blocker fix (missing outer }): closing brace is now mandatory in both the row reader and the schema reader. {"x":{"y":1} (truncated) now raises CANNOT_PARSE_INPUT_ASSERTION_FAILED.
  2. Major fix (trailing ;): readSuffix preserves the optional ; terminator that sibling JSONEachRow accepts. INSERT ... FORMAT JSONObjectEachRow {"row":{"x":1}}; reaches assertEOF cleanly.
  3. Wrong-results assertions: your six assertions (format_json_object_each_row_column_for_object_name divergence plus the divisibility cases) plus a missing-brace case and a ; case are all in 05241, all { serverError CANNOT_PARSE_INPUT_ASSERTION_FAILED }.

Changelog wording updated per your note: "accepted when the row count is a multiple of max_block_size" (divisibility, not "single block").

JSON / JSONStrings block-size-independent laxity remains out of scope, as you flagged.

arm_tidy: still pending. I owe you the CI log — will pull it next tick and paste the clang-tidy lines here for you to address.

The added `allow_new_rows` member landed in a second protected: block
that was inside the already-protected section. clang-tidy's
readability-redundant-access-specifiers flags it; drop the redundant
specifier and keep the field in the existing protected section.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
qoega added a commit to qoega/ClickHouse that referenced this pull request Sep 22, 2026
660 keywords x 22 parser positions round-tripped through
formatQuerySingleLineOrNull; asserts no shape loses back-quotes and
reads back as different syntax. Guard row computes the sweep's own
extent (every keyword examined, position count, parse rate) so an
empty failure list cannot pass vacuously.

Numbered 05248 (groeneai's file was 05241 which collides with the
JSONObjectEachRow test in the sibling PR ClickHouse#121055; 05243-05247 are
also taken by other JSON-fuzzer PR groups).

Test authored by @groeneai from ClickHouse#121054 (comment)

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@qoega

qoega commented Sep 22, 2026

Copy link
Copy Markdown
Member Author

arm_tidy fix landed as 4f59b2b8bd5. The follow-up commit dc2c46f9564 left the second protected: in src/Processors/Formats/Impl/JSONEachRowRowInputFormat.h from the original commit; readability-redundant-access-specifiers flagged it at line 99. Removed the redundant specifier; the allow_new_rows member stays in the pre-existing protected: section that starts at line 84. Comment kept.

@clickhouse-gh

clickhouse-gh Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 4f59b2b8b with master de0be4574 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 5af633e4c; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

9 translation units recompiled, 17 s compile time in total, 9 of them have a recent master baseline.

Job report

`IRowWithNamesSchemaReader::readSchema()` stops as soon as the per-row
reader sets `eof = true` and never calls a suffix hook, so schema-only
entry points (`DESCRIBE format(JSONObjectEachRow, ...)`,
`DESCRIBE file(..., JSONObjectEachRow)`) still accepted trailing bytes
after the outer `}` that the row-reader path now rejects via
`readSuffix`. Factor the whitespace + optional `;` + `assertEOF` check
into a helper and reuse it from both paths so semantics match.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@qoega

qoega commented Nov 1, 2026

Copy link
Copy Markdown
Member Author

Review follow-up landed as eb45cf5ea97. Addresses the 2026-09-21 line review on the schema-reader path.

  • JSONObjectEachRowSchemaReader::readRowAndGetNamesAndDataTypes now calls assertJSONObjectEachRowSuffix right after checkAndSkipObjectEnd returns true, before setting eof = true. That mirrors the row-reader's readSuffix contract (skipWhitespaceIfAny → optional ; → skipWhitespaceIfAny → assertEOF). Factored the two call sites into one anonymous-namespace helper so they stay in lock-step.
  • 05241_json_object_each_row_trailing_data_any_block_size.sql extended with three schema-only rows:
    • DESCRIBE format(JSONObjectEachRow, '{"x":{"y":1}}xyz') → CANNOT_PARSE_INPUT_ASSERTION_FAILED (the regression — previously accepted via IRowWithNamesSchemaReader::readSchema() returning on eof with no suffix hook)
    • DESCRIBE format(JSONObjectEachRow, '{"x":{"y":1}}') → control, succeeds
    • DESCRIBE format(JSONObjectEachRow, '{"x":{"y":1}} ;') → optional ;, succeeds

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

Labels

comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). fuzz Problem found by one of the fuzzers pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSONObjectEachRow accepts bytes after the closing } only when max_block_size = 1 — input validation depends on block-size setting

2 participants