Skip to content

fix(cors): replace wildcard origin with environment-based whitelist and secure localhost fallback#315

Open
V1ctor-o wants to merge 2 commits into
StellarFlow-Network:mainfrom
V1ctor-o:fix/cors-restriction
Open

fix(cors): replace wildcard origin with environment-based whitelist and secure localhost fallback#315
V1ctor-o wants to merge 2 commits into
StellarFlow-Network:mainfrom
V1ctor-o:fix/cors-restriction

Conversation

@V1ctor-o

Copy link
Copy Markdown

Summary

This PR removes the insecure wildcard CORS configuration and replaces it with a strict, environment-driven whitelist system to improve backend security for financial contract operations.

Changes Made

Security Fixes

  • Removed wildcard CORS policy (origin: "*") from the backend
  • Replaced with explicit origin validation using FRONTEND_URL and DASHBOARD_URL
  • Ensured only approved origins can access backend resources

Environment-Aware Behavior

  • Production:
    • Only allows requests from explicitly configured FRONTEND_URL and DASHBOARD_URL
    • Throws a clear error if required environment variables are missing
  • Staging/Testing:
    • Allows configured origins plus localhost variants (localhost, 127.0.0.1)
  • Defaults:
    • No unsafe fallback to wildcard or open access

Code Improvements

  • Removed redundant/insecure CORS middleware from src/index.ts
  • Centralized and hardened CORS logic in src/app.ts
  • Improved origin validation with strict matching
  • Added clearer error messages for rejected origins

Security Improvements

  • Prevents unauthorized cross-origin access to sensitive financial APIs
  • Eliminates wildcard exposure risk in production
  • Enforces explicit allowlist configuration via environment variables
  • Restricts localhost access to non-production environments only

Files Changed

  • src/app.ts — updated CORS configuration and origin validation
  • src/index.ts — removed insecure/default CORS middleware

Testing Performed

  • Verified allowed access from configured frontend and dashboard URLs
  • Verified localhost access in staging/testing environments
  • Confirmed rejection of unauthorized external origins
  • Confirmed production fails fast if required environment variables are missing

Notes

This change strengthens the backend security posture by enforcing strict origin control and eliminating unsafe default CORS behavior.

Closes #295

V1ctor-o added 2 commits May 29, 2026 18:52
…back for testing

- Remove wildcard-like behavior that allowed any origin via dashboardUrl fallback
- Prioritize FRONTEND_URL as primary allowed origin
- Support DASHBOARD_URL as secondary origin if configured differently
- Explicitly allow localhost only for non-production environments (NODE_ENV !== 'production')
- Add clear error messages indicating which origins are allowed
- Maintain backward compatibility by continuing to allow no-origin requests (same-origin)
- Follow existing code style with inline helper functions

Fixes: CORS policy enforcement for financial contract configuration security
…back for testing

Changes:
- Remove redundant cors() middleware from index.ts that allowed all origins by default
- Implement explicit CORS origin whitelist in app.ts (FRONTEND_URL and DASHBOARD_URL)
- Add production environment validation requiring explicit FRONTEND_URL configuration
- Allow localhost only for non-production environments (NODE_ENV !== 'production')
- Provide clear error messages indicating allowed origins and testing exceptions
- Maintain backward compatibility for same-origin requests (no origin header)
- Clean up unused imports and validation code in index.ts

Security improvements:
- Eliminates wildcard CORS policy that exposed financial contract configurations
- Enforces strict origin validation for production deployments
- Enables safe localhost access for staging and testing environments
- Provides audit trail via descriptive error messages

Follows existing code style and maintains all existing functionality.
@drips-wave

drips-wave Bot commented May 29, 2026

Copy link
Copy Markdown

@V1ctor-o 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

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.

🔀 QA | CORS Policy Strict Whitelisting

1 participant