Repository navigation
fix(realtime): close connections when their session or JWT expires - #14039
ArnabChatterjee20k merged 7 commits into
Conversation
There was a problem hiding this comment.
Your trial has ended. Reactivate Greptile to resume code reviews.
🔵 Tier A · Mergeable after minor fixes
The pull request records session and JWT expiry on realtime connections and closes expired connections before event, stats, or console-tail delivery. It preserves expiry across subscriptions and updates it after session extensions, with unit and E2E coverage for JWT expiry. The latest changes also refresh impersonated connections when the actor's session is extended and close them when the actor loses impersonation capability. Latest changes: The new commits refresh impersonated connections' session expiry, revoke them when impersonation capability is removed, and add E2E coverage for both behaviors.
Fix with agent prompt### Issue 1
app/realtime.php:1040-1041
**Keep expiry when the session expires during connection setup**
If the session expires after the `user` resource authenticates it but before the `session` resource rechecks it, `connection.php:315–340` returns null while the authenticated user remains populated. This stores no session expiry or session ID, so a cookie socket can keep receiving protected events indefinitely until another access-change event; retain the originally authenticated session and its expiry, or reject setup when that session has just expired.
### Issue 2
app/realtime.php:689-693
**Cover session timeout and extension through realtime E2E tests**
The new E2E tests exercise JWT expiry only, so they would still pass if cookie connections never recorded session expiry or session extensions failed to refresh it. Please add a short-lived cookie-session test covering both timeout delivery suppression and an extension that keeps the socket receiving past its original deadline, alongside the existing realtime session-revocation tests.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.📂 Walkthrough · 11
⏳ Still open from earlier reviews · 2
Reviewed the commits since |
There was a problem hiding this comment.
Your trial has ended. Reactivate Greptile to resume code reviews.
There was a problem hiding this comment.
Your trial has ended. Reactivate Greptile to resume code reviews.
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
There was a problem hiding this comment.
Your trial has ended. Reactivate Greptile to resume code reviews.
Expiry fires no event, so a connection outlived the session or JWT it was opened with until some other user event re-checked it, while the HTTP API refuses that session's next request. Connections now record when their session (`expire`) and JWT (`jwtExpire`) end; delivery skips expired connections and closes them with a 401 frame, and a per-worker tick closes expired connections that receive nothing. Extending a session moves its expiry, so `sessions.*.update` re-resolves the user's connections again and refreshes `expire`. Re-authenticating with a session cookie replaces any JWT the connection was opened with. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the 60s sweep. Expiry is checked before every send instead: event delivery, the console stats broadcast and the live-tail flush. An idle expired connection stays open but receives nothing, and is closed at its next send. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The user resource verifies the JWT, then the session and jwtExpire resources decode it again. If it expired in between, they returned null and the connection lost both its session and its JWT expiry. The signature is checked before expiry, so on an expiry error their claims are read unverified and the connection is closed at its first send. Adds an e2e regression for a console live tail opened with a JWT: after expiry it delivers no tail frames and is closed with 401. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
83f6dbf to
89f6a8a
Compare
dde35a1 to
eb623ca
Compare
There was a problem hiding this comment.
Your trial has ended. Reactivate Greptile to resume code reviews.
…g is removed The HTTP API re-checks the impersonator flag on every request, but an impersonated socket kept the target's roles until the impersonator's session ended. users.*.update.impersonator now closes the connections that user opened while impersonating. The impersonator's events also refresh the expiry of those connections, so extending the impersonator's session extends them instead of closing them at the expiry they were opened with. Co-Authored-By: Claude Opus 5.5 <[email protected]>
| // Impersonated or not, the connection lasts only as long as the session it runs on. | ||
| $expire = $session !== null ? $user->getSessionExpiry($session->getId()) : null; |
There was a problem hiding this comment.
Keep expiry when the session expires during connection setup
If the session expires after the user resource authenticates it but before the session resource rechecks it, connection.php:315–340 returns null while the authenticated user remains populated. This stores no session expiry or session ID, so a cookie socket can keep receiving protected events indefinitely until another access-change event; retain the originally authenticated session and its expiry, or reject setup when that session has just expired.
Prompt To Fix With AI
This is a comment left during a code review.
Path: app/realtime.php
Line: 1040-1041
Comment:
**Keep expiry when the session expires during connection setup**
If the session expires after the `user` resource authenticates it but before the `session` resource rechecks it, `connection.php:315–340` returns null while the authenticated user remains populated. This stores no session expiry or session ID, so a cookie socket can keep receiving protected events indefinitely until another access-change event; retain the originally authenticated session and its expiry, or reject setup when that session has just expired.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.🟡 Minor · bug · Reply if this doesn't apply.
| // Re-read, as extending the session moves it. An impersonated | ||
| // connection has no sessionId and keeps what it was opened with. | ||
| $expire = $sessionId !== null | ||
| ? $user->getSessionExpiry($sessionId) | ||
| : ($realtime->connections[$connection]['expire'] ?? null); |
There was a problem hiding this comment.
Cover session timeout and extension through realtime E2E tests
The new E2E tests exercise JWT expiry only, so they would still pass if cookie connections never recorded session expiry or session extensions failed to refresh it. Please add a short-lived cookie-session test covering both timeout delivery suppression and an extension that keeps the socket receiving past its original deadline, alongside the existing realtime session-revocation tests.
Prompt To Fix With AI
This is a comment left during a code review.
Path: app/realtime.php
Line: 689-693
Comment:
**Cover session timeout and extension through realtime E2E tests**
The new E2E tests exercise JWT expiry only, so they would still pass if cookie connections never recorded session expiry or session extensions failed to refresh it. Please add a short-lived cookie-session test covering both timeout delivery suppression and an extension that keeps the socket receiving past its original deadline, alongside the existing realtime session-revocation tests.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.🟡 Minor · testing · Reply if this doesn't apply.
…once extended Runs on its own project with the 60s minimum session duration, extends the impersonator's session, waits until an unextended session from the same moment is refused, then checks the impersonated socket still gets the target's events. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…oked fix(realtime): end impersonated connections when the impersonator flag is removed
Stacked on #14038 (→ #14040 → #14024 → #14021).
Why
Expiry fires no event. #14021 closes a connection when its session is deleted or the user is blocked, but a session that simply times out was only noticed when some unrelated
users.*event happened to re-check it. The HTTP API refuses that session's next request; realtime kept delivering.What
expire: when the session the connection runs on ends (User::getSessionExpiry()). Set for cookie and JWT connections alike; for impersonated connections it's the impersonator's session.jwtExpire: theexpclaim of the JWT the connection was opened with, if any. A connection can't present a fresh token, so it ends with the JWT, as an HTTP request would.Realtime::isExpired()checks both, before every send: event delivery, the console stats broadcast, and the console live-tail flush (via the newEventTailRegistry::getConnections()). An expired connection gets nothing more and is closed with the existing 401 error frame. There's no separate sweep: an idle expired connection stays open but receives nothing, and is closed at its next send.sessions.*.update(session extension) comes back off the fix(realtime): skip re-resolving connections on user events that change no access #14038 skip list, so the role rebuild re-readsexpireand an extended session keeps its connections.userresource verifying it and thesession/jwtExpireresources decoding it again keeps its claims (the signature is checked before expiry), so the connection doesn't lose its expiry; it's closed at its first send.expirefrom that session and clearsjwtExpire.$closingis keyed by connection so one connection isn't closed twice.Behaviour change
JWT-authenticated sockets are now closed when the JWT expires (default 900s, max 3600s). Clients authenticating realtime with a JWT need to reconnect with a fresh one, same as for HTTP.
Not covered
Tests
UserTest::testGetSessionExpiry,MessagingTest::testIsExpired,EventTailRegistryTest::testGetConnections;testSubscribeKeepsConnectionAuthorizationStatecovers carryingexpire/jwtExpireacross resubscribes;sessions.S.updatemoved to the re-resolving cases.testConnectionEndsWithJwtopens one socket with a 2s JWT and one with the session cookie behind it, waits (polling) untilGET /accountrefuses the JWT, then updates prefs: the cookie socket gets the event and stays open, the JWT socket gets nothing and is closed with 401.RealtimeCustomClientQueryTestWithMessage::testConsoleTailEndsWithJwtdoes the same for a console live tail opened with a 2s root JWT: no tail frames after expiry, then 401 and close. Not run locally (no Docker); relying on CI.🤖 Generated with Claude Code