Skip to content

Text index: make mergeTreeTextIndex stricter when a row policy applies - #119032

Merged
ahmadov merged 19 commits into
masterfrom
ahmadov/mergetreetextindex-row-policy
Sep 15, 2026
Merged

ahmadov merged 19 commits into
masterfrom
ahmadov/mergetreetextindex-row-policy

Conversation

@ahmadov

@ahmadov ahmadov commented Sep 9, 2026 •

Copy link
Copy Markdown
Member

Previously, reading the text index was denied only when the row policy referenced an indexed column. But the index contains tokens of all rows, including those hidden by a policy on any other column, so the tokens of hidden rows were exposed.

Now reading from mergeTreeTextIndex is not allowed whenever a row policy applies to the table, regardless of which columns it uses.

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):

Make mergeTreeTextIndex stricter with row policies: reading the text index is now denied whenever a row policy applies to the table, regardless of which columns the policy uses.


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

Version info

  • Merged into: 26.9.1.1560 (included in 26.9 and later)
  • Backported to: 26.8.7.7, 26.7.11.6, 26.6.9.7, 26.3.33.101

Previously, reading the text index was denied only when the row policy
referenced an indexed column. But the index contains tokens of all rows,
including those hidden by a policy on any other column, so the tokens of
hidden rows were exposed.

Now reading from `mergeTreeTextIndex` is not allowed whenever a row
policy applies to the table, regardless of which columns it uses.

Closes ClickHouse/clickhouse-private#69196.
@ahmadov ahmadov added the comp-text-index Full-text search: inverted/text index in MergeTree (tokenizers, posting lists, GIN-style index bu... label Sep 9, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a1aab05]

Summary: ❌

job_name test_name status info comment
Build profile diff ERROR

AI Review

Summary

This PR fixes the main row-policy leak in mergeTreeTextIndex and closes several metadata / persisted-wrapper carriers around the table function, but one distributed RBAC gap remains: the new secondary-query guard still trusts every TCP_INTERSERVER hop even when the initiator's current role set was not transmitted to the shard. That can make a cluster(...) read evaluate row policies under the wrong roles and re-expose tokens from rows the initiating query should not see, so the PR is not ready to merge yet.

Findings
❌ Blockers
  • [src/Storages/StorageMergeTreeTextIndex.cpp:437-442] The new SECONDARY_QUERY gate fails open for interserver hops that did not carry current_roles. Session::makeQueryContextImpl only reenacts the initiator's roles when query_context->getClientInfo().current_roles.has_value() (src/Interpreters/Session.cpp:784-805); otherwise the shard falls back to the user's default roles, and ClientInfo treats nullopt as "not sent / keep defaults" (src/Interpreters/tests/gtest_client_info_read.cpp:349-350). With push_external_roles_in_interserver_queries = 0, or during a hop to a peer older than DBMS_MIN_PROTOCOL_VERSION_WITH_INTERSERVER_CURRENT_ROLES, a restrictive non-default role can silently stop applying on the shard and mergeTreeTextIndex will expose tokens from hidden rows.
    Suggested fix: require transmitted current_roles for the TCP_INTERSERVER path as well, and add a regression test that runs SET ROLE <narrow> over test_cluster_interserver_secret with role propagation disabled and expects ACCESS_DENIED.
Tests
  • Existing coverage exercises ordinary access, metadata, remote, cluster, and persisted-definition carriers, but it does not cover the role-scoped interserver case where the initiating role set is absent on the shard.
Final Verdict

Changes requested.

LLVM Coverage Report

Measured on commit a1aab05.

Metric Baseline Current Δ
Lines 88.40% 88.40% +0.00%
Functions 91.70% 91.70% +0.00%
Branches 80.50% 80.50% +0.00%

Changed lines: Changed C/C++ lines covered: 57/62 (91.94%) · Uncovered code

Full report · Diff report

@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 9, 2026
@CurtizJ CurtizJ self-assigned this Sep 9, 2026
@ahmadov
ahmadov enabled auto-merge September 9, 2026 15:12
@clickhouse-gh

clickhouse-gh Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing commit a1aab05f231660f619b2e00a4642666d9a7af5e3 with master failed: RuntimeError: CI logs cluster query failed: SELECT count() AS c FROM binary_sizes W….

See the job log for details.

@ahmadov
ahmadov deleted the ahmadov/mergetreetextindex-row-policy branch September 15, 2026 22:45
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 15, 2026
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Sep 15, 2026
robot-ch-test-poll2 added a commit that referenced this pull request Sep 16, 2026
Cherry pick #119032 to 26.6: Text index: make `mergeTreeTextIndex` stricter when a row policy applies
robot-clickhouse added a commit that referenced this pull request Sep 16, 2026
robot-ch-test-poll2 added a commit that referenced this pull request Sep 16, 2026
Cherry pick #119032 to 26.7: Text index: make `mergeTreeTextIndex` stricter when a row policy applies
robot-clickhouse added a commit that referenced this pull request Sep 16, 2026
robot-ch-test-poll2 added a commit that referenced this pull request Sep 16, 2026
Cherry pick #119032 to 26.8: Text index: make `mergeTreeTextIndex` stricter when a row policy applies
robot-clickhouse added a commit that referenced this pull request Sep 16, 2026
PedroTadim added a commit that referenced this pull request Sep 17, 2026
Backport #119032 to 26.7: Text index: make `mergeTreeTextIndex` stricter when a row policy applies
PedroTadim added a commit that referenced this pull request Sep 17, 2026
Backport #119032 to 26.8: Text index: make `mergeTreeTextIndex` stricter when a row policy applies
CurtizJ added a commit that referenced this pull request Sep 17, 2026
Backport #119032 to 26.6: Text index: make `mergeTreeTextIndex` stricter when a row policy applies
PedroTadim added a commit that referenced this pull request Sep 17, 2026
Cherry pick #119032 to 26.3: Text index: make `mergeTreeTextIndex` stricter when a row policy applies
PedroTadim added a commit that referenced this pull request Sep 17, 2026
Resolutions:
- `TableFunctionMergeTreeTextIndex.cpp`: dropped `dependsOnCurrentUserGrants`.
  The virtual does not exist on `ITableFunction` here, nor on any release
  branch; it and its `InterpreterCreateQuery` consumer are master-only, so the
  `override` would not compile. Kept `canBeUsedToCreateTable`, which the
  cherry-pick carries as context and which the branch's `InterpreterCreateQuery`
  does consume, as the merged 26.6 and 26.8 backports did. 26.3's
  `MergeTreeIndexFactory::get` takes only the index description, so
  `resolveIndex` calls the single-argument form and the `MergeTreeData` cast
  that master needs only to reach `getSettings()` is dropped.
- `StorageMergeTreeTextIndex.{h,cpp}`: kept `checkAccess` and dropped
  `createVirtuals`, which the cherry-pick carries as context and 26.3 does not
  have. 26.3 still names the read entry point `read`, not `readImpl`, so that
  name is kept.

`05153_text_index_mergeTreeTextIndex_create_table_as` is reduced to the form
26.6 and 26.8 carry: its two nested-`remote` cases assert the refusal that
`dependsOnCurrentUserGrants` drives, which this branch cannot do. All twelve
test files are now identical to the merged 26.8 backport.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
robot-clickhouse added a commit that referenced this pull request Sep 17, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 17, 2026
PedroTadim added a commit that referenced this pull request Sep 17, 2026
Backport #119032 to 26.3: Text index: make `mergeTreeTextIndex` stricter when a row policy applies
kodiakhq Bot pushed a commit to ClickHouse/ClickStack that referenced this pull request Oct 2, 2026
…index read fails (#3268)

## Summary

`Metadata.getMapKeys` returned `[]` whenever its `mergeTreeTextIndex` query threw, so the rollup table and bounded `mapKeys` scan paths never ran.

Since ClickHouse/ClickHouse#119032 (backported to 26.6), ClickHouse refuses `mergeTreeTextIndex` with `ACCESS_DENIED` (497) on **any** table with a row policy. On those tables search bar autocomplete showed no Map keys. The same short-circuit also hides keys on Distributed tables (`BAD_ARGUMENTS`, #2880).

The fix: both text-index `catch` blocks in `getMapKeys` now log and fall through to the next strategy, the same as when the index query succeeds with zero keys. This makes `getMapKeys` consistent with `getMapTextIndexKeyValues` and `getTextIndexKeyValues`, which already skip a failed strategy. The scan reads the table itself, so it respects row policies. Its result is cached under the normal key, so the failing index read isn't re-issued on later calls.

### Row policies

Rollup tables (`metadataMaterializedViews`) are separate tables, and a row policy on the source table doesn't cover them. When the text index read fails with `ACCESS_DENIED`, we know a policy applies, so `getMapKeys` skips the rollup and reads keys only from the source-table scan, which ClickHouse filters by the policy. Failures that aren't access denials (e.g. `BAD_ARGUMENTS` on Distributed tables) still try the rollup first. Detection uses the client error's `type`/`code` (`ACCESS_DENIED`/497), with a message match as a fallback (`isAccessDeniedError` in `clickhouse/index.ts`).

The shipped OTel rollups only index native columns, so on the default schema they never return Map keys. This only matters for custom rollups that aggregate Map columns. Some paths still read the rollup with no policy signal: no text index, no `system.data_skipping_indices` grant, or ClickHouse < 26.3. This PR doesn't change them, so admins using custom Map-indexing rollups should mirror the source's row policies onto the rollup tables.

### Aborts and slow reads

If the caller aborts during the text index read, `getMapKeys` rethrows instead of falling through. Otherwise the rollup would run with a dead signal, reject at once, and have its `[]` cached under the rollup key with no TTL. The index read is capped at `max_execution_time: 15` with `timeout_overflow_mode: 'throw'`, matching the rollup and scan caps. A slow read falls through to rollup/scan promptly, and `'throw'` (not `'break'`) means a timeout never caches a partial key list.

Tests cover a key index failure, a kv index failure, the rollup being skipped after an ACCESS_DENIED (from either the error `cause` or its message), the rollup still being tried for other failures, the 15s throw-on-timeout cap and abort signal, a timeout falling through to the rollup, an abort rethrowing without running or caching the fallback, and the fallback result being cached. They fail when the fix is reverted.

Follow-ups tracked on the Linear ticket, not in this PR:
- File a ClickHouse feature request for a policy-aware text-index read (refs ClickHouse/ClickHouse#119032, ClickHouse/ClickHouse#99507)
- Confirm whether ClickStack Cloud-managed schemas or default roles can hit this row-policy path
- A per-source toggle to skip the Map key text index was considered and left out: with fall-through, the only cost is one failed query before the result is cached

### How to test on Vercel preview

N/A — non-UI change. To reproduce locally, follow the steps in #3265: create a row policy on `otel_logs` for a user that can read `system.data_skipping_indices`, then type `LogAttributes.` in the search bar. Keys now appear.

### References

- Linear Issue: https://linear.app/clickhouse/issue/HDX-5598/key-discovery-fails-with-access-denied-497-under-row-policies-add-fall
- Related PRs: Fixes #3265, related #2880
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-text-index Full-text search: inverted/text index in MergeTree (tokenizers, posting lists, GIN-style index bu... 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.

7 participants