Skip to content

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

Open
FarhanAliRaza wants to merge 2 commits into
mainfrom
farhan/friendly-brahmagupta-e48jze
Open

FarhanAliRaza wants to merge 2 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:

  • Dev mode: unchanged, any origin is allowed.
  • Prod mode: the default narrows to the origin of deploy_url. If deploy_url is None, no cross-origin request is accepted.
  • 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, not by comparing against the default tuple.

Both enforcement points, the Starlette CORSMiddleware and the Socket.IO AsyncServer, now resolve their origins through one helper, _cors_allowed_origins() in reflex/app.py. Passing the list straight through to engineio is equivalent to the old "*" special case, since engineio treats a list containing "*" as allow-all.

deploy_url is reduced to an origin serialized the way browsers serialize the Origin header: lowercase host, no path, and the scheme's default port omitted (https://Example.COM:443/app → https://example.com). Non-default ports and IPv6 hosts are kept.

Notes

  • Integration tests are unaffected: AppHarness pins dev mode before building the app, so AppHarnessProd still resolves to ["*"].
  • 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.

Tests

Unit tests cover dev vs prod, path-bearing and default-port deploy_url forms, unset deploy_url, explicit lists, explicit ["*"] via kwargs and via env, and request-level enforcement: a preflight through Starlette's TestClient from the deploy_url origin passes while other origins are refused, with the same cases checked against Engine.IO's origin resolution. News fragments added for reflex (breaking) and reflex-base (docs).

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 Outdated
@greptile-apps

greptile-apps Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; the remaining WebSocket test gap is non-blocking.

Summary

Restricts default production CORS origins to the origin of deploy_url, while keeping development open and preserving explicit cors_allowed_origins.

  • Production backends now default to the origin of deploy_url.

Reviews (2) · Last reviewed commit: "fix(app): normalize deploy_url to a brow..." · Reviewed by Greptile

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

@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 Outdated
Comment thread packages/reflex-base/news/+cors-prod-default.docs.md
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
@codspeed

codspeed Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 20 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 (119337d) with main (aeaac72)

Open in CodSpeed

@masenf masenf added security on deck PRs lined up to review / merge next labels Oct 22, 2026

This branch has not been deployed

No deployments
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 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