Repository navigation
Add web terminal interface to clickhouse-server - #100277
Conversation
Add a WebSocket-based terminal protocol (webterminal) that runs a pseudoterminal with embedded clickhouse-client functionality, accessible via a browser at /webterminal. The implementation reuses the same `PtyClientDescriptorSet` and `ClientEmbeddedRunner` infrastructure as the SSH protocol, but connects via WebSocket instead of SSH. Authentication works the same way as in the HTTP protocol (URL params, Basic auth, X-ClickHouse headers). The HTML page uses xterm.js for terminal emulation and connects to the server via WebSocket, bridging data bidirectionally between the browser and the PTY. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
|
Workflow [PR], commit [8ac8ddb] Summary: ✅ AI ReviewSummaryThe PR adds an experimental browser Findings❌ Blockers
Final VerdictStatus: Minimum required actions:
|
- Remove the overlay login form; the terminal fills the entire page - Auto-connect with default credentials (or credentials from URL params) - On auth failure, prompt for username/password directly in the terminal - If user was provided in the URL, only prompt for password - After session ends, prompt for credentials again to reconnect - Hide the scrollbar while keeping the terminal scrollable Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
The C++ `#embed` directive includes file contents at compile time, but the build system does not automatically track these as dependencies. When an embedded file (e.g., `play.html`) changes, the .cpp file that embeds it is not recompiled. Fix this by adding `OBJECT_DEPENDS` properties to all source files that use `#embed`, covering: HTML pages, JavaScript files, XML configs, and NLP data files. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Erase the "Connecting..." line before PTY output starts, so the terminal content renders from a clean line. Also erase it on auth failure before showing the error prompt. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Embed xterm.js 5.5.0, addon-fit 0.10.0, and addon-web-links 0.11.0 (minified JS and CSS) into the server binary, served at /js/*. This removes the dependency on CDN and makes the web terminal page fully self-contained, like the dashboard. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
When the `query` URL parameter is present (e.g., `/webterminal?query=SELECT 1`), paste it into the terminal after a successful connection, similar to how `play.html` handles the query parameter. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
ccache's direct mode uses its own source parser to find included files, but it does not recognize C23 `#embed` directives. This means changes to files included via `#embed` (HTML pages, JS libraries, XML configs) do not invalidate the ccache, leading to stale build artifacts. Enable depend mode (available since ccache 4.0), which uses the compiler's dependency file (.d) instead. The compiler correctly lists `#embed` files in its dependency output. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
- Handle continuation frames and fragmented messages per RFC 6455 Section 5.4, with proper reassembly buffer - Handle interleaved control frames (ping/pong/close) between data fragments - Fix integer overflow in resize message parser by clamping values to a maximum of 500 - Combine WebSocket frame header and payload into a single sendBytes call to prevent partial frame writes - Keep socket in blocking mode throughout the I/O loop instead of toggling, eliminating exception-safety issues - Validate Sec-WebSocket-Version: 13 per RFC 6455 and respond with the supported version if mismatched - Check for Upgrade: websocket (case-insensitive) in handleRequest before entering the WebSocket path Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Add a Quake-style drop-down terminal panel to the play.html query interface: - Shell icon (>_) in the left sidebar, above the GitHub icon - Click the icon or press ~ (backtick) to toggle the terminal - Terminal appears as an iframe occupying the top 40% of the main area - Bottom border is draggable to resize (same UX as the textarea resizer) - Dragging below one line height deactivates the terminal - Authentication parameters (user/password) are passed from the play.html inputs to the /webterminal iframe URL - The ~ key only triggers when not focused on input/textarea elements Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Move the terminal panel out of #main into its own grid row that spans both the menu and main columns, so it occupies the full page width like a Quake-style drop-down console. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
When the embedded client exits inside the iframe, webterminal.html sends a postMessage to the parent window instead of redirecting. play.html listens for this message and deactivates the terminal panel. When opened standalone (not in an iframe), the redirect to the ClickHouse GitHub repo is preserved. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Toggling the terminal (icon click, ~ key, or resize-to-close) now only hides the panel while keeping the iframe and its WebSocket session alive. Re-opening shows the same session. Quitting from the client (exit, Ctrl-D) still fully destroys the iframe via the postMessage handler. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…minal tests Two reviewer-requested strengthenings of `tests/integration/test_webterminal`: 1. `test_invalid_auth_rejects_session` no longer accepts EOF (`1006` on the client side) as success. The handler explicitly sends `1008` on a failed `Session::authenticate`, and the test now asserts that exact close code. A regression to ungraceful close would have silently passed before. 2. New `test_origin_match_accepted_same_origin` exercises the default-allow branch of the `Origin` check: a non-empty `Origin` whose scheme + host matches the request's `Host` must reach the `101` upgrade. The existing negative tests would still pass against an over-zealous CSWSH check that rejects every non-empty origin; this test locks in the positive case.
|
@groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100277&sha=5e956ab02379b6fca74430a22413b07a95340104&name_0=PR&name_1=libFuzzer%20tests and provide a fix in a separate PR. If the fix is already in progress, link it here. The |
|
@groeneai, the The |
|
@alexey-milovidov — addressing all three CI failures on this PR. None are caused by this PR; all are tracked chronic trunk issues:
Both |
Algunenano
left a comment
There was a problem hiding this comment.
Some notes from AI review. I did not check them, but I think it's good input:
- Most important (Medium): /webterminal registration via addCommonDefaultHandlersFactory ends up registered on the interserver HTTP port as well as the user-facing port, because createInterserverHTTPHandlerFactory (src/Server/HTTPHandlerFactory.cpp:287-296) also calls the common factory. Once allow_experimental_webterminal=true, an interactive PTY shell becomes reachable on the interserver port — a port with a different (HMAC) trust model and typically less-firewalled inside the cluster. Suggested fix: register only in addDefaultHandlersFactory, or skip when building the interserver factory.
- Other mediums: Origin check is skipped when the Origin header is missing (if (!origin.empty()) guard); no CORS / X-Frame-Options framing on the WebSocket-HTML page.
- Lows: 16 MiB pre-auth JSON parse per WebSocket connection (DoS amplifier — first-frame cap should be a few KiB since auth JSON is small); brittle Connection: upgrade substring match; ClientEmbedded::run builds CLI flags from an envs map (currently empty for webterminal, but a forward-compat hazard).
…minal frames Reviewer noted that `readWebSocketFrame` previously treated `payload_len > MAX_FRAME_SIZE` as an invalid frame (`valid = false`) without flagging a size error, so the main loop just terminated the session and the epilogue sent a normal `1000 Session ended` close. A client sending a single oversized non-fragmented frame would see a success-looking close instead of the required `1009` size error, weakening the protocol contract and hiding the path from clients/tests. Add an explicit `message_too_big` state to `WebSocketFrame`, set it when the advertised payload length exceeds `MAX_FRAME_SIZE`, and emit a dedicated `1009 Message too big` close from both the auth path and the main read loop. The integration test exercises the new contract by sending only the 14-byte frame header advertising a `17 MiB` payload and asserting the server closes with code `1009`.
# Conflicts: # ci/jobs/scripts/check_style/aspell-ignore/en/aspell-dict.txt
The handler was registered via `addCommonDefaultHandlersFactory`, which is also called from `createInterserverHTTPHandlerFactory`. Once `allow_experimental_webterminal=true`, the interactive PTY shell would become reachable on the interserver HTTP port as well. The interserver port has a different (HMAC) trust model and is typically less-firewalled inside the cluster, so this would punch a hole through that boundary. Move the registration into `addDefaultHandlersFactory` so the route is only mounted on the user-facing HTTP port. See: #100277
* Add `Content-Security-Policy: frame-ancestors 'none'` and `X-Frame-Options: DENY` headers to the served HTML page so a malicious embedding page cannot frame the terminal and trick a logged-in user into typing into an invisible PTY. * Reject a missing `Origin` header on the WebSocket upgrade with `403` rather than allowing the connection through. Browsers always send `Origin` on a WebSocket upgrade, so an unattributed upgrade is either a non-browser client or a forged request — accepting either is too risky for an interactive shell. * Update the integration test helper to default to a same-origin `Origin` header (matching browser behaviour) and add an explicit test for the missing-`Origin` rejection. See: #100277
…ction parsing
* Apply a 4 KiB cap on the first (auth) WebSocket frame, separate from
the 16 MiB post-auth cap. The auth JSON is small and the user/password
field limits already cap content to ~1.3 KiB, so 4 KiB is comfortably
above any legitimate first frame and removes the DoS amplifier where
an unauthenticated peer could force a 16 MiB allocation per connection.
* Parse the `Connection` header as RFC 7230 tokens rather than a
substring `find("upgrade")`, which would also match future values like
`upgrade-something`.
* Add integration tests for the pre-auth size cap and for the
clickjacking response headers on the HTML page.
See: #100277
The map is unused in WebTerminal today, but `ClientEmbedded::run` turns each entry into a `--key value` CLI flag for the embedded `clickhouse-client`. Add a comment so a future change that wires browser-supplied data into this map does not silently introduce CLI-flag injection. See: #100277
|
@groeneai, the two CI failures on the latest run are both unrelated to this PR:
Please investigate both and provide fixes in separate PRs. If fixes are already in progress, link them here. |
| /// invisible terminal. `frame-ancestors 'none'` (CSP) is the modern check | ||
| /// honoured by current browsers, and `X-Frame-Options: DENY` is the legacy | ||
| /// fallback for older browsers / proxies that ignore CSP. | ||
| response.set("Content-Security-Policy", "frame-ancestors 'none'"); |
There was a problem hiding this comment.
❌ frame-ancestors 'none' + X-Frame-Options: DENY forbids all embedding, including same-origin.
This breaks the new /play integration added in this PR, because /play loads /webterminal in an <iframe> (programs/server/play.html). With these headers the browser will refuse to render the terminal frame, so the terminal button appears but cannot actually embed the terminal.
Could we scope this to same-origin framing instead (for example Content-Security-Policy: frame-ancestors 'self' and X-Frame-Options: SAMEORIGIN), or otherwise make /play use a non-iframe flow? The current combination violates the feature contract described in docs (/play dockable panel).
LLVM Coverage Report
Changed lines: 64.98% (334/514) | lost baseline coverage: 3 line(s) · Uncovered code |
@Algunenano, how is it supposed to work then? This comment totally missed the point. |
Add a WebSocket-based terminal protocol (
webterminal) that runs a pseudoterminal with embedded clickhouse-client functionality, accessible via a browser at/webterminal.The implementation reuses the same
PtyClientDescriptorSetandClientEmbeddedRunnerinfrastructure as the SSH protocol, but connects via WebSocket instead of SSH. The user is authenticated through the sameSessionmachinery as the HTTP protocol, but credentials are sent in-band over the established WebSocket connection (in the first JSON message) instead of as part of the upgrade request, so they never appear in URLs, access logs, or browser history.The HTML page uses xterm.js for terminal emulation and connects to the server via WebSocket, bridging data bidirectionally between the browser and the PTY.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Added an experimental web terminal interface at
/webterminalthat provides an interactiveclickhouse-clientsession in the browser over WebSocket. Disabled by default; enable with theallow_experimental_webterminalserver setting.Documentation entry for user-facing changes
Navigate to
/webterminalon any ClickHouse HTTP port to open an interactive terminal session in the browser. Authenticate with ClickHouse user credentials. The terminal supports the full clickhouse-client experience including syntax highlighting, autocompletion, and multi-line queries.Companion docs sidebar PR: ClickHouse/clickhouse-docs#6118
Version info
26.5.1.684