Skip to content

feat: implement production-safe CORS configuration with allowlist and… - #1044

Open
sudo-robi wants to merge 2 commits into
StellarDevHub:mainfrom
sudo-robi:fix-cors-configuration
Open

feat: implement production-safe CORS configuration with allowlist and…#1044
sudo-robi wants to merge 2 commits into
StellarDevHub:mainfrom
sudo-robi:fix-cors-configuration

Conversation

@sudo-robi

Copy link
Copy Markdown

closes #883

CORS Configuration Overhaul

This PR implements a production-safe CORS configuration with these key improvements:

Key Changes:

  1. Configurable Origin Allowlist:

    • Created cors.config.ts with environment-based origin validation
    • Supports comma-separated origin lists in CORS_ORIGIN
    • Provides sensible defaults for development/test environments
  2. Production-Ready Security:

    • Explicit origin allowlist (no wildcard in production)
    • Proper credential handling
    • Preflight request support
    • Secure headers configuration
  3. Testing:

    • Added SuperTest suite for CORS behavior
    • Tests preflight requests and simple cross-origin requests
    • Verifies both allowed and blocked origins
  4. Documentation:

    • Updated .env.example with CORS configuration guidance
    • Added environment variable documentation

Implementation Details:

  • Replaced generic cors() middleware with custom configuration
  • Updated GraphQL server to use the same CORS configuration
  • Added logging for rejected origins
  • Maintained backward compatibility for development environments

Testing:

  • Run tests with npm test (or npm run test:watch)
  • Verify CORS behavior with:
    curl -H "Origin: http://localhost:3000" -I http://localhost:8080/health
    curl -H "Origin: http://evil.example" -I http://localhost:8080/health

Review Checklist:

  • Verify production CORS_ORIGIN is properly configured
  • Confirm all development origins are properly listed
  • Verify preflight requests work as expected
  • Check that unlisted origins are properly rejected
  • Confirm credential handling is correct

This description provides:

  1. Clear context about what was changed
  2. Explanation of the security improvements
  3. Implementation details for reviewers
  4. Testing instructions
  5. A review checklist for the team

@vercel

vercel Bot commented Jul 30, 2026

Copy link
Copy Markdown

@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.

@drips-wave

drips-wave Bot commented Jul 30, 2026

Copy link
Copy Markdown

@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! 🚀

Learn more about application limits

@ayomideadeniran

Copy link
Copy Markdown
Contributor

pr under review

Copilot AI review requested due to automatic review settings August 3, 2026 11:41
@sudo-robi

Copy link
Copy Markdown
Author

@ayomideadeniran i have resolved the issue

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.com for @types/supertest, while the rest of the lockfile mostly uses registry.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.com for semver, while the rest of the lockfile mostly uses registry.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.com for ts-jest, while the rest of the lockfile mostly uses registry.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.

Comment thread backend/package.json
Comment on lines +13 to 14
"test:migrations": "echo 'No migrations tests'",
"test:migrations": "bash scripts/test-migration-rollback.sh",
Comment on lines +3 to 5
import { json } from 'express';
import cors from 'cors';
import { json, type RequestHandler } from 'express';
Comment thread backend/src/index.ts
@@ -1,3 +1,4 @@
// @ts-nocheck
Comment thread backend/src/index.ts
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';
Comment thread backend/src/index.ts
});

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 {
Comment on lines +73 to +77
return {
environment: config.app.env,
allowedOrigins,
hasWildcard: allowedOrigins.length === 0,
};
Comment thread package.json
Comment on lines +1 to +9
{
"dependencies": {
"cors": "^2.8.6",
"dotenv": "^17.4.2",
"express": "^5.2.1",
"jest": "^30.4.2",
"supertest": "^7.2.2"
}
}
Comment thread backend/package-lock.json
"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",
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.

[Security] Enforce an environment-driven CORS allowlist for the backend API

3 participants