Skip to content

fix(users): align session route scopes with their admin/server auth types - #13994

Merged
abnegate merged 8 commits into
mainfrom
fix/users-session-scopes
Oct 8, 2026
Merged

abnegate merged 8 commits into
mainfrom
fix/users-session-scopes

Conversation

@Meldiron

Copy link
Copy Markdown
Contributor

What does this PR do?

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 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.write
  • GET /v1/users/:userId/sessions -> users.read

Server 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

  • New console e2e (testCreateUserSessionRequiresServerScope): a plain account session (no server scope) is rejected with general_unauthorized_scope on the create and list session routes.
  • composer lint, composer analyze on the four routes, composer refactor:check.

Related PRs and Issues

  • None

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? (scope labels only; no SDK-visible metadata changed)

🤖 Generated with Claude Code

…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]>
@ghost

ghost commented Sep 29, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The incremental change removes a test for unchanged behavior and introduces no runtime defects.

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.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 4
File Change
src/Appwrite/Platform/Modules/Users/Http/Users/Sessions/Bulk/Delete.php Require a server key or privileged role before deleting all user sessions.
src/Appwrite/Platform/Modules/Users/Http/Users/Sessions/Create.php Require a server key or privileged role before creating a user session.
src/Appwrite/Platform/Modules/Users/Http/Users/Sessions/Delete.php Require a server key or privileged role before deleting an individual session.
tests/e2e/Services/Users/UsersConsoleClientTest.php Cover ordinary-session rejection and sessions-only key access; remove the impersonator session-listing test.

Merged main (22514e5); no changes to this PR's code since 504484b · 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

@greptile-apps

greptile-apps Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

[Critical risk] Changes authorization scopes for session management endpoints.

The PR should not merge until existing sessions-scoped server keys retain a supported path and the required DELETE-route coverage is added.

Fix All in Claude CodeFindings

  1. P1 Existing session keys lose access ▶
  2. P2 Deletion routes lack regression coverage ▶
Fix with agent prompt
### Issue 1
src/Appwrite/Platform/Modules/Users/Http/Users/Sessions/Create.php:44
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.

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!

### Issue 2
tests/e2e/Services/Users/UsersConsoleClientTest.php:187-193
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.

---

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

Summary

The PR narrows four Users session routes to users.read or users.write and adds a client-session denial test.

  • Existing keys granted only session scopes lose access despite those scopes’ advertised meaning.
  • The regression test omits both changed DELETE routes.

Reviews (1) · Last reviewed commit: "fix(users): align session route scopes w..."

->groups(['api', 'users'])
->label('event', 'users.[userId].sessions.[sessionId].create')
->label('scope', ['users.write', 'sessions.write'])
->label('scope', 'users.write')

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.

P1 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!

Fix in Claude Code Fix in Codex

Comment on lines +187 to +193
$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']);

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.

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

Fix in Claude Code Fix in Codex

@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/users-session-scopes (after).

Metric Before After Change
🚀 Requests/sec 178.68 182.96 ⚪ +2.4%
⏱️ Latency P50 97.24 ms 93.36 ms ⚪ -4%
⏱️ Latency P95 232.47 ms 227.52 ms ⚪ -2.1%
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 93.36 227.52 11,400 182.96 -4.95
Account 185.61 393.46 600 10.2 +20.43
TablesDB 88.9 174.69 6,200 102.11 -1.71
Storage 89.34 192.57 3,000 51.26 -5.93
Functions 139.14 282.84 1,600 28.08 -4.53

Top API waits (after)

API request Max wait (ms)
account.prefs.update 587.28
account.name.update 571.9
functions.variables.update 461.47
functions.delete 439.33
functions.create 436.92

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.

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

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

HarshMN2345 and others added 5 commits October 1, 2026 13:40
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.
@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

32 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (17)
  • php.appwrite.permissive-write-permission (8)
  • php.appwrite.secret-compare-timing (5)
  • 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.

@abnegate
abnegate merged commit 6d89eb0 into main Oct 8, 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.

3 participants