Skip to content

fix(app): restrict default CORS origins to deploy_url in prod mode - #7641

Merged
masenf merged 9 commits into
mainfrom
farhan/friendly-brahmagupta-e48jze
Oct 28, 2026
Merged

masenf merged 9 commits into
mainfrom
farhan/friendly-brahmagupta-e48jze

Conversation

@FarhanAliRaza

@FarhanAliRaza FarhanAliRaza commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

Description

closes #7640

The ["*"] default for cors_allowed_origins let any site connect to the backend. This PR makes the default mode-aware, following the design in @masenf's review:

  • Dev mode: unchanged, any origin is allowed.
  • Prod mode, deploy_url unset (the new default): CORS is left unconfigured. No CORSMiddleware is 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.
  • Prod mode, deploy_url set: only that origin is allowed, serialized the way browsers serialize Origin (lowercase host, no path, default port omitted; non-default ports and IPv6 hosts kept).
  • Explicit cors_allowed_origins (from rxconfig.py or REFLEX_CORS_ALLOWED_ORIGINS) is used as is in both modes, so ["*"] remains an opt-in to the old behavior. Explicitness is detected via Config._non_default_attributes.

Both enforcement points, the Starlette CORSMiddleware and the Socket.IO AsyncServer, resolve their origins through one helper, _cors_allowed_origins() in reflex/app.py.

Config changes

  • deploy_url now defaults to None instead of http://localhost:3000, and is no longer derived from frontend_port. It is only meant for frontends hosted on a different origin than the backend.
  • New Config.frontend_url property: deploy_url when set, otherwise http://localhost:<frontend_port> (the previous deploy_url default). The sitemap plugin builds absolute links from it.

Notes

  • Integration tests are unaffected: AppHarness pins dev mode before building the app and sets deploy_url itself.
  • The docs site keeps its http://localhost:3000 sitemap origin through frontend_url.

Tests

Unit tests cover dev vs prod, unset deploy_url (no middleware, Socket.IO same-origin default), path-bearing and default-port deploy_url forms, explicit lists, explicit ["*"] via kwargs and via env, request-level enforcement through Starlette's TestClient and Engine.IO's origin resolution, the frontend_url fallback, and the sitemap plugin. News fragments added for reflex and reflex-base (both breaking).

https://claude.ai/code/session_01C1xrGYmKsaacgEafTz9BTJ

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
@FarhanAliRaza
FarhanAliRaza requested review from a team and Alek99 as code owners October 22, 2026 19:52
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-22T19:55:55.451787Z ed3738c PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@FarhanAliRaza FarhanAliRaza added the breaking Public API facing change considered for minor release bump pre-v1.0 label Oct 22, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment thread reflex/app.py Resolved
@greptile-apps

greptile-apps Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[High impact] The PR appears safe to merge; no new blocking issues were found.

Summary

Production CORS now defaults to the origin of deploy_url, or same-origin connections when it is unset. Explicit origin lists remain supported.

  • Production apps allow only their frontend origin by default.
  • Frontend URLs keep a localhost fallback when deploy_url is unset.

Reviews (8) · Last reviewed commit: "test(sitemap): exercise the frontend_url..." · Reviewed by Greptile

Comment thread reflex/app.py Resolved
Comment thread tests/units/test_app.py Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Comment thread reflex/app.py Resolved
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

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Comment thread tests/units/test_app.py Resolved
@codspeed

codspeed Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 16 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 148 untouched benchmarks


Comparing farhan/friendly-brahmagupta-e48jze (c392251) with main (5ec18fa)

Open in CodSpeed

@masenf masenf added security on deck PRs lined up to review / merge next documentation Improvements or additions to documentation bug Something isn't working labels Oct 22, 2026

@masenf masenf left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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_url in the config to be None. Add a property for backend_url that gives either deploy_url or api_url when the former is unset. Wherever we currently use deploy_url (like the sitemap plugin), we should move to backend_url instead.
  • when deploy_url is 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

Copy link
Copy Markdown
Contributor Author

@masenf thanks, you're right: with the frontend mounted into the backend, a default deploy_url narrowed CORS to an origin the browser never presents, and Socket.IO rejected the same-origin websocket. Pushed 509b30e implementing your proposal:

  • deploy_url now defaults to None and is no longer derived from frontend_port in _replace_defaults (otherwise reflex run --env prod, which persists frontend_port=backend_port=port, would have re-created a localhost:<port> deploy_url and narrowed CORS again for 127.0.0.1 / LAN access).
  • New Config.backend_url property: deploy_url when set, otherwise api_url. The sitemap plugin now builds absolute links from it.
  • In prod mode with deploy_url unset, _cors_allowed_origins() returns None: _add_cors installs no CORSMiddleware, and the Socket.IO server gets cors_allowed_origins=None, so Engine.IO's same-origin default (Host / X-Forwarded-* based) applies. With deploy_url set, only that origin is allowed; an explicit cors_allowed_origins is still used as is; dev mode stays ["*"].

Two things worth your eye:

  1. Docs site sitemap. docs/app/tests/test_config.py asserted the framework's http://localhost:3000 default, and the README noted the Helm sitemap rewrite expects that localhost origin. With backend_url, the docs sitemap now carries api_url, which in single-port prod is http://localhost:<port>. If the docs deployment does not run on port 3000, the rewrite pattern needs updating, or REFLEX_DEPLOY_URL should be set to the public origin. I updated the test and README to the new contract rather than pinning deploy_url in the docs rxconfig.py, since an explicit localhost deploy_url would narrow CORS in prod.
  2. reflex_site_shared.utils.url.public_url still reads deploy_url directly: with it unset it returns a relative URL, which seems to be the right semantic for canonical links (api_url would be wrong there), and switching it to backend_url would need a reflex-base .dev0 floor bump in reflex-site-shared. Left as is; happy to change if you'd rather have it uniform.

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 reflex_cli and reflex_bench browser tests that reproduce on a clean checkout in this environment and are unrelated.


Generated by Claude Code

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Comment thread news/+cors-prod-default.breaking.md Resolved
Comment thread tests/units/plugins/test_sitemap.py Resolved
@masenf

masenf commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

New Config.backend_url property: deploy_url when set, otherwise api_url. The sitemap plugin now builds absolute links from it.

okay i wrote this wrong in my original suggestion. the new config property should be frontend_url and it should be deploy_url when set, otherwise the http://localhost:{frontend_port} (previous deploy_url default)

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

Copy link
Copy Markdown
Contributor Author

Done in 5601d34: the property is now Config.frontend_url, returning deploy_url when set and otherwise http://localhost:{frontend_port} (falling back to the default frontend port when frontend_port is unset). The sitemap plugin builds its links from it, backend_url is gone, and the docs site's test and README are back to asserting the http://localhost:3000 default, so the Helm sitemap rewrite concern from my earlier comment no longer applies. Unit, config, sitemap and docs-site suites pass.


Generated by Claude Code

Comment thread tests/units/plugins/test_sitemap.py Resolved

@masenf masenf left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

low impact breaking change, shouldn't affect most typical deployments (because they're already setting deploy_url or using reverse proxy)

@masenf
masenf enabled auto-merge (squash) October 28, 2026 19:37
@masenf
masenf merged commit 23a7821 into main Oct 28, 2026
169 checks passed
@masenf
masenf deleted the farhan/friendly-brahmagupta-e48jze branch October 28, 2026 19:59
masenf pushed a commit that referenced this pull request Nov 11, 2026
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
FarhanAliRaza added a commit that referenced this pull request Nov 11, 2026
* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking Public API facing change considered for minor release bump pre-v1.0 bug Something isn't working documentation Improvements or additions to documentation on deck PRs lined up to review / merge next security

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cors_allowed_origins should default to restrictive in prod mode

2 participants