Skip to content

Do not run the format_schema_source='query' schema query without a user - #117737

Merged
alexey-milovidov merged 3 commits into
masterfrom
fix-format-schema-source-query-access
Sep 3, 2026
Merged

alexey-milovidov merged 3 commits into
masterfrom
fix-format-schema-source-query-access

Conversation

@Avogar

@Avogar Avogar commented Sep 2, 2026 •

Copy link
Copy Markdown
Member

FormatSchemaInfo::querySchema fell back to the global context when the thread had no query
context. A context without a user has full access, so the schema query, which comes from the
user-controlled format_schema setting, was executed with all privileges. Background tasks are
affected: streaming engine consumers (Kafka, NATS, RabbitMQ, ObjectStorageQueue) and
Buffer background flushes build a query context of their own from the global context and never
call setUser.

The schema query is now refused when the context has no user. The check is on the user and not on
the presence of a query context, precisely because those storages do have a query context. A schema
that is already in the on-disk cache keeps working in background tasks, since the cache can only
have been populated by an access-checked execution.

Changelog category (leave one):

  • Critical Bug Fix (crash, data loss, RBAC)

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

Fixed format_schema_source='query' running the schema query with full privileges when used from a
background task, such as a streaming engine consumer (Kafka, NATS, RabbitMQ,
ObjectStorageQueue) or a Buffer background flush. The schema query is now rejected there; use
format_schema_source='file' or 'string' instead, or pre-populate the schema cache.


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

Version info

  • Merged into: 26.9.1.586 (included in 26.9 and later)
  • Backported to: 26.8.3.21, 26.7.7.14, 26.6.5.38, 26.3.31.8

`FormatSchemaInfo::querySchema` fell back to the global context when the thread
had no query context. A context without a user has full access, so the schema
query, which comes from the user-controlled `format_schema` setting, was executed
with all privileges. Background tasks are affected: streaming engine consumers
(`Kafka`, `NATS`, `RabbitMQ`, `ObjectStorageQueue`) and `Buffer` background
flushes have no user in their context.

Refuse to run the schema query when the context has no user. The check is on the
user and not on the presence of a query context, because those storages build a
query context of their own from the global one and never call `setUser`.

A schema that is already in the on-disk cache keeps working in background tasks,
since the cache can only have been populated by an access-checked execution.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [dfcde89]

Summary: ✅


AI Review

Summary

This PR closes the obvious userless-execution hole in format_schema_source='query', scopes the on-disk schema cache by user, and adds coverage for Buffer and Kafka background paths. The userless-path fix itself looks right, and the previous missing-consumer-path test gap is addressed, but the cache key is still too coarse: reusing the cached schema for the same user_id still bypasses the schema-query access check after a role change or later grant change, so the RBAC fix is not complete.

PR Metadata
  • Changelog category is correct: Critical Bug Fix (crash, data loss, RBAC).
  • Changelog entry is required, but the current text is inaccurate. It still tells users to "pre-populate the schema cache" for background tasks, while the final code now throws before any cache lookup when there is no user. The free-form description above the template repeats the same stale contract.
  • Replacement text:
    Fixes format_schema_source='query' running the schema query with full privileges in userless contexts such as streaming engine consumers, Buffer background flushes, and client-side INSERT parsing. Such contexts now reject format_schema_source='query'; use format_schema_source='file' or 'string' instead. Query-derived schema cache entries are now isolated per user.
Findings

❌ Blockers

  • [src/Formats/FormatSchemaInfo.cpp:208] user_id is not a stable proxy for the access context. A user can warm this cache while a role granting access to the schema source table is enabled, then run SET ROLE NONE (or lose that grant later) and still reuse the same cached schema because the hit path never re-executes querySchema. That means the PR still allows same-user RBAC bypasses for query-derived schemas.
    Suggested fix: key or invalidate the cache by the full effective access context, including active roles and later access-control changes, or stop serving query-derived schemas from cache without a fresh access check.
Tests
  • ⚠️ Add a focused regression where the same user warms the schema cache with a role that grants SELECT on the source table, then does SET ROLE NONE and repeats the same insert. That is the minimal proof that cached query-derived schemas still honor the current RBAC state.
Final Verdict

❌ Changes requested. The userless execution path is fixed and the old consumer-path test gap is covered, but the cache still reuses query-derived schemas across different effective privilege sets for the same user.

LLVM Coverage Report

⚠️ No coverage measurement for commit dfcde89: incomplete coverage measurement: 1 of 21 shard profiles are missing: LLVM_COVERAGE_FILE_it_7.profdata.

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels Sep 2, 2026
Avogar and others added 2 commits September 2, 2026 19:12
The access check added in the previous commit lives in `querySchema`, which
`handleSchemaSourceQuery` calls only on a cache miss, and the cache file name is
derived from the schema query text alone. A warm cache therefore served the schema
to any principal supplying the same text, skipping the check entirely: it let a
background task through again, and it let a user without the grants reuse an entry
cached by someone who had them.

Refuse a context without a user before the cache lookup, and add the user id to the
hash of the cache file name. Entries written before this change no longer match, so
they are not reused after an upgrade.

The exception no longer claims the cause is a background task: the context also has
no user when `INSERT` data is parsed on the client side.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The stateless test covers the output path, a `Buffer` background flush, while a
streaming consumer reaches `FormatSchemaInfo::querySchema` through the input format.
Cover that path on the engine where the setting is actually reachable: `FileLog`
never applies the format settings from its `CREATE TABLE` query, so
`format_schema_source` never gets there.

The second test covers the schema cache: a user without the grants on the table the
schema query reads stays denied after another user has warmed the cache with the
same query text.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
auto file_name = generateSchemaFileName(format_schema, default_file_extension);
/// The query is access-checked only when it is actually executed, so an entry cached for one user
/// must not be served to another one.
auto file_name = generateSchemaFileName(format_schema, default_file_extension, toString(*user_id));

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.

user_id is still too coarse for this cache key. A user's effective access can change without the user_id changing: SET ROLE NONE, different external or authentication-limited roles, or a later GRANT/REVOKE. On a cache hit we skip querySchema, so a schema cached while the user could read the source table stays reusable after those access changes.

Concrete trace: grant SELECT on the schema table only through a role, run one INSERT ... format_schema_source='query' with that role enabled, then SET ROLE NONE and rerun the same INSERT. getUserID() is unchanged, so this hits the same cache file and accepts a schema the current access context is no longer allowed to fetch. That leaves the RBAC bypass in place for same-user role transitions.

Please either key or invalidate the cache by the full effective access context, including changes in active roles and later access-control updates, or stop reusing query-derived schemas without a fresh access check. A focused regression that warms the cache, does SET ROLE NONE, and reruns the same insert would catch it.

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.

This particular cache isn't a problem for that case.

@clickhouse-gh

clickhouse-gh Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing dfcde89d2 with master 13c33af5e (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of cda48f97d; 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

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

Job report

@alexey-milovidov

Copy link
Copy Markdown
Member

Thanks! This is proof that my point #75023 (comment) was right. It was not good to ignore it, @tavplubix, @tuanpach.

@alexey-milovidov alexey-milovidov self-assigned this Sep 3, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 3, 2026
Merged via the queue into master with commit a828c13 Sep 3, 2026
174 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-format-schema-source-query-access branch September 3, 2026 11:08
@robot-ch-test-poll robot-ch-test-poll added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Sep 3, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 3, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 3, 2026
Backport #117737 to 26.8: Do not run the format_schema_source='query' schema query without a user
clickhouse-gh Bot pushed a commit that referenced this pull request Sep 3, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 3, 2026
Backport #117737 to 26.7: Do not run the format_schema_source='query' schema query without a user
clickhouse-gh Bot pushed a commit that referenced this pull request Sep 3, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 3, 2026
Backport #117737 to 26.6: Do not run the format_schema_source='query' schema query without a user
robot-clickhouse added a commit that referenced this pull request Sep 3, 2026
Cherry pick #117737 to 26.3: Do not run the format_schema_source='query' schema query without a user
robot-clickhouse added a commit that referenced this pull request Sep 3, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 3, 2026
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Sep 4, 2026
alexey-milovidov added a commit that referenced this pull request Sep 4, 2026
Backport #117737 to 26.3: Do not run the format_schema_source='query' schema query without a user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-critical-bugfix 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.

5 participants