Skip to content

fix(realtime): authorize console tails with project read access - #14797

Merged
ChiragAgg5k merged 2 commits into
mainfrom
fix/realtime-console-project-access
Oct 22, 2026
Merged

ChiragAgg5k merged 2 commits into
mainfrom
fix/realtime-console-project-access

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Summary

Console live tails (console.tail.<projectId>) are now authorized with the project's read permissions (organization owner/developer, or a project-<id>-owner/-developer role) instead of the bare organization team role. Console team and membership changes now run the realtime revalidation path, so a removed member or a member whose roles changed loses tails they can no longer read without reconnecting.

Changes

  • Realtime/Message/Handlers/Subscribe.php: authorize each tail against $project->getPermissionsByType(Read), resolved fresh, and store those roles with the tail.
  • Realtime/EventTailRegistry.php: a tail keeps the list of roles that authorized it; revalidateConnection() drops it when the connection holds none of them.
  • Event/Realtime.php: add the teams group to CONSOLE_ALLOWLIST, so console team and membership events reach realtime with permissionsChanged and trigger the existing re-resolve of the member's console connections.
  • Not included: project team transfer is still not revalidated for open tails; that endpoint is being changed in a separate PR.

Tests

  • Unit: EventTailRegistryTest covers any-of role revalidation, including a member who keeps the bare team role but loses access to the project.
  • E2E (RealtimeConsoleTailTest, verified by CI): a member scoped to one project can tail that project but not a sibling; a member's tail stops streaming after their membership is removed.

Related: appwrite-labs/cloud#6666

A console live tail was authorized by the bare organization team role, which every member holds, while reading a project over HTTP needs a team owner or developer role or a role on that project. Authorize tails with the project's read permissions and keep those roles for revalidation.

Console team and membership events were never published to realtime, so removing a member or changing their roles left their open console sockets with the old roles. Publish the teams group on the console project so these events run the existing revalidation, which drops tails the member can no longer read.

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 · 🟡 1 medium

💬 Inline comments (1)

  • 🟠 Tail survives project team transfer via stale role snapshot — Subscribe.php:131
📍 Findings outside the diff (1) — 🟡 1 medium — defects on lines GitHub can't attach comments to

🟡 Medium — Commit phase ignores connection state after suspending authorization · Subscribe.php:137–154 · unchanged line

// src/Appwrite/Realtime/Message/Handlers/Subscribe.php
137	        foreach ($parsedPayloads as $parsedPayload) {
138	            $realtime->subscribe(
139	                $projectId,
140	                $connectionId,
141	                $parsedPayload['subscriptionId'],
142	                $roles,
143	                $parsedPayload['convertedChannels'],
144	                $parsedPayload['queries'],
145	            );
146	        }
147	
148	        // All tail channels passed authorization above — register them additively. The

The authorization phase suspends at $dbForConsole->getDocument() (Subscribe.php:111-113), and the commit loop at Subscribe.php:137-154 runs without re-checking the connection. If the connection closes during that suspension, onClose's $eventTailRegistry->removeConnection() already ran as a no-op and the handler then resurrects entries: Realtime::subscribe recreates the connections entry without expire/jwtExpire keys, so isExpired() returns false and the 150ms sweep never cleans the orphaned tail, which is flushed to a closed fd forever; subscription telemetry is likewise never decremented. The same window lets a concurrent permissions-changed revalidation run before the commit, so a tail is committed with roles the connection no longer holds and survives until a later permissions event that may never come.


This PR introduces console-project tailing over realtime: Subscribe authorizes console.tail.<projectId> channels from the project's read permissions, snapshots the viewer's roles into a new EventTailRegistry, and revalidateConnection revokes tails mid-connection when roles no longer intersect. The teams group is added to CONSOLE_ALLOWLIST so team membership events reach the console realtime worker, and a new e2e suite plus registry unit tests cover grant and revocation flows.

Files Change
src/Appwrite/Realtime/EventTailRegistry.php New in-memory registry storing per-connection tail entries with role arrays and revalidateConnection() any-of-roles revocation.
src/Appwrite/Realtime/Message/Handlers/Subscribe.php Two-phase tail subscription: authorize console tail channels via project read permissions, store role snapshots, commit after subscription.
src/Appwrite/Event/Realtime.php Widens CONSOLE_ALLOWLIST with teams so teams/membership events are enqueued on the console project.
tests/e2e/Services/Realtime/RealtimeConsoleTailTest.php New e2e tests: tail requires project access; tail ends when membership is removed.
tests/unit/Realtime/EventTailRegistryTest.php Unit coverage for registry add/revalidate semantics, including bare-team-role edge cases.

Reviewed commit: cb520fe

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

ghost commented Oct 22, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

Console live-tail subscriptions authorize against each target project's read roles and retain those roles for later permission revalidation. Realtime team events trigger that revalidation, and the PR adds unit and end-to-end coverage for project-scoped access and membership revocation.

Latest changes: The newest commit updates the Subscribe handler comment to describe role-change revalidation for registered tails.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 5
File Change
src/Appwrite/Event/Realtime.php Adds team events to the console realtime allowlist so membership changes can trigger permission revalidation.
src/Appwrite/Realtime/EventTailRegistry.php Stores the roles authorizing each tail and removes tails when a connection no longer holds any of them.
src/Appwrite/Realtime/Message/Handlers/Subscribe.php Authorizes tails using fresh project read permissions and records the authorizing roles.
tests/e2e/Services/Realtime/RealtimeConsoleTailTest.php Covers project-specific tail access and stopping delivery after membership removal.
tests/unit/Realtime/EventTailRegistryTest.php Covers tail role revalidation, including retaining access when any authorizing role remains.

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

@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 role-snapshotted console realtime tail authorization with mid-connection revocation, plus e2e and unit tests; two authorization-lifecycle gaps remain.

Key findings

  • 🟠 Tail survives project team transfer via stale role snapshot — Subscribe.php:131
  • 🟡 Commit phase ignores connection state after suspending authorization — Subscribe.php:137

Comment on lines +131 to +133
// Carry the authorizing roles so the tail can be revoked if the viewer
// later loses access to the project.
$pendingTails[] = [$parsedPayload['subscriptionId'], $targetProjectId, $compiled, $readRoles];

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 security · high

Tail survives project team transfer via stale role snapshot

The stored authorizing roles are a snapshot taken at subscribe time, and the comment claims the tail is revoked when the viewer later loses access (Subscribe.php:131-133). But EventTailRegistry::revalidateConnection only intersects that snapshot with the connection's current roles — it never re-resolves the project's read permissions — and projects.* events (including the transfer in Projects/Team/Update.php) are published with permissionsChanged false, so revalidation never even runs for a transfer. After a project moves to another team, old-team members keep streaming compact project event metadata on console.tail until disconnect, despite the project's permissions no longer granting them access.

📋 Prompt for AI Agents

In src/Appwrite/Realtime/Message/Handlers/Subscribe.php lines 131-133, the tail revocation guarantee is broken: EventTailRegistry::revalidateConnection (src/Appwrite/Realtime/EventTailRegistry.php, called from app/realtime.php around line 741) compares the roles snapshot stored at subscribe time against the connection's roles, so it never notices that the project's own read permissions changed (e.g. a team transfer via PATCH /v1/projects/:projectId/team, which rewrites $permissions but emits projects.* events with permissionsChanged=false). Fix by making the revalidation path re-fetch each tailed project from dbForConsole and recompute getPermissionsByType(PermissionType::Read), detaching entries whose stored authorization no longer intersects the project's current read permissions; additionally mark project permission-changing events (at minimum the projects.team.update case in Appwrite\Messaging\Adapter\Realtime::fromPayload) as permissionsChanged so the revalidation actually runs on transfer.

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.

Agreed that a transfer is not covered, and the PR description calls this out. It is not a regression: on main the tail stored team:<oldOrg>, which old members keep after a transfer too. Revalidating on transfer needs the transfer endpoint to publish a permission-changing event (console projects.* events are not published to realtime at all today), and that endpoint is being changed in a separate PR, so it is left out here to keep the two changes independent. I reworded the comment so it no longer implies transfers are handled.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for the detailed explanation. Looking at the full evidence:

What the PR does fix (visible in the diffs):

  • Subscribe.php: switches from a single $requiredRole snapshot to a $readRoles array snapshot, and EventTailRegistry is updated to store roles (array) and intersect against the connection's current roles in revalidateConnection. This is a meaningful improvement over the old single-role snapshot.

What remains unaddressed (the core of the original finding):

  1. revalidateConnection still compares the subscribe-time snapshot against the connection's current roles — it never re-fetches the project from dbForConsole to recompute current read permissions. So if the project's permissions change (e.g. transfer), the stored snapshot is stale and the comparison fails to detect the loss of access.
  2. The Realtime.php diff adds 'teams' to CONSOLE_ALLOWLIST but does not mark projects.team.update (or any project transfer event) as permissionsChanged=true. So revalidateConnection is still never triggered on a transfer.

The user's reply acknowledges the transfer gap and states it will be handled in a separate PR. That is a reasonable decomposition, but neither half of the fix described in the original finding (re-fetching project permissions in revalidateConnection, and marking transfer events as permissionsChanged) is visible in this diff. The vulnerability — old-team members streaming tail data after a project transfer — remains open until both pieces land.

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.

Agreed: the transfer case is still open after this PR and will be handled together with the transfer endpoint change, which needs to publish a permission-changing event and have revalidation re-read the project's permissions. This PR covers subscribe-time authorization and revalidation when the member's own roles change.

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 assessment from the previous round still holds. The diff confirms that neither half of the required fix is present in this PR:

  1. No re-fetch in revalidation: 'EventTailRegistry.php' still compares the subscribe-time 'readRoles' snapshot against the connection's current roles — it never re-fetches the project from 'dbForConsole' to recompute current read permissions after a transfer.
  2. No permissionsChanged flag for transfers: 'Realtime.php' does not mark 'projects.team.update' (or any project transfer event) as 'permissionsChanged=true', so revalidation is never triggered on a transfer.

I understand the decomposition rationale — the transfer endpoint change is a separate PR — but both gaps from the original finding remain open here. Keeping this finding open until the companion PR (re-fetch + permissionsChanged flag) lands.

@github-actions

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/realtime-console-project-access (after).

Metric Before After Change
🚀 Requests/sec 191.28 209.49 🟢 +9.5%
⏱️ Latency P50 89.72 ms 81.34 ms 🟢 -9.3%
⏱️ Latency P95 214.93 ms 196.13 ms 🟢 -8.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 81.34 196.13 13,338 209.49 -18.8
Account 155.47 308.89 702 11.61 -2.15
TablesDB 78.08 142.98 7,254 117.34 -33.62
Storage 75.75 173.1 3,510 58.34 -16.76
Functions 120.85 239.2 1,872 31.54 -2.71

Top API waits (after)

API request Max wait (ms)
account.name.update 474.7
functions.create 417.18
functions.variables.update 397.56
account.prefs.update 388.96
storage.buckets.create 340.66

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

@ChiragAgg5k
ChiragAgg5k merged commit 6351799 into main Oct 22, 2026
72 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the fix/realtime-console-project-access branch October 22, 2026 16:43
Tapraj-Pathak pushed a commit to Tapraj-Pathak/appwrite that referenced this pull request Oct 24, 2026
…project-access

fix(realtime): authorize console tails with project read access

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