Skip to content

Apply the CORS middleware once per request - #8060

Open
FarhanAliRaza wants to merge 3 commits into
mainfrom
farhan/cors-applied-once
Open

FarhanAliRaza wants to merge 3 commits into
mainfrom
farhan/cors-applied-once

Conversation

@FarhanAliRaza

@FarhanAliRaza FarhanAliRaza commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Why

Every backend response repeats Origin in Vary. Without an api_transformer a response carries Vary: Origin, Origin. With a Starlette or FastAPI api_transformer it carries Vary: Origin, Origin, Origin, also on /ping.

#7624 meant to apply CORS once ("Requests no longer pass through … duplicate CORS middleware"). The outer CORS layer in App.__call__ was added, but the internal API and each Starlette transformer kept a CORS layer of their own. Each layer runs the origin check and appends Origin to Vary. Each layer overwrites access-control-allow-origin, so the tests named "exactly once" did not see the extra layers.

What changed

  • Remove the CORS layer on the internal API and on each Starlette or FastAPI api_transformer, and the App._add_cors helper that added them.
  • The outer layer stays. It covers the internal API, the transformer routes, the routes that a callable transformer adds, and the 500 responses of the outermost error handler.
  • Reflex no longer adds CORS middleware to the app passed as api_transformer. An app served through app() behaves the same.

How tested

  • Regression test first (3c3f3d0): test_api_transformer_routes_get_cors_headers and test_unhandled_route_error_gets_cors_headers now compare the complete Vary value. Before the fix: assert ['Origin, Origin'] == ['Origin']. After the fix: they pass.
  • The tests that called App._add_cors now use App._cors_options().
  • Before the fix, a dev app with a Starlette api_transformer answered /ping and /_reflex/session with vary: Origin, Origin, Origin in Chromium. I did not run the app again after the fix. The unit tests pin the single value.
  • tests/units passes. ruff check, ruff format --check and pyright are clean.

Known limits

  • test_registered_middleware_are_classes now iterates an empty list, because Reflex registers no middleware on the internal API. I kept it as a guard for middleware that is added later.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

View guided diff Turn on auto-fix

Each CORS layer appends Origin to Vary, while it overwrites
access-control-allow-origin. The tests named "exactly once" checked only
the header that cannot show a second layer. A route of a Starlette
api_transformer answers with "Vary: Origin, Origin", so these tests fail.
The outermost CORS layer covers every route: the internal API, the routes
of a Starlette or FastAPI api_transformer, the routes a callable
transformer adds, and the 500 responses of the outermost error handler.
The internal API and each Starlette transformer still got a CORS layer of
their own, so a request ran the CORS check up to three times and every
layer appended Origin to Vary. Remove those inner layers and the helper
that added them. Reflex no longer adds middleware to the app passed as
api_transformer.
@FarhanAliRaza
FarhanAliRaza requested a review from a team as a code owner November 30, 2026 10:30
@FarhanAliRaza FarhanAliRaza added the bug Something isn't working label Nov 30, 2026 — with ChatGPT Codex Connector
@greptile-apps

greptile-apps Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · The PR appears safe to merge.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

Reflex keeps its outer CORS layer and removes the extra layers it added to the internal API and Starlette or FastAPI transformers.

  • Backend responses now pass through one Reflex CORS layer.

Reviews (2) · Last reviewed commit: "Scope the CORS release note to the layer..." · Reviewed by Greptile

@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.

1 issue found across 3 files

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="news/+cors-applied-once.bugfix.md">

<violation number="1" location="news/+cors-applied-once.bugfix.md:1">
P2: This release note overstates the fix: a documented callable `api_transformer` can still add its own CORS layer, while `App.__call__` adds the outer Reflex layer. Scope the single-header claim to Reflex-added middleware layers, or mention that custom transformer CORS middleware can still produce duplicates.</violation>
</file>

Comment thread news/+cors-applied-once.bugfix.md Resolved
A callable api_transformer that wraps the app in its own CORSMiddleware, as the api routes guide shows, still adds a second Vary: Origin next to the Reflex layer. The note now says that Reflex applies its own CORS layer once.
@codspeed

codspeed Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 15 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

✅ 146 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing farhan/cors-applied-once (4fa005e) with main (7ee72a1)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

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

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant