Repository navigation
End impersonated realtime connections when a project invalidates its sessions - #15089
Conversation
The project-wide sessions.delete sweep skipped every connection without a sessionId, and impersonated connections never carry one: they run on the impersonator's session. After "invalidate all sessions" they kept serving the target user's data on an invalidated session. The sweep now checks impersonated connections whose impersonator belongs to the project, against that impersonator's session, status and impersonator flag, reading each impersonator once per event. Console impersonators hold console sessions and are left alone. The fail-closed path closes the same set. The harness can now open impersonated and session-bearing connections and invalidate a project's sessions, and three tests cover the verified, console-impersonator and timed-out sweeps. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Review complete. No issues found — approved ✅. This PR extends the realtime event handler so permission-change events trigger a connection sweep: connections are validated against current session/user state and closed when access was revoked, with impersonator connections handled per project and fail-closed behavior on stale DB reads.
Reviewed commit: 92587c2 |
🟢 Tier S · Ready to merge
The project-wide realtime session sweep now validates impersonator sessions for connections opened on the swept project's session, and its fail-closed path closes those connections too. The realtime harness and permission-change tests add coverage for project impersonators, console impersonators, and timed-out sweeps.
📂 Walkthrough · 3
Reviewed |
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
578e93d
into
fix/realtime-permission-ingest-fail-closed
Issue / Description
Tracking: appwrite-labs/cloud#7109
Stacked on #15086 (base:
fix/realtime-permission-ingest-fail-closed). It lands there when the stack is merged.When a project invalidates every session (
projects.{projectId}.sessions.delete), the realtime server sweeps the project's connections and closes those whose session has ended. The sweep skipped every connection without asessionId. Impersonated connections never have one: they run on the impersonator's session, which is stored on the connection asimpersonator(projectId,userId,sessionId). So after "invalidate all sessions", an impersonated connection kept serving the target user's data on a session that had just been invalidated. The same gap was in the sweep's fail-closed path (timeout, error or coroutine limit). The bug is present onmaintoo.Solution
In
Appwrite\Realtime\Event\Handler::checkProject(), an impersonated connection is now checked against its impersonator when the impersonator belongs to the swept project. The connection is closed when the impersonator:sessionActive()check against the new invalidation time), orThese are the same rules the per-user branch applies to impersonator connections. Each impersonator is read once per event, however many connections it holds.
Impersonated connections whose impersonator belongs to another project are left alone. In practice that means console impersonators, who hold console sessions that a project's invalidation does not touch.
The fail-closed path now also closes impersonated connections whose impersonator belongs to the swept project. It closes exactly what the sweep would have checked, no more. The existing late-check guards and the
realtime.permissionsspan stay as they are, andrealtime.closedcounts these closes.Tests
The harness (
tests/unit/Realtime/Harness/server.php) can now open impersonated connections (testImpersonate,testImpersonatorProject) and session-bearing connections (testSession). Every fake user holds one session created an hour ago.TEST_SESSIONS_INVALIDATEDmakes a project report that all of its sessions are invalidated. A project lookup forfrozen-projectblocks, so a sweep can be made to time out.testSessionSweepClosesImpersonatedConnection: a project impersonator's connection and a normal session connection both receive the 401 frame and close after the sweep. The span showsverifiedandrealtime.closed = 2.testSessionSweepKeepsConsoleImpersonatorConnection: a console impersonator's connection stays open through the project's sweep and still receives the next event, while the project's own session connection is closed.testStuckSessionSweepClosesImpersonatedConnection: when the sweep times out, the impersonated connection is closed (fail closed), the span showstimed_outandrealtime.closed = 1, and a guest connection stays open.Red → green: on the #15086 head (
a2a716ed27), the first and third tests fail at "Expected the unauthorized error frame before the close", because the impersonated connection is never closed. The second test passes there, which confirms the harness opens impersonated connections correctly. With the fix, the wholetests/unit/Realtimesuite passes (56 tests).composer lint,composer analyzeand Rector are clean on the changed files.🤖 Generated with Claude Code