feat(config): make CORS allowed origins configurable via env#697
feat(config): make CORS allowed origins configurable via env#697raulanatol wants to merge 4 commits into
Conversation
Replaces the hardcoded `origins` list in `src/main.py` with a new `CORSSettings` block (env prefix `CORS_`), exposed as `settings.CORS.ORIGINS`. Defaults match the prior hardcoded values, so self-hosted deployments behind custom domains can now whitelist their frontend without editing source. Documented in `.env.template` under a new CORS Settings section.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
WalkthroughAdds a new AppSettings field ChangesCORS settings
Sequence DiagramsequenceDiagram
participant Application
participant AppSettings
participant CORSMiddleware
Application->>AppSettings: read CORS_ORIGINS
Application->>CORSMiddleware: add_middleware(..., allow_origins=settings.CORS_ORIGINS)
CORSMiddleware->>CORSMiddleware: enforce CORS using allow_origins
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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 |
| return self | ||
|
|
||
|
|
||
| class CORSSettings(HonchoSettings): |
There was a problem hiding this comment.
instead of adding a separate CORSSetting block, it would be simpler to just add a CORS_ORIGINS field to AppSettings. if we ever see the need to add other CORS_* values then we can promote this to a nested class.
There was a problem hiding this comment.
Good call, thanks! I just pushed a commit that drops the dedicated CORSSettings block and adds CORS_ORIGINS directly to AppSettings. The env var (CORS_ORIGINS) keeps working as-is since AppSettings has no env prefix. If more CORS_* knobs come up later we can promote it back to a nested class.
Drop the dedicated CORSSettings nested model and expose CORS_ORIGINS directly on AppSettings. The CORS_ORIGINS env var keeps working as before since AppSettings has no env prefix.
Summary
CORSSettingsblock (env prefixCORS_) and exposes it assettings.CORS.ORIGINS.src/main.pynow feedsallow_originsfromsettings.CORS.ORIGINSinstead of a hardcoded list. Defaults preserve the prior values (http://localhost,http://127.0.0.1:8000,https://api.honcho.dev) so existing deployments behave identically..env.template.Motivation: self-hosted deployments behind custom domains currently have to patch
src/main.pyto whitelist their frontend. With this change a single env var is enough:Scope is intentionally limited to
origins. Theallow_methods/allow_headerswildcards flagged in #394 are left as-is to keep this PR focused on the env-configurability hole; happy to follow up in a separate PR if maintainers want them tightened too.Test plan
uv run ruff check src/uv run ruff format --check src/uv run basedpyright src/main.py src/config.py— 0 errorsuv run python -c "from src.config import settings; print(settings.CORS.ORIGINS)"CORS_ORIGINS='["http://foo.test","https://bar.test"]' uv run python -c ...CORS_ORIGINSis setSummary by CodeRabbit
New Features
Chores