Skip to content

fix(realtime): require project access for admin mode connections - #14790

Merged
ChiragAgg5k merged 4 commits into
mainfrom
fix/realtime-admin-mode-membership
Oct 22, 2026
Merged

ChiragAgg5k merged 4 commits into
mainfrom
fix/realtime-admin-mode-membership

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Summary

Realtime connections opened with mode=admin against a project now follow the same rules as HTTP admin mode: the console user must be a confirmed member of the project's organization and hold read access to the project (team owner/developer, or a project-<id>-* role for that project), and the connection must come from a console origin. Otherwise the connection is refused with an error frame.

Changes

  • app/init/realtime/connection.php: expose the resolved connection mode as a resource. In admin mode the origin validator no longer accepts the project's platform hostnames and schemes, and accepts a custom domain only when it belongs to a trusted console project. This mirrors fix(api): accept admin mode only from console origins #14813, which applies the same rule to HTTP.
  • app/realtime.php: on open, for admin mode on a non-console project, check the membership and the project's read permissions as app/controllers/shared/api.php does. Authorized connections subscribe with the organization admin roles (owner, developer, ...), not the console user's user:, users, team: or label roles, and carry no user id in the project, so account channels, presence and per-user revalidation never resolve the console user as a project user.

Admin connection roles

Needs human review: admin-mode connections on a project now subscribe with the bare organization roles (owner, developer, ...) instead of the console user's own roles, so they receive far fewer events than before.

  • Why: project events are addressed with the project's own role strings (user:<projectUserId>, users, team:<projectTeamId>, label:<x>, or a resource's permissions). The console user's user:<consoleId>, users, team:<orgId>/* and label roles are console identities, so inside a project's role space they only ever matched project users, teams or labels whose IDs or names happen to equal the console ones. They never meant "admin of this project".
  • What admin connections still receive: events whose permissions include any, as getSubscribers() delivers those to every subscription. The bare roles also sit on the connection's authorization, so privileged checks (for example the disabled-websocket bypass) behave as they do over HTTP admin mode.
  • Where console users get project data: on their console connection (project=console), through the console and projects.<id> channels, which is what the Console uses.
  • Delivering every project event to admin connections would need a new wildcard in the realtime adapter. That would be a feature, not a restoration of earlier behavior, so it is left out.

Tests

  • RealtimeConsoleClientTest::testAdminModeRequiresProjectAccess: the organization owner and a member with a role on the target project connect; a console user outside the organization gets a 401 error frame, and a member scoped to a different project gets a 404 error frame. A project user created with the console user's ID does not deliver account events to the owner's admin connection.
  • RealtimeConsoleClientTest::testAdminModeRequiresConsoleOrigin: admin mode from a project platform origin is refused with Invalid Origin; admin mode from the console origin, and a default-mode connection from the platform origin, still connect.
  • E2E is verified by CI. Pint and PHPStan pass on the touched files.

Related: appwrite-labs/cloud#6666

Admin mode over HTTP only lets a console user act on a project when they are a confirmed member of its organization with read access to it, and then grants the organization's admin roles. Realtime accepted any console session in admin mode and evaluated the console user's own roles inside the project's role space. Apply the same membership and project permission checks when the connection opens, subscribe with the admin roles only, and keep the console user out of the project's user-addressed paths.

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

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

Copy link
Copy Markdown

Review complete. 🟠 1 high

💬 Inline comments (1)


The change lets privileged console users open realtime connections in admin mode: connection setup now resolves a mode resource and, when admin mode applies, derives subscription roles from the user's confirmed project memberships instead of the session's roles. The e2e suite gains a RealtimeConsoleClientTest exercising this path.

Files Change
app/init/realtime/connection.php Adds a lazy mode DI resource for realtime connections
app/realtime.php Adds admin-mode gate in onOpen: parses project-{id}-{role} memberships, verifies project read permission, and builds admin subscription roles
tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php New console-client e2e suite covering admin-mode realtime subscriptions

One high-confidence issue remains: the derived admin roles do not match the role format events are published with, so admin-mode subscriptions may never receive events (see inline findings).

Reviewed commit: f9f3bf7

@ChiragAgg5k ChiragAgg5k self-assigned this Oct 22, 2026
@ghost

ghost commented Oct 22, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The incremental test changes explicitly verify refusal frames and socket closure, and introduce no issue requiring a fix.

Project-scoped realtime admin connections require confirmed organization membership and project read access, and use organization roles without resolving the console identity as a project user. Admin-mode origin validation restricts project connections to console origins, with E2E coverage for authorization, identity isolation, origin handling, and refusal frames.

Latest changes: The newest test changes read and assert each refusal error frame before waiting for the server to close the WebSocket.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 3
File Change
app/init/realtime/connection.php Exposes the resolved connection mode and applies admin-mode origin validation that excludes project platform origins.
app/realtime.php Authorizes project-scoped admin connections using organization membership and project read access, then subscribes with organization roles.
tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php Covers admin access and origin behavior, identity isolation, and verifies refusal frames before confirming socket closure.

Reviewed the commits since 045fde2 · 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

@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.

This PR adds console-admin impersonated realtime access to Appwrite's WebSocket gateway, deriving admin roles from project memberships in app/realtime.php via a new mode resource in app/init/realtime/connection.php, covered by a new console-client e2e test.

Key findings

Comment thread app/realtime.php
Comment on lines +992 to +1005
$adminRoles = [];
foreach ($membershipRoles as $membershipRole) {
if (!\str_starts_with($membershipRole, 'project-')) {
$adminRoles[] = $membershipRole;
} elseif (\substr($membershipRole, 0, (int) \strrpos($membershipRole, '-')) === 'project-' . $project->getId()) {
$adminRoles[] = \substr($membershipRole, \strrpos($membershipRole, '-') + 1);
}
}
$adminRoles = \array_values(\array_unique($adminRoles));

foreach ($adminRoles as $role) {
$authorization->addRole($role);
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 bug · high

Admin-mode realtime roles never match event roles

The new admin-mode branch stores bare membership role names (owner, developer) in $adminRoles (app/realtime.php:992-1005) and subscribes the connection under them via $roles (app/realtime.php:1053). Realtime::getSubscribers only matches when the subscription role key appears verbatim in the event's roles array (src/Appwrite/Messaging/Adapter/Realtime.php:516), and published events carry prefixed strings such as team:{teamId} and team:{teamId}/{role} — never bare role names. Admin-mode websocket connections therefore complete the handshake but silently receive zero events, a regression from the pre-PR path that used $targetUser->getRoles().

📋 Prompt for AI Agents

In app/realtime.php around lines 992-1005, the admin-mode loop strips project-{projectId}- from membership roles and collects bare names (owner, developer) into $adminRoles, which are then used verbatim as realtime subscription roles (app/realtime.php:1053). Since Realtime::getSubscribers matches subscription role keys exactly against event role strings like team:{teamId} and team:{teamId}/{role}, admin connections currently receive no events. Split the two uses: keep adding the bare names to $authorization (needed by User::isPrivileged), but produce a separate $subscriptionRoles array containing Role::team($project->getAttribute('teamId'))->toString() and Role::team($project->getAttribute('teamId'), $role)->toString() for each derived role (import Utopia\Database\Role), and pass that array as $roles at app/realtime.php:1053. Extend tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php to assert an admin-mode connection actually receives a published event.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Intentional. Inside a project's role space, team:<id> and team:<id>/<role> name that project's own teams, and project users can create teams with custom IDs, so subscribing an admin connection under team:<orgId>/owner would hand it events meant for a project team that reuses the organization's ID. Before this change admin connections matched project events only through such collisions (or through any, which they still receive, as getSubscribers() matches any subscription role when the event allows any). Project data events for console users are delivered on the console connection (console/projects.<id> channels). The bare roles are kept on the connection's authorization for the privileged checks, mirroring HTTP admin mode.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The explanation doesn't hold up under scrutiny. The concern isn't about org-ID collisions — it's that bare names like owner or developer are stored as subscription role keys, while Realtime::getSubscribers matches those keys against prefixed strings like team:{teamId}/owner from published events. These will never match verbatim, so admin-mode connections will receive no project-scoped events regardless of the collision concern.

The any role fallback only covers events explicitly published with any access — it won't deliver project document or membership events that carry team-scoped roles. The pre-PR path using $targetUser->getRoles() returned properly prefixed role strings (e.g. team:{teamId}/owner), which is why it worked. The fix needs to pass correctly prefixed role strings as subscription keys, while keeping the bare names only for the $authorization object.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, admin-mode connections on a project get only events open to any, and that is the intent. The pre-PR team:{orgId}/owner roles never meant "owner of this project" in the project tree: project events carry permissions of the project's own teams and users (team:{projectTeamId}, user:{projectUserId}), so those roles matched only a project team that happens to share the organization ID, which is the problem this PR fixes. Console users get project events (deployments, migrations, attributes, ...) on their console connection through the console / projects.<id> channels. A wildcard "see everything" role for admin connections would be a new feature, not a restoration of earlier behavior. Leaving this for a human reviewer to decide.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is a design disagreement rather than a fix claim, so the finding remains open. To be precise about the technical issue: the current diff shows bare role names like owner and developer being added to $adminRoles (lines 992-1005), and these will be used verbatim as subscription keys. The assertion that 'admin connections on a project get only events open to any' confirms the bug — it means admin-mode websocket connections will silently receive no project-scoped events (document changes, membership updates, function executions, etc.), even when the admin user is an owner of the project team. Whether a 'see everything' admin role is desirable is a separate design question, but the current implementation subscribes under keys that provably never match any published event role, making the feature non-functional. A human reviewer should evaluate both the design intent and the silent failure mode.

@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

Admin mode carries the console session, so as over HTTP it should not be usable from a project's own platform hostnames, schemes or non-console custom domains.

Co-Authored-By: Claude <[email protected]>
@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/realtime-admin-mode-membership (after).

Metric Before After Change
🚀 Requests/sec 207.69 212.12 ⚪ +2.1%
⏱️ Latency P50 81.52 ms 80.48 ms ⚪ -1.3%
⏱️ Latency P95 201 ms 193.58 ms ⚪ -3.7%
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 80.48 193.58 13,452 212.12 -7.42
Account 153.1 282.35 708 11.77 -19.43
TablesDB 76.94 145.13 7,316 118.38 -23.93
Storage 77.18 178.08 3,540 58.76 +6.09
Functions 116.91 237.63 1,888 32.03 +15.68

Top API waits (after)

API request Max wait (ms)
functions.create 580.13
account.name.update 438.29
account.prefs.update 409.45
functions.variables.create 349.64
tablesdb.rows.create 342.22

@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.

The websocket client connects lazily, so waiting for the close before the first read saw a disconnected client and no frames.

Co-Authored-By: Claude <[email protected]>
@ChiragAgg5k
ChiragAgg5k merged commit 7090683 into main Oct 22, 2026
119 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the fix/realtime-admin-mode-membership branch October 22, 2026 18:41
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…kport #14790)

A realtime connection opened in admin mode against a project only needed a console session. It now needs a confirmed membership in the project organization and read access to the project, and must come from a console origin. Authorized connections subscribe with the organization admin roles instead of the console user identity roles, and carry no user ID in the project.

(cherry picked from commit 7090683)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…kport #14790)

A realtime connection opened in admin mode against a project only needed a console session. It now needs a confirmed membership in the project organization and read access to the project, and must come from a console origin. Authorized connections subscribe with the organization admin roles instead of the console user identity roles, and carry no user ID in the project.

(cherry picked from commit 7090683)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…kport #14790)

A realtime connection opened in admin mode against a project only needed a console session. It now needs a confirmed membership in the project organization and read access to the project, and must come from a console origin. Authorized connections subscribe with the organization admin roles instead of the console user identity roles, and carry no user ID in the project.

(cherry picked from commit 7090683)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…kport #14790)

A realtime connection opened in admin mode against a project only needed a console session. It now needs a confirmed membership in the project organization and read access to the project, and must come from a console origin. Authorized connections subscribe with the organization admin roles instead of the console user identity roles, and carry no user ID in the project.

(cherry picked from commit 7090683)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…kport #14790)

A realtime connection opened in admin mode against a project only needed a console session. It now needs a confirmed membership in the project organization and read access to the project, and must come from a console origin. Authorized connections subscribe with the organization admin roles instead of the console user identity roles, and carry no user ID in the project.

(cherry picked from commit 7090683)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…kport #14790)

A realtime connection opened in admin mode against a project only needed a console session. It now needs a confirmed membership in the project organization and read access to the project, and must come from a console origin. Authorized connections subscribe with the organization admin roles instead of the console user identity roles, and carry no user ID in the project.

(cherry picked from commit 7090683)
Tapraj-Pathak pushed a commit to Tapraj-Pathak/appwrite that referenced this pull request Oct 24, 2026
…de-membership

fix(realtime): require project access for admin mode connections

(cherry picked from commit 7090683)
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