Skip to content

fix(realtime): apply the HTTP session rules to realtime connections - #14803

Merged
ChiragAgg5k merged 4 commits into
mainfrom
fix/realtime-session-checks
Oct 22, 2026
Merged

ChiragAgg5k merged 4 commits into
mainfrom
fix/realtime-session-checks

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Summary

Realtime connections now follow the same session rules as the HTTP API: a session that still owes an MFA factor and a blocked user no longer authenticate a connection, and the per-connection message limiter is keyed per connection instead of per connection id alone.

Changes

  • User::getMinimumFactors() and User::sessionFactorsMet() hold the MFA factor rule; the HTTP api hook now uses getMinimumFactors() instead of computing it inline.
  • Realtime user resource: a cookie or header session, or a JWT bound to a session, that lacks its required factors connects as a guest, the same way an invalid session does. A blocked user (status === false) connects as a guest on every path (session, JWT, account key).
  • Realtime onOpen: impersonation requires a session and a target that is not blocked, as over HTTP (user_more_factors_required / user_blocked).
  • In-band authentication message refuses a blocked user (user_blocked) and a session missing factors (user_more_factors_required).
  • Permission revalidation also closes a connection whose session no longer holds its required factors, and users.*.update.mfa now triggers it (it was listed as an event without access change), so enabling MFA or verifying an authenticator revalidates open connections. The impersonation gate also checks the impersonator session's factors explicitly.
  • The message limiter key is now project:{project},container:{container},connection:{connection}; it previously named {url} and {connection} while filling {connection} and {container}, so it reduced to the connection id, which repeats across containers.

Tests

  • Unit: UserTest::testGetMinimumFactors, UserTest::testSessionFactorsMet.
  • E2E (verified by CI): RealtimeCustomClientTest::testConnectionRequiresAllFactors (a session-less JWT for the user receives the account event, while the MFA-pending session and a JWT bound to it connect as guests, receive nothing, and in-band authentication with that session is refused) and RealtimeCustomClientTest::testBlockedUserConnectsAsGuest.
  • The limiter key is built inline in app/realtime.php and the e2e stack runs a single realtime container, where connection ids do not repeat, so the cross-container collision cannot be reproduced in e2e; the change is limited to the key string and its parameters.

A follow-up PR applies the same session rules to MQTT and builds on the User methods added here.

Related: appwrite-labs/cloud#6666

A realtime connection authenticated by a session that still owed an MFA factor, or by a blocked user, was treated as signed in, while HTTP refuses both. The factor rule now lives on the User document so HTTP and realtime share it; such connections are treated as guests, in-band authentication refuses them, and impersonation requires a session and an unblocked target as over HTTP.

The per-connection message limiter key also named placeholders it never filled, so it collapsed to the connection id alone, which repeats across containers. It now carries the project, container and connection.

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

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

Copy link
Copy Markdown

Review complete. No issues found — approved ✅.


This PR moves the MFA factor-counting logic that previously lived inline in the HTTP auth path into reusable methods on the User document model, and applies the same gating to realtime WebSocket connections so session-bound JWTs are subject to the same MFA requirements as HTTP requests. Scanners reported no defects meeting the confidence threshold; the refactor was verified as semantically equivalent to the previous inline computation, and the realtime connection/auth handler changes behave consistently with the HTTP path.

Reviewed commit: 33f09d6

@ghost

ghost commented Oct 22, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The pull request centralizes minimum MFA-factor and session-factor checks on the User model, then applies them to HTTP and realtime authentication. Realtime connections treat blocked or MFA-pending users as guests, enforce those checks during authentication, impersonation, and permission revalidation, and use a limiter key scoped to project, container, and connection. Unit and E2E tests cover the shared factor rules and realtime authentication behavior.

Latest changes: The newest commit adds a realtime impersonation-gate check requiring the impersonator's session to meet the user's minimum MFA factors.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 9
File Change
app/controllers/shared/api.php Uses the shared User method when enforcing HTTP session-factor requirements.
app/init/realtime/connection.php Applies session-factor and blocked-user checks when resolving realtime identities.
app/realtime.php Requires an MFA-complete session for impersonation, revalidates sessions, and scopes message limits by project, container, and connection.
src/Appwrite/Messaging/Adapter/Realtime.php Triggers realtime permission revalidation for user MFA updates.
src/Appwrite/Realtime/Message/Handlers/Authentication.php Rejects blocked users and sessions that have not met their required factors during in-band authentication.
src/Appwrite/Utopia/Database/Documents/User.php Adds shared helpers for minimum MFA factors and validating a session's factors.
tests/e2e/Services/Realtime/RealtimeCustomClientTest.php Covers MFA-pending and blocked-user behavior for realtime connections and authentication.
tests/unit/Messaging/MessagingTest.php Verifies which user events trigger realtime permission revalidation.
tests/unit/Utopia/Database/Documents/UserTest.php Tests minimum-factor calculation and session-factor validation.

Reviewed the commits since 0ec62a6 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

ghost
ghost previously requested changes Oct 22, 2026

@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 B · 1 blocking finding to address. Summary

Comment thread app/realtime.php Resolved
@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/realtime-session-checks (after).

Metric Before After Change
🚀 Requests/sec 174.8 166.63 ⚪ -4.7%
⏱️ Latency P50 97.64 ms 105.7 ms 🔴 +8.3%
⏱️ Latency P95 229.61 ms 241.1 ms ⚪ +5%
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 105.7 241.1 10,260 166.63 +11.49
Account 202.06 354.81 540 9.47 +34
TablesDB 103.29 192.91 5,580 93.74 +11.1
Storage 96.7 230.35 2,700 47.01 +16.66
Functions 140.21 273.68 1,440 25.72 +5.96

Top API waits (after)

API request Max wait (ms)
storage.buckets.create 554.82
account.name.update 511.88
account.prefs.update 509.77
tablesdb.rows.create 426.51
functions.variables.create 409.72

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

@ghost
ghost dismissed their stale review October 22, 2026 16:05

The findings that requested changes are resolved. See the summary comment for what is still open.

@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

@ChiragAgg5k
ChiragAgg5k merged commit 3f829a1 into main Oct 22, 2026
66 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the fix/realtime-session-checks branch October 22, 2026 18:19
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…port #14803)

A session or session-bound JWT that still owes an MFA factor, and a blocked user, no longer authenticate a realtime connection, and in-band authentication refuses them. Impersonation needs an impersonator session with its factors met. The per-connection message limiter is keyed by project, container and connection. 1.9 has no realtime permission revalidation, so the update.mfa revalidation trigger is not ported. Adds User::getMinimumFactors() and User::sessionFactorsMet().

(cherry picked from commit 3f829a1)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…port #14803)

A session or session-bound JWT that still owes an MFA factor, and a blocked user, no longer authenticate a realtime connection, and in-band authentication refuses them. A blocked user cannot impersonate either. The per-connection message limiter is keyed by project, container and connection. 1.9 has no realtime permission revalidation, so the update.mfa revalidation trigger is not ported, and HTTP impersonation on 1.9 does not require a session, so the onOpen impersonation session check is not ported. Adds User::getMinimumFactors() and User::sessionFactorsMet().

(cherry picked from commit 3f829a1)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…port #14803)

A session or session-bound JWT that still owes an MFA factor, and a blocked user, no longer authenticate a realtime connection, and in-band authentication refuses them. A blocked user cannot impersonate either. The per-connection message limiter is keyed by project, container and connection. 1.9 has no realtime permission revalidation, so the update.mfa revalidation trigger is not ported, and HTTP impersonation on 1.9 does not require a session, so the onOpen impersonation session check is not ported. Adds User::getMinimumFactors() and User::sessionFactorsMet().

(cherry picked from commit 3f829a1)
Tapraj-Pathak pushed a commit to Tapraj-Pathak/appwrite that referenced this pull request Oct 24, 2026
…checks

fix(realtime): apply the HTTP session rules to realtime connections

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