Description
getAllowedOrigins in cors.ts reads from an env var but falls back to a wildcard in development. This wildcard can leak into staging if the env var is not set. The allowed-origins list should be explicit and validated at startup.
Requirements and context
- Must be secure, tested, and documented where applicable
- Should stay reviewable and fit the current monorepo structure
- Relevant files:
apps/backend/src/lib/api/cors.ts, apps/backend/src/lib/api/cors.test.ts, apps/frontend/src/lib/api/cors.ts
Suggested execution
- Create branch:
issue-018-harden-cors-configuration
- Keep changes scoped to the issue and reference the task IDs in the PR
Implement changes
- Throw at startup if
ALLOWED_ORIGINS is unset in non-development environments
- Validate each origin is a well-formed URL (no trailing slashes, no wildcards in production)
- Add
Vary: Origin header to all CORS responses
Test and commit
- Extend
cors.test.ts with: missing env var in production, malformed origin, preflight for disallowed origin
- Run
vitest run apps/backend/src/lib/api/cors.test.ts
- Edge case:
localhost origins must only be allowed in development
Example commit message
fix(cors): enforce explicit allowed-origins in non-development environments
Guidelines
- Prefer small, reviewable PRs
- Keep naming and data contracts consistent with the spec docs
Description
getAllowedOriginsincors.tsreads from an env var but falls back to a wildcard in development. This wildcard can leak into staging if the env var is not set. The allowed-origins list should be explicit and validated at startup.Requirements and context
apps/backend/src/lib/api/cors.ts,apps/backend/src/lib/api/cors.test.ts,apps/frontend/src/lib/api/cors.tsSuggested execution
issue-018-harden-cors-configurationImplement changes
ALLOWED_ORIGINSis unset in non-development environmentsVary: Originheader to all CORS responsesTest and commit
cors.test.tswith: missing env var in production, malformed origin, preflight for disallowed originvitest run apps/backend/src/lib/api/cors.test.tslocalhostorigins must only be allowed in developmentExample commit message
Guidelines