feat: implement production-safe CORS configuration with allowlist and… - #1044
feat: implement production-safe CORS configuration with allowlist and…#1044sudo-robi wants to merge 2 commits into
Conversation
… environment validation
|
@sudo-robi is attempting to deploy a commit to the Ayomide Adeniran's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
@sudo-robi Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
|
pr under review |
|
@ayomideadeniran i have resolved the issue |
There was a problem hiding this comment.
Pull request overview
Implements an environment-driven CORS allowlist intended to make backend CORS behavior production-safe (closing #883) by replacing permissive defaults with explicit origin validation and adding automated coverage.
Changes:
- Added a centralized CORS middleware factory (
backend/src/config/cors.config.ts) with env-based allowlist parsing. - Switched the main Express app to use the new CORS middleware and added a SuperTest suite to verify preflight + simple requests.
- Updated environment documentation and refreshed backend dependency lock entries.
Reviewed changes
Copilot reviewed 7 out of 9 changed files in this pull request and generated 10 comments.
Show a summary per file
| File | Description |
|---|---|
| package.json | Adds root-level dependencies relevant to Express/CORS/testing. |
| backend/tests/cors.test.ts | Adds SuperTest coverage for allowed vs blocked origins and preflight behavior. |
| backend/src/index.ts | Replaces generic cors() with createCorsMiddleware() at the app level. |
| backend/src/graphql/server.ts | Switches GraphQL middleware chain to use the shared CORS middleware. |
| backend/src/config/cors.config.ts | Introduces env-driven allowlist parsing and CORS options. |
| backend/package.json | Updates scripts and devDependencies for testing/types. |
| backend/package-lock.json | Updates lockfile entries for test tooling dependencies. |
| .env.example | Documents new CORS environment variables and examples. |
Files not reviewed (1)
- backend/package-lock.json: Generated file
Suppressed comments (3)
backend/package-lock.json:4983
- This lockfile introduces
registry.npmmirror.comfor@types/supertest, while the rest of the lockfile mostly usesregistry.npmjs.org. Mixed registries can break installs in CI/CD environments that cannot reach the mirror domain.
backend/package-lock.json:11354 - This lockfile introduces
registry.npmmirror.comforsemver, while the rest of the lockfile mostly usesregistry.npmjs.org. Mixed registries can break installs in CI/CD environments that cannot reach the mirror domain.
backend/package-lock.json:12331 - This lockfile introduces
registry.npmmirror.comforts-jest, while the rest of the lockfile mostly usesregistry.npmjs.org. Mixed registries can break installs in CI/CD environments that cannot reach the mirror domain.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| "test:migrations": "echo 'No migrations tests'", | ||
| "test:migrations": "bash scripts/test-migration-rollback.sh", |
| import { json } from 'express'; | ||
| import cors from 'cors'; | ||
| import { json, type RequestHandler } from 'express'; |
| @@ -1,3 +1,4 @@ | |||
| // @ts-nocheck | |||
| import { createCorsMiddleware } from './config/cors.config.js'; | ||
| import { setRateLimitEnvOverrides } from './config/rateLimit.config.js'; | ||
| import { swaggerSpec } from './config/swagger.js'; | ||
| import type { CorsRequest } from 'cors'; |
| }); | ||
|
|
||
| app.use(cors()); | ||
| app.use(createCorsMiddleware()); |
| } | ||
|
|
||
| function buildAllowedOrigins(): string[] { | ||
| const originEnv = process.env.CORS_ORIGIN || ''; |
|
|
||
| const allowedOrigins = buildAllowedOrigins(); | ||
|
|
||
| export function createCorsMiddleware(): (req: any, res: any, next: any) => void { |
| return { | ||
| environment: config.app.env, | ||
| allowedOrigins, | ||
| hasWildcard: allowedOrigins.length === 0, | ||
| }; |
| { | ||
| "dependencies": { | ||
| "cors": "^2.8.6", | ||
| "dotenv": "^17.4.2", | ||
| "express": "^5.2.1", | ||
| "jest": "^30.4.2", | ||
| "supertest": "^7.2.2" | ||
| } | ||
| } |
| "node_modules/@types/jest": { | ||
| "version": "30.0.0", | ||
| "resolved": "https://registry.npmjs.org/@types/jest/-/jest-30.0.0.tgz", | ||
| "resolved": "https://registry.npmmirror.com/@types/jest/-/jest-30.0.0.tgz", |
closes #883
CORS Configuration Overhaul
This PR implements a production-safe CORS configuration with these key improvements:
Key Changes:
Configurable Origin Allowlist:
cors.config.tswith environment-based origin validationCORS_ORIGINProduction-Ready Security:
Testing:
Documentation:
.env.examplewith CORS configuration guidanceImplementation Details:
cors()middleware with custom configurationTesting:
npm test(ornpm run test:watch)Review Checklist:
This description provides: