Skip to content

Do not let server-side AST fuzzer queries read stream-like tables directly - #122461

Merged
PedroTadim merged 6 commits into
ClickHouse:masterfrom
groeneai:groeneai/ast-fuzzer-no-direct-select-stream-like
Oct 8, 2026
Merged

PedroTadim merged 6 commits into
ClickHouse:masterfrom
groeneai:groeneai/ast-fuzzer-no-direct-select-stream-like

Conversation

@groeneai

@groeneai groeneai commented Sep 26, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #119625

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

Not required: CI-only change.

Description

02023_storage_filelog failed in the Fast test of #118581 (job, report): its third SELECT returned 0 rows instead of 103. Per the server log, 0.4 s earlier a server-side AST fuzzer copy of a query of the concurrent 04104_ast_fuzzer_preserves_caller_transaction, SELECT n FROM merge(REGEXP('.*[0-9].*'), '^') ORDER BY n ASC SETTINGS ast_fuzzer_runs = 3, read test_j5u0x8q6q7b3.file_log and polled its 103 pending records.

A direct read of a stream-like table consumes its messages. The fuzz context inherited stream_like_engine_allow_direct_select from the original query, where it is 1 in the CI default profile (tests/config/users.d/filelog.xml) or when the query sets it, and the fuzzer rewrites table references into cross-database merge(), remote() or corpus names.

The fuzz context now pins the setting to 0, and strips it and profile from every query-level SETTINGS clause of the fuzzed query, like the resource caps (#108212): a profile switch would re-install the CI profile's 1 over the pin, and can lift a resource cap. This single site covers FileLog, Kafka, RabbitMQ, NATS and S3Queue/AzureQueue on both the native and the HTTP fuzzer path, since all of them check a context derived from the fuzz context; merge() child reads and remote() loopback shards inherit it. Fuzzed variants of a direct stream-like read now fail with QUERY_NOT_ALLOWED; the original query still reads.

In a cross-database replay of the #118581 shape, a fuzzed merge() consumed all 100 rows of another database's FileLog before the change; after it, all 12 such reads were refused. No regression test is added: the test was removed in review.


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

Version info

  • Merged into: 26.10.1.1858-master (included in 26.10 and later)

groeneai and others added 4 commits September 26, 2026 10:59
…ectly

A direct SELECT from a stream-like table (FileLog, Kafka, RabbitMQ, NATS,
S3Queue/AzureQueue) consumes its messages. The fuzz context inherited
stream_like_engine_allow_direct_select from the seed query, which is 1 in
the CI default profile (tests/config/users.d/filelog.xml) and in any seed
that sets it, while the fuzzer rewrites table references into
cross-database merge(REGEXP(...)), remote() or corpus names. A fuzzed copy
of an unrelated seed could therefore read and commit another test's FileLog
rows: in the Fast test of ClickHouse#118581 a fuzzed
`SELECT n FROM merge(REGEXP('.*[0-9].*'), '^')` of
04104_ast_fuzzer_preserves_caller_transaction polled the 103 pending
records of 02023_storage_filelog 0.4 s before that test's SELECT.

Pin the setting to 0 on the fuzz context, and strip it from every
query-level SETTINGS carrier of the fuzzed AST like the resource caps. Strip
`profile` as well: a query-level profile switch installs every setting of
that profile over the pins, so `SETTINGS profile = 'default'` alone would
restore the CI profile's 1 (and could equally re-open a resource cap).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e tables directly

The HTTP query allows direct reads through a settings profile and through the setting
itself; every fuzzed copy of it must run with stream_like_engine_allow_direct_select = 0.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The style check requires a retry loop around `SYSTEM FLUSH LOGS` in a test
that sends a query with curl and then reads `system.query_log`
(ClickHouse#84364): the query's own
entry can be written after the HTTP response. Wait for the entry of the HTTP
query; its fuzzed copies, which the test checks, are logged before it. Also
restrict the `system.query_log` reads to the last 10 minutes, like the other
tests that read system logs.

CI report of the original failure:
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118581&sha=ab4ec36ccd27eed40f615efaa31bc4116a7088c9&name_0=PR&name_1=Fast%20test
(ClickHouse#118581)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Name exactly what stripFuzzerSafetyLimitSettings removes and keep the rest of its doc comment as on master.

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 Sep 26, 2026
@groeneai

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

Pre-publication review by an independent model (engine: codex; 2 findings; 1 full pass).

# Sev Finding Verdict Evidence / action
1 ❌ A stored view whose own SETTINGS enable direct reads re-enables them for its inner read, past the pin (src/Analyzer/QueryTreeBuilder.cpp:278-318) DISAGREE Mechanism real, but no test defines such a view (0 of the 51 stateless tests that create a stream-like table), and a fuzzed CREATE VIEW cannot make one: the same strip walks every nested SELECT (src/Parsers/stripQuerySettings.cpp:86-100). Closing it needs a marker that DEFINER/NONE views and remote shards honor; not covered by this PR.
2 ⚠️ The test checks only the logged setting of the fuzzed copies, not a refused read DISAGREE It fails on master (5/5) and with each of the three changes removed (3/3 each). A per-run refused-read check fails on a correct binary (1 of 200 parallel runs had none: the fuzzer can chain into EXPLAIN variants that build no pipeline); refusal through merge() and loopback remote() was measured separately.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding.

Session id: cron:clickhouse-review-slot-8:20260926-184400

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. With the CI configs, 05267_ast_fuzzer_no_direct_select_from_stream_like_tables fails on every run on master (5/5) with 1 0: every fuzzed copy of its HTTP query inherits stream_like_engine_allow_direct_select = 1.
b Root cause explained? The fuzz context inherited the setting (1 in the CI default profile or from the query's own SETTINGS), and fuzzed merge()/remote()/corpus rewrites reach other databases, so a fuzzed read consumed another test's FileLog rows (#118581 Fast test).
c Fix matches root cause? Yes. The fuzz context pins the setting to 0 next to its other safety pins, and the fuzzed query's own SETTINGS can no longer re-enable it, neither by name nor through profile.
d Test intent preserved / new tests added? New stateless test: at least one fuzzed copy logged, every one with the ban, and the query itself still reads its 3 rows. 02023_storage_filelog is unchanged.
e Both directions demonstrated? Master 5/5 red; each of the three changes removed 3/3 red; fix 200/200 with --jobs 16 and 50/50 sequential green, parallel replicas and serialize_query_plan 20/20 each, 48 related tests green.
f Fix is general across code paths? One site: FileLog, Kafka, RabbitMQ, NATS and S3Queue/AzureQueue all check a context derived from the fuzz context, on both fuzzer paths (native, HTTP); merge() children and remote() loopback shards inherit it.
g Fix generalizes across inputs (params/datatypes/wrappers)? Stripped in every SETTINGS clause of the fuzzed query, nested ones included, as name = 1, name = DEFAULT or a profile switch; compatibility cannot override it. Data types N/A.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, default or format change; only fuzzed copies run by ast_fuzzer_runs, an experimental testing setting, are affected.
i Invariants and contracts preserved? No query-level SETTINGS of a fuzzed query can override a fuzz-context pin, now including this one and profile; the original query's own settings and profile stay in effect. No locking change.

Session id: cron:clickhouse-impl-slot-41:20260926-180600

@clickhouse-gh

clickhouse-gh Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e0265a7]

Summary: ✅


AI Review

Summary

This PR hardens server-side ast_fuzzer_runs execution by pinning stream_like_engine_allow_direct_select = 0 on the fuzz context and stripping both that setting and profile from fuzzed query SETTINGS clauses before re-serialization. That closes the direct top-level override path, but the guard is still bypassable through stored views, so the fix is not yet complete.

Findings

❌ Blockers

  • [src/Interpreters/executeQuery.cpp:3726] The new fuzz-context pin is not authoritative for reads that go through a stored view. A fuzzed SELECT ... FROM v copies the pinned context into the view context (src/Storages/StorageView.cpp:343, src/Storages/StorageView.cpp:937), but then the view body's own SETTINGS clause is reapplied on top of that derived context in src/Analyzer/QueryTreeBuilder.cpp:316 through src/Analyzer/QueryTreeBuilder.cpp:319. So a view such as CREATE VIEW v AS SELECT * FROM file_log SETTINGS stream_like_engine_allow_direct_select = 1 (or SETTINGS profile = 'default') can still reach src/Processors/QueryPlan/ReadFromStreamLikeEngine.cpp:34 with direct reads enabled again. That means fuzzed queries can still consume another test's queue rows via a view even after this patch.
    Suggested fix: make these safety settings non-overridable for view-subquery execution too — either strip stream_like_engine_allow_direct_select and profile from stored view bodies before analysis/planning, or enforce the same safety pin when QueryTreeBuilder applies nested SETTINGS onto a view/subquery context.
Final Verdict

❌ Changes requested — the stream-like direct-select ban remains bypassable via stored views.

LLVM Coverage Report

Measured on commit e0265a7.

Metric Baseline Current Δ
Lines 88.30% 88.30% +0.00%
Functions 91.80% 92.10% +0.30%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 6/8 (75.00%) · Uncovered code

Full report · Diff report

fuzz_context->setSetting("ast_fuzzer_runs", Field(Float64(0)));
fuzz_context->setSetting("allow_experimental_parallel_reading_from_replicas", Field(UInt64(0)));
/// A direct read of a stream-like table (Kafka, FileLog, ...) consumes its messages, which may belong to another session.
fuzz_context->setSetting("stream_like_engine_allow_direct_select", Field(false));

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.

This still has an override path through stored views. StorageView::readImpl starts from a context copied from the outer fuzz context (src/Storages/StorageView.cpp:562-565, src/Storages/StorageInMemoryMetadata.cpp:212-238), but QueryTreeBuilder::buildSelectExpression then reapplies the view body's own SETTINGS clause onto that derived context (src/Analyzer/QueryTreeBuilder.cpp:279-318). Because stream_like_engine_allow_direct_select and profile are not pinned there, a view such as CREATE VIEW v AS SELECT * FROM file_log SETTINGS stream_like_engine_allow_direct_select = 1 still reaches ReadFromStreamLikeEngine::initializePipeline with direct reads enabled (src/Processors/QueryPlan/ReadFromStreamLikeEngine.cpp:34-36).

So the new fuzz-context pin is not yet authoritative: a fuzzed SELECT ... FROM v can still consume another test's queue rows through the view. This needs either a non-overridable constraint on the fuzz context, or equivalent stripping when planning stored view bodies, not just when formatting the outer fuzzed AST.

@clickhouse-gh clickhouse-gh Bot added the comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. label Sep 26, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 975c209

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Performance Comparison (arm_release, master_head, 5/6) / optimize_time_filter_with_preimage #1 dashboard confirmed_regression only: 0.251 s -> 0.364 s (+44.9%), while every row of this query in the shard's own report is success. #1 is the toDate baseline arm, which runs with optimize_time_filter_with_preimage = 0: a long single-threaded scan whose time swings on arm runners for unrelated PRs and on master alike. This PR only changes the context of the server-side AST fuzzer, which a performance run never enters (no performance test sets ast_fuzzer_runs) #122156 (mine, open), which removes that arm
Mergeable Check, PR rollups, red only because of the row above (Mergeable Check names that one shard) #122156 (mine, open), the same as the row above

All 169 check runs completed (127 pass, 41 not selected, 1 red), Finish Workflow is green and
CH Inc sync passed. CIDB has no FAIL or ERROR test row on this head. The shard's 4 too long
is informational: it never fails a shard on its own.

Session id: cron:our-pr-ci-monitor:20260927-003314

@PedroTadim

Copy link
Copy Markdown
Member

cc @qoega

@PedroTadim PedroTadim self-assigned this Oct 6, 2026
@@ -0,0 +1,48 @@
#!/usr/bin/env bash
# Queries run by the server-side AST fuzzer (`ast_fuzzer_runs`) must not read a stream-like table directly, even when

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@groeneai remove the test

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Removed in e0265a7, and the description no longer mentions the test.

@PedroTadim
PedroTadim added this pull request to the merge queue Oct 8, 2026
Merged via the queue into ClickHouse:master with commit c04b3d3 Oct 8, 2026
152 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 8, 2026
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-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. groeneai-origin-request PR origin: a maintainer pinged or directed groeneai pr-ci 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.

3 participants