Repository navigation
fix(server): same-origin by default, and refuse unknown Host names - #583
Merged
Merged
Conversation
Without --cors-origin the server answered every GET with Access-Control-Allow-Origin: *, so any website the user visited could read a local `cook server`: the whole recipe collection, /api/static, the sync status and the base path in /preferences. The wildcard has been the default since the Rust rewrite (11adb04) and was made explicit in 979addf / 26f7bf0 (cooklang#484). - No --cors-origin now means no CORS layer at all. `--cors-origin '*'` restores the previous read-only wildcard. - A new host_guard answers only requests sent to localhost, an IP address, or the host of a --cors-origin. A page that rebinds its own name to the server is same-origin as far as the browser is concerned and its GETs carry no Origin, so Host is the only thing that gives it away. cooklang#523 already applied this rule to writes; it now covers reads, the SSE stream and the websocket too. --no-csrf-check turns it off. - Help text, docs/server.md, docs/api.md, /api-docs, README and docker-compose.yml describe the new behaviour. Closes cooklang#546 Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
Closes #546
Problem
Without
--cors-origin,cook serveranswered everyGETwithAccess-Control-Allow-Origin: *, so any website the user visited could read the whole server: recipes,/api/static/…,/api/sync/status, and/preferenceswith the absolute base path in it. There was also noHostcheck on reads, so a DNS-rebound name could read everything, including the SSE stream, even without CORS.Change
--cors-origin(and noCOOK_CORS_ORIGIN), theCorsLayeris not applied at all (CorsOrigins::None).--cors-origin '*'keeps the old read-only wildcard for anyone relying on it.Hostcheck on every request (cors::host_guard). A request is answered only when itsHostis an IP literal,localhost, or the host of a--cors-origin. This is the same rule fix(server): trust only localhost and IP addresses as the server's own origin #523 applies to writes, now applied to reads. A rebound page's same-originGETs carry noOrigin, soHostis the only signal. Details:Hostvalues ([email protected],local%68ost) and duplicateHostheaders fail closed.Hostpasses, because no browser sends one.403and the log line name the exact--cors-originto add, with the same "do not add it if you don't recognise it" wording as the write guard.--no-csrf-checkturns the check off, along with the existing ones.docs/server.md,docs/api.md+/api-docs, the README anddocker-compose.ymlare updated.Behaviour changes to discuss (the issue is
needs discussion)Cross-origin reads. Any external front end that reads the API without naming its origin stops working until it passes
--cors-origin <its origin>(or'*').Web UI at a host name. Opening the web UI at a host name (
http://nas.local:9080, a proxy'shttps://cook.example.compassingHostthrough) now needs that origin named even to read. Before, only saving needed it.Non-browser clients using a host name. The
Hostcheck applies tocurl, scripts and other non-browser clients too. For example, a sibling container callinghttp://cookcli:9080now needsCOOK_CORS_ORIGIN=http://cookcli:9080. I looked for a way to exempt non-browsers and found none that works:GETcarries noOrigin.Sec-Fetch-*headers to plain-httphosts that aren'tlocalhost.User-Agent.This matches what Vite, webpack-dev-server and Django (
ALLOWED_HOSTS) do. If you'd prefer a separate--allowed-hostflag instead of reusing--cors-origin, that's an easy split.Tests
src/server/cors.rs: the no-flag policy has no layer; which hosts are trusted and refused (including malformed ones); hosts of listed origins; the wildcard trusts no names;host_guardviaoneshot.tests/cors_test.rs:GETs.--cors-origin '*'.Hostgets403on/,/api/recipes,/api/recipes/raw/…,/api/static/…,/preferencesand/static/…, and the body names the flag without leaking content.http/httpsscheme,cookcli:9080),localhost,127.0.0.1and[::1]are served.--no-csrf-checkdisables the check.cargo fmt,cargo clippy --all-targets(no warnings) andcargo testall pass locally.🤖 Generated with Claude Code