Repository navigation
Do not run the format_schema_source='query' schema query without a user - #117737
Conversation
`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]>
|
Workflow [PR], commit [dfcde89] Summary: ✅
AI ReviewSummaryThis PR closes the obvious userless-execution hole in PR Metadata
Findings❌ Blockers
Tests
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 |
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)); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
This particular cache isn't a problem for that case.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units42 translation units recompiled, 205 s compile time in total, 42 of them have a recent master baseline. |
|
Thanks! This is proof that my point #75023 (comment) was right. It was not good to ignore it, @tavplubix, @tuanpach. |
Backport #117737 to 26.8: Do not run the format_schema_source='query' schema query without a user
… schema query without a user
Backport #117737 to 26.7: Do not run the format_schema_source='query' schema query without a user
… schema query without a user
Backport #117737 to 26.6: Do not run the format_schema_source='query' schema query without a user
Cherry pick #117737 to 26.3: Do not run the format_schema_source='query' schema query without a user
… schema query without a user
…ce='query' schema query without a user
Backport #117737 to 26.3: Do not run the format_schema_source='query' schema query without a user
FormatSchemaInfo::querySchemafell back to the global context when the thread had no querycontext. A context without a user has full access, so the schema query, which comes from the
user-controlled
format_schemasetting, was executed with all privileges. Background tasks areaffected: streaming engine consumers (
Kafka,NATS,RabbitMQ,ObjectStorageQueue) andBufferbackground flushes build a query context of their own from the global context and nevercall
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):
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 abackground task, such as a streaming engine consumer (
Kafka,NATS,RabbitMQ,ObjectStorageQueue) or aBufferbackground flush. The schema query is now rejected there; useformat_schema_source='file'or'string'instead, or pre-populate the schema cache.Workflow [PR]
Sync PR [sync-upstream/pr/117737]
Version info
26.9.1.586(included in26.9and later)26.8.3.21,26.7.7.14,26.6.5.38,26.3.31.8