Repository navigation
fix(server): trust only localhost and IP addresses as the server's own origin - #523
Merged
Merged
Conversation
LeMyst
force-pushed
the
fix/dns-rebinding
branch
from
September 23, 2026 18:54
ed8fa8f to
9899de7
Compare
LeMyst
marked this pull request as ready for review
September 23, 2026 20:09
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]>
LeMyst
force-pushed
the
fix/dns-rebinding
branch
from
September 25, 2026 13:54
70657b6 to
16bb237
Compare
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
force-pushed
the
fix/dns-rebinding
branch
from
September 25, 2026 14:05
16bb237 to
dc23f37
Compare
Collaborator
Author
fixed |
This was referenced Sep 25, 2026
Closed
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]>
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
cook server's CSRF write guard (src/server/cors.rs) counted a request as same-origin whenever itsOriginmatched theHostit was sent to. Nothing checked that theHostwas actually this machine.A page served from
http://evil.test:9080can re-pointevil.testat127.0.0.1once it has loaded — DNS rebinding. Its requests then carryOrigin: http://evil.test:9080andHost: evil.test:9080, which the guard read as same-origin. Any site the user visited could therefore:POST /api/sync/loginand read the response, since the request counts as same-origin, and finish the device flow with its own cook.md account;POST /api/sync/logout.The web UI's new-recipe form (
validate_same_origininsrc/server/ui.rs) had the same hole, including through itsRefererfallback — a request withoutOriginpasses 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:
localhostexactly, 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: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
localhostor an IP address:http://nas.local:9080, a Docker service name, or a reverse proxy that passesHostthrough (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 CHANGEfooter, so release-please keeps this a patch — say the word if you would rather it were flagged.Second commit:
COOK_CORS_ORIGINThat upgrade note is friction the fix creates, and containers feel it most: the image's command already carries
/recipesand--host, so adding one flag means restating all of it. The second commit reads the same origins fromCOOK_CORS_ORIGIN, comma-separated, which a compose file names in one line:--cors-originoverrides the variable, so a command line can still correct what an image imposes. An empty value reads as unset, becauseCOOK_CORS_ORIGIN=${UNDEFINED}is what a compose file expands to and refusing to start over it would be a poor trade.--no-csrf-checkgets 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 ofdocs/server.md, and as a commented block indocker-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.rsgains a DNS-rebinding section. The three tests that matter fail onmain(the rebinding write returns200, the form returns303and creates the recipe, and/newrejects a listed--cors-originwith403) and pass here.a_host_header_override_reaches_the_serverpins down that theHostheader a test sets really reaches the server, so the other refusals cannot pass for the wrong reason.src/server/cors.rscover 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), plusOrigin: nulland the suggested flag being one--cors-originaccepts.cargo fmt --all -- --check,cargo clippy --workspace --all-targets --all-features -- -D warnings,cargo test --workspaceandcargo test --no-default-featuresare all clean.curland in a browser: a rebound host name is refused,localhostand127.0.0.1still write, and creating and saving a recipe from the UI works.Out of scope
/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/lsphas noOrigincheck at all, so any page can open it (cross-site WebSocket hijacking). A separate fix is in progress and will reuseCorsConfig::trusts.Originon 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