Repository navigation
Fix currentUser() being empty during async_insert flush - #107541
alexey-milovidov merged 3 commits into
Conversation
On the async_insert flush path, AsynchronousInsertQueue::processData built the flush context from the server global context (empty ClientInfo) and only called Context::setUser, which restores the access-control identity (user id, profiles, roles, default database) but not the ClientInfo user-name strings. As a result currentUser()/user()/SESSION_USER/authenticatedUser() evaluated to an empty string in DEFAULT/MATERIALIZED column expressions and in the materialized views triggered by the flush, silently corrupting audit/ownership columns. The synchronous path sets these names via Session::makeQueryContextImpl and is unaffected. Capture the originating query's ClientInfo identity (current_user, initial_user, authenticated_user) into AsynchronousInsertQueue::InsertQuery at push time and restore it onto the flush context in processData. The identity is part of the batching key so inserts from different identities are never coalesced into one flush. Closes ClickHouse#107530 Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-5:20260615-160100 |
|
cc @CheSema @CurtizJ — could you review this? It restores the originating query's ClientInfo identity (current_user/initial_user/authenticated_user) on the async_insert flush context, so currentUser()/user()/authenticatedUser() and the MVs the flush triggers see the inserting user instead of an empty string. The identity is folded into the InsertQuery batching key so inserts from different users are never coalesced. |
|
Workflow [PR], commit [12e12d6] Summary: ✅
AI ReviewSummaryThis PR restores the originating query Missing context / blind spots
Final VerdictStatus: ✅ Approve |
The async insert queue is indexed by InsertQuery::hash alone
(QueueIteratorByKey = unordered_map<UInt128, QueueIterator>), and equality
is never consulted on the push path. SipHash::update(String) streams only
the bytes, so concatenating the three identity strings without a delimiter
makes ("a","a","aaa") and ("aa","aa","a") hash to the same value: the
second insert would be appended to the first user's pending batch and
flushed under the wrong ClientInfo, which is the same wrong-user corruption
this change set is meant to prevent.
Hash each identity field with its length prefix so distinct triples produce
distinct hashes. Add a key unit test covering the collision boundary cases.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
…t flush The existing 04338 test only asserts currentUser() (ClientInfo.initial_user), so it would still pass if setAuthenticatedUserName were removed or mis-wired: authenticatedUser() reads a different field (ClientInfo.authenticated_user). This test uses EXECUTE AS so currentUser() and authenticatedUser() diverge (impersonated user vs originally authenticated user) and asserts both are restored independently on the async-insert flush path, in DEFAULT columns and in a materialized view. Verified the async/MV assertions fail when setAuthenticatedUserName is removed and pass with the fix. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (follow-up: authenticatedUser test coverage, commit 12e12d6)
Session id: cron:clickhouse-worker-slot-1:20260615-181200 |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 81/90 (90.00%) | Lost baseline coverage: none · Uncovered code |
alexey-milovidov
left a comment
There was a problem hiding this comment.
This is good! Thank you!
|
CI finished on HEAD
No PR-caused failures. The async_insert currentUser/authenticatedUser fix, the length-prefixed identity hashing, and both regression tests (04338 + 04339 EXECUTE AS) pass. Ready for review. Session id: cron:our-pr-ci-monitor:20260615-233000 |
…flush context On 26.3 `async_insert` is enabled by default, and the flush context of `AsynchronousInsertQueue` is built from the global context with only `Context::setUser`, so its `ClientInfo` user names are empty. A direct `INSERT` into a `Distributed` table over a cluster with an interserver `<secret>` was therefore flushed to the shard with an empty `initial_user`, which the shard executes in interserver mode with full access. This is the same hole that #117798 closes for the definer of `SQL SECURITY DEFINER` views, and `test_streaming_engines_definer_interserver_secret::test_rabbitmq[tenant]` caught it in its control case: the tenant's direct insert was accepted on the shard. Bring the fix from #107541 (merged to master in edb9295): capture `current_user`, `initial_user` and `authenticated_user` in the async insert queue key at push time and restore them on the flush context. The only conflict is in `InsertQuery::toTupleCmp`, where 26.3 already uses `query_str_with_secrets` instead of `query_str`. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=128668&sha=0dc411340520ff9f52dfdb04638aa63196712372&name_0=BackportPR&name_1=Integration%20tests%20%28amd_tsan%2C%203%2F6%29 PR: #128668 Related: #107541 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…flush context On 26.3 `async_insert` is enabled by default, and the flush context of `AsynchronousInsertQueue` is built from the global context with only `Context::setUser`, so its `ClientInfo` user names are empty. A direct `INSERT` into a `Distributed` table over a cluster with an interserver `<secret>` was therefore flushed to the shard with an empty `initial_user`, which the shard executes in interserver mode with full access. This is the same hole that #117798 closes for the definer of `SQL SECURITY DEFINER` views, and `test_streaming_engines_definer_interserver_secret::test_rabbitmq[tenant]` caught it in its control case: the tenant's direct insert was accepted on the shard. Bring the fix from #107541 (merged to master in edb9295): capture `current_user`, `initial_user` and `authenticated_user` in the async insert queue key at push time and restore them on the flush context. The only conflict is in `InsertQuery::toTupleCmp`, where 26.3 already uses `query_str_with_secrets` instead of `query_str`. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=128668&sha=0dc411340520ff9f52dfdb04638aa63196712372&name_0=BackportPR&name_1=Integration%20tests%20%28amd_tsan%2C%203%2F6%29 PR: #128668 Related: #107541 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #107530
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
currentUser(),user(),SESSION_USERandauthenticatedUser()evaluating to an empty string on the asynchronous insert flush path (withasync_insert = 1). This affectedDEFAULT/MATERIALIZEDcolumn expressions and materialized views that reference these functions, which silently stored an empty string instead of the inserting user.Description
Reported in #107530 (confirmed on master, 26.5, 26.4, 26.3, 26.2, 26.1; present since at least v24.1).
Root cause: with
async_insert = 1, the rows are written later on a background flush thread.AsynchronousInsertQueue::processDatabuilds the flush context from the server global context (which carries an emptyClientInfo) and then calls onlyContext::setUser.setUserrestores the access-control identity (user id, profiles, roles, default database) but not theClientInfouser-name strings (current_user/initial_user/authenticated_user).currentUser()readsClientInfo.initial_user, so it returned an empty string on the flush path and in every materialized view the flush triggered. The synchronous path sets these names inSession::makeQueryContextImpland was unaffected, so the sameDEFAULT currentUser()column recorded the user forasync_insert = 0but an empty string forasync_insert = 1.Fix: capture the originating query's
ClientInfoidentity (current_user,initial_user,authenticated_user) intoAsynchronousInsertQueue::InsertQuerywhen the insert is queued, and restore it onto the flush context inprocessData. The identity is included in the batching key so inserts issued by different users (e.g. impersonation /EXECUTE AS, forwarded distributed queries) are never coalesced into a single flush.Added a stateless regression test (
04338_async_insert_current_user) covering aDEFAULT currentUser()column, aMATERIALIZED currentUser()column, and a materialized view selectingcurrentUser(), asserting the async-flushed value matches the inserting user. The test fails without the fix (async rows record an empty string) and passes with it.Version info
26.6.1.946