Skip to content

feat: implement CORS configuration and handling#243

Merged
PierreLeGuen merged 2 commits intomainfrom
add-cors
Dec 9, 2025
Merged

feat: implement CORS configuration and handling#243
PierreLeGuen merged 2 commits intomainfrom
add-cors

Conversation

@PierreLeGuen
Copy link
Contributor

@PierreLeGuen PierreLeGuen commented Dec 9, 2025

  • Added CORS support to the API by introducing a new CorsConfig struct to manage allowed origins and wildcard suffixes.
  • Implemented is_origin_allowed function to validate incoming requests against the CORS configuration.
  • Integrated CORS layer into the application router to handle preflight requests and specify allowed methods and headers.
  • Updated tests to verify CORS functionality, including exact matches, localhost, and wildcard suffixes.
  • Modified ApiConfig to include CORS settings, ensuring they can be configured via environment variables.

Note

Adds env-driven CorsConfig and enforces CORS via CorsLayer with predicate-based origin checks, plus comprehensive tests.

  • CORS Configuration:
    • Introduce config::CorsConfig with exact_matches and wildcard_suffixes; default parses CORS_ALLOWED_ORIGINS (supports *.domain, *-suffix).
    • Extend config::ApiConfig with cors: CorsConfig and update test configs to include it.
  • API Integration:
    • Add is_origin_allowed to validate origins (exact matches; http://localhost[:port], http://127.0.0.1[:port]; https:// with allowed suffixes).
    • Wire tower_http::cors::CorsLayer using AllowOrigin::predicate in build_app_with_config; allow and expose Any methods/headers.
  • Tests:
    • Add parsing tests for CorsConfig variants and whitespace/empty entries.
    • Add CORS behavior tests for exact matches, localhost/127.0.0.1, and HTTPS wildcard suffixes.

Written by Cursor Bugbot for commit d8bca65. This will update automatically on new commits. Configure here.

- Added CORS support to the API by introducing a new CorsConfig struct to manage allowed origins and wildcard suffixes.
- Implemented is_origin_allowed function to validate incoming requests against the CORS configuration.
- Integrated CORS layer into the application router to handle preflight requests and specify allowed methods and headers.
- Updated tests to verify CORS functionality, including exact matches, localhost, and wildcard suffixes.
- Modified ApiConfig to include CORS settings, ensuring they can be configured via environment variables.
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

This is the final PR Bugbot will review for you during this billing cycle

Your free Bugbot reviews will reset on January 9

Details

Your team is on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle for each member of your team.

To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

if remainder.is_empty() || remainder.starts_with(':') {
return true;
}
}
Copy link

Choose a reason for hiding this comment

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

Bug: Localhost CORS bypass is hardcoded and cannot be disabled

The is_origin_allowed function unconditionally allows http://localhost and http://127.0.0.1 origins regardless of the CorsConfig settings. This means in production environments, any local web server can make CORS requests to the API, which could be exploited by malware or malicious local processes. This bypass cannot be disabled through configuration, making it impossible to secure the API against local origin attacks in production.

Fix in Cursor Fix in Web

Copy link
Contributor

@nickpismenkov nickpismenkov left a comment

Choose a reason for hiding this comment

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

LGTM

@PierreLeGuen PierreLeGuen merged commit 2c81da6 into main Dec 9, 2025
2 checks passed
@PierreLeGuen PierreLeGuen deleted the add-cors branch December 9, 2025 23:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants