Repository navigation
fix(users): align session route scopes with their admin/server auth types - #13994
Conversation
…ypes The user session routes are declared for admin and server callers (AuthType::ADMIN, AuthType::KEY) but their scope labels also accepted the account-level sessions.read / sessions.write scopes that every session carries. Require the users.read / users.write scope instead, matching the other user routes and the routes' declared auth types. Co-Authored-By: Claude Opus 4.8 <[email protected]>
🟢 Tier S · Ready to merge
The Users session creation and deletion routes now require a server key or privileged admin role before accessing user data, while retaining both users.* and sessions.* scopes. Console e2e tests cover ordinary-session rejection and successful session management with sessions-only keys. Latest changes: The newest commit removes the impersonator session-listing e2e test without changing production code.
📂 Walkthrough · 4
Merged |
|
| ->groups(['api', 'users']) | ||
| ->label('event', 'users.[userId].sessions.[sessionId].create') | ||
| ->label('scope', ['users.write', 'sessions.write']) | ||
| ->label('scope', 'users.write') |
There was a problem hiding this comment.
Existing session keys lose access
A server API key can have sessions.write without users.write. It previously passed the scope check for this route, but the new label rejects it with general_unauthorized_scope. The same change affects session listing and both DELETE routes. Existing keys granted only session scopes will lose access even though the project scope descriptions still say those scopes allow users to read and manage sessions. Please preserve a supported path for those keys or provide a compatible transition and update the advertised scope contract.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Appwrite/Platform/Modules/Users/Http/Users/Sessions/Create.php
Line: 44
Comment:
**Existing session keys lose access**
A server API key can have `sessions.write` without `users.write`. It previously passed the scope check for this route, but the new label rejects it with `general_unauthorized_scope`. The same change affects session listing and both DELETE routes. Existing keys granted only session scopes will lose access even though the project scope descriptions still say those scopes allow users to read and manage sessions. Please preserve a supported path for those keys or provide a compatible transition and update the advertised scope contract.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
| $response = $this->client->call(Client::METHOD_POST, '/users/' . $targetId . '/sessions', $memberHeaders); | ||
| $this->assertEquals(401, $response['headers']['status-code']); | ||
| $this->assertEquals('general_unauthorized_scope', $response['body']['type']); | ||
|
|
||
| $response = $this->client->call(Client::METHOD_GET, '/users/' . $targetId . '/sessions', $memberHeaders); | ||
| $this->assertEquals(401, $response['headers']['status-code']); | ||
| $this->assertEquals('general_unauthorized_scope', $response['body']['type']); |
There was a problem hiding this comment.
Deletion routes lack regression coverage
This test checks that a regular session cannot create or list another user’s sessions, but it does not check either changed DELETE route. The existing single-delete test uses server credentials, and bulk deletion has no e2e test. A scope regression on deletion could therefore pass unnoticed. The repository’s HTTP-test guide requires success and failure coverage for every route; please add observable denial checks for both DELETE endpoints and an authorized bulk-delete check before merging.
Context Used: AGENTS.md (source)
Prompt To Fix With AI
This is a comment left during a code review.
Path: tests/e2e/Services/Users/UsersConsoleClientTest.php
Line: 187-193
Comment:
**Deletion routes lack regression coverage**
This test checks that a regular session cannot create or list another user’s sessions, but it does not check either changed DELETE route. The existing single-delete test uses server credentials, and bulk deletion has no e2e test. A scope regression on deletion could therefore pass unnoticed. The repository’s HTTP-test guide requires success and failure coverage for every route; please add observable denial checks for both DELETE endpoints and an authorized bulk-delete check before merging.
**Context Used:** AGENTS.md ([source](https://github.com/appwrite/appwrite/blob/main/AGENTS.md))
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
Restore the sessions.* scopes and reject non-key, non-privileged callers at runtime instead of dropping the scopes. Dropping them broke least-privilege API keys scoped to sessions.* (added in #12178), while restoring them alone reopens the routes to guest/user roles that carry sessions.write. The guard closes that hole and keeps the key scope. Cover all four routes with e2e: members are rejected, and a key scoped to sessions.* only still succeeds.
There was a problem hiding this comment.
Your trial has ended. Reactivate Greptile to resume code reviews.
There was a problem hiding this comment.
Your trial has ended. Reactivate Greptile to resume code reviews.
The list route is not reachable by guest or user roles (neither users.read nor sessions.read is a client scope), so it needs no runtime guard. The guard also rejected impersonators, who are intentionally granted users.read to keep reading the users API while impersonating. Keep the guard only on the write routes, which carry the real scope overlap with client roles.
It asserted an impersonator could list a user's sessions, but impersonators are granted users.read only for discovery (/v1/users and /v1/users/:userId), never for /v1/users/:userId/sessions (app/controllers/shared/api.php), so that call is denied by the scope gate regardless of any route guard. The member test already covers the list route being denied to non-server callers.
Security rulesNo new WARNING or ERROR findings from security rules. 32 existing findings tracked in
|
What does this PR do?
The user session routes are declared for admin and server callers (
AuthType::ADMIN,AuthType::KEY), but theirscopelabels also accepted the account-levelsessions.read/sessions.writescopes that every session already carries. That made the runtime scope gate broader than the declared auth types.This aligns the scope labels with the declared auth types:
POST /v1/users/:userId/sessions,DELETE /v1/users/:userId/sessions,DELETE /v1/users/:userId/sessions/:sessionId->users.writeGET /v1/users/:userId/sessions->users.readServer keys and console admin mode already carry
users.read/users.write, so legitimate callers are unaffected. Client SDKs use the Account API for self-service session management.Test Plan
testCreateUserSessionRequiresServerScope): a plain account session (no server scope) is rejected withgeneral_unauthorized_scopeon the create and list session routes.composer lint,composer analyzeon the four routes,composer refactor:check.Related PRs and Issues
Checklist
🤖 Generated with Claude Code