Skip to content

feat: implement CORS policy management with dynamic origin whitelisting - #1037

Open
menawar wants to merge 1 commit into
Smartdevs17:mainfrom
menawar:feat/cors-policy-management-issue-1000
Open

feat: implement CORS policy management with dynamic origin whitelisting#1037
menawar wants to merge 1 commit into
Smartdevs17:mainfrom
menawar:feat/cors-policy-management-issue-1000

Conversation

@menawar

@menawar menawar commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #1000

This PR implements CORS policy management with dynamic origin whitelisting as described in issue #1000.

Changes

backend/services/shared/index.ts

Exported all CORS policy management symbols from the shared barrel so any service or consumer can import them cleanly without deep path imports.

backend/server.ts

  • Import and integrate processCorsRequest from the CORS middleware
  • Apply CORS headers to every incoming request before routing begins
  • Short-circuit OPTIONS preflight requests with a 204 No Content (or 403 if the origin is blocked)
  • Seed a default CORS policy from the CORS_ALLOWED_ORIGIN environment variable on server startup
  • Add GET /cors/analytics endpoint exposing CORS telemetry
  • Add GET /cors/violations endpoint for querying blocked request logs

backend/services/shared/__tests__/corsMiddleware.test.ts (new)

Comprehensive test suite with 47 unit and integration tests covering:

  • Policy CRUD (upsertPolicy, getPolicy, getAllPolicies, deletePolicy)
  • Wildcard and exact-match origin validation via testOrigin
  • processCorsRequest for simple and OPTIONS preflight requests
  • Preflight cache hit/miss and clearPreflightCache
  • CORS analytics counters (totalRequests, allowedRequests, blockedRequests, per-method, per-origin, per-tenant)
  • getViolations filtering by tenant, origin, limit, and since-timestamp
  • createCorsMiddleware Express-style factory
  • resetAnalytics
  • Full browser CORS lifecycle integration scenario

Test Results

Tests: 47 passed, 47 total
Test Suites: 1 passed, 1 total

Acceptance Criteria

  • Feature implemented with full functionality
  • Unit tests added with >80% coverage
  • Integration tests for critical paths
  • No regression introduced
  • Documentation updated (inline JSDoc preserved)
  • Performance benchmarks met (sub-millisecond per-request overhead)

Closes Smartdevs17#1000

- Export all CORS policy management symbols from backend/services/shared/index.ts
  so consumers can import them from the shared barrel

- Integrate CORS middleware into backend/server.ts:
  * Apply CORS headers to every incoming request before routing
  * Short-circuit OPTIONS preflight with 204/403 response
  * Seed a default 'default' CORS policy from CORS_ALLOWED_ORIGIN env var
  * Add GET /cors/analytics endpoint
  * Add GET /cors/violations endpoint

- Add comprehensive test suite (47 tests) covering:
  * Policy CRUD: upsertPolicy, getPolicy, getAllPolicies, deletePolicy
  * Origin matching: exact, wildcard subdomains, tenant scoping
  * processCorsRequest: allowed/blocked, credentials, exposed headers,
    OPTIONS preflight, preflight cache hit/miss, clearPreflightCache
  * Analytics counters: totalRequests, allowedRequests, blockedRequests,
    requestsByMethod, violationsByOrigin, violationsByTenant
  * getViolations filtering: by tenantId, origin, limit, since
  * createCorsMiddleware Express-style factory
  * resetAnalytics
  * Integration: full browser CORS flow, wildcard policy, preflight caching
@drips-wave

drips-wave Bot commented Aug 27, 2026

Copy link
Copy Markdown

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

@menawar
menawar deployed to security-review August 27, 2026 09:50 — with GitHub Actions Active
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.

Implement CORS policy management with dynamic origin whitelisting

1 participant