Skip to content

Fix CapnProto schema depth check bypass through comments and backtick text lines - #125487

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-capnproto-depth-check-lexer-forms
Oct 14, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-capnproto-depth-check-lexer-forms

Conversation

@groeneai

@groeneai groeneai commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #116032
Related: #115700

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 into CHANGELOG.md):

Fixed a stack overflow crash on a deeply nested CapnProto schema that the nesting depth check missed: a comment or a byte order mark between import and its path, an escape sequence in the import path, or a backtick text line. A backtick text line with many brackets is no longer rejected as nested too deeply.

Description

On 26.8.18.2 and 26.9.11.2, a CapnProto schema that puts a comment between import and its path, or has a backtick text line containing ", makes DESC format(CapnProto, '') die with SIGSEGV (stack overflow in the Cap'n Proto parser) instead of failing with The CapnProto schema is nested too deeply. With format_schema_source = 'string' this is reachable from one query.

checkCapnProtoSchemaNestingDepth (#115700) re-tokenizes the schema to count nesting and follow imports before the recursive parser runs, but it tokenized three things differently from the Cap'n Proto lexer:

  • # comments and UTF-8 byte order marks between tokens cleared the pending import, so the imported file was not checked;
  • backtick text lines were unknown: a " or # inside one hid the brackets after it, a backtick import path was not followed, and brackets inside one were counted, so a valid schema could be rejected;
  • escapes in a quoted import path (\x2e, \056) were decoded differently, so the checked path was not the opened one.

The change keeps the pending import across comments and byte order marks, handles backtick lines as text literals (ended by \r or \n, with the trailing \n Cap'n Proto adds), and lexes "..." with Cap'n Proto's own kj::parse::doubleQuotedString, falling back to the previous skip loop when that fails (e.g. a multi-line 0x"..." data literal), so the scan never stops early.

The new test has one case per form; on master the six bypass cases crash and the brackets case is rejected. It passes 50 randomized runs, the existing CapnProto tests pass, and no upstream Cap'n Proto schema changes its result. A chain of individually shallow imports (the open risk in the issue) is not addressed here.


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

Version info

  • Merged into: 26.10.1.2350-master (included in 26.10 and later)
  • Backported to: 26.9.20.8, 26.8.28.8, 26.7.32.4, 26.3.52.8

… text lines

checkCapnProtoSchemaNestingDepth re-tokenizes a CapnProto schema before the
recursive Cap'n Proto parser runs, to bound bracket nesting and to follow
imports. Its tokenizer diverged from the Cap'n Proto lexer in three ways,
each of which let a deeply nested schema reach the parser unchecked and
overflow the stack (SIGSEGV):

- A `#` comment or a UTF-8 byte order mark between `import` and its path
  cleared the pending `import` (the lexer allows both between any tokens),
  so the imported file was never checked (ClickHouse#116032).
- Backtick text lines (to the first `\r` or `\n`, value plus `\n`) were
  unknown: a `"` inside one opened a fake literal that hid real brackets, a
  `#` inside one hid the rest of the `\n` line although the lexer ends the
  token at `\r`, a backtick import path was never followed, and brackets
  inside one were counted, so a valid schema could be rejected.
- Escapes in a quoted import path were decoded differently (`\x2e`
  became `x2e`), so the checked path was not the one Cap'n Proto opened.

Comments and byte order marks now terminate the word like whitespace does,
backtick lines are handled as text literals and followed as import paths,
and `"..."` is lexed with kj::parse::doubleQuotedString, the parser the
lexer itself uses, so escapes decode identically. When kj cannot lex the
literal (e.g. a `0x"..."` data literal spanning lines, which is valid),
the previous skip-to-next-quote loop runs unchanged, so the scan never
stops early.

Closes ClickHouse#116032

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Oct 12, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (click to expand)

Own cold review + Gate B (codex) on 19ad9210a575: 0 blockers, 0 majors.

Finding Verdict
💡 Gate B: use Critical Bug Fix for a crash DISAGREE: that category is chosen only on a maintainer's request; Bug Fix holds (crash on 26.8.18.2 and 26.9.11.2 through the documented format_schema surface)
💡 utf8_bom constant could live at namespace scope noted, no effect
💡 A NUL in a decoded import path is checked as the truncated name noted: over-checking only, cannot bypass the limit

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. Each of the 6 bypass schemas in the new test (20000 nested List() crashes master clickhouse local with SIGSEGV on every run; the 1001-( backtick line is rejected on every run. Also reproduced on official 26.8.18.2 and 26.9.11.2 images.
b Root cause explained? checkCapnProtoSchemaNestingDepth re-tokenizes the schema before the recursive Cap'n Proto parser, but # comments and byte order marks cleared the pending import, backtick text lines were unknown (a " or # inside one hid real brackets, a backtick import path was not followed, brackets inside were counted), and quoted-path escapes were decoded differently. The real nesting was then never checked and the parser overflowed the stack.
c Fix matches root cause? Yes. The pre-check's tokenizer now matches the lexer for those three cases, and "..." is lexed by Cap'n Proto's own kj::parse::doubleQuotedString. No widened limit, no try/catch, no guard at the crash site.
d Test intent preserved / new tests added? New 05366_capnproto_deep_schema_lexer_forms.sh, one case per form plus a multi-line data literal (guards the fallback) and the false-rejection case. Existing CapnProto tests unchanged and green.
e Both directions demonstrated? Master: 6 cases SIGSEGV, brackets case rejected. Fix: output matches the reference. Three one-line mutations of the fix each flip exactly their own case. 50/50 randomized runs green.
f Fix is general across code paths? getMessageSchema is the only parseFromDirectory call site and calls the check first; CapnProto input, output and schema inference all use it; entry and imported files share one loop.
g Fix generalizes across inputs (params/datatypes/wrappers)? Covered: whitespace, # with and without a space, byte order mark; "..." with hex, octal and quote escapes; backtick ended by \n or \r; one-line and multi-line 0x"..."; relative and absolute import paths. Valid shallow imports through each form resolve as before.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format change. Only newly accepted input: backtick text lines with more than 1000 brackets (valid Cap'n Proto). Only newly rejected input: deep schemas that crash today. All 16 upstream Cap'n Proto schemas give the same result as before.
i Invariants and contracts preserved? Every import Cap'n Proto follows and every bracket it nests through is seen before parseFromDirectory. The loop index ends on the literal's last character in every branch (a failed kj parse does not move it), and the pending word is cleared after every literal as before.

Session id: cron:clickhouse-impl-slot-4:20261012-184400

@clickhouse-gh

clickhouse-gh Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [19ad921]

Summary: ✅


AI Review

Summary

This PR tightens checkCapnProtoSchemaNestingDepth so its pre-scan matches Cap'n Proto's lexer for comments, UTF-8 BOMs, backtick text literals, and escaped double-quoted import paths, and it adds a focused stateless regression test for each bypass shape. I reviewed the current diff, the linked issue, prior PR discussion, the upstream Cap'n Proto lexer rules, and the current all-green CI surface, and I did not find any remaining correctness, safety, or test coverage issues that warrant an inline comment.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). labels Oct 12, 2026
@alexey-milovidov alexey-milovidov self-assigned this Oct 14, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 14, 2026
Merged via the queue into ClickHouse:master with commit 5c2d6ef Oct 14, 2026
177 of 178 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 14, 2026
@Algunenano Algunenano added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Oct 14, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 14, 2026
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 14, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 14, 2026
Backport #125487 to 26.9: Fix CapnProto schema depth check bypass through comments and backtick text lines
clickhouse-gh Bot added a commit that referenced this pull request Oct 14, 2026
Backport #125487 to 26.8: Fix CapnProto schema depth check bypass through comments and backtick text lines
alexey-milovidov added a commit that referenced this pull request Oct 15, 2026
Backport #125487 to 26.3: Fix CapnProto schema depth check bypass through comments and backtick text lines
clickhouse-gh Bot added a commit that referenced this pull request Oct 15, 2026
Backport #125487 to 26.7: Fix CapnProto schema depth check bypass through comments and backtick text lines
strtgbb pushed a commit to Altinity/ClickHouse that referenced this pull request Oct 15, 2026
…bypass through comments and backtick text lines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). groeneai-origin-request PR origin: a maintainer pinged or directed groeneai pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CapnProto import scanner loses import across a comment; deep schema still SIGSEGVs

6 participants