Skip to content

fix(server): refuse cross-site WebSocket connections to the language server - #524

Merged
LeMyst merged 1 commit into
cooklang:mainfrom
LeMyst:fix/lsp-websocket-origin
Sep 27, 2026
Merged

LeMyst merged 1 commit into
cooklang:mainfrom
LeMyst:fix/lsp-websocket-origin

Conversation

@LeMyst

@LeMyst LeMyst commented Sep 22, 2026

Copy link
Copy Markdown
Collaborator

Stacked on #523. This branch sits on top of fix/dns-rebinding, because the check here reuses the CorsConfig::trusts helper that PR introduces. Until #523 merges, the diff below shows both commits; only the second one belongs to this PR. I'll rebase onto main once #523 lands, and this becomes a single commit.

The problem

GET /api/ws/lsp upgraded any handshake that reached it. Browsers apply no CORS to WebSockets, and the handshake is a GET, which the CSRF write guard lets through, so any page a user visited could open ws://127.0.0.1:9080/api/ws/lsp while cook server was running — classic cross-site WebSocket hijacking.

What a hostile page could then do:

  • List recipe and menu file names anywhere the server's user can read. Each connection spawns a cook lsp subprocess, and cooklang-language-server takes its workspace root from the client's initialize params (workspaceFolders / rootUri / rootPath). Completion scans that root recursively for .cook and .menu files, and the server also reads config/aisle.conf under it. The root could be /, or any directory the page cared to name.
  • Start unbounded subprocesses, since every connection spawns one with no limit.

The fix

1. Check the Origin before upgrading. The handshake is refused with 403 unless the origin is one the server trusts, using CorsConfig::trusts — exactly the rule writes use after #523, so the socket and the write guard cannot drift apart:

  • the server's own address, when the Host is localhost or an IP literal, or
  • an origin named with --cors-origin.

Clients that send no Origin are not browsers and still connect (editors, scripts), and --no-csrf-check turns this off along with the rest of the same-origin enforcement.

2. Pin the workspace root, so the check is not the only thing standing between a client and the filesystem. In the bridge, initialize gets its rootUri, rootPath and workspaceFolders replaced with the directory the server serves, workspace/didChangeWorkspaceFolders is dropped, and so is anything that is not a single JSON object. Every forwarded message is re-serialized from what the bridge parsed, so the language server cannot read a message differently than the bridge did (a duplicated "method" key, for instance). The editor already names the base path, so it loses nothing.

Upgrade note

Same shape as #523's: if you open the web UI at a host name — http://nas.local:9080, a Docker service name, or a reverse proxy passing Host through — the editor's completions and diagnostics now need that origin in --cors-origin, just as saving does. Opened at localhost or an IP address, nothing changes. The server logs each refusal with the origin and host it saw.

Side effect: recipe reference completion now works on Windows

It never did. The base path is canonicalized to a \\?\ prefix, so the editor's own 'file://' + basePath does not parse as a path — the ? starts a query string. With no usable root, the language server fell back to the parent directory of the document's URI, which for the editor's file://Risotto.cook is the network path \\Risotto.cook\; the completion request then hung (over 10 s, measured). Pinning the root means a valid file: URI is always sent, and @./ completes.

That fallback is worth noting for its own sake: a client that chooses the document URI can steer it at a \\host\share, which on Windows means the server's user reaches out to a network share someone else named. Through this bridge it can no longer happen, since the root is always set. cook lsp driven directly by an editor still behaves that way; that is upstream in cooklang-language-server.

Also here

  • The /api/ws/lsp entry in the API reference said messages are framed with Content-Length headers "exactly as LSP over stdio would be". They are not — each WebSocket text message is one bare JSON-RPC message, and the bridge adds and strips that framing on the subprocess side. Corrected, along with the new behaviour, in src/web/api_docs.rs and the regenerated docs/api.md.
  • docs/server.md gets a note about the socket, and the --no-csrf-check help now says it covers the language server too.
  • tokio-tungstenite is added as a dev-dependency. It is already in Cargo.lock through axum's ws feature, so no new crates enter the tree.

Tests

Seven tests in tests/cors_test.rs, all driving real WebSocket handshakes against a spawned cook server (a preflight cannot observe any of this):

Test Expects
a_cross_origin_page_is_refused_with_403 403
a_rebound_dns_name_is_refused_even_though_it_matches 403 — Origin and Host both rebind.test
a_listed_origin_is_upgraded_and_others_are_not 101 / 403
the_servers_own_page_is_upgraded 101 at 127.0.0.1 and localhost
a_client_without_origin_is_upgraded 101
no_csrf_check_disables_the_origin_check 101
session::the_workspace_root_is_the_base_path_whatever_the_client_names a full LSP conversation: hostile rootUri, a folder change, then completion listing only the served recipes

Plus unit tests for the message filter in src/server/lsp_bridge.rs (pinning, dropped folder changes, batches, duplicate keys, pass-through, and the Windows \\?\ path).

Each protection was removed in turn to confirm its test fails: without the origin check the three refusal tests fail; without the rebinding rule only the rebinding test fails; without pinning — or with folder changes forwarded — the end-to-end test lists ./Elsewhere from outside the base path.

Checked by hand in a browser as well: the editor still reports "LSP Connected" and completes; a page served from another port is refused and logged; and a same-origin session that asks for C:\Windows as its root still gets only the served recipes.

Not in this PR

There is still no limit on concurrent cook lsp subprocesses. The cross-site route is closed, but a client that sends no Origin can still open many. That's a separate change.

cargo fmt, cargo clippy --workspace --all-targets --all-features -- -D warnings and cargo test --workspace are clean (1010 tests).

🤖 Generated with Claude Code

…server

`GET /api/ws/lsp` upgraded any handshake that reached it. WebSockets are
exempt from CORS and the handshake is a GET, which the write guard lets
through, so any page the user had open could connect to
ws://127.0.0.1:9080/api/ws/lsp and drive a language server of its own.

Each connection also spawns a `cook lsp` subprocess, and that server
takes its workspace root from the client's `initialize` params. Its
completion then lists every .cook and .menu file under that root, so a
hostile page could enumerate recipe and menu file names anywhere the
server's user can read.

Refuse the upgrade with 403 unless the request's Origin is trusted. The
check calls `CorsConfig::trusts`, the rule writes already use, so the
socket and the write guard cannot drift apart: the server's own address
when the Host is localhost or an IP literal, or an origin named with
--cors-origin. Clients that send no Origin are not browsers and still
connect, and --no-csrf-check turns the check off along with the rest.
Sharing that rule means COOK_CORS_ORIGIN reaches the socket too, which a
test pins, since a container names its origin that way and its editor
would otherwise lose the language server.

The check runs before a session slot is claimed, so a page that may not
connect cannot take slots from the editor by being refused over and
over, and a server whose --max-lsp-sessions are all in use answers such
a page 403 rather than telling it how many sessions there are. A test
pins that order.

Pin the workspace root as well, so no client chooses it. `initialize`
gets rootUri, rootPath and workspaceFolders written over with the served
directory, `workspace/didChangeWorkspaceFolders` is dropped, and so is
anything that is not a single JSON object. Messages are forwarded as
re-serialized from what the bridge parsed, so the language server cannot
read a message differently than the bridge did.

This also fixes recipe reference completion on Windows, where the
editor's own `'file://' + basePath` never parsed: the base path is
canonicalized to a `\\?\` prefix, whose `?` starts a query string. The
language server fell back to the document URI's parent directory, which
for `file://Risotto.cook` is the network path `\\Risotto.cook\`, and the
completion request hung.

Co-Authored-By: Claude Opus 5 <[email protected]>
@LeMyst
LeMyst force-pushed the fix/lsp-websocket-origin branch from 55c2cd5 to 2d98c4a Compare September 26, 2026 18:07
@LeMyst
LeMyst marked this pull request as ready for review September 27, 2026 00:36
@LeMyst
LeMyst merged commit c3c1b9e into cooklang:main Sep 27, 2026
6 checks passed
@LeMyst
LeMyst deleted the fix/lsp-websocket-origin branch September 27, 2026 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant