Repository navigation
add support for signout all users in project level - #14647
Conversation
🔵 Tier A · Mergeable after minor fixes
This pull request adds project-wide session invalidation by recording a cutoff in project auth settings, checking that cutoff across HTTP, realtime, MQTT, JWT, and account-authentication paths, and queuing asynchronous session cleanup. It also adds the project API endpoint and Console control, documentation, and coverage for the invalidation behavior; the latest update refreshes the Console package snapshot and converts the session test data providers to generators. Latest changes: The newest commits update the Console package snapshot (including its MQTT dependency) and change the two session-invalidation data providers to yield cases from
Note Not approving while a bug finding is open: Avoid closing admin sockets during project session cleanup Fix with agent prompt### Issue 1
app/realtime.php:775-777
**Avoid closing admin sockets during project session cleanup**
An admin-mode connection's session belongs to a console user, but this sweep loads that user from the target project's database and marks the connection revoked when the document is absent. A project signout therefore closes admin sockets attached to that project even though their console sessions were not invalidated; validate these connections against the console user store or exclude them.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.📂 Walkthrough · 27
⏳ Still open from earlier reviews · 1
Reviewed the commits since |
…en realtime connections, rename to sessionsInvalidatedAt, require users.write scope, worker user-cache purge, restructure e2e tests, add console force sign out card
d79c029 to
bfa23de
Compare
New ConsoleProject ID: Tip Teams feature lets you group users with membership management and role permissions |
| $user->isEmpty() | ||
| || $user->getAttribute('status') === false | ||
| || !$user->sessionActive($sessionId, $invalidatedAt) |
There was a problem hiding this comment.
Avoid closing admin sockets during project session cleanup
An admin-mode connection's session belongs to a console user, but this sweep loads that user from the target project's database and marks the connection revoked when the document is absent. A project signout therefore closes admin sockets attached to that project even though their console sessions were not invalidated; validate these connections against the console user store or exclude them.
Prompt To Fix With AI
This is a comment left during a code review.
Path: app/realtime.php
Line: 775-777
Comment:
**Avoid closing admin sockets during project session cleanup**
An admin-mode connection's session belongs to a console user, but this sweep loads that user from the target project's database and marks the connection revoked when the document is absent. A project signout therefore closes admin sockets attached to that project even though their console sessions were not invalidated; validate these connections against the console user store or exclude them.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.🟡 Minor · bug · Reply if this doesn't apply.
The findings that requested changes are resolved. See the summary comment for what is still open.
|
Note from Luke, to investigate and maybe consider: how delete by group is working? i'd assume you can do it in one update/delete query right? |
Dug into it in the repo it's not per-doc SQL: |
# Conflicts: # app/init/mqtt/connection.php # app/init/realtime/connection.php # app/realtime.php
… JWTs Co-Authored-By: Claude Opus 5.5 <[email protected]>
The findings that requested changes are resolved. See the summary comment for what is still open.
commit: |
Co-Authored-By: Claude Opus 5.5 <[email protected]>



What does this PR do?
Adds a new API endpoint
DELETE /v1/project/sessionsthat allows a project developer (via API key withsessions.write+users.writescope, or a privileged console user) to immediately invalidate every active session across the entire project — without touching a single session document at request time.The problem
Previously there was no way to force-sign-out all users in a project in one shot. The naive approach — iterating over every user's sessions and deleting them — is O(N) at request time (N = number of active sessions), which for a large project means seconds of latency, potential timeouts, and a heavy write storm on the database.
The approach: epoch bump (O(1) at request time)
Instead of deleting documents, the endpoint writes a single timestamp (
sessionsValidAfter) into the project's existingauthsJSON attribute:Every auth path that verifies a session then compares the session's immutable
$createdAtagainst this epoch. If the session was created at or before the epoch, it is treated as invalid — the user is effectively signed out — without any document being modified or deleted.Cleanup of the now-dead session documents happens asynchronously in the background via the existing
Deletesworker, reusing theDELETE_TYPE_SESSIONSqueue type, which already runs during maintenance. The worker now accepts an optionalvalidAftercutoff: when supplied (force sign-out), it deletes sessions created before that timestamp; when absent (routine maintenance), it uses the project's session duration as before.Approach & Time Complexity
projectsdoc (auths.sessionsValidAfter)DeleteMessageto the Deletes queuesessionsValidAfterfrom already-loaded$projectdoc; compare to$createdAt$createdAt <= cutoffin batchesThe epoch is checked inside
User::sessionVerify()andUser::sessionActive(), which are called in every auth path. The$projectdocument is already loaded in the request context — no extra DB queries are added to the hot path.Workflow
sequenceDiagram participant Dev as Developer (API Key) participant API as DELETE /v1/project/sessions participant DB as Platform DB (projects collection) participant Queue as Deletes Queue participant Worker as Deletes Worker Dev->>API: DELETE /v1/project/sessions API->>DB: updateDocument('projects', id,\n{ auths.sessionsValidAfter: now() }) API->>Queue: enqueue(DELETE_TYPE_SESSIONS, datetime=now()) API-->>Dev: 204 No Content Note over DB,Worker: Hot path — every subsequent request Dev->>API: Any authenticated request API->>DB: load $project (already cached) API->>API: sessionVerify(secret, proof, validAfter) API->>API: session.$createdAt <= validAfter? → reject API-->>Dev: 401 Unauthorized Note over Queue,Worker: Async — off hot path Queue->>Worker: consume DELETE_TYPE_SESSIONS message Worker->>DB: deleteByGroup('sessions',\n createdAt <= validAfter, batch) Worker-->>Queue: doneflowchart TD A[DELETE /v1/project/sessions received] --> B{Auth check:\nsessions.write + users.write\nor privileged user?} B -- No --> C[403 Forbidden] B -- Yes --> D[Write auths.sessionsValidAfter = now\nto projects doc — O1] D --> E[Enqueue DELETE_TYPE_SESSIONS\nwith datetime = now — O1] E --> F[204 No Content] F --> G{Next user request\nto any endpoint} G --> H[Load user + sessions\nfrom DB] H --> I{session.createdAt\n<= sessionsValidAfter?} I -- Yes --> J[Reject session\n→ 401] I -- No --> K[Session is valid\n→ continue] E --> L[Deletes Worker picks up message] L --> M[deleteByGroup sessions\nwhere createdAt <= cutoff\nin batches — ON async] M --> N[Dead session docs removed\nfrom DB]What was changed
New endpoint
src/Appwrite/Platform/Modules/Project/Http/Project/Sessions/Delete.php—DELETE /v1/project/sessions, scoped tosessions.write+users.write. Writes the epoch and enqueues cleanup.src/Appwrite/Platform/Modules/Project/Services/Http.php— Registers the new action.Epoch check — session verification
src/Appwrite/Utopia/Database/Documents/User.php—sessionVerify()andsessionActive()accept an optional?string $validAfter. If set andsession.$createdAt <= $validAfter, the session is rejected before any secret comparison.Epoch threaded into all auth paths
app/init/resources/request.phpapp/init/realtime/connection.phpapp/init/mqtt/connection.phpapp/realtime.phpapp/controllers/api/account.phpsrc/.../Account/Sessions/IdToken/Create.phpsrc/.../Realtime/Message/Handlers/Authentication.phpAsync cleanup
src/Appwrite/Platform/Workers/Deletes.php—deleteExpiredSessions()accepts optional?string $validAfter. TheDELETE_TYPE_SESSIONSswitch case now passes$datetime(the epoch from the queue message). Routine maintenance (DELETE_TYPE_MAINTENANCE) still passesnull, preserving the original duration-based behaviour.Tests
tests/unit/Utopia/Database/Documents/UserTest.php— 8 new data-driven assertions covering the epoch matrix for bothsessionVerifyandsessionActive(before/at/after epoch, and null passthrough).tests/e2e/Services/Project/SessionsDeleteIntegrationTest.php— Integration test: creates a session, calls the endpoint, confirms the session is rejected on next use.tests/e2e/Security/baseline.json— RegistersDELETE /v1/project/sessionsso the security baseline test doesn't flag it as an undocumented route.Test Plan
Unit tests (already green):
Integration test:
OK (3 tests, 43 assertions)
Time: 00:06.878, Memory: 30.00 MB
Manual smoke test:
DELETE /v1/project/sessionswith a project API key (sessions.write+users.write).GET /v1/accountreturns 401 on the old session token.Related PRs and Issues
N/A
Checklist