Repository navigation
fix(app): restrict default CORS origins to deploy_url in prod mode - #7641
Conversation
The `["*"]` default for `cors_allowed_origins` let any site connect to the backend. In prod mode the default now narrows to the origin of `deploy_url` (or nothing when `deploy_url` is unset), while an explicit `cors_allowed_origins`, from rxconfig or the environment, is used as is. Dev mode keeps allowing any origin. Both the Starlette CORS middleware and the Socket.IO server resolve their origins through one helper. Closes #7640 Claude-Session: https://claude.ai/code/session_01C1xrGYmKsaacgEafTz9BTJ
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ed3738cb94
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
[High impact] The PR appears safe to merge; no new blocking issues were found. SummaryProduction CORS now defaults to the origin of
Reviews (8) · Last reviewed commit: "test(sitemap): exercise the frontend_url..." · Reviewed by Greptile |
There was a problem hiding this comment.
All reported issues were addressed across 6 files
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
Browsers serialize the Origin header with a lowercase host and without the scheme's default port, so `https://Example.com:443/app` must allow `https://example.com`. Also cover CORS enforcement with request-level tests for the HTTP middleware and the Socket.IO origin check. Claude-Session: https://claude.ai/code/session_01C1xrGYmKsaacgEafTz9BTJ
There was a problem hiding this comment.
All reported issues were addressed across 2 files (changes from recent commits).
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
Merging this PR will not alter performance1 warning
Comparing |
masenf
left a comment
There was a problem hiding this comment.
With reflex run --env prod and the default deploy_url of http://localhost:3000, opening the app via 127.0.0.1:3000 is now rejected. Set deploy_url or cors_allowed_origins for alternate origins.
This is majorly problematic!
Reflex prod mode intentionally runs same origin now, serving the prod frontend build and the websocket from the same asgi stack. And with this change, those same origin connections to the websocket are blocked by socket.io.
I think we need to do the following:
- change default for
deploy_urlin the config to beNone. Add a property forbackend_urlthat gives eitherdeploy_urlorapi_urlwhen the former is unset. Wherever we currently usedeploy_url(like the sitemap plugin), we should move tobackend_urlinstead. - when
deploy_urlis not set in production mode, then DO NOT set CORS options at all -- this defaults to only allowing same origin requests, which is different than allowing cross origin requests from a specific place. socket.io and asgi by default permit same origin requests to come through, which is the sane default that users want.
This keeps common deploy methods working without having to make any config changes, while bolstering security significantly.
Reflex prod mode serves the frontend and the websocket from the same origin, so narrowing CORS to a default deploy_url blocked those same origin connections. deploy_url now defaults to None and is no longer derived from frontend_port; when it is unset in prod mode no CORS middleware is installed and Socket.IO keeps its same-origin default. Config gains a backend_url property (deploy_url, else api_url) and the sitemap plugin builds absolute links from it. Claude-Session: https://claude.ai/code/session_01C1xrGYmKsaacgEafTz9BTJ
|
@masenf thanks, you're right: with the frontend mounted into the backend, a default
Two things worth your eye:
Tests: unit suites for app, config, sitemap, telemetry, export, hosting and site-shared pass; the docs-site suite (727 tests) passes. The full unit run has pre-existing failures in Generated by Claude Code |
There was a problem hiding this comment.
All reported issues were addressed across 12 files (changes from recent commits).
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
…agupta-e48jze # Conflicts: # tests/units/test_app.py
okay i wrote this wrong in my original suggestion. the new config property should be |
Per review, the fallback for an unset deploy_url is the frontend's own localhost origin, http://localhost:<frontend_port>, which is what the sitemap plugin now builds absolute links from. Claude-Session: https://claude.ai/code/session_01C1xrGYmKsaacgEafTz9BTJ
|
Done in 5601d34: the property is now Generated by Claude Code |
…agupta-e48jze # Conflicts: # docs/app/tests/test_config.py # reflex/app.py # tests/units/test_app.py
masenf
left a comment
There was a problem hiding this comment.
low impact breaking change, shouldn't affect most typical deployments (because they're already setting deploy_url or using reverse proxy)
An explicit `cors_allowed_origins=[]` is passed to engine.io as before #7641 and this PR: engine.io treats an empty list as "no origin check", whereas the Reflex check rejected every browser origin for it. The rejection warning no longer interpolates the trusted_proxy_hops value (CodeQL's name heuristic classifies "trusted" identifiers as secrets; the value is a non-secret int, and only constant text now depends on it), and cuts the logged Origin, Host and X-Forwarded-Host values to 200 characters. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
* fix(app): accept the backend's own origin behind a TLS-terminating proxy #7641 left engine.io's built-in same-origin check in charge of the prod event websocket when deploy_url is unset. That check only accepts `{X-Forwarded-Proto or "http"}://{Host}` (plus an X-Forwarded-Host variant it trusts from anyone), so a same-origin app behind a TLS terminator that sends no X-Forwarded-Proto (stunnel, L4 TLS load balancers, HAProxy without a set-header rule, nginx without the header) or rewrites it to http (Caddy behind another TLS edge, i.e. docker-example/production on Render/Heroku) rejected its own https origin, and the browser reported the server as unreachable. Reflex now passes engine.io its own origin check (engine.io >= 4.12 calls it with the request environ). The prod default accepts an Origin whose host and port equal the request's Host; an https Origin also matches a request that arrived as http. X-Forwarded-Host and X-Forwarded-Proto count only through trusted_proxy_hops trusted proxies, taking the same entry from the right of the chain as the client IP does (now a shared helper); a trusted proto of https rejects an http Origin. A cross-site page can set none of these headers on a websocket handshake, and a client that sets them directly is not a browser and carries no victim cookies. deploy_url's origin is accepted on top of the backend's own one, so setting REFLEX_DEPLOY_URL can no longer lock out direct access. Explicit cors_allowed_origins lists and "*" are used as before. The first rejection per worker logs a warning with the origin, the Host/X-Forwarded-Host the backend saw and the setting to change: trusted_proxy_hops when an untrusted X-Forwarded-Host names the origin, otherwise REFLEX_DEPLOY_URL. Split frontend/backend, static export/CDN and Host-rewriting proxies need REFLEX_DEPLOY_URL on the backend (or trusted_proxy_hops when the proxy sends X-Forwarded-Host, e.g. Azure Front Door); the self-hosting, scaling, configuration and docker-example docs now say so. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi * Correct the prod CORS breaking-change entry for deploy_url Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi * Keep an empty cors_allowed_origins list and bound the rejection log line An explicit `cors_allowed_origins=[]` is passed to engine.io as before #7641 and this PR: engine.io treats an empty list as "no origin check", whereas the Reflex check rejected every browser origin for it. The rejection warning no longer interpolates the trusted_proxy_hops value (CodeQL's name heuristic classifies "trusted" identifiers as secrets; the value is a non-secret int, and only constant text now depends on it), and cuts the logged Origin, Host and X-Forwarded-Host values to 200 characters. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi * Clarify the CORS docs, app-platform README and news fragment Split the configuration docs' CORS paragraph into the HTTP CORS layer (still driven by cors_allowed_origins / deploy_url) and the websocket handshake check, say the rejection warning is logged once per worker, qualify the app-platform README for an explicit cors_allowed_origins, and trim the bugfix fragment (a trusted X-Forwarded-Proto: https still rejects an http page origin). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi * Read a short X-Forwarded-Host/-Proto chain; keep rejected origins out of advice Proxies set X-Forwarded-Host and X-Forwarded-Proto instead of appending to them, so with trusted_proxy_hops >= 2 (e.g. Azure Front Door in front of Caddy, where X-Forwarded-For has two entries) the single entry was dropped: the handshake got 403, and the https proto no longer rejected an http origin. A chain of these two headers shorter than the trusted hops is now read from its leftmost entry; X-Forwarded-For keeps requiring the full chain. The warning's "trusted" label is then accurate whenever trusted_proxy_hops is set. Any client can send the first rejected handshake of a worker, so the warning no longer advises REFLEX_DEPLOY_URL=<rejected origin> or adding that origin to cors_allowed_origins unconditionally; it says to set the frontend's public URL if the rejected origin is the frontend's. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi * Parenthesize the implicitly concatenated warning hints in the origin tests Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi --------- Co-authored-by: Claude <[email protected]> Co-authored-by: Farhan Ali Raza <[email protected]>
All Submissions:
Type of change
Changes To Core Features:
Description
closes #7640
The
["*"]default forcors_allowed_originslet any site connect to the backend. This PR makes the default mode-aware, following the design in @masenf's review:deploy_urlunset (the new default): CORS is left unconfigured. NoCORSMiddlewareis installed and the Socket.IO server keeps Engine.IO's same-origin default, which is what a backend that serves its own frontend needs. Browsers cannot read cross-origin responses.deploy_urlset: only that origin is allowed, serialized the way browsers serializeOrigin(lowercase host, no path, default port omitted; non-default ports and IPv6 hosts kept).cors_allowed_origins(fromrxconfig.pyorREFLEX_CORS_ALLOWED_ORIGINS) is used as is in both modes, so["*"]remains an opt-in to the old behavior. Explicitness is detected viaConfig._non_default_attributes.Both enforcement points, the Starlette
CORSMiddlewareand the Socket.IOAsyncServer, resolve their origins through one helper,_cors_allowed_origins()inreflex/app.py.Config changes
deploy_urlnow defaults toNoneinstead ofhttp://localhost:3000, and is no longer derived fromfrontend_port. It is only meant for frontends hosted on a different origin than the backend.Config.frontend_urlproperty:deploy_urlwhen set, otherwisehttp://localhost:<frontend_port>(the previousdeploy_urldefault). The sitemap plugin builds absolute links from it.Notes
AppHarnesspins dev mode before building the app and setsdeploy_urlitself.http://localhost:3000sitemap origin throughfrontend_url.Tests
Unit tests cover dev vs prod, unset
deploy_url(no middleware, Socket.IO same-origin default), path-bearing and default-portdeploy_urlforms, explicit lists, explicit["*"]via kwargs and via env, request-level enforcement through Starlette'sTestClientand Engine.IO's origin resolution, thefrontend_urlfallback, and the sitemap plugin. News fragments added forreflexandreflex-base(both breaking).https://claude.ai/code/session_01C1xrGYmKsaacgEafTz9BTJ