Skip to content

feat(cors): harden CORS config for production — restrict origins, methods & headers explicitly#273

Merged
robertocarlous merged 1 commit into
Neurowealth:mainfrom
BernardOnuh:feature/harden-cors-config-255
Jun 27, 2026
Merged

feat(cors): harden CORS config for production — restrict origins, methods & headers explicitly#273
robertocarlous merged 1 commit into
Neurowealth:mainfrom
BernardOnuh:feature/harden-cors-config-255

Conversation

@BernardOnuh

Copy link
Copy Markdown
Contributor

Closes #255

What changed

  • src/middleware/corsandbody.ts — replaced permissive/wildcard origin with an env-var allowlist (CORS_ALLOWED_ORIGINS); added startup validation that throws a fatal error in production if the var is empty; returns a proper 403 for disallowed origins instead of a missing-header response.
  • src/app.ts — wired validateCorsConfig() before server start; added request-origin logging middleware.
  • .env.example — documented CORS_ALLOWED_ORIGINS, CORS_MAX_AGE, and CORS_ALLOW_CREDENTIALS.
  • .env.development / .env.production — added environment-specific CORS origin lists.
  • tests/cors.test.ts — added integration tests covering: allowed origins, disallowed origins (403), HTTP methods (GET/POST/PUT/DELETE), OPTIONS preflight, and required custom headers.

Acceptance criteria

  • Requests from unlisted origins receive 403
  • Preflight OPTIONS requests respond correctly for allowed origins
  • CORS_ALLOWED_ORIGINS validated at startup; missing in NODE_ENV=production is a fatal error
  • Integration tests cover allowed origin, disallowed origin, and preflight
  • CORS_ALLOWED_ORIGINS documented in .env.example

Testing

npm run test:cors # CORS-specific suite
npm test # full test run

Notes

Credentials (credentials: true) are enabled globally; if needed, this can be scoped to authenticated routes only in a follow-up.

…Replace wildcard origin with env-driven allowlist\n- Allow only GET, POST, PUT, DELETE, PATCH, OPTIONS\n- Restrict headers to Content-Type, Authorization,\n Idempotency-Key, X-Correlation-ID, X-Request-ID\n- Return 403 (not 403 header missing) for blocked origins\n- Validate CORS_ALLOWED_ORIGINS at startup; fatal in prod\n- Add integration tests: allowed, disallowed, preflight\n- Document CORS_ALLOWED_ORIGINS in .env.example
@robertocarlous robertocarlous merged commit 07144a9 into Neurowealth:main Jun 27, 2026
1 of 5 checks passed
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.

Harden CORS configuration for production — restrict origins, methods, and headers explicitly

2 participants