Repository navigation
Apply the CORS middleware once per request - #8060
FarhanAliRaza wants to merge 3 commits into
Conversation
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.
[Medium impact] The PR appears safe to merge. SummaryReflex keeps its outer CORS layer and removes the extra layers it added to the internal API and Starlette or FastAPI transformers.
Reviews (2) · Last reviewed commit: "Scope the CORS release note to the layer..." · Reviewed by Greptile |
There was a problem hiding this comment.
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>
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.
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
Why
Every backend response repeats
OrigininVary. Without anapi_transformera response carriesVary: Origin, Origin. With a Starlette or FastAPIapi_transformerit carriesVary: 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 appendsOrigintoVary. Each layer overwritesaccess-control-allow-origin, so the tests named "exactly once" did not see the extra layers.What changed
api_transformer, and theApp._add_corshelper that added them.api_transformer. An app served throughapp()behaves the same.How tested
test_api_transformer_routes_get_cors_headersandtest_unhandled_route_error_gets_cors_headersnow compare the completeVaryvalue. Before the fix:assert ['Origin, Origin'] == ['Origin']. After the fix: they pass.App._add_corsnow useApp._cors_options().api_transformeranswered/pingand/_reflex/sessionwithvary: Origin, Origin, Originin Chromium. I did not run the app again after the fix. The unit tests pin the single value.tests/unitspasses.ruff check,ruff format --checkandpyrightare clean.Known limits
test_registered_middleware_are_classesnow 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