fix(backend): add CORSMiddleware with env-based dynamic origins and startup error handling - #3946
fix(backend): add CORSMiddleware with env-based dynamic origins and startup error handling#3946Aryanbansal-05 wants to merge 1 commit into
Conversation
…tartup error handling
|
@Aryanbansal-05 is attempting to deploy a commit to the ritesh Team on Vercel. A member of the Team first needs to authorize it. |
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Hi @riteshbonthalakoti! The issue has been resolved and the PR is ready to get merged. Thankyou! |
🔐 Summary
Resolves #3884 by registering
CORSMiddlewareinmain.pyusing origins loaded dynamically from theALLOWED_ORIGINSenvironment variable (already validated inconfig.py), replacing the missing/hardcoded wildcard configuration. Adds startup error handling so misconfigured CORS fails loudly with a logged error instead of silently allowing all origins.🛠️ Changes Made
main.py— Register CORSMiddleware with env-based originsBefore:
CORSMiddlewarewas imported but NEVER registered withapp.add_middleware()After:
app.add_middleware(CORSMiddleware, ...)now registered at startupsettings.cors_origins(parsesALLOWED_ORIGINSenv var)GET, POST, PUT, PATCH, DELETE, OPTIONSAuthorization, Content-Type, X-CSRF-Token, X-Request-IDallow_credentials=Truefor cookie-based auth supportALLOWED_ORIGINSis empty or misconfigured*is never used — all origins must be explicitly listed.env.exampleALLOWED_ORIGINSwith production + local dev defaults✅ Security Issues Fixed
*origins — explicit allowlist onlyconfig.pyfield_validatorCloses #3884