Repository navigation
fix(realtime): require project access for admin mode connections - #14790
Conversation
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]>
|
Review complete. 🟠 1 high 💬 Inline comments (1)
The change lets privileged console users open realtime connections in admin mode: connection setup now resolves a
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 |
🟢 Tier S · Ready to merge
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.
📂 Walkthrough · 3
Reviewed the commits since |
There was a problem hiding this comment.
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
- 🟠 Admin-mode realtime roles never match event roles — realtime.php:992
| $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); | ||
| } | ||
| } |
There was a problem hiding this comment.
🟠 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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
…users Co-Authored-By: Claude <[email protected]>
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]>
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
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]>
…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)
…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)
…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)
…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)
…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)
…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)
…de-membership fix(realtime): require project access for admin mode connections (cherry picked from commit 7090683)
Summary
Realtime connections opened with
mode=adminagainst 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 aproject-<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 connectionmodeas 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 asapp/controllers/shared/api.phpdoes. Authorized connections subscribe with the organization admin roles (owner,developer, ...), not the console user'suser:,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.user:<projectUserId>,users,team:<projectTeamId>,label:<x>, or a resource's permissions). The console user'suser:<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".any, asgetSubscribers()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.project=console), through theconsoleandprojects.<id>channels, which is what the Console uses.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 withInvalid Origin; admin mode from the console origin, and a default-mode connection from the platform origin, still connect.Related: appwrite-labs/cloud#6666