Repository navigation
fix(server): refuse cross-site WebSocket connections to the language server - #524
Merged
Merged
Conversation
LeMyst
force-pushed
the
fix/lsp-websocket-origin
branch
5 times, most recently
from
September 25, 2026 14:08
04b03a9 to
55c2cd5
Compare
This was referenced Sep 25, 2026
…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
force-pushed
the
fix/lsp-websocket-origin
branch
from
September 26, 2026 18:07
55c2cd5 to
2d98c4a
Compare
LeMyst
marked this pull request as ready for review
September 27, 2026 00:36
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The problem
GET /api/ws/lspupgraded any handshake that reached it. Browsers apply no CORS to WebSockets, and the handshake is aGET, which the CSRF write guard lets through, so any page a user visited could openws://127.0.0.1:9080/api/ws/lspwhilecook serverwas running — classic cross-site WebSocket hijacking.What a hostile page could then do:
cook lspsubprocess, and cooklang-language-server takes its workspace root from the client'sinitializeparams (workspaceFolders/rootUri/rootPath). Completion scans that root recursively for.cookand.menufiles, and the server also readsconfig/aisle.confunder it. The root could be/, or any directory the page cared to name.The fix
1. Check the
Originbefore upgrading. The handshake is refused with403unless the origin is one the server trusts, usingCorsConfig::trusts— exactly the rule writes use after #523, so the socket and the write guard cannot drift apart:Hostislocalhostor an IP literal, or--cors-origin.Clients that send no
Originare not browsers and still connect (editors, scripts), and--no-csrf-checkturns 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,
initializegets itsrootUri,rootPathandworkspaceFoldersreplaced with the directory the server serves,workspace/didChangeWorkspaceFoldersis 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 passingHostthrough — the editor's completions and diagnostics now need that origin in--cors-origin, just as saving does. Opened atlocalhostor 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://' + basePathdoes 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'sfile://Risotto.cookis the network path\\Risotto.cook\; the completion request then hung (over 10 s, measured). Pinning the root means a validfile: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 lspdriven directly by an editor still behaves that way; that is upstream in cooklang-language-server.Also here
/api/ws/lspentry in the API reference said messages are framed withContent-Lengthheaders "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, insrc/web/api_docs.rsand the regenerateddocs/api.md.docs/server.mdgets a note about the socket, and the--no-csrf-checkhelp now says it covers the language server too.tokio-tungsteniteis added as a dev-dependency. It is already inCargo.lockthrough axum'swsfeature, so no new crates enter the tree.Tests
Seven tests in
tests/cors_test.rs, all driving real WebSocket handshakes against a spawnedcook server(a preflight cannot observe any of this):a_cross_origin_page_is_refused_with_403403a_rebound_dns_name_is_refused_even_though_it_matches403—OriginandHostbothrebind.testa_listed_origin_is_upgraded_and_others_are_not101/403the_servers_own_page_is_upgraded101at127.0.0.1andlocalhosta_client_without_origin_is_upgraded101no_csrf_check_disables_the_origin_check101session::the_workspace_root_is_the_base_path_whatever_the_client_namesrootUri, a folder change, then completion listing only the served recipesPlus 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
./Elsewherefrom 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:\Windowsas its root still gets only the served recipes.Not in this PR
There is still no limit on concurrent
cook lspsubprocesses. The cross-site route is closed, but a client that sends noOrigincan still open many. That's a separate change.cargo fmt,cargo clippy --workspace --all-targets --all-features -- -D warningsandcargo test --workspaceare clean (1010 tests).🤖 Generated with Claude Code