Skip to content

fix(server): same-origin by default, and refuse unknown Host names - #583

Merged
LeMyst merged 1 commit into
cooklang:mainfrom
LeMyst:fix/default-cors-same-origin
Sep 30, 2026
Merged

LeMyst merged 1 commit into
cooklang:mainfrom
LeMyst:fix/default-cors-same-origin

Conversation

@LeMyst

@LeMyst LeMyst commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

Closes #546

Problem

Without --cors-origin, cook server answered every GET with Access-Control-Allow-Origin: *, so any website the user visited could read the whole server: recipes, /api/static/…, /api/sync/status, and /preferences with the absolute base path in it. There was also no Host check on reads, so a DNS-rebound name could read everything, including the SSE stream, even without CORS.

Change

  1. Default is same-origin. With no --cors-origin (and no COOK_CORS_ORIGIN), the CorsLayer is not applied at all (CorsOrigins::None). --cors-origin '*' keeps the old read-only wildcard for anyone relying on it.
  2. Host check on every request (cors::host_guard). A request is answered only when its Host is 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-origin GETs carry no Origin, so Host is the only signal. Details:
    • Malformed Host values ([email protected], local%68ost) and duplicate Host headers fail closed.
    • A request with no Host passes, because no browser sends one.
    • The 403 and the log line name the exact --cors-origin to add, with the same "do not add it if you don't recognise it" wording as the write guard.
    • --no-csrf-check turns the check off, along with the existing ones.
  3. Help text, docs/server.md, docs/api.md + /api-docs, the README and docker-compose.yml are 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's https://cook.example.com passing Host through) now needs that origin named even to read. Before, only saving needed it.

  • Non-browser clients using a host name. The Host check applies to curl, scripts and other non-browser clients too. For example, a sibling container calling http://cookcli:9080 now needs COOK_CORS_ORIGIN=http://cookcli:9080. I looked for a way to exempt non-browsers and found none that works:

    • A same-origin GET carries no Origin.
    • Browsers send no Sec-Fetch-* headers to plain-http hosts that aren't localhost.
    • Firefox lets page scripts set User-Agent.

    This matches what Vite, webpack-dev-server and Django (ALLOWED_HOSTS) do. If you'd prefer a separate --allowed-host flag instead of reusing --cors-origin, that's an easy split.

Tests

  • Unit tests in 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_guard via oneshot.
  • tests/cors_test.rs:
    • The default sends no ACAO on preflights or simple GETs.
    • The wildcard tests now pass --cors-origin '*'.
    • A rebound Host gets 403 on /, /api/recipes, /api/recipes/raw/…, /api/static/…, /preferences and /static/…, and the body names the flag without leaking content.
    • Listed hosts (http/https scheme, cookcli:9080), localhost, 127.0.0.1 and [::1] are served.
    • --no-csrf-check disables the check.
  • cargo fmt, cargo clippy --all-targets (no warnings) and cargo test all pass locally.

🤖 Generated with Claude Code

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]>
@LeMyst
LeMyst marked this pull request as ready for review September 29, 2026 18:21
@LeMyst
LeMyst merged commit 0ef9105 into cooklang:main Sep 30, 2026
6 checks passed
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.

Default CORS policy Access-Control-Allow-Origin: * lets any website read the whole server

1 participant