Repository navigation
Add signed browser sessions and bound client tokens - #7361
Conversation
[Medium impact] The PR appears safe to merge as part of the reviewed stack, with #7364 restoring enforcement by default. SummaryAdds signed browser sessions, session-bound client tokens, app-specific credentials, key rotation, and shared temporary signing keys.
Reviews (15) · Last reviewed commit: "Restore the enforce value of the session..." · Reviewed by Greptile |
Merging this PR will improve performance by 6.86%2 warnings
Performance Changes
Tip Curious why performance improved? Comment Comparing Footnotes
|
514269c to
b7efd10
Compare
b227f18 to
427f2c0
Compare
427f2c0 to
db30d29
Compare
Introduce a stdlib HS256 session codec, environment-only signing keys with rotation, and persistent local secrets excluded from exports. Preserve the local secret during web reinitialization. Validation: 421 affected tests passed, plus unknown-key diagnostic coverage; Ruff and targeted pyright passed. Refs: ENG-12910
db30d29 to
44c4b4e
Compare
There was a problem hiding this comment.
1 issue found across 11 files
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="reflex/utils/frontend_skeleton.py">
<violation number="1" location="reflex/utils/frontend_skeleton.py:471">
P1: This branch follows a symlinked `.web/backend` and removes its target's contents, which can erase files outside the generated web directory during `reflex init`. Guard against symlink traversal and handle the link without modifying its target.</violation>
</file>
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
Drop the persisted .web/backend/session_secret file and its init and export special cases. When REFLEX_SECRET_KEY is unset, reflex run generates a temporary key and sets it in the environment before the backend starts, so workers and reloads share one key; sessions reset on restart and a warning says so (large in prod, one line in dev). Session signing keys are derived from the secret key with HMAC, so other future uses of REFLEX_SECRET_KEY cannot accept session cookies.
There was a problem hiding this comment.
2 issues found across 12 files (changes from recent commits).
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="reflex/reflex.py">
<violation number="1" location="reflex/reflex.py:627">
P2: In full-stack runs, this puts the generated signing key in `os.environ`, which the dev and production frontend launchers pass wholesale to Node. Filter `REFLEX_SECRET_KEY` from frontend child environments so plugins and build scripts cannot read the backend signing key.</violation>
</file>
<file name="packages/reflex-base/src/reflex_base/environment.py">
<violation number="1" location="packages/reflex-base/src/reflex_base/environment.py:735">
P2: Update the session guide to use `REFLEX_SECRET_KEY` and document the temporary fallback; deployments following its `REFLEX_SESSION_SECRET` instructions will otherwise generate different keys and reject each other’s sessions.</violation>
</file>
Tip: Get a Slack DM when cubic finishes reviewing your PRs. Connect Slack
View guided diff | Turn on auto-fix | Re-trigger cubic
# Conflicts: # reflex/reflex.py
reflex now imports reflex_base.session, which is not in reflex-base 0.11.0.
Compare full warning and error messages in the session tests, clear the session timing env vars before checking config defaults, and say that only reflex run generates REFLEX_SECRET_KEY.
A key shorter than 32 bytes raised on the first session request, so the backend started and then answered each session request with a 500. ensure_secret_key now validates the configured key ring, and reflex run calls it before the backend starts. Add the reflex changelog fragment for the reflex run change.
REFLEX_SESSION_TOKEN_MODE now defaults to enforce. Set it to warn or off to keep the earlier behaviour during a migration.
A blank value read as unset, so reflex run generated a key. An environment file that holds the blank value restored it on the next config load, and each worker then generated its own key.
A blank REFLEX_SECRET_KEY counts as not set: reflex run warns and generates a key, as for a missing one. The generated key now lives in an internal environment variable. An environment file that holds a blank REFLEX_SECRET_KEY restored that blank value in each worker, and each worker then generated its own key.
The transport and the frontend that handle enforcement arrive in later pull requests of this stack, so the default changes to enforce in the last one. With enforce here, the transport pull request failed its browser tests against the frontend on main.
The drop_connection button reconnected with socket.connect(), which resends the query of the socket's last connect through the frontend. Since main's session-bound tokens (#7361), a new tab connects with an empty token and the backend issues one by new_token, so that query still carries the empty token: the backend issued yet another token on the reconnect. The awaited call stayed filed under the old token, which no socket linked any more, so neither its result nor its BrowserDisconnectedError reached the page. Reconnect through socket.reconnect(), the frontend's own path after a dropped connection, which connects with the tab's current token and the session cookie that authorizes it. The two reconnect tests now also check that the tab comes back with the token it had, which failed before this change. Claude-Session: https://claude.ai/code/session_01LwJUCVxPh3ihrnaFn6CWnf
Add signed browser sessions and cryptographically bound per-tab client tokens. A client token alone cannot prove ownership of another session. Credentials are bound to the application's name, use HS256 with a pinned algorithm, and validate schema, lifetime, signing-key ID, and audience. Rotation preserves existing tab bindings.
REFLEX_SECRET_KEYsupplies a comma-separated secret key ring; session signing keys are derived from it with HMAC. No key is stored on disk. Without it,reflex rungenerates a temporary key and sets it in the environment before the backend starts, so workers and reloads share one key; sessions reset on restart, with a large warning in prod and one line in dev. Config exposes a seven-day default TTL and half-TTL refresh interval; enforcement modes areoff,warn(default in this pull request), andenforce, set withREFLEX_SESSION_TOKEN_MODE. #7364 changes the default toenforce.Merged with current main. Review order: #7361 → #7362 → #7363 → #7364. The earlier #7360 dependency is merged.
Validation: 244 focused session, initialization, export, and locking tests passed. Audience-isolation and persistence regressions fail without their fixes. The full assembled stack passes 12,147 unit tests with 80.15% coverage; see #7364 for integration validation.
Session ownership is intentionally valid for an already established connection; expiry gates HTTP credentials and reconnects. This is not application login or per-session revocation. The review discussion records the rationale.
A configured
REFLEX_SECRET_KEYshorter than 32 bytes now stopsreflex runat startup. Before, the backend started and each session request returned a 500. A blankREFLEX_SECRET_KEYcounts as not set:reflex runwarns and generates a key, and keeps it in an internal environment variable that workers inherit, so an environment file with a blank value cannot replace it.Related to ENG-12910, ENG-9088.