Skip to content

Hand the connection to the built-in web UI pages over postMessage - #115067

Open
alexey-milovidov wants to merge 94 commits into
masterfrom
postmessage-auth
Open

alexey-milovidov wants to merge 94 commits into
masterfrom
postmessage-auth

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 17, 2026 •

Copy link
Copy Markdown
Member

The built-in web UI pages — /play, /docs, /dashboard, /schema, /merges, /jemalloc, /processors-profile, /binary — take their connection from their own query string, which cannot carry a password: a URL ends up in browser history, Referer headers, server access logs and proxy logs. Only /webterminal could be handed credentials by the page embedding it, in a postMessage message.

Every page now speaks that same exchange:

page -> host   {type: 'clickhouse-hello'}
host -> page   {type: 'clickhouse-credentials', url, user, password}

The page announces itself as soon as it can apply a connection, and the host answers. The page has to speak first because a host that opened it with window.open cannot observe when a cross-origin document became ready, and an embedded page would otherwise race its host's load handler.

Having adopted a connection, each page redoes what it had already loaded with the previous one, so the handover also works when it arrives after the page started querying: /play re-probes the credentials, /docs rebuilds its cross-link name cache and re-runs the displayed search, /dashboard and /processors-profile chain their reload behind the load already in flight (two concurrent loads would otherwise duplicate charts, or let a stale failure overwrite a good listing), /schema and /jemalloc reload, and /binary drops its cached tiles and redraws.

A handed-over server URL may carry a query string of its own — a default database, or a proxy route that selects a cluster or a namespace, as in https://proxy.example/clickhouse/?cluster=a. /play and /docs already appended their request parameters to such an address with &; /binary, /dashboard, /merges, /schema, /jemalloc and /processors-profile started a second ?, which is not a separator — their first parameter was folded into the value of the last parameter of the endpoint, and the request went somewhere other than the connection just handed over. All of them now pick the separator the same way.

Opening /docs from /play

The Documentation icon in the /play sidebar is the first user of the handover. A plain click opens /docs through window.open — the anchor's rel=noopener would sever the handle — and hands the whole connection over, so the documentation search runs against the same server as the same user without the password appearing in a URL. A middle or modifier click keeps the browser's native new-tab handling, which carries ?user= only, since such a window has no link back to /play.

Security

A reply is accepted only from the one window that opened or embeds the page, and only from an exact origin: the page's own, https://console.clickhouse.cloud, https://console.clickhouse-staging.com or https://clickhouse.com. The list is exact rather than a domain suffix on purpose — every ClickHouse Cloud service endpoint is a *.clickhouse.cloud host that serves content of its owner's choosing, and a trusted host can aim a page at a server of its own through url, which the user may then type credentials into believing it is still their own service. webterminal.html gated its own exchange on such a suffix and now uses the same exact list.

url is optional and defaults to the server the page already points at, but the credentials are replaced wholesale: a message that names only a server cannot attach credentials it did not carry to it, which is what keeps a password filled in by the browser's password manager for this page's origin from following a url elsewhere. Outbound, credentials are always sent with an exact target origin, never '*'; the readiness announcement carries no secrets, only the fact that the page exists, which its host knows already.

Verified in Chromium against a mock endpoint: the /play → /docs handover, the handover into an iframe for all eight pages, rejection of a handover from an untrusted origin and from a same-origin window that is not the page's host, the wholesale credential replacement, and the exactness of the origin list for all nine pages (https://myserviceendpoint.clickhouse.cloud, https://evil.clickhouse.com, http://console.clickhouse.cloud and https://console.clickhouse.cloud.evil.com are all rejected). The committed test asserts that every served page implements both halves of the exchange, that its origin list stayed exact, and that it appends its request parameters to a query-bearing endpoint rather than starting a second query string. /webterminal is covered by it too, with its own webterminal-hello / webterminal-credentials markers: this change replaced its endsWith('.clickhouse.cloud') trust with the same exact list, and the check rejects a bare host-name comparison as well, which would ignore the scheme. The protocol is documented in docs/concepts/features/interfaces/web-ui-embedding.mdx.

Changelog category (leave one):

  • Improvement

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

The built-in web UI pages (/play, /docs, /dashboard, /schema, /merges, /jemalloc, /processors-profile, /binary) can now be handed their connection — server URL, user and password — in a postMessage message by the page that opened or embedded them, instead of taking it from their own query string, where a password would be recorded in browser history, referrer headers and access logs. /play uses this when opening /docs, so the documentation search connects to the same server as the same user. A page accepts a connection only from the window that opened or embeds it, and only from an exact list of trusted origins.


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

The built-in web UI pages take their connection from their own query string,
which cannot carry a password: a URL ends up in browser history, referrer
headers, server access logs and proxy logs. Only `/webterminal` could be handed
credentials by the page embedding it, over `postMessage`.

Every page now speaks the same exchange. It announces itself to the window that
opened or embedded it with `clickhouse-hello`, and adopts the server URL, user
and password from the `clickhouse-credentials` reply, redoing whatever it had
already loaded with the previous connection: `/play` re-probes the credentials,
`/docs` rebuilds its name cache and re-runs the displayed search, `/dashboard`
and `/processors-profile` reload behind the load already in flight, `/schema`
and `/jemalloc` reload, and `/binary` drops its cached tiles and redraws.

`/play` uses it for the Documentation icon: a plain click opens `/docs` through
`window.open`, so a handle on it remains and the whole connection can be handed
over - the documentation search then runs against the same server as the same
user, with no password in a URL. A middle or modifier click keeps the anchor's
native new-tab path, which carries `?user=` only, because such a window has no
link back to `/play`.

The reply is accepted only from that one host window, and only from an exact
origin: the page's own, `https://console.clickhouse.cloud`,
`https://console.clickhouse-staging.com` or `https://clickhouse.com`. The
credentials are replaced wholesale, so a message that names only a server cannot
attach credentials it did not carry to it - they may have been filled in by the
browser's password manager for this page's origin.

`webterminal.html` gated its own exchange on a `*.clickhouse.cloud` domain
suffix, which trusts every Cloud service endpoint, and a service endpoint serves
content of its owner's choosing. It now uses the same exact list.

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

clickhouse-gh Bot commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [25b2403]

Summary: ✅


AI Review

Summary

Reviewed #115067 at 25b24030dd3512e684f3635ec02fedd4430ff3cd, including all prior discussion and the current code. The change adds connection handover through postMessage to the built-in web interfaces, restricts trusted senders, isolates connection changes, and preserves proxy routing. Prior findings are addressed; no unresolved blockers or majors remain. Asset URL consumers checked include the HTML rewrites, browser URL resolution, embedded JavaScript handler, and /play Trusted Types policy. Local harnesses passed all 164 /play checks and 37 web-terminal checks; the fetched CI reports show no failed tests.

Final Verdict

Status: ✅ Approve

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the unrelated Fast test failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=115067&sha=fe0e9f7e5f444133abd2aa94e5d54361240cb77b&name_0=PR&name_1=Fast%20test. 01528_play emitted a duplicate clickhouse.com line; no matching open issue or fix PR was found.

@mintlify

mintlify Bot commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated
ClickHouse-docs 🟢 Ready View Preview Nov 20, 2026, 1:38 AM

@groeneai

Copy link
Copy Markdown
Collaborator

It is not unrelated: this PR's own diff to programs/server/play.html causes it, though the defect is in the test's oracle rather than in your change.

01528_play.sh is curl .../play | grep -o -F 'clickhouse.com' against a one-line .reference, so it asserts the string occurs exactly once in the whole page. Counts in programs/server/play.html:

  • master: 1 (line 1852, <a href="https://clickhouse.com/">)
  • 5c06ada: 2 (plus line 7894, 'https://clickhouse.com', in TRUSTED_HOST_ORIGINS)

So the test reports intended new content as a failure. CIDB confirms it is deterministic and yours alone: 3 FAIL rows across fe0e9f7 and 5c06ada with the identical +clickhouse.com diff, and zero hits on any other PR or on master in 30 days.

The sibling page tests all survived the same allowlist addition because each greps a page-specific marker with --max-count 1:

test marker master 5c06ada
02952_binary addressToSymbol 2 2
04257_schema_route ClickHouse Schema Visualizer 2 2
03256_merges Merges Visualizer 1 1
04141_webterminal_enabled_by_default Web Terminal 1 1
02389_dashboard 🌚 1 1

binary and schema already carry two occurrences and pass anyway, purely because of the match cap. 01528_play is the only test in the family with neither a page-specific marker nor a cap, which is why it is the only one that broke.

Suggested fix, matching the siblings:

${CLICKHOUSE_CURL} -sS ".../play" | grep -o -F -m1 '<title>ClickHouse Query</title>'

Worth avoiding the cheaper variant: adding only -m1 to the existing pattern makes the test pass on a body that is not the play UI but merely contains the allowlist string, so the assertion goes vacuous. The title marker passes on master and on 5c06ada, does not match dashboard.html or a bare allowlist fragment, reddens when I corrupt the title, and counted exactly 1 in every historical revision of play.html I sampled.

This is the second sighting. The same test failed the same way on 2026-07-15, also from a PR editing play.html, and was written off as the PR's own test, so it recurred.

I can open the test fix as a separate PR, or you can fold the one-line change into this one.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Folded the suggested test correction into 10139fa: 01528_play now asserts the unique /play title instead of the generic clickhouse.com occurrence. The branch was also merged with current master in 51e1a55. Source-level validation confirms the marker matches the reference exactly once; CI will rerun the HTTP test.

Comment thread programs/server/play.html Resolved
Comment thread programs/server/play.html Resolved
Comment thread programs/server/binary.html Resolved
Comment thread programs/server/jemalloc.html Resolved
@clickhouse-gh

clickhouse-gh Bot commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 25b24030d with master c50de3b98 (stripped binary size, per-symbol sizes and ThinLTO time; 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.

Object file sizes

5 object files changed (+147.70 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Server/WebUIRequestHandler.cpp.o 12.39 MiB 12.51 MiB +127.85 KiB (+1.01%)

716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

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

Job report

Comment thread programs/server/play.html Resolved
Comment thread programs/server/play.html Resolved
Comment thread programs/server/play.html Resolved
…dover test

`04502_http_static_compression` and `04504_http_static_compression_fallback`
asserted that `clickhouse.com` occurs exactly once in the whole `/play` page,
the same oracle defect that `01528_play` had: this change adds
`https://clickhouse.com` to `TRUSTED_HOST_ORIGINS`, so the string now occurs
twice and the tests report intended new content as a failure. Assert the unique
`<title>ClickHouse Query</title>` marker with `-m1` instead, exactly as
`01528_play` now does. Adding only `-m1` to the old pattern was avoided on
purpose: it would pass on any body that merely contains the allowlist string.

Also renumber the new test to `05026` - master has grown three unrelated
`04907_` tests since it was written.
Matching with `grep -m1` closes the pipe before `curl` finished writing, so
`curl` reported `Failure writing output to destination`, the same breakage
`01528_play` hit in f530668. The title occurs exactly once in the page,
so the match cap is unnecessary; drop it and let the response drain.
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Pushed eeae73e9eabf:

  • Merged current master.
  • 04502_http_static_compression and 04504_http_static_compression_fallback were the only red tests, deterministically on all five shards, and for the same reason 01528_play was red earlier: both grep the whole /play page for clickhouse.com against a one-line reference, so the https://clickhouse.com entry this change adds to TRUSTED_HOST_ORIGINS makes the string occur twice. They now assert the unique <title>ClickHouse Query</title> marker, matching the fix already applied to 01528_play. Adding a match cap instead was rejected on purpose — it would pass on any body that merely contains the allowlist string. The -m1 variant was also tried and dropped: it closes the pipe early and makes curl report Failure writing output to destination, exactly as in f530668.
  • Renumbered the handover test to 05026_…; master has since grown three unrelated 04907_ tests.

Verified locally against a server built from this branch: 01528_play, 04502_http_static_compression (3 runs), 04504_http_static_compression_fallback (3 runs), 05026_web_ui_postmessage_connection_handover, plus the sibling page tests 02389_dashboard, 02952_binary, 03256_merges, 04141_webterminal_enabled_by_default, 04257_schema_route — all pass.

The remaining AI-review Major (ping gating checkCredentials for a cross-origin url) is answered in the review thread: add_http_cors_header is not consulted for OPTIONS at all — HTTPHandler::handleRequest answers OPTIONS through processOptionsRequest before settings are parsed, using only the http_options_response config headers — so the suggested change is a no-op server-side. The probe it guards is a cross-origin POST with an Authorization header, whose browser preflight hits that very same handler, so the ping is not the stricter check. None of that code is touched by this pull request.

Comment thread programs/server/play.html Resolved
Comment thread programs/server/play.html Resolved
Comment thread programs/server/dashboard.html Resolved
The dashboard names in the selector (`dashboard_queries` and the
`search-options` datalist) were loaded only by `start`, so after a handover
from server A to server B they still described A: dashboards that exist only
on B were submitted as SQL. Now `applyConnectionFromHost` reloads the list
from the new server before reloading the dashboard, and
`populateSearchOptions` drops the previous entries first. The selected
dashboard is resolved to its search query with the previous list and shown
again with the new one, so it stays selected if B has it too, and otherwise
its query runs instead of its name.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=115067&sha=e47530e067b049ca274d2fe0b0cda28eeab6604f&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%2C%20old_compatibility%29 (Logical error: 'Equal values are not contiguous within the range assumed to be sorted', STID 2508-31ed, query SELECT DISTINCT ... FROM (SELECT DISTINCT a, -2 AS x ... ORDER BY x WITH FILL STEP 1023 STALENESS ...) ORDER BY a DESC) and provide a fix in a separate PR. This PR only touches the web UI pages. It may be covered by #118077 (DISTINCT with WITH FILL). If the fix is already in progress, link it here.

@groeneai

Copy link
Copy Markdown
Collaborator

This is the WITH FILL Int8 wrap again, this time through STEP: -2 + 1023 does not fit Int8, so the fill emits -3, -4, ... after -2, the output of ORDER BY x WITH FILL is no longer sorted by x, and the in-order DISTINCT on top trips the assertion. Not #118077; it is covered by your #112650 (open).

Measured with clickhouse local, a Nullable(Int32) table with 100 rows and the failing query:

No separate PR from me.

Comment thread src/Server/WebUIRequestHandler.cpp Resolved
alexey-milovidov and others added 2 commits October 31, 2026 15:44
…om asset URLs

`routingQueryOfRequest` compared the raw parameter names of the page's query
string, so an encoded name such as `%70assword` or `pass%77ord` was copied into
the URLs of the embedded JavaScript and CSS files, while the page itself (via
`URLSearchParams`) recognizes it as `password`. Compare the names decoded the
way the browser decodes them, keeping the original bytes of the retained
routing parameters, and extend the asset regression test with encoded names.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged master (ffd50f90272) and pushed faaa2d381635, which fixes the review thread about percent-encoded credential names in asset URLs.

The only red on 2311aee896b was the prewhere_subcolumns #1 query in Performance Comparison (arm_release, master_head, 4/6), with a 1.4 ms difference on a 6 ms query. The job was also marked disk-almost-full. This PR only changes the web UI pages and their HTTP handlers, which this query does not use, so the result is noise unrelated to the PR. The new run on the merged head will show whether it comes back.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged master (bc3b91f35b7, clean merge, PR diff unchanged; WebUIRequestHandler.cpp and WebTerminalRequestHandler.cpp compile) because the branch had not been updated since 2026-10-31. CI on faaa2d38163 was fully green, and all review threads were already resolved.

alexey-milovidov and others added 2 commits November 13, 2026 12:47
Resolve the conflict in `WebUIRequestHandler.cpp` with the vendoring of the
third-party assets on master: keep `replaceExternalAsset` (which also removes the
`integrity` attribute of the CDN link), but pass it the relative URL made by
`serverAssetURL`, so the links keep the path prefix and the routing parameters of
a reverse proxy. The newly rewritten links in `/binary` (Leaflet) and `/jemalloc`
(uPlot) use `serverAssetURL` as well, for consistency with the other pages.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The test added on master pins root-absolute `/js/...` links in `/binary`,
`/jemalloc` and `/dashboard`. This PR intentionally makes these links relative
to the page (`./js/...` for a top-level route), so that they keep the path
prefix of a reverse proxy.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged master (c89eff2308d) and pushed a2a885f1a25.

  • Resolved the conflict in WebUIRequestHandler.cpp with the vendoring of third-party assets on master (2d2da44). replaceExternalAsset stays, so the integrity attribute is still removed along with the CDN link, but it now gets the relative URL from serverAssetURL. The links that master newly rewrites in /binary (Leaflet) and /jemalloc (uPlot) now use serverAssetURL too, like the other pages.
  • Updated 05627_web_ui_no_external_assets, the test master added. It expected root-absolute "/js/..." links, and this PR deliberately makes them relative ("./js/...").

I tested this locally on a debug build. 05627_web_ui_no_external_assets, 05443_web_ui_assets_behind_proxy_prefix, 05026_web_ui_postmessage_connection_handover, 01528_play, 04502_http_static_compression and 04504_http_static_compression_fallback all pass.

# Conflicts:
#	programs/server/binary.html
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged master (c3ec2bd66a2) and pushed 8fa2794424b to resolve the conflict in programs/server/binary.html. Master (93e7902) now puts the symbol and source line into the /binary popup as text (DOM nodes instead of an HTML string, so encodeXMLComponent is no longer needed in the query). The merge keeps that, and keeps this PR's Authorization: never header and the generation == connection_generation check. The PR diff against master has the same files and line counts as before. I checked the merged play.html against the Trusted Types policy that is now on master: every innerHTML sink goes through trustedHTML, and _assetURL matches the policy's createScriptURL. reconcile_harness.js passes locally on the merged play.html (164 scenarios, including the handover ones), and the inline scripts of binary.html, docs.html, and play.html pass node --check.

@clickhouse-gh clickhouse-gh Bot added comp-web-ui Web UI embedded in the server: Play, the dashboards, the merges and processors visualisers, the w... and removed comp-protocols Server wire protocols + request/response handling (native TCP, HTTP, MySQL/PostgreSQL wire protoc... labels Nov 19, 2026
Master moved the documentation under `docs/concepts/features/connect-to-clickhouse/`;
the new embedding page now lives at `built-in-web-interfaces/web-sql/embedding.mdx`
next to the other Web SQL UI pages, with its links updated to the new paths.

This branch was successfully deployed

1 active deployment
staging - docs — 25b24030 Deployed Nov 20, 2026 by mintlify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-web-ui Web UI embedded in the server: Play, the dashboards, the merges and processors visualisers, the w... pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants