Skip to content

fix(realtime): end member sockets when their project team is deleted - #15335

Merged
ChiragAgg5k merged 2 commits into
mainfrom
fix/realtime-deleted-team-role
Nov 5, 2026
Merged

ChiragAgg5k merged 2 commits into
mainfrom
fix/realtime-deleted-team-role

Conversation

@ChiragAgg5k

Copy link
Copy Markdown
Member

Problem

A realtime connection's roles — including team:{teamId} and team:{teamId}/{role} — are computed once at connect time (app/realtime.php → User::getRoles()) and stored on the connection. They are only rebuilt when revalidateUser → resubscribe runs (Realtime/Event/Handler.php).

For a team delete, Messaging/Adapter/Realtime.php sets permissionsChanged = ($parts[2] === 'create') — so teams.{teamId}.delete yields false — and endDeletedTeams only closed console admin-mode sockets (getTeamAdminConnections, which matches the admin stash, not the normal roles array). So when a team inside a customer project was deleted, its members' already-open sockets kept the team:{teamId} role, and the team-scoped realtime access it grants, until they happened to reconnect.

This is the realtime sibling of the role-eviction that already works for membership removal and user/session changes (which do run resubscribe).

Fix

  • Add Realtime::getTeamConnections($projectId, $teamId), which finds every connection in a project still holding the team's role (mirrors getTeamAdminConnections, but scans the connection roles).
  • Extend endDeletedTeams to close those member sockets on a project team delete, while keeping the existing console-organization admin-socket behavior.

Closing forces a reconnect that rebuilds roles without the deleted team — the same revocation mechanism endDeletedTeams already applies to admin sockets, rather than a broad project-wide re-read (checkProject does not rebuild roles). Only sockets actually holding the role are closed; others in the project are untouched.

Test

Extends HandlerTest with testDeletingAProjectTeamEndsOnlyItsMemberSockets: a member holding team:team1 and a bystander holding only a user role are subscribed in one project; a teams.team1.delete event closes the member's socket and leaves the bystander's open.

Found via the Strix pentest remediation engagement (finding #17).

A realtime connection's roles, including team:{teamId}, are built once at connect time and only rebuilt when revalidateUser/resubscribe runs. teams.{teamId}.delete set permissionsChanged=false and endDeletedTeams only closed console admin-mode sockets (getTeamAdminConnections, matching the admin stash). So after a team inside a project was deleted, its members' live sockets kept the team:{teamId} role — and the team-scoped access it grants — until they happened to reconnect.

Add Realtime::getTeamConnections(), which finds every connection in a project still holding a team's role, and extend endDeletedTeams to close those member sockets on a project team delete (while keeping the console-organization admin-socket behavior). Closing forces a reconnect that rebuilds roles without the gone team, matching how the function already revokes admin sockets.
@luxor-code-review

luxor-code-review Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Review complete. 🟠 1 high

💬 Inline comments (1)

  • 🟠 Dotted team IDs evade team-delete event matching — Handler.php:602

The patch fixes a security-relevant stale: websockets previously kept a deleted team's role until reconnect. Deletion events are now intercepted in Realtime\Event\Handler and the matching sockets are force-closed so roles re-resolve.

Files Change
src/Appwrite/Realtime/Event/Handler.php Detects team-delete events, looks up affected connections, evicts them (console and project branches).
src/Appwrite/Messaging/Adapter/Realtime.php Adds getTeamConnections to find sockets subscribed with the deleted team's role and closes them.
tests/unit/Realtime/Event/HandlerTest.php Adds unit tests for the new team-delete connection eviction logic.

One high-severity issue limits the fix's coverage: team IDs may contain periods, and event parsing assumes exactly three dot-separated segments, so deletions of such teams still leak stale roles (see finding-001).

Reviewed commit: f523f6e

@appwrite

appwrite Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

Realtime team deletion now closes project sockets that still hold the deleted team’s role, while preserving the existing console admin-socket handling. The handler identifies the deleted team from the event payload so dotted team IDs are supported and membership deletions are not mistaken for team deletions; tests cover both cases and ensure unrelated project sockets remain open.

Latest changes: The follow-up derives the team ID from the deleted document payload, matches the exact team-delete event, and adds coverage for dotted IDs and membership-delete events.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 3
File Change
src/Appwrite/Messaging/Adapter/Realtime.php Adds a project-scoped lookup for connections that still hold a team role.
src/Appwrite/Realtime/Event/Handler.php Closes connections holding the deleted team role and keeps console admin-connection handling, using the payload ID to recognize dotted IDs and distinguish membership deletes.
tests/unit/Realtime/Event/HandlerTest.php Covers selective socket closure, dotted team IDs, and avoiding closures on membership deletion.

Reviewed the commits since f523f6e · Details · Comment @appwrite review to re-run, or mention @appwrite with a question.

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

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

This PR closes realtime websocket connections holding stale team roles when a team is deleted, fixing permission revocation for both console and project team paths.

Key findings

  • 🟠 Dotted team IDs evade team-delete event matching — Handler.php:602

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

github-actions Bot commented Nov 5, 2026

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.

Team ids may contain periods (CustomId/Key), so splitting the event name positionally missed teams.my.team.delete and left stale team roles on live sockets. Derive the team id from the deleted document's payload and confirm the message is that team's delete via the concrete teams.{id}.delete event, which handles dotted ids and does not mistake a membership delete (payload id is the membership) for the team's own delete.
@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/realtime-deleted-team-role (after).

Metric Before After Change
🚀 Requests/sec 191.97 205.13 🟢 +6.9%
⏱️ Latency P50 88.15 ms 84.15 ms ⚪ -4.5%
⏱️ Latency P95 212.89 ms 199.22 ms 🟢 -6.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 84.15 199.22 12,597 205.13 -13.67
Account 158.16 293.92 663 11.04 -0.31
TablesDB 81.88 152.89 6,851 112.77 -18.6
Storage 77.52 186.36 3,315 57.29 +2.1
Functions 117.25 231.74 1,768 31.2 -21.85

Top API waits (after)

API request Max wait (ms)
storage.buckets.create 495.37
tablesdb.rows.get 396.83
account.name.update 394.78
account.prefs.update 386.87
functions.create 370.94

@ChiragAgg5k
ChiragAgg5k merged commit f349dd7 into main Nov 5, 2026
32 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the fix/realtime-deleted-team-role branch November 5, 2026 12:05
ChiragAgg5k added a commit that referenced this pull request Nov 12, 2026
fix(realtime): end member sockets when their project team is deleted

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