Skip to content

End impersonated realtime connections when a project invalidates its sessions - #15089

Merged
ArnabChatterjee20k merged 1 commit into
fix/realtime-permission-ingest-fail-closedfrom
fix/realtime-impersonated-session-sweep
Oct 29, 2026
Merged

ArnabChatterjee20k merged 1 commit into
fix/realtime-permission-ingest-fail-closedfrom
fix/realtime-impersonated-session-sweep

Conversation

@ArnabChatterjee20k

Copy link
Copy Markdown
Member

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 a sessionId. Impersonated connections never have one: they run on the impersonator's session, which is stored on the connection as impersonator (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 on main too.

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:

  • no longer exists or is blocked,
  • no longer holds an active session (the same sessionActive() check against the new invalidation time), or
  • is no longer an impersonator.

These 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.permissions span stay as they are, and realtime.closed counts 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_INVALIDATED makes a project report that all of its sessions are invalidated. A project lookup for frozen-project blocks, 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 shows verified and realtime.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 shows timed_out and realtime.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 whole tests/unit/Realtime suite passes (56 tests). composer lint, composer analyze and Rector are clean on the changed files.

🤖 Generated with Claude Code

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]>
@luxor-code-review

luxor-code-review Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

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.

Files Change
src/Appwrite/Realtime/Event/Handler.php Sweeps and closes connections whose sessions or impersonated users no longer have access when permissions change.
tests/unit/Realtime/Harness/server.php Test harness server supporting the permission-sweep scenario.
tests/unit/Realtime/PermissionsChangedTest.php New unit test covering the permissions-changed sweep behavior.

Reviewed commit: 92587c2

@ghost

ghost commented Oct 29, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The sweep validates the correct impersonator session while preserving connections backed by another project's sessions.

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.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 3
File Change
src/Appwrite/Realtime/Event/Handler.php Checks project impersonators during the sweep and includes them in fail-closed closure decisions.
tests/unit/Realtime/Harness/server.php Adds impersonation/session connection setup and configurable project-session invalidation and blocking.
tests/unit/Realtime/PermissionsChangedTest.php Covers normal project impersonators, console impersonators, and fail-closed timeout behavior.

Reviewed 92587c2 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Tier S · Looks good to merge. Summary

@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

6 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (3)
  • php.appwrite.secret-compare-timing (1)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@github-actions

Copy link
Copy Markdown

✨ Benchmark results

Comparing fix/realtime-permission-ingest-fail-closed (before) → fix/realtime-impersonated-session-sweep (after).

Metric Before After Change
🚀 Requests/sec 201.39 209.83 ⚪ +4.2%
⏱️ Latency P50 86.5 ms 82.06 ms 🟢 -5.1%
⏱️ Latency P95 205.16 ms 194.04 ms 🟢 -5.4%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 82.06 194.04 13,338 209.83 -11.13
Account 153.69 285.76 702 11.63 -7.46
TablesDB 79.39 138.54 7,254 116.93 -29.7
Storage 76.81 185.13 3,510 57.93 -1.06
Functions 113.84 239.1 1,872 31.58 +11.06

Top API waits (after)

API request Max wait (ms)
account.name.update 490.56
functions.variables.update 457.09
storage.buckets.create 449.04
functions.delete 412.76
storage.buckets.delete 393.33

@ArnabChatterjee20k
ArnabChatterjee20k merged commit 578e93d into fix/realtime-permission-ingest-fail-closed Oct 29, 2026
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant