Skip to content

fix(account): device creation adopts only a session proven by its secret (appwrite:appwrite:alert#4723) - #15404

Merged
ChiragAgg5k merged 1 commit into
mainfrom
fix/device-create-requires-proven-session
Nov 10, 2026
Merged

ChiragAgg5k merged 1 commit into
mainfrom
fix/device-create-requires-proven-session

Conversation

@claudear

@claudear claudear commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Fixes code-scanning alert appwrite:appwrite:alert#4723 (high).

What was wrong

POST /v1/account/devices adopted the caller's current session straight from the injected session resource. That resource (app/init/resources/request.php) falls back to the sessionId claim inside x-appwrite-jwt whenever sessionVerify() on the real secret fails — it accepts the claim after only checking that userId matches and the session is active. No proofForToken verification of the session secret happens on that path.

So anyone holding a user's JWT for a moment — function runtime code and its transitive dependencies (executions mint a session-bound JWT for the invoking user), an app backend, a token scraped from logs — could:

  1. POST /v1/account/devices with only x-appwrite-jwt. The device adopted the JWT's session (deviceId defaults to '' for every session created without a device cookie, so isAdoptable() always said yes), and the device secret came back in Set-Cookie / X-Fallback-Cookies.
  2. PATCH /v1/account/devices/:deviceId/session with just that device secret. It picks a session the device holds, mints a fresh secret, and returns the plaintext in the a_session cookie — keeping the row's original expire and its already-satisfied MFA factors.

Result: a session cookie lasting the project's session duration (one year by default) with MFA already met. Worse, the rotation left the victim's own browser copy dead, so the attacker held the only live secret — persistent and exclusive takeover. The sessionId needed in step 2 is readable from the JWT payload without any key.

This defeats the bounded-delegation rule POST /v1/account/jwts enforces: it throws USER_JWT_CREATION_DENIED on any request carrying x-appwrite-jwt, and requires sessionVerify on the real secret.

Confirmed against a local stack before the fix:

DEVICE sessions: [{"$id":"6af06a9c…","expire":"2027-11-08T12:27:08+00:00","factors":["password"],…}]
SWITCH status: 200  got session cookie: YES
ATTACKER /account status: 200  email: …[email protected]
VICTIM  /account status: 401

The fix

Devices/Create.php no longer consumes the session resource. A private proven() resolves the adoption target by verifying the session secret from the request store with $user->sessionVerify() — the same check createJWT makes — and returns null in admin mode (there the secret belongs to a console session in the platform database, not the project database the device is written to).

A JWT request leaves that store empty, so sessionVerify fails and nothing is adopted: the device comes back with no sessions, and the device session route then has nothing to hand out. Every legitimate carrier of the session secret — the cookie, x-appwrite-session for SSR, x-fallback-cookies — still adopts, because they all populate the same store.

Alongside it, a docblock on the session resource recording that its JWT branch identifies a session without proving possession of its secret, so no future route mistakes it for proof.

I also tried dropping AuthType::JWT from the two device routes' specs as belt-and-braces, and backed it out: AuthType::JWT maps to APP_SDK_PLATFORM_SERVER, so removing it deletes account.createDevice from every server SDK. tests/unit/SDK/ParametersTest caught it as the breaking change it is. It was only ever spec metadata — enforcement is the secret check above — so the route still advertises JWT, and a JWT caller still gets a device; it just comes back holding no session.

Tests

New testDeviceDoesNotAdoptTheSessionNamedInAJwt in AccountDevicesCustomClientTest (the suite previously had zero JWT cases). Written first and confirmed red against the unfixed code:

Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
 Array (
+    0 => [...]
 )

It asserts a JWT-only POST /v1/account/devices leaves the device with no sessions and no active session, that the device secret then yields 404 user_device_session_not_found with no a_session cookie, that the victim's own cookie still authenticates, and — so the fix cannot silently break the feature — that a session cookie still does adopt.

Local results with the fix:

  • tests/e2e/Services/Account/AccountDevicesCustomClientTest — OK (8 tests, 187 assertions)
  • tests/e2e/Services/Account — OK, Tests: 208, Assertions: 4179, Skipped: 3
  • tests/unit/SDK/ParametersTest — OK (2 tests, 10843 assertions)
  • pint, phpstan (level 4) and rector --dry-run clean on the touched files

Console's ensureConsoleDevice() calls this route through sdk.forConsole, which runs in default mode with the console session cookie, so account switching in the Console keeps adopting as before.

🤖 Generated with Claude Code

@luxor-code-review

luxor-code-review Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

Review complete. No issues found — approved ✅.


This PR closes a session-escalation gap in the account-devices surface: previously, a JWT's sessionId claim could resolve the injected session resource without the caller proving possession of the session secret, letting a JWT holder adopt that session into a new device and trade up to its rotated secret.

Files Change
app/init/resources/request.php Documents the JWT fallback resolution of the session resource and its unproven nature.
src/Appwrite/Platform/Modules/Account/Http/Account/Devices/Create.php Adds a proven() gate that runs sessionVerify() against the caller's session secret before adopting sessions into a new device; removes AuthType::JWT from SDK metadata.
src/Appwrite/Platform/Modules/Account/Http/Account/Devices/Sessions/Update.php Removes AuthType::JWT from SDK metadata for the device session-switch route.
tests/e2e/Services/Account/AccountDevicesCustomClientTest.php Adds e2e coverage for the device creation and session-switch contracts under the new proof requirement.

The fix is a genuine security improvement; the remaining JWT reachability of POST /v1/account/devices now only mints an empty device container, which grants no additional privilege. Sibling device endpoints (e.g. Devices/Get.php) still advertise AuthType::JWT in their SDK metadata — likely intentional, but worth confirming for documentation consistency.

Reviewed commit: 44d5bfd

@appwrite

appwrite Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

Device creation now adopts a session only after verifying the session secret from the request store, while admin-mode requests adopt none. The session resource documents that JWT session claims do not prove secret possession, and an end-to-end test covers JWT-only and session-cookie behavior.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 3
File Change
app/init/resources/request.php Documents that JWT-based session resolution identifies a session without proving possession of its secret.
src/Appwrite/Platform/Modules/Account/Http/Account/Devices/Create.php Verifies the request's session secret before adopting a session and keeps device-cookie storage separate.
tests/e2e/Services/Account/AccountDevicesCustomClientTest.php Adds a regression test for JWT-only device creation, blocked session switching, and cookie-based adoption.

Reviewed f874fab · Details · Comment @appwrite review to re-run, or mention @appwrite with a question.

@appwrite appwrite Bot 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

github-actions Bot commented Nov 8, 2026

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 Nov 8, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/device-create-requires-proven-session (after).

Metric Before After Change
🚀 Requests/sec 157.11 163.66 ⚪ +4.2%
⏱️ Latency P50 107.94 ms 106.4 ms ⚪ -1.4%
⏱️ Latency P95 257.58 ms 252.49 ms ⚪ -2%
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 106.4 252.49 10,260 163.66 -5.08
Account 202.5 371.86 540 9.3 -11.34
TablesDB 102.42 190.21 5,580 92.31 +4.62
Storage 100.46 231.48 2,700 46.15 -16.19
Functions 150.5 287.35 1,440 25.27 -33.45

Top API waits (after)

API request Max wait (ms)
account.name.update 541.2
tablesdb.rows.create 509.66
account.prefs.update 478.26
functions.variables.update 438.99
functions.create 436.55

Creating an account switching device adopted the caller's current session
straight from the injected `session` resource. That resource also resolves
from the `sessionId` claim inside `x-appwrite-jwt`, which names a session
without proving possession of its secret.

So a JWT-only caller -- function runtime code and its transitive
dependencies, an app backend, a token scraped from logs -- could create a
device that adopted the victim's session, then trade the returned device
secret for a fresh plaintext session cookie through
`PATCH /v1/account/devices/:deviceId/session`. The cookie kept the
session's original expiry (one year by default) and its already satisfied
MFA factors, and the rotation left the victim's own browser copy dead, so
the attacker held the only live secret. That defeats the bounded, short
lived, non-exchangeable design `POST /v1/account/jwts` enforces by
refusing any request that carries a JWT.

Resolve the adoption target by verifying the session secret from the
request store with `$user->sessionVerify()`, the same check createJWT
makes, instead of consuming the `session` resource. A JWT request leaves
that store empty, so nothing is adopted and the device comes back with no
sessions. Every legitimate carrier of the secret -- the cookie,
`x-appwrite-session`, `x-fallback-cookies` -- still adopts, because they
all populate that same store. Admin mode still adopts nothing: there the
secret belongs to a console session in the platform database, not the
project database the device is written to.

Also document on the `session` resource that it identifies a session
without proving it, so no future route mistakes it for proof of
possession.

Co-Authored-By: Claude Opus 5 <[email protected]>
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.

2 participants