Skip to content

Web Terminal: accept cross-origin postMessage from clickhouse.com/cloud - #105191

Merged
alexey-milovidov merged 2 commits into
masterfrom
webterminal-cross-origin-postmessage
May 18, 2026
Merged

alexey-milovidov merged 2 commits into
masterfrom
webterminal-cross-origin-postmessage

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented May 18, 2026 •

Copy link
Copy Markdown
Member

Summary

The web terminal (programs/server/webterminal.html) uses postMessage for three things:

  • inbound: receive webterminal-credentials and webterminal-refit from the parent
  • outbound: post webterminal-escape (on Escape key) and webterminal-closed (on WebSocket close) to the parent

Both directions previously assumed the parent is same-origin:

// inbound
if (e.origin !== window.location.origin) return;
// outbound
window.parent.postMessage({...}, window.location.origin);

That second arg is the target origin — the browser drops the post if the parent's actual origin doesn't equal it. So when the terminal is embedded from a different origin (e.g. https://clickhouse.com embedding https://play.clickhouse.com/webterminal), inbound is rejected and outbound is silently dropped. The terminal effectively can't be embedded outside play.clickhouse.com.

Changes

  • Inbound allowlist: accept messages from same-origin and from any https origin whose hostname is clickhouse.com, clickhouse.cloud, or a subdomain of either. Apex domains are matched by equality; subdomains by endsWith('.clickhouse.com') / endsWith('.clickhouse.cloud'). URL parsing anchors on hostname so suffixes like evil.clickhouse.com.attacker.tld are not matched.
  • Outbound parent origin: remember the parent origin from the first valid inbound message (including an optional webterminal-hello handshake), and post back to that exact origin. If we've never heard from a trusted parent, outbound posts are skipped rather than leaked to '*'.

Why an allowlist instead of '*'

'*' would let any site iframe the terminal and feed it forged webterminal-credentials. The user typing their own credentials into an attacker-controlled embed is "their problem," but accepting credentials only from origins ClickHouse controls is a cheap and clear improvement.

Compatibility

  • Same-origin embeds (existing play.html flow) keep working — same-origin is the first branch in the trust check, and the outbound target is still the same string.
  • Cross-origin embeds from *.clickhouse.com / *.clickhouse.cloud now work. Other origins are still rejected.

Test plan

  • Same-origin: load /play on a server with allow_experimental_webterminal=1, press the terminal toggle, type a query, press Escape — terminal hides as before.
  • Cross-origin: from https://clickhouse.com (marketing site PR: https://github.com/ClickHouse/marketing-website/pull/1448), open the dropdown terminal, verify webterminal-refit is applied (terminal sizes correctly), and Escape closes the panel.
  • Untrusted origin: embed the terminal from a non-clickhouse origin and verify postMessage is ignored both ways (devtools console shows no credential acceptance).

Changelog category (leave one):

  • Experimental Feature

Changelog entry:

The experimental web terminal (allow_experimental_webterminal) can now be embedded cross-origin from clickhouse.com, clickhouse.cloud, and their subdomains. Previously, postMessage from any non-same-origin parent was rejected on inbound and silently dropped on outbound, making the terminal effectively un-embeddable outside play.clickhouse.com. The accepted origin list is an explicit allowlist (https only, hostname-anchored), so untrusted origins are still rejected.

Documentation entry for user-facing changes

  • Documentation is unchanged: the experimental setting and message protocol are not user-facing.

The web terminal page exchanges postMessage with its embedding parent for
three things: receiving credentials, receiving refit requests, and
notifying the parent on Escape / WebSocket close. Both directions
previously required the parent to be same-origin: the inbound check
rejected anything where e.origin !== window.location.origin, and the
outbound calls used window.location.origin as targetOrigin, which the
browser silently drops when the parent is on a different origin.

That makes the terminal effectively non-embeddable from clickhouse.com
or any other site outside play.clickhouse.com. Loosen the policy:

  - Inbound: accept messages from same-origin and from any https origin
    whose hostname is (or is a subdomain of) clickhouse.com /
    clickhouse.cloud.
  - Outbound: remember the parent origin from the first valid message
    (including an optional 'webterminal-hello' handshake) and post back
    to that exact origin, so the browser delivers it.

Credentials and other messages from untrusted origins are still
rejected. Apex domains are matched explicitly; URL parsing anchors on
hostname so suffixes like evil.clickhouse.com.attacker.tld are not
matched.
@clickhouse-gh

clickhouse-gh Bot commented May 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [23fbd3c]

Summary: ✅


AI Review

Summary

This PR updates webterminal.html to support cross-origin embedding from trusted ClickHouse domains by validating parent message origins against an allowlist, persisting the trusted parent origin for outbound messages, and enforcing e.source === window.parent for inbound messages. The previously reported sender-window validation gap is fixed in the current head, and no additional correctness or security issues were identified in the changed code.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-experimental Experimental Feature label May 18, 2026
Comment thread programs/server/webterminal.html Resolved
Reviewer pointed out that validating `e.origin` alone allows any window
from a trusted origin (e.g. one obtained via `window.open` by the parent)
to drive `webterminal-credentials` and `webterminal-refit`. Tighten the
check so only messages from the embedding parent are accepted.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

@groeneai, please investigate the failure: 02180_group_by_lowcardinality in https://github.com/ClickHouse/ClickHouse/actions/runs/26010805714/job/76457429680 — this PR only modifies programs/server/webterminal.html and the test failed 18 times on master and 109 times total on 2026-05-18 across many PRs, so it is broken on master and unrelated to this change. Please provide a fix in a separate PR. If a fix is already in progress, link it here.

@groeneai

Copy link
Copy Markdown
Collaborator

@alexey-milovidov — 02180_group_by_lowcardinality is already fixed by your own merged PR #105194. PR #105191 just needs to merge master to pick it up.

Evidence:

Sibling reply on PR #105176 yesterday: #105176 (comment)

@alexey-milovidov alexey-milovidov self-assigned this May 18, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue May 18, 2026
Merged via the queue into master with commit efb3583 May 18, 2026
166 checks passed
@alexey-milovidov
alexey-milovidov deleted the webterminal-cross-origin-postmessage branch May 18, 2026 23:58
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-experimental Experimental Feature 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.

3 participants