Skip to content

fix(server): trust only localhost and IP addresses as the server's own origin - #523

Merged
dubadub merged 2 commits into
cooklang:mainfrom
LeMyst:fix/dns-rebinding
Sep 25, 2026
Merged

dubadub merged 2 commits into
cooklang:mainfrom
LeMyst:fix/dns-rebinding

Conversation

@LeMyst

@LeMyst LeMyst commented Sep 22, 2026 •

Copy link
Copy Markdown
Collaborator

The problem

cook server's CSRF write guard (src/server/cors.rs) counted a request as same-origin whenever its Origin matched the Host it was sent to. Nothing checked that the Host was actually this machine.

A page served from http://evil.test:9080 can re-point evil.test at 127.0.0.1 once it has loaded — DNS rebinding. Its requests then carry Origin: http://evil.test:9080 and Host: evil.test:9080, which the guard read as same-origin. Any site the user visited could therefore:

  • add, change and delete recipes, pantry entries and the shopping list;
  • call POST /api/sync/login and read the response, since the request counts as same-origin, and finish the device flow with its own cook.md account;
  • call POST /api/sync/logout.

The web UI's new-recipe form (validate_same_origin in src/server/ui.rs) had the same hole, including through its Referer fallback — a request without Origin passes the guard untouched, so that fallback was the only check it got.

The fix

Same-origin now also requires a host that no DNS answer can redirect:

  • an IP address, v4 or v6 — rebinding needs a name to re-point;
  • localhost exactly, which browsers and operating systems resolve to loopback on their own.

Everything else — *.localhost, .local, a reverse proxy's public domain, https:// origins alike — has to be named with --cors-origin, which the new-recipe form now honours the way the API already did. The refusal names the exact flag to add, and the log line warns that an unrecognised address may be a rebinding attempt rather than telling the operator to allow it:

Only localhost and IP addresses are trusted as this server's own address, since another site
could point any other host name at it (DNS rebinding). If you opened the web UI at
http://nas.local:9080, start the server with --cors-origin http://nas.local:9080, or with
--no-csrf-check to disable this check.

CorsConfig::trusts(origin, host) is now the single decision point, used by the write guard and the form.

Upgrade note

This changes behaviour for anyone who opens the web UI at a host name rather than localhost or an IP address: http://nas.local:9080, a Docker service name, or a reverse proxy that passes Host through (Caddy and Traefik do by default). Those setups need --cors-origin <that origin> before the UI can save changes again. Reads are unaffected.

No BREAKING CHANGE footer, so release-please keeps this a patch — say the word if you would rather it were flagged.

Second commit: COOK_CORS_ORIGIN

That upgrade note is friction the fix creates, and containers feel it most: the image's command already carries /recipes and --host, so adding one flag means restating all of it. The second commit reads the same origins from COOK_CORS_ORIGIN, comma-separated, which a compose file names in one line:

environment:
  COOK_CORS_ORIGIN: https://cook.example.com

--cors-origin overrides the variable, so a command line can still correct what an image imposes. An empty value reads as unset, because COOK_CORS_ORIGIN=${UNDEFINED} is what a compose file expands to and refusing to start over it would be a poor trade. --no-csrf-check gets no variable on purpose: turning the check off should take a command someone typed.

Documented in the README beside COOK_CONFIG_DIR, in a new Containers section of docs/server.md, and as a commented block in docker-compose.yml. Four integration tests cover it: origin from the environment, several separated by commas, the flag winning over the variable, and an empty variable leaving the default policy alone.

Happy to split this into its own PR if you would rather review the fix on its own.

Tests

  • tests/cors_test.rs gains a DNS-rebinding section. The three tests that matter fail on main (the rebinding write returns 200, the form returns 303 and creates the recipe, and /new rejects a listed --cors-origin with 403) and pass here. a_host_header_override_reaches_the_server pins down that the Host header a test sets really reaches the server, so the other refusals cannot pass for the wrong reason.
  • Unit tests in src/server/cors.rs cover IP literals, localhost, LOCALHOST, default ports, and names that only look local (sub.localhost, localhost., localhost.evil.test, 127.0.0.1.nip.io, nas.local), plus Origin: null and the suggested flag being one --cors-origin accepts.
  • cargo fmt --all -- --check, cargo clippy --workspace --all-targets --all-features -- -D warnings, cargo test --workspace and cargo test --no-default-features are all clean.
  • Checked by hand with curl and in a browser: a rebound host name is refused, localhost and 127.0.0.1 still write, and creating and saving a recipe from the UI works.

Out of scope

  • Reads are still readable through rebinding, including /api/sync/status, which returns the account email. Under today's wildcard default any site can already read the API cross-origin, so this closes nothing; it becomes worth revisiting if the default CORS read policy is tightened.
  • GET /api/ws/lsp has no Origin check at all, so any page can open it (cross-site WebSocket hijacking). A separate fix is in progress and will reuse CorsConfig::trusts.
  • Browsers old enough to omit Origin on a same-origin fetch still pass the "no Origin means not a browser" rule on API routes. That is unchanged here.

🤖 Generated with Claude Code

@dubadub

dubadub commented Sep 25, 2026

Copy link
Copy Markdown
Member

Hey, thanks for all you PRs 🤟 This one has a conflict to resolve before I can merge it...

…n origin

The CSRF write guard and the new-recipe form counted a request as
same-origin whenever its Origin matched the Host it was sent to. A page
served from a domain its author then re-points at 127.0.0.1 -- DNS
rebinding -- sends exactly that matching pair, so it could add, change and
delete recipes, pantry and shopping list, and start or end a sync login.

Same-origin now also requires a host no DNS answer can redirect: an IP
address, or `localhost`, which browsers resolve to loopback themselves.
Any other host name has to be named with --cors-origin, which the
new-recipe form now honours as the API already did -- its Referer
fallback, which the guard never sees, went through the same check.

A refusal of that kind names the exact flag to add, and the log warns that
an address the operator does not recognise may be a rebinding attempt
rather than telling them to allow it.

Upgrade note: opening the web UI at a host name -- http://nas.local:9080,
a Docker service name, or a reverse proxy that passes Host through --
now needs --cors-origin with that origin before the UI can save changes.

Co-Authored-By: Claude Opus 5 <[email protected]>
Naming an origin is how the web UI keeps working when it is reached by
host name, which is exactly the case containers hit: the image's command
already carries the recipe directory and --host, so adding one flag means
restating all of it, while a compose file names a variable in one line.

Values are the same as the flag's, comma-separated, and the flag overrides
the variable so a command line can correct what an image imposes. An empty
value reads as unset, because COOK_CORS_ORIGIN=${UNDEFINED} is what a
compose file expands to and a server that refuses to start over it would
be a poor trade.

--no-csrf-check gets no variable on purpose: turning the check off should
take a command someone typed.

Co-Authored-By: Claude Opus 5 <[email protected]>
@LeMyst

LeMyst commented Sep 25, 2026

Copy link
Copy Markdown
Collaborator Author

Hey, thanks for all you PRs 🤟 This one has a conflict to resolve before I can merge it...

fixed

@dubadub
dubadub merged commit e65180f into cooklang:main Sep 25, 2026
6 checks passed
LeMyst added a commit that referenced this pull request Sep 30, 2026
)

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 (#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. #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 #546

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

2 participants