Skip to content

fix(realtime): reset connection authorization on re-authentication - #15064

Merged
ArnabChatterjee20k merged 3 commits into
mainfrom
security/realtime-reauth-roles
Oct 29, 2026
Merged

ArnabChatterjee20k merged 3 commits into
mainfrom
security/realtime-reauth-roles

Conversation

@ArnabChatterjee20k

Copy link
Copy Markdown
Member

What does this PR do?

A realtime authentication message switches the connection to the identity of the session it carries. It re-subscribed the connection with the new identity's roles, but then put back the connection's previous authorization object. Every later message builds its database authorization from that object plus the subscription roles, so roles held only by the previous identity still applied to the new one.

A plain session connection's stored authorization holds only the default role, so in practice this affects connections opened in admin mode: those hold the console user's admin roles (owner, developer, ...). After switching to a project user's session, that user still counted as privileged inside message handlers. For example, it could share its presence with roles it does not hold.

This PR stores a fresh Authorization when the identity switches, the same object a session connection gets when it opens. The new identity's roles still come from its subscriptions, which the handler already rebuilds.

Interaction with #15060: that PR refuses authentication on admin-mode connections outright, which closes the admin-mode case at its source. This change stays useful as a second layer: no path that switches identity can keep the previous identity's roles. The two diffs touch separate lines of Authentication.php, and the test below passes whichever PR merges first. It accepts either a refused switch or a switch without the admin roles.

Tracking: appwrite-labs/cloud#7060

Test Plan

New e2e test RealtimeConsoleClientTest::testAdminModeReauthenticationDropsAdminRoles:

  • Opens an admin-mode connection as the project owner, then sends authentication with a project user's session.
  • SUCCESS: the user can write its own presence.
  • FAILURE: a presence shared with another user's role is rejected (401, Permissions must be one of ...).

Red/green on a local stack:

  • Before the fix, the FAILURE presence was accepted (response instead of error).
  • After the fix, the new test passes, and the full RealtimeConsoleClientTest (20 tests) and PresenceRealtimeClientTest (12 tests) pass.
  • RealtimeCustomClientTest fails the same three transaction tests with and without the change. Those failures come from the local stack and are unrelated.

Related PRs and Issues

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

🤖 Generated with Claude Code

ArnabChatterjee20k and others added 2 commits October 28, 2026 19:41
The authentication message restored the connection's previous
authorization, so roles granted to the old identity (admin-mode roles)
kept applying to later messages from the new session.

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

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

Copy link
Copy Markdown

Review complete. No issues found — approved ✅.


This PR extends realtime e2e coverage for console-client authentication switching (impersonation): it verifies that after re-authenticating a live websocket connection, the connection's roles reflect the impersonated identity and subsequent presence/permission behavior matches. No behavioral production changes were identified.

Files Change
src/Appwrite/Realtime/Message/Handlers/Authentication.php Minor touch alongside the new test; no behavioral change to re-authentication handling.
tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php New e2e assertions covering role refresh on a re-authenticated realtime connection.

Reviewed commit: f0ed689

@ghost

ghost commented Oct 28, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The realtime authentication handler now replaces the connection’s stored authorization when a session identity changes, preventing roles from the prior identity from being carried into later messages. An E2E test covers re-authentication from admin mode and verifies either that the switch is refused or that the new identity cannot share presence with an unrelated user role.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 2
File Change
src/Appwrite/Realtime/Message/Handlers/Authentication.php Reset the connection authorization to a fresh instance after re-authentication.
tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php Add coverage for admin-mode re-authentication and presence permissions.

Merged main (7df604e); no changes to this PR's code since f0ed689 · 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

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

@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → security/realtime-reauth-roles (after).

Metric Before After Change
🚀 Requests/sec 198.85 199.07 ⚪ +0.1%
⏱️ Latency P50 85.37 ms 88.96 ms ⚪ +4.2%
⏱️ Latency P95 208.04 ms 199 ms ⚪ -4.3%
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 88.96 199 12,540 199.07 -9.04
Account 158.62 285.41 660 11.1 -14.75
TablesDB 87.75 156.02 6,820 110.31 +4.59
Storage 78.96 196.2 3,300 55.18 +9.05
Functions 122 223.86 1,760 30.17 -77.66

Top API waits (after)

API request Max wait (ms)
account.name.update 436.28
account.prefs.update 416.67
storage.buckets.create 380.33
storage.files.preview 372.95
tablesdb.rows.create 344.5

@ArnabChatterjee20k
ArnabChatterjee20k merged commit c0b34bb into main Oct 29, 2026
32 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