Skip to content

Add signed browser sessions and bound client tokens - #7361

Merged
FarhanAliRaza merged 16 commits into
mainfrom
codex/codex/session-token-primitives
Nov 25, 2026
Merged

FarhanAliRaza merged 16 commits into
mainfrom
codex/codex/session-token-primitives

Conversation

@masenf

@masenf masenf commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

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_KEY supplies a comma-separated secret key ring; session signing keys are derived from it with HMAC. No key is stored on disk. Without it, 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, 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 are off, warn (default in this pull request), and enforce, set with REFLEX_SESSION_TOKEN_MODE. #7364 changes the default to enforce.

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_KEY shorter than 32 bytes now stops reflex run at startup. Before, the backend started and each session request returned a 500. A blank REFLEX_SECRET_KEY counts as not set: reflex run warns 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.

@masenf
masenf added this pull request to stack #7365 September 30, 2026 00:41
@masenf masenf changed the title codex/codex/session token primitives Add signed browser sessions and bound client tokens Sep 30, 2026
@greptile-apps

greptile-apps Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge as part of the reviewed stack, with #7364 restoring enforcement by default.

Summary

Adds signed browser sessions, session-bound client tokens, app-specific credentials, key rotation, and shared temporary signing keys.

  • Browser sessions now sign app-scoped credentials and bind client tokens to them.
  • Backend runs now share signing keys across workers and reloads.

Reviews (15) · Last reviewed commit: "Restore the enforce value of the session..." · Reviewed by Greptile

@codspeed

codspeed Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 6.86%

2 warnings

⚠️ 16 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 3 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 3 improved benchmarks
✅ 143 untouched benchmarks
⏩ 8 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_route_matching[1000] 96.7 ms 90 ms +7.48%
⚡ test_route_matching[100] 10.1 ms 9.4 ms +6.97%
⚡ test_route_matching[10] 1.3 ms 1.2 ms +6.11%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing codex/codex/session-token-primitives (8c111a0) with main (a0689d6)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@masenf
masenf force-pushed the codex/codex/session-token-primitives branch from 514269c to b7efd10 Compare October 5, 2026 21:24
@linear-code

linear-code Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

ENG-9088

ENG-12910

@masenf
masenf force-pushed the codex/codex/session-token-primitives branch 3 times, most recently from b227f18 to 427f2c0 Compare October 8, 2026 18:36
Base automatically changed from codex/session-cookie-privacy to main October 8, 2026 19:03
@masenf
masenf force-pushed the codex/codex/session-token-primitives branch from 427f2c0 to db30d29 Compare October 8, 2026 19:03
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
@masenf
masenf force-pushed the codex/codex/session-token-primitives branch from db30d29 to 44c4b4e Compare October 14, 2026 09:35
@masenf
masenf marked this pull request as ready for review October 14, 2026 09:54
@masenf
masenf requested a review from a team as a code owner October 14, 2026 09:54

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread reflex/utils/frontend_skeleton.py Resolved
Comment thread tests/units/reflex_base/test_session.py Resolved
@masenf masenf mentioned this pull request Oct 20, 2026
10 of 13 tasks
@masenf masenf added roadmap https://github.com/reflex-dev/reflex/issues/2727 security labels Oct 23, 2026
@FarhanAliRaza FarhanAliRaza added the enhancement Anything you want improved label Nov 12, 2026 — with ChatGPT Codex Connector
@FarhanAliRaza FarhanAliRaza added the on deck PRs lined up to review / merge next label Nov 12, 2026
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.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread reflex/reflex.py Resolved
# Conflicts:
#	reflex/reflex.py
reflex now imports reflex_base.session, which is not in reflex-base 0.11.0.
Comment thread tests/units/reflex_base/test_session.py Resolved
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.
@FarhanAliRaza
FarhanAliRaza merged commit 38744ee into main Nov 25, 2026
171 checks passed
@FarhanAliRaza
FarhanAliRaza deleted the codex/codex/session-token-primitives branch November 25, 2026 16:22
FarhanAliRaza added a commit that referenced this pull request Nov 25, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Anything you want improved on deck PRs lined up to review / merge next roadmap https://github.com/reflex-dev/reflex/issues/2727 security

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants