Skip to content

fix(realtime): end admin-mode connections with console access - #15060

Merged
ArnabChatterjee20k merged 11 commits into
mainfrom
security/realtime-admin-revocation
Oct 29, 2026
Merged

ArnabChatterjee20k merged 11 commits into
mainfrom
security/realtime-admin-revocation

Conversation

@ArnabChatterjee20k

Copy link
Copy Markdown
Member

What does this PR do?

An admin-mode realtime connection (a console user connecting to a project with x-appwrite-mode: admin) is registered in the project it administers, with no project user. Every path that ends a connection when access changes looks connections up by project user, or by the console user in the console project, so none of them reached it. A console user who signed out of the console or was removed from the organization kept receiving the project's events until the socket closed or the console session expired.

This PR:

  • Records on an admin-mode connection the console user it runs as and the membership that granted it access (admin in the connection tree), with Realtime::getAdminConnections() to find them.
  • On a console permission change for that user, re-checks those connections like the HTTP API would: the console session still active with its factors, the user not blocked or deleted, the membership still there with the same roles, the project still in the same organization. Any failure closes the connection with the usual unauthorized frame.
  • On a project transfer (projects.{id}.update.team), closes admin-mode connections opened through the previous organization.
  • Adds account to Realtime::CONSOLE_ALLOWLIST, so console account events (sign-out, session deletion, status) reach realtime. Before this, nothing reached realtime when a console user signed out.
  • Moves the membership lookup to User::getMembershipRoles(), used both when the connection opens and when it is re-checked.

Tracking: appwrite-labs/cloud#7054

Test Plan

New e2e test RealtimeConsoleClientTest::testAdminModeEndsWithConsoleAccess:

  • SUCCESS: ending another of the member's console sessions leaves the admin connection open, and it still receives a project document event.
  • FAILURE: signing out of the console session the connection was opened with closes it.
  • FAILURE: removing the member from the organization closes it.

Red/green on a local stack:

  • Before the fix, both FAILURE paths fail with Timed out waiting for the server to close the socket. Each was run on its own.
  • After the fix, the new test passes, and the full RealtimeConsoleClientTest passes (20 tests).
  • RealtimeCustomClientTest has the same pass/fail set with and without the change. Its 8 failures there come from the local stack (policy and transaction endpoints are not on the HTTP side) and are unrelated.

Related PRs and Issues

  • Tracking: appwrite-labs/cloud#7054

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

🤖 Generated with Claude Code

ArnabChatterjee20k and others added 2 commits October 28, 2026 18:23
An admin-mode connection is registered in the project it administers,
so console events about its user never reached it. Record the console
user and membership on the connection and re-check them when that user's
console access changes: session ended, user blocked or deleted,
membership removed or changed, or the project moved to another
organization. Console account events now reach realtime so signing out
of the console is one of them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@luxor-code-review

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

Copy link
Copy Markdown

Review complete. 🟠 1 high · 🟡 1 medium

💬 Inline comments (1)

  • 🟠 Block/delete events never recheck admin connections — Realtime.php:15
📍 Findings outside the diff (1) — 🟡 1 medium — defects on lines GitHub can't attach comments to

🟡 Medium — Re-auth leaves stale admin metadata on connection · src/Appwrite/Realtime/Message/Handlers/Authentication.php

An admin-mode connection stores admin metadata at open (app/realtime.php:1240), and subscribe() carries that key across every re-subscribe (src/Appwrite/Messaging/Adapter/Realtime.php:223). The Authentication message handler rebuilds the entry for a new identity — new roles, new sessionId/expire (src/Appwrite/Realtime/Message/Handlers/Authentication.php:165-172) — but neither clears nor recomputes admin, and it only blocks re-auth for impersonated connections (Authentication.php:114-116). The connection then runs as a normal user while getAdminConnections() still matches it, so the revocation loop (app/realtime.php:676-694) closes it based on the abandoned admin identity's membership and overwrites its expire with the stale admin session's expiry.

🧹 Nitpicks (2) — 🟢 2 low
  • 🟢 Memoize administered project fetches in revocation loop (realtime.php:679) — The admin-connection revocation loop calls getDocument('projects', ...) once per connection (app/realtime.php:679), so several admin connections on the same project re-fetch the identical console project document in one pass.
  • 🟢 Cover role-change and transfer revocation triggers (RealtimeConsoleClientTest.php:1481) — The new test covers only session logout and membership removal as admin-mode revocation triggers (tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php:1369-1481).

This PR closes admin-mode realtime connections when the console access that granted them ends. checkForProjectUsage in app/realtime.php now snapshots the console user's team membership at connect time (admin metadata), finds those connections via getAdminConnections(), and closes them when the governing session is revoked, the project leaves the team, or the membership roles no longer match. Supporting changes: src/Appwrite/Utopia/Database/Documents/User.php extracts getMembershipRoles(), src/Appwrite/Messaging/Adapter/Realtime.php preserves the new admin key across re-subscribes, and src/Appwrite/Event/Realtime.php allows account events to reach the console so logouts propagate. A new e2e test covers logout and membership-removal revocation.

Files Change
app/realtime.php Snapshot admin identity at connect; recheck and close admin connections on console user events and team transfer
src/Appwrite/Event/Realtime.php Add account to the console event allowlist
src/Appwrite/Messaging/Adapter/Realtime.php Preserve admin metadata on re-subscribe; add getAdminConnections()
src/Appwrite/Utopia/Database/Documents/User.php Add getMembershipRoles() helper replacing the inline membership loop
tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php New e2e test testAdminModeEndsWithConsoleAccess

Review note: one scanner covering connection-lifecycle corner cases completed after the main verification pass; its low-severity performance candidate was adjudicated and rejected, so no findings were omitted from the verified set.

Reviewed commit: cea47e9

@ghost

ghost commented Oct 28, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The reviewed changes and surrounding event, authentication, and connection flows show no actionable correctness issues.

Admin-mode realtime connections now retain the console user and organization membership that authorized them. Console account and membership events revalidate those connections, while organization deletion and project transfers close connections whose access has ended. The change also centralizes membership-role lookup and adds end-to-end coverage for revocation, transfer, blocking, and re-authentication behavior.

Verdict New comments Fixed Still open
✅ Approved 0 1 0
📂 Walkthrough · 6
File Change
app/realtime.php Tracks admin-mode connections and closes them after session, membership, organization, or project-transfer access changes.
src/Appwrite/Event/Realtime.php Allows console account and user events to be published to realtime.
src/Appwrite/Messaging/Adapter/Realtime.php Persists admin connection metadata across subscriptions and adds lookups by console user and organization.
src/Appwrite/Realtime/Message/Handlers/Authentication.php Rejects session re-authentication on admin-mode connections.
src/Appwrite/Utopia/Database/Documents/User.php Provides a shared lookup for confirmed membership roles.
tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php Covers admin connection survival and closure for console access changes.
✅ Fixed since the last review · 1
  • Handle organization deletion for admin connections · app/realtime.php:639

Merged main (b5b69cb); no changes to this PR's code since aa55b8f · 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 A · Looks good to merge. Summary

Comment thread app/realtime.php Resolved
@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.

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

Adds admin-mode connection tracking and revocation to realtime, extending console allowlist and User membership helpers, with a new e2e test.

Key findings

Comment thread src/Appwrite/Event/Realtime.php Resolved
@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → security/realtime-admin-revocation (after).

Metric Before After Change
🚀 Requests/sec 206.55 217.52 🟢 +5.3%
⏱️ Latency P50 82.09 ms 77.94 ms 🟢 -5.1%
⏱️ Latency P95 190.91 ms 190.32 ms ⚪ -0.3%
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 77.94 190.32 13,680 217.52 -0.59
Account 160.14 312.65 720 12.14 +30.84
TablesDB 76.12 152.85 7,440 120.61 +1.65
Storage 69.34 160.21 3,600 60.27 -33.78
Functions 115.88 223.84 1,920 32.79 +24.24

Top API waits (after)

API request Max wait (ms)
account.name.update 439.41
account.prefs.update 437.99
tablesdb.rows.create 433.57
functions.variables.update 402.45
functions.create 372.09

Console user events reach realtime too, so blocking or deleting a console
user ends their admin-mode connections. An admin-mode connection can no
longer re-authenticate with a project session, which would keep its admin
roles while dropping out of the console checks. Covers the transfer and
re-authentication paths in the e2e test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@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 A · Looks good to merge. Summary

Comment thread src/Appwrite/Event/Realtime.php Resolved

@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

@ArnabChatterjee20k

Copy link
Copy Markdown
Member Author

@hansi-codes review

@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 A · See the inline comments. Summary

Comment thread app/realtime.php Resolved
@ArnabChatterjee20k

Copy link
Copy Markdown
Member Author

@hansi-codes review

@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

@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

…in-revocation

# Conflicts:
#	tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php
@ArnabChatterjee20k

Copy link
Copy Markdown
Member Author

Follow-up on the tenki review of cea47e9, so every finding has an answer:

  • 🟠 Block/delete events never recheck admin connections: fixed. users was added to CONSOLE_ALLOWLIST, and the fix was confirmed in the inline thread.
  • 🟡 Re-auth leaves stale admin metadata (outside the diff, so it had no thread): fixed. Re-authentication on an admin-mode connection is now refused (Authentication.php, error 1003), so the admin entry can never outlive the identity that set it. Covered by the re-auth case in testAdminModeEndsWithConsoleAccess.
  • 🟢 Memoize administered project fetches: done in 2042b3b. All administered projects now load in one find.
  • 🟢 Cover role-change and transfer triggers: transfer is covered (testAdminModeEndsWithConsoleAccess). Organization deletion is also covered now (testAdminModeEndsWithOrganizationDeletion). A role change is caught by the getMembershipRoles() comparison but has no dedicated test yet; it can follow up separately.

@ArnabChatterjee20k
ArnabChatterjee20k merged commit 80cbe53 into main Oct 29, 2026
66 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