Repository navigation
Description
Summary
cook server applies a hardcoded CORS policy that can't be configured. It should be possible to control the allowed origins (and related CORS settings) from the CLI/config.
Current behaviour
src/server/mod.rs:200:
.layer(
CorsLayer::new()
.allow_origin("*".parse::<HeaderValue>().unwrap())
.allow_methods([Method::GET, Method::POST, Method::PUT, Method::DELETE]),
);Two problems with this being fixed:
- Too permissive by default. Any web page in the user's browser can call the API of a running
cook server— including the mutatingPOST/PUT/DELETEroutes (shopping list, recipe editing, pantry). That matters more now that--hostexposes the server on the LAN. Users who care can't lock it down. - Too restrictive in the parts that matter.
allow_headersis never set, so tower-http defaults to allowing none. A cross-originPOSTwithContent-Type: application/jsonfails preflight, andallow_credentialscan't be used at all alongside a wildcard origin. Anyone building a custom frontend against the API has to patch the binary.
Proposal
Add CORS flags to ServerArgs, e.g.:
--cors-origin <ORIGIN>— repeatable; explicit allowed origins.*for the current wildcard behaviour.--cors-allow-credentials— requires explicit origins (error out if combined with*).- possibly
--cors-allow-header <HEADER>/ a sensible default ofcontent-type.
Open questions:
- What should the default be? Keeping
*preserves backwards compatibility; restricting the default to same-origin (no CORS layer) is safer but could break existing users of the HTTP API. Leaning towards keeping*as default for now and documenting it, but worth deciding. - Should this also be expressible in a config file rather than flags only?
- Should
allow_headersincludecontent-typeunconditionally, since the current setup effectively breaks JSON POSTs from browsers anyway?
Notes
- Also update
docs/for theservercommand and the API docs entry that mentions CORS (src/web/api_docs.rs:56).
Metadata
Metadata
Assignees
Labels
No labels
Activity
daviddehoog commentedon Sep 1, 2026
Some thoughts on the open questions relating to problem 1:
For the default, is there an option to continue to allow origins from *, but with a reduced set of allowed methods (ie. exclude mutating methods by default, so only GET) ? This could minimise breaks for existing API uses and reduce the chance of misuse if exposed on a local network.
For the configuration approach, I would expect most deployments/use of the server are as a service or in a docker container, so a longer set of arguments at runtime shouldn't be problematic. More importantly, these configuration options should be set in the same way as the host and port settings, since they shouldn't be set in isolation from each other.
docs: add design spec for configurable server CORS
feat(server): add --cors-origin and --cors-allow-credentials