Keep browser sessions working over plain HTTP
Type of change
- Bug fix (non-breaking change which fixes an issue)
- This change requires a documentation update
What and why
On 0.12.0a1, a prod app served over plain HTTP on a non-loopback host (an intranet box, docker-compose on a LAN, a VM test box), and a dev app opened by its LAN address with the default api_url, lose their state on every reload, and every rx.upload returns 403. SessionMiddleware chose cookie security once at startup from api_url, so these pages got __Host-reflex_session_<hash>; Secure; SameSite=None; Partitioned. The frontend rewrites a loopback api_url to the page's host, so the browser calls the backend over plain HTTP and silently drops that Secure cookie. /_reflex/session still answers 200, and nothing is logged on either side. 0.11.0 keeps state in the same setup. A startup check on api_url cannot catch this, because most deployments leave it at http://localhost:….
This PR decides cookie security for each request:
- Plain HTTP means the
Originishttp://on a host other than localhost,*.localhostor a loopback IP,api_urlis not https (the frontend keeps an httpsapi_urlon any page), and the request's scheme is not https. That scheme comes from the trustedX-Forwarded-Protothroughtrusted_proxy_hops, read the same way as the socket origin check, and otherwise from the ASGI scheme. - On plain HTTP,
/_reflex/sessionsetsreflex_session_<hash>=…; Path=/; Max-Age=…; HttpOnly; SameSite=Lax. Dev already sent this cookie for a non-loopbackapi_url. Every other request keeps today's__Host-cookie. - The cookie a request reads depends on its transport. A plain-HTTP request reads only the plain cookie. Any other request, including one with no
Origin, reads only__Host-…, so a cookie tossed over plain HTTP or by a sibling subdomain cannot fixate an HTTPS session. An HTTPS site behind a TLS proxy that sends noX-Forwarded-ProtokeepsSecure, because its pages send anhttps://Origin, and an https page cannot call a plain-HTTP backend. - The first plain-HTTP request in a worker logs:
Session cookies for the page at 'http://192.0.2.2:3061' are not Secure, because it calls the backend over plain HTTP, where browsers drop Secure cookies. Serve the app over HTTPS. If browsers already use HTTPS through a reverse proxy, set api_url to the public https URL or set trusted_proxy_hops so that X-Forwarded-Proto counts. This warning is logged once per worker. _clipand_trusted_forwarded_valuemove fromapp.pytosession_middleware.py, so the socket origin check and the middleware share them without a circular import.session_endpointdrops itsapi_urlargument and reads the middleware's decision from the scope. Both functions are new in 0.12.
Security: a Secure cookie cannot exist on a plain-HTTP origin. The choice is between today's outcome (no cookie, so state is lost on every reload and uploads return 403) and an HttpOnly non-Secure cookie. A leaked client token still grants nothing without the cookie, and page scripts still cannot read the cookie. A network attacker on plain HTTP can already read the websocket frames and the state.
Things for the reviewer to decide
- Same-origin
GETwithout anOriginheader reads only__Host-, so on plain HTTP a customapi_transformerGET route that is called same-origin does not see the session. The event websocket, uploads,/_reflex/session, and cross-origin or non-GET fetches all sendOrigin. Falling back to the ASGI scheme here would expose HTTPS sites behind a TLS proxy (withtrusted_proxy_hops=0, the default) to cookie tossing. The sessions guide now says this. - The warning is per worker, not per origin. Any client can send an
http://Origin, so a per-origin set would be unbounded. A scanner that sendsOrigin: http://evilcan trigger it on an HTTPS site that leavesapi_urlat its default and runs behind a proxy withtrusted_proxy_hops=0. The message names the origin, and its advice (setapi_urlto https, as the guide already says) silences it. - An http page on another site calling an https backend behind a TLS proxy, when the backend's runtime
api_urlis not https andtrusted_proxy_hops=0, now gets the Lax cookie, which the browser refuses cross-site. On a1 it got a partitioned Secure cookie that worked. Settingapi_urlortrusted_proxy_hops, as the warning says, restores it. - Not included, possible follow-ups: a
session_cookie_on_http: "auto" | "never"config knob for teams that want plain HTTP to fail closed, and a client hint (session_expectedin the CONNECT auth) to diagnose cookies dropped for other reasons, such as privacy settings.
Release train
The 0.12 prerelease train (r/pre-2026.12.04-47424220534) ships the a1 behavior. This commit cherry-picks onto the train with no conflicts: the touched files are identical on main and the train. I recommend also cherry-picking it to the train for the next alpha, since plain-HTTP intranet and LAN deployments otherwise regress from 0.11.0 in 0.12.0.
Tests
- Regression test first:
test_plain_http_session_survives_reload(dev and prod) failed on main withKeyError: 'reflex_session_…', because the endpoint issued__Host-…to thehttp://192.0.2.2origin. It passes now. test_cookie_policynow covers the transport matrix: LAN origin with a loopback or LANapi_url, trusted and untrustedX-Forwarded-Proto, localhost,*.localhost,127.0.0.2and[::1], an https origin behind a plain-HTTP proxy, an httpsapi_url, a direct TLS scheme, no Origin, and a malformed Origin. It checks the fullSet-Cookievalue and the exact warning. Further tests cover the once-per-worker warning, which cookie each transport reads, and the plain-HTTP websocket handshake.uv run pytest tests/units/test_session_middleware.py: 87 passed.tests/integration/test_granian_session.pypasses.- Full
tests/units: 17023 passed. 264 failures and errors are all intests/units/reflex_cli/v2/*(Usage: root …) andtests/units/reflex_bench/drivers/test_browser.py(the Playwright browser build is missing). Neither depends on this change, andreflex_cli/v2/test_apps.pyfails the same 90 tests on the parent commit. ruff check,ruff format --check,pyright reflex tests(0 errors) and pre-commit on the changed files are clean.- Real browser: I ran a prod app (
reflex run --env prod, single port 3061, defaultapi_url) and drove Chromium athttp://192.0.2.2:3061/. It storedreflex_session_<hash>(HttpOnly, SameSite=Lax, not Secure), the counter stayed at 3 after reload, the upload returned 200, and the server logged the warning once.
Generated by Claude Code