Repository navigation
fix(realtime): skip re-resolving connections on user events that change no access - #14038
ArnabChatterjee20k merged 10 commits into
Conversation
There was a problem hiding this comment.
Your trial has ended. Reactivate Greptile to resume code reviews.
🟡 Tier B · Needs changes before merging
The PR skips permission re-resolution for user events that do not change access, while retaining conservative handling for other events. It also tracks impersonator bindings and revokes impersonated connections when the actor loses access. The latest changes add session and JWT expiry tracking, refresh session deadlines after extension, and check expiry before event delivery, with unit and E2E coverage. Latest changes: The newest commits revoke connections when impersonator capability is removed and add session/JWT expiry enforcement, session-extension handling, and regression tests.
Fix with agent prompt### Issue 1
app/realtime.php:801-807
**Enforce expiry before authenticated message operations too**
Expiry is checked only on event delivery: `onMessage` still dispatches using the stored user and roles, so an expired JWT/session connection with no subscriptions can keep issuing `presence` messages and writing presence documents indefinitely (`Presence.php:68–95`). Please check expiry before dispatching authenticated operations as well, rather than relying on a future subscribed event to close the socket.
### Issue 2
tests/e2e/Services/Realtime/RealtimeCustomClientTest.php:1128-1134
**Exercise an access-changing target event before revoking the actor**
The name update is now allowlisted, so this test never rebuilds the target's connection state and still passes if the impersonator restoration at `app/realtime.php:690` is removed. Use an access-changing update, such as labels or verification, before deleting the actor's session to actually verify that the binding survives re-resolution.
---
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 · 1
✅ Fixed since the last review · 1
Reviewed the commits since |
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)
|
…ge no access Every users.* event was flagged permissionsChanged, so a prefs or name update re-fetched the user and rebuilt roles for all of their open connections. User events that neither shape roles nor end a session are now listed in USER_EVENTS_WITHOUT_ACCESS_CHANGE and skipped; anything not listed still re-resolves, so new user events are safe by default. update.password and recovery.*.update stay re-resolving: with invalidateSessions they delete sessions without emitting sessions.*.delete. update.email and update.phone reset verification. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Creating a session can end others without emitting sessions.*.delete: the session limit hook and ID-token sign-in delete older sessions directly. Magic URL, email/phone OTP and OAuth sign-in can also set emailVerification or phoneVerification, which changes roles. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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
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]>
ghost
left a comment
There was a problem hiding this comment.
🔴 Tier D · 1 blocking finding to address. Summary
app/realtime.php:636-639
Close impersonated connections when impersonation is revoked
Setting /users/{actorId}/impersonator to false emits users.{actorId}.update.impersonator and reaches this new branch, but an active session still passes $revoked, leaving the socket receiving the target user's private events. Please also check the actor's impersonator capability here, matching the check in app/init/realtime/connection.php:348.
Prompt To Fix With AI
This is a comment left during a code review.
Path: app/realtime.php
Line: 636-639
Comment:
**Close impersonated connections when impersonation is revoked**
Setting `/users/{actorId}/impersonator` to false emits `users.{actorId}.update.impersonator` and reaches this new branch, but an active session still passes `$revoked`, leaving the socket receiving the target user's private events. Please also check the actor's `impersonator` capability here, matching the check in `app/init/realtime/connection.php:348`.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.🔴 Critical · security · Reply if this doesn't apply.
tests/e2e/Services/Realtime/RealtimeCustomClientTest.php:1128-1134
Exercise an access-changing target event before revoking the actor
The name update is now allowlisted, so this test never rebuilds the target's connection state and still passes if the impersonator restoration at app/realtime.php:690 is removed. Use an access-changing update, such as labels or verification, before deleting the actor's session to actually verify that the binding survives re-resolution.
Prompt To Fix With AI
This is a comment left during a code review.
Path: tests/e2e/Services/Realtime/RealtimeCustomClientTest.php
Line: 1128-1134
Comment:
**Exercise an access-changing target event before revoking the actor**
The name update is now allowlisted, so this test never rebuilds the target's connection state and still passes if the impersonator restoration at `app/realtime.php:690` is removed. Use an access-changing update, such as labels or verification, before deleting the actor's session to actually verify that the binding survives re-resolution.
---
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]>
|
Re the latest Hansi review on 89f6a8a: 🔴 Close impersonated connections when impersonation is revoked — already fixed further up the stack, in #14041 (stacked on #14039 → this PR). The impersonating loop now also closes a connection when the actor's 🟡 Exercise an access-changing target event before revoking the actor — valid, and not covered elsewhere in the stack. This PR puts |
…oked fix(realtime): end impersonated connections when the impersonator flag is removed
fix(realtime): close connections when their session or JWT expires
89bfac8
into
fix/realtime-impersonation
| // Expiry fires no event, so it is checked on every send. Nothing more | ||
| // once the session or JWT has expired, as the HTTP API would refuse it; | ||
| // the connection is closed below. | ||
| if ($realtime->isExpired($id, $now)) { | ||
| $closing[$id] = true; | ||
| continue; | ||
| } |
There was a problem hiding this comment.
Enforce expiry before authenticated message operations too
Expiry is checked only on event delivery: onMessage still dispatches using the stored user and roles, so an expired JWT/session connection with no subscriptions can keep issuing presence messages and writing presence documents indefinitely (Presence.php:68–95). Please check expiry before dispatching authenticated operations as well, rather than relying on a future subscribed event to close the socket.
Prompt To Fix With AI
This is a comment left during a code review.
Path: app/realtime.php
Line: 801-807
Comment:
**Enforce expiry before authenticated message operations too**
Expiry is checked only on event delivery: `onMessage` still dispatches using the stored user and roles, so an expired JWT/session connection with no subscriptions can keep issuing `presence` messages and writing presence documents indefinitely (`Presence.php:68–95`). Please check expiry before dispatching authenticated operations as well, rather than relying on a future subscribed event to close the socket.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.🟠 Major · security · Reply if this doesn't apply.
Stacked on #14040 (→ #14024 → #14021).
What
#14021 sets
permissionsChangedon everyusers.*event, so a prefs, name or avatar update re-fetches the user and rebuilds roles for all of their open realtime connections.This adds
USER_EVENTS_WITHOUT_ACCESS_CHANGE(next toRESOURCE_LEAF_NAMES): user events, written afterusers.{userId}with*for an ID segment, that neither shape roles nor end a session.fromPayload()doesn't flag those. Anything not listed still re-resolves, so a new user event is safe by default.Deliberately not skipped
update.password,recovery.*.update— withinvalidateSessionson, these delete sessions directly without emittingsessions.*.delete.sessions.*.create— the session limit hook and ID-token sign-in delete older sessions directly, and magic URL, OTP and OAuth sign-in can setemailVerification/phoneVerification.update.email,update.phone— resetemailVerification/phoneVerification, which changes the verified/unverified roles.update.status,update.labels,update.verification,verification.*.update,sessions.*.delete,sessions.delete,delete.update.mfaand the other MFA events are skipped because the realtime connection doesn't check MFA factors. If MFA enforcement is added there, they need to come off the list.Tests
testFromPayloadUserEventsChangePermissionsnow also covers email, phone, password, verification and recovery updates.testFromPayloadUserEventsWithoutAccessChangechecks every skipped event returnspermissionsChanged: false.🤖 Generated with Claude Code