Skip to content

fix(realtime): bind account-key impersonators to the console project - #14040

Merged
ArnabChatterjee20k merged 1 commit into
fix/realtime-impersonationfrom
fix/realtime-impersonator-account-key
Oct 1, 2026
Merged

ArnabChatterjee20k merged 1 commit into
fix/realtime-impersonationfrom
fix/realtime-impersonator-account-key

Conversation

@ArnabChatterjee20k

Copy link
Copy Markdown
Member

Stacked on #14024. Addresses #14024 (comment).

Why

#14024 binds an impersonated connection to the impersonator (projectId, userId, sessionId) so the impersonator's block, delete or session end closes it. The binding's projectId was console only in admin mode or on the console project.

But the realtime user resource also authenticates x-appwrite-key + x-appwrite-user (account keys) against the platform database on any project. A console impersonator using an account key on a custom project was therefore bound to that custom project:

  • their users.* events are published under console, so a console block/delete never reached the binding and the socket stayed open;
  • a users.* event for an unrelated project user with the same ID matched the binding and could close it.

What

impersonatorUser sets projectId to console when both account-key headers are present as well. That's exactly when the user resource loaded the user from the key: it throws USER_API_KEY_AND_SESSION_SET when a key and a session/JWT are both sent, and a failed key leaves the user empty, which yields no impersonator.

Tests

No e2e regression: account keys can't be created through the API (there's no endpoint or mock for user-owned keys), and the scenario also needs a separate console user to block. Lint and PHPStan pass on the file.

🤖 Generated with Claude Code

The user resource authenticates x-appwrite-key + x-appwrite-user against
the platform database on any project, but the impersonator binding used
the connection's project unless in admin mode or on the console. A
console impersonator using an account key on a custom project was bound
to that project: their console block, delete or session end never
closed the socket, while events for a project user with the same ID
could close it.

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

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Your trial has ended. Reactivate Greptile to resume code reviews.

@ghost

ghost commented Oct 1, 2026

Copy link
Copy Markdown

🔵 Tier A · Mergeable after minor fixes

The fix matches the authentication path, but its account-key-specific behavior lacks regression coverage.

Updates realtime impersonator bindings to use the console project when authentication supplies both account-key headers. This aligns the binding with the platform database used to authenticate account keys, while retaining existing admin-mode and console-project behavior.

Verdict New comments Fixed Still open
✅ Approved 1 0 0
Finding Where
🟡 Add coverage for account-key impersonator project isolation app/init/realtime/connection.php:394
Fix with agent prompt
### Issue 1
app/init/realtime/connection.php:394-395
**Add coverage for account-key impersonator project isolation**

The existing `testConnectionEndsWithImpersonatorSession` only authenticates a custom-project actor with a session, so it still passes if this account-key condition is removed. Please add a fixture-backed regression covering a console account-key actor on a custom project: console block/delete should close the socket, while events for a custom-project user with the same ID should not.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
📂 Walkthrough · 1
File Change
app/init/realtime/connection.php Includes account-key authentication in the conditions that bind an impersonator to the console project.

Reviewed 42d50f3 · 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 on lines +394 to +395
|| (!empty($request->getHeaderLine('x-appwrite-key', '')) && !empty($request->getHeaderLine('x-appwrite-user', '')))
) ? 'console' : $project->getId(),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Add coverage for account-key impersonator project isolation

The existing testConnectionEndsWithImpersonatorSession only authenticates a custom-project actor with a session, so it still passes if this account-key condition is removed. Please add a fixture-backed regression covering a console account-key actor on a custom project: console block/delete should close the socket, while events for a custom-project user with the same ID should not.

Prompt To Fix With AI
This is a comment left during a code review.
Path: app/init/realtime/connection.php
Line: 394-395

Comment:
**Add coverage for account-key impersonator project isolation**

The existing `testConnectionEndsWithImpersonatorSession` only authenticates a custom-project actor with a session, so it still passes if this account-key condition is removed. Please add a fixture-backed regression covering a console account-key actor on a custom project: console block/delete should close the socket, while events for a custom-project user with the same ID should not.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

🟡 Minor · testing · Reply if this doesn't apply.

@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown

✨ Benchmark results

Comparing fix/realtime-impersonation (before) → fix/realtime-impersonator-account-key (after).

Metric Before After Change
🚀 Requests/sec 201.88 210.12 ⚪ +4.1%
⏱️ Latency P50 84.98 ms 82.27 ms ⚪ -3.2%
⏱️ Latency P95 201.73 ms 191.92 ms ⚪ -4.9%
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 82.27 191.92 13,281 210.12 -9.8
Account 160.53 298.09 699 11.59 -18.21
TablesDB 79.26 148.59 7,223 117.4 -23.6
Storage 75.37 168.51 3,495 58.19 +6.79
Functions 117.25 229.27 1,864 31.69 -0.95

Top API waits (after)

API request Max wait (ms)
account.name.update 495.06
account.prefs.update 476.1
functions.variables.update 391.39
storage.files.preview 357.11
tablesdb.rows.create 353.04

@ArnabChatterjee20k
ArnabChatterjee20k merged commit 97a1f58 into fix/realtime-impersonation Oct 1, 2026
52 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