Repository navigation
feat(server): make CORS configuration configurable - #484
Conversation
Design for #465. Default becomes wildcard origin with GET-only methods; explicit --cors-origin unlocks POST/PUT/DELETE and --cors-allow-credentials. content-type is always allowed. The existing --no-cors flag (a CSRF check, not CORS) is renamed to --no-csrf-check with a hidden alias. Claude-Session: https://claude.ai/code/session_013cPWotrjEY4Jac87e6vHsh
tower-http compares the browser's Origin header byte for byte, so a configured origin differing in case or carrying a path, query, fragment, userinfo or bad port silently never matches. Reject those at startup instead of failing invisibly at request time. Claude-Session: https://claude.ai/code/session_013cPWotrjEY4Jac87e6vHsh
Splitting the port off the first colon broke http://[::1]:3000, which is what a browser sends when the server is reached over IPv6 — and it made userinfo report as a bad port. Check the host rules first, then split the port off the right. Claude-Session: https://claude.ai/code/session_013cPWotrjEY4Jac87e6vHsh
http://[] and http://:3000 both parsed cleanly and would then never match anything. Capture the host from both the bracketed and plain branches and require it to be non-empty. Claude-Session: https://claude.ai/code/session_013cPWotrjEY4Jac87e6vHsh
The default is now a wildcard origin restricted to GET. Naming explicit origins unlocks POST/PUT/DELETE, and content-type is always allowed so cross-origin JSON requests can pass preflight. Closes #465 Claude-Session: https://claude.ai/code/session_013cPWotrjEY4Jac87e6vHsh
POST is a CORS-safelisted method, so Access-Control-Allow-Methods: GET does not stop a cross-origin POST; verified in a real browser. Adding content-type to allow_headers also removes the accidental protection today's unset allow_headers provides, making the new default more permissive than the code it replaces. Enforce reads-only server-side with a write guard instead, and leave the CORS headers to describe the policy rather than enforce it. Claude-Session: https://claude.ai/code/session_013cPWotrjEY4Jac87e6vHsh
ensure_usable_cors_rules runs in Layer::layer, not in the builder chain, so constructing a CorsLayer never exercised it. Also narrow methods() to private and drop redundant clap attributes. Claude-Session: https://claude.ai/code/session_013cPWotrjEY4Jac87e6vHsh
The flag gates same-origin enforcement on modifying requests, not the CORS layer. --no-cors is kept as a hidden alias so existing invocations keep working. Claude-Session: https://claude.ai/code/session_013cPWotrjEY4Jac87e6vHsh
POST is a CORS-safelisted method, so Access-Control-Allow-Methods: GET never stops a cross-origin POST — the header policy cannot make the default read-only. Enforce it server-side instead: a mutating request carrying an Origin must be same-origin or named by --cors-origin. Requests with no Origin still pass, so curl and existing scripts are unaffected, and --no-csrf-check skips the guard for proxy setups that need it. Claude-Session: https://claude.ai/code/session_013cPWotrjEY4Jac87e6vHsh
The cross-origin POST case needs a real request, not a preflight: a wildcard origin always answers Access-Control-Allow-Origin: *, so the headers alone cannot show whether the write was refused. Claude-Session: https://claude.ai/code/session_013cPWotrjEY4Jac87e6vHsh
axum_extra's Host extractor prefers Forwarded / X-Forwarded-Host, so `Origin: http://evil.test` plus `X-Forwarded-Host: evil.test` read as same-origin and the cross-origin write went through. Those headers are set by whoever is on the other end of a direct connection, so a security decision must not depend on them. Not browser-reachable — a page cannot set X-Forwarded-Host without a preflight, and allow_headers permits only content-type — and a non-browser client never needed it, since omitting Origin passes by design. Removed anyway rather than left as a latent hazard. Deployments behind a proxy that rewrites Host name their public origin with --cors-origin. Claude-Session: https://claude.ai/code/session_013cPWotrjEY4Jac87e6vHsh
create_recipe used axum-extra's Host extractor, which prefers X-Forwarded-Host over the real Host header — the same precedence removed from the write guard. Share one host_header helper between them, and treat a duplicated Host header as no host so a raw client cannot choose which one the check sees. Claude-Session: https://claude.ai/code/session_013cPWotrjEY4Jac87e6vHsh
Also records why origin_matches_host deliberately ignores the scheme: a TLS-terminating proxy passing Host through is the normal case for an https origin, and the converse is blocked by mixed-content policy. Claude-Session: https://claude.ai/code/session_013cPWotrjEY4Jac87e6vHsh
The fixture had no pantry.conf, so POST /api/pantry/add returned 404 whether or not the guard allowed it, and assert_ne!(403) proved nothing. Give the fixture a pantry, assert the real status, and pin HOME so a global config on the developer's machine cannot change the outcome. Claude-Session: https://claude.ai/code/session_013cPWotrjEY4Jac87e6vHsh
Fourteen tests each boot their own server, so a parallel run spawns more at once than the suite this pattern came from; 10s was not always enough on a loaded machine. Claude-Session: https://claude.ai/code/session_013cPWotrjEY4Jac87e6vHsh
It claimed a wildcard origin allows GET only. The header policy cannot enforce that -- POST is CORS-safelisted -- so say what actually happens: cross-origin writes are refused with 403. Claude-Session: https://claude.ai/code/session_013cPWotrjEY4Jac87e6vHsh
ReviewOverall this is a well-designed, well-tested fix. The core insight — that Strengths
Things worth a second look
Nits
No blocking issues found — the write-guard design, the 🤖 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 (#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]>
Closes #465.
cook serverapplied one hardcoded CORS policy that was wrong in both directions: too permissive (any page could reach the mutating routes of a--host-exposed server) and too restrictive (allow_headerswas never set, so a cross-origin JSONPOSTfailed preflight and legitimate integrations had to patch the binary).What changed
--cors-origin <ORIGIN>— repeatable;*for any origin, the default.--cors-allow-credentials— requires explicit origins; errors against a wildcard.content-typeis now always an allowed request header, so cross-origin JSON works.403. Naming origins with--cors-originlets those origins write.--no-corsis renamed--no-csrf-check— it gates the same-origin check and never touched the CORS layer. The old spelling still works as a hidden alias.The part worth reviewing
The obvious implementation —
allow_methods([GET])for a wildcard origin — does not work, and shipping it would have made the default more permissive than the code it replaced.POSTis a CORS-safelisted method, so per the Fetch standard a browser only consultsAccess-Control-Allow-Methodsfor methods that are not safelisted.Access-Control-Allow-Methods: GETblocksPUTandDELETEbut neverPOST. Demonstrated in a real browser: a cross-origin page wrote toseed/config/pantry.confviaPOST /api/pantry/add. And the only thing blocking cross-origin JSONPOSTbefore this PR was the unsetallow_headersfailing the preflight by accident — which the issue correctly asks us to remove.So read-only is enforced server-side, by a small write guard, and the CORS headers describe the policy rather than enforce it. The guard, in order:
GET/HEAD/OPTIONSpass.Originheader → pass.curl, scripts and every non-browser client send none, and the API has no authentication for them to bypass.Originmatching the request'sHost→ pass (same-origin; the web UI's own writes).Originin the--cors-originlist → pass.403.It reads the real
Hostheader only, neverForwarded/X-Forwarded-Host— an earlier revision used axum-extra'sHostextractor, andOrigin: http://evil.testplusX-Forwarded-Host: evil.testpassed as same-origin (abe2976).Compatibility
Cross-origin browser writes now need
--cors-origin. Same-origin traffic is unaffected, and so is anything that isn't a browser —curland existing scripts send noOriginand pass by rule 2.Behind a reverse proxy that rewrites
Host, pass the public origin:--cors-origin https://cook.example.com. A proxy that passesHostthrough unchanged needs no configuration.A deliberate non-fix
origin_matches_hostignores the scheme, soOrigin: https://a.testmatchesHost: a.test. That is the normal TLS-terminating-proxy case, and rejecting it would break those deployments. The converse reading — anhttps://page reaching a plaintext server on port 80 — is blocked by browsers' mixed-content policy. Documented and pinned by a test rather than left implicit.Testing
24 unit tests in
src/server/cors.rs(flag validation, origin parsing, guard rules) and 14 end-to-end tests intests/cors_test.rsthat boot a real server. The cross-originPOSTcase is tested with a real request, not a preflight:AllowOrigin::any()answers*on every preflight, so the headers alone cannot show whether the write was refused.Design:
docs/superpowers/specs/2026-09-03-server-cors-config-design.mdFollow-up
Re-run cooklang.org's
scripts/sync-cli-docs.shafter merge to pick updocs/api.mdanddocs/server.md.https://claude.ai/code/session_013cPWotrjEY4Jac87e6vHsh