Authorize browser state through session-aware transports

Merged
MA
10d ago
+2644-134

Bind HTTP requests, Reflex socket events, custom WebSocket routes, and uploads to verified browser sessions before accessing state. Cookie-less sockets receive session/client tokens in-band so hydration can start immediately. Fresh clients in off mode also receive a routing token.

POST /_reflex/session installs an HttpOnly cookie only from a trusted Origin when exchanging an in-band credential; wildcard CORS alone cannot authorize that exchange. Existing valid cookies take precedence. Cookies use Secure, SameSite=None, and Partitioned with the documented development fallback. Enterprise's /_reflex/auth/token contract is unchanged.

Event contexts inherit browser authority, explicit None means anonymous, and trusted server work can use SessionToken.SYSTEM. Custom WebSockets no longer inherit SYSTEM accidentally. Authenticated live sockets can renew their own credential after cookie expiry while preserving owner and tab bindings. HTTP responses advertise the refresh interval. Streamed uploads use a distinct transaction context.

The internal transport module is named reflex.session_middleware to preserve the public rx.session() database helper across import orders. Dependency floors and deprecation versions match current main.

Depends on #7361; followed by #7363 and #7364. Merged with current main.

Validation: 390 transport/context/upload tests against an isolated real Redis server, two fresh-process database import regressions, full stack unit tests (12,147 passed; 80.15% coverage), and clean pyright reflex tests. Regressions reproduce the custom-WebSocket authority, off-mode token, and idle-renewal failures before their fixes. Real Uvicorn/Granian/browser coverage is in #7364.

Review fixes:

  • /_reflex/session keeps a client token that the session does not own outside enforce, as the socket connection does. A browser that stores no cookie (plain HTTP in prod, blocked cookies) got a new token on each connection, and the client reconnected without end.
  • The exchange trusts a page on the host that the request reached. The frontend replaces a loopback API host with the host of its page, so a dev server opened through a LAN address got a 403.
  • Only POST /_reflex/session renews the cookie. The middleware no longer adds it to other responses, which a shared cache can store.
  • A socket that connected with a valid cookie gets no credential on session_refresh; the client renews the HttpOnly cookie over HTTP. The in-band credential stays for sockets without a valid cookie.
  • SharedState._link_to applies the session check when the link target is a client token, so enforce covers that path.
  • A request body nested past the recursion limit returns 400.

Not changed: any port of the backend host can open a socket in the same session, because cookies are not scoped by port. The guide in #7364 states this.

The default mode stays warn in this pull request. #7364 changes it to enforce, where the frontend that handles enforcement is present.

Structure changes after review:

  • authorize(session, token) in reflex_base.session is the one place that applies the session mode. EventContext, the upload route and the shared-state link call it.
  • SessionTokenManager.client_token_for decides a tab's token for both the socket connection and the session endpoint.
  • The socket sends a credential encoded for exchange: valid for one minute and only at POST /_reflex/session. A cookie value is not accepted for exchange, and an exchange credential is not accepted as a cookie.
  • One record per socket holds its session, next refresh time and whether it connected with a cookie.

Maximum session age: the credential carries the time of first issue, renewal keeps it, and the expiry cannot pass first issue plus session_token_max_age (new rx.Config field, seven days by default). A live socket gets no new credential after that age. Before, each renewal set a new expiry for the same session, so a credential that kept renewing never ended.

Second review round:

  • A socket that connected with a cookie always renews at POST /_reflex/session and never receives a credential. The backend compared against the session as it was at connect, so an older socket received an exchangeable credential although the browser held a renewed cookie.
  • A session past its maximum age is sent to the session endpoint for a new session, in place of no reply.
  • The same-host origin rule requires the scheme of the request, and in production the exact origin.
  • link_token_to_sid works without an ambient event context, as on main (the benchmark harness connects that way).

Related to ENG-12911, ENG-12912, ENG-12913, ENG-12914, ENG-12915, ENG-9088.