Keep browser sessions working over plain HTTP

MA
12h ago
+420-119

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 Origin is http:// on a host other than localhost, *.localhost or a loopback IP, api_url is not https (the frontend keeps an https api_url on any page), and the request's scheme is not https. That scheme comes from the trusted X-Forwarded-Proto through trusted_proxy_hops, read the same way as the socket origin check, and otherwise from the ASGI scheme.
  • On plain HTTP, /_reflex/session sets reflex_session_<hash>=…; Path=/; Max-Age=…; HttpOnly; SameSite=Lax. Dev already sent this cookie for a non-loopback api_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 no X-Forwarded-Proto keeps Secure, because its pages send an https:// 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.
  • _clip and _trusted_forwarded_value move from app.py to session_middleware.py, so the socket origin check and the middleware share them without a circular import. session_endpoint drops its api_url argument 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

  1. Same-origin GET without an Origin header reads only __Host-, so on plain HTTP a custom api_transformer GET 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 send Origin. Falling back to the ASGI scheme here would expose HTTPS sites behind a TLS proxy (with trusted_proxy_hops=0, the default) to cookie tossing. The sessions guide now says this.
  2. 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 sends Origin: http://evil can trigger it on an HTTPS site that leaves api_url at its default and runs behind a proxy with trusted_proxy_hops=0. The message names the origin, and its advice (set api_url to https, as the guide already says) silences it.
  3. An http page on another site calling an https backend behind a TLS proxy, when the backend's runtime api_url is not https and trusted_proxy_hops=0, now gets the Lax cookie, which the browser refuses cross-site. On a1 it got a partitioned Secure cookie that worked. Setting api_url or trusted_proxy_hops, as the warning says, restores it.
  4. 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_expected in 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 with KeyError: 'reflex_session_…', because the endpoint issued __Host-… to the http://192.0.2.2 origin. It passes now.
  • test_cookie_policy now covers the transport matrix: LAN origin with a loopback or LAN api_url, trusted and untrusted X-Forwarded-Proto, localhost, *.localhost, 127.0.0.2 and [::1], an https origin behind a plain-HTTP proxy, an https api_url, a direct TLS scheme, no Origin, and a malformed Origin. It checks the full Set-Cookie value 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.py passes.
  • Full tests/units: 17023 passed. 264 failures and errors are all in tests/units/reflex_cli/v2/* (Usage: root …) and tests/units/reflex_bench/drivers/test_browser.py (the Playwright browser build is missing). Neither depends on this change, and reflex_cli/v2/test_apps.py fails 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, default api_url) and drove Chromium at http://192.0.2.2:3061/. It stored reflex_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