Repository navigation
fix(account): device creation adopts only a session proven by its secret (appwrite:appwrite:alert#4723) - #15404
Conversation
|
Review complete. No issues found — approved ✅. This PR closes a session-escalation gap in the account-devices surface: previously, a JWT's
The fix is a genuine security improvement; the remaining JWT reachability of Reviewed commit: 44d5bfd |
🟢 Tier S · Ready to mergeDevice 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.
📂 Walkthrough · 3
Reviewed |
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
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]>
44d5bfd to
f874fab
Compare
Fixes code-scanning alert appwrite:appwrite:alert#4723 (high).
What was wrong
POST /v1/account/devicesadopted the caller's current session straight from the injectedsessionresource. That resource (app/init/resources/request.php) falls back to thesessionIdclaim insidex-appwrite-jwtwheneversessionVerify()on the real secret fails — it accepts the claim after only checking thatuserIdmatches and the session is active. NoproofForTokenverification 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:
POST /v1/account/deviceswith onlyx-appwrite-jwt. The device adopted the JWT's session (deviceIddefaults to''for every session created without a device cookie, soisAdoptable()always said yes), and the device secret came back inSet-Cookie/X-Fallback-Cookies.PATCH /v1/account/devices/:deviceId/sessionwith just that device secret. It picks a session the device holds, mints a fresh secret, and returns the plaintext in thea_sessioncookie — keeping the row's originalexpireand 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
sessionIdneeded in step 2 is readable from the JWT payload without any key.This defeats the bounded-delegation rule
POST /v1/account/jwtsenforces: it throwsUSER_JWT_CREATION_DENIEDon any request carryingx-appwrite-jwt, and requiressessionVerifyon the real secret.Confirmed against a local stack before the fix:
The fix
Devices/Create.phpno longer consumes thesessionresource. A privateproven()resolves the adoption target by verifying the session secret from the request store with$user->sessionVerify()— the same checkcreateJWTmakes — and returnsnullin 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
sessionVerifyfails 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-sessionfor SSR,x-fallback-cookies— still adopts, because they all populate the same store.Alongside it, a docblock on the
sessionresource 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::JWTfrom the two device routes' specs as belt-and-braces, and backed it out:AuthType::JWTmaps toAPP_SDK_PLATFORM_SERVER, so removing it deletesaccount.createDevicefrom every server SDK.tests/unit/SDK/ParametersTestcaught 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
testDeviceDoesNotAdoptTheSessionNamedInAJwtinAccountDevicesCustomClientTest(the suite previously had zero JWT cases). Written first and confirmed red against the unfixed code:It asserts a JWT-only
POST /v1/account/devicesleaves the device with no sessions and no active session, that the device secret then yields404 user_device_session_not_foundwith noa_sessioncookie, 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: 3tests/unit/SDK/ParametersTest—OK (2 tests, 10843 assertions)pint,phpstan(level 4) andrector --dry-runclean on the touched filesConsole's
ensureConsoleDevice()calls this route throughsdk.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