From 1265e8d500b6d63012548b840cb1de855f9b0885 Mon Sep 17 00:00:00 2001 From: eldadfux Date: Thu, 6 Aug 2026 20:33:21 +0100 Subject: [PATCH 1/2] fix(auth): reject JWT when linked session is expired JWT auth only checked that the session still existed on the user document, so tokens remained valid after session timeout. Align JWT validation with sessionVerify by requiring a non-expired session. Co-authored-by: Cursor --- app/init/realtime/connection.php | 2 +- app/init/resources/request.php | 6 ++-- .../Utopia/Database/Documents/User.php | 18 +++++++++++ .../Utopia/Database/Documents/UserTest.php | 31 +++++++++++++++++++ 4 files changed, 52 insertions(+), 5 deletions(-) diff --git a/app/init/realtime/connection.php b/app/init/realtime/connection.php index 2a0b97bfdd..a28b4a35dd 100644 --- a/app/init/realtime/connection.php +++ b/app/init/realtime/connection.php @@ -295,7 +295,7 @@ return function (Container $container): void { } $jwtSessionId = $payload['sessionId'] ?? ''; - if (!empty($jwtSessionId) && empty($user->find('$id', $jwtSessionId, 'sessions'))) { + if (!empty($jwtSessionId) && !$user->sessionActive($jwtSessionId)) { $user = new User([]); } } diff --git a/app/init/resources/request.php b/app/init/resources/request.php index 70ddf17d90..a07bd72597 100644 --- a/app/init/resources/request.php +++ b/app/init/resources/request.php @@ -542,10 +542,8 @@ return function (Container $context): void { } } $jwtSessionId = $payload['sessionId'] ?? ''; - if (! empty($jwtSessionId)) { - if (empty($user->find('$id', $jwtSessionId, 'sessions'))) { // Match JWT to active token - $user = new User([]); - } + if (! empty($jwtSessionId) && ! $user->sessionActive($jwtSessionId)) { // Match JWT to active session + $user = new User([]); } } diff --git a/src/Appwrite/Utopia/Database/Documents/User.php b/src/Appwrite/Utopia/Database/Documents/User.php index 3004efe382..360ea8b279 100644 --- a/src/Appwrite/Utopia/Database/Documents/User.php +++ b/src/Appwrite/Utopia/Database/Documents/User.php @@ -174,4 +174,22 @@ class User extends Document return false; } + + /** + * Check that a session exists on the user and has not expired. + * + * Used by JWT authentication, which binds to a session ID rather than a + * session secret. + */ + public function sessionActive(string $sessionId): bool + { + $session = $this->find('$id', $sessionId, 'sessions'); + + if (empty($session)) { + return false; + } + + return $session->isSet('expire') + && DateTime::formatTz(DateTime::format(new \DateTime($session->getAttribute('expire')))) >= DateTime::formatTz(DateTime::now()); + } } diff --git a/tests/unit/Utopia/Database/Documents/UserTest.php b/tests/unit/Utopia/Database/Documents/UserTest.php index c21c5f6272..3e2df588ac 100644 --- a/tests/unit/Utopia/Database/Documents/UserTest.php +++ b/tests/unit/Utopia/Database/Documents/UserTest.php @@ -97,6 +97,37 @@ final class UserTest extends TestCase $this->assertEquals(false, $user2->sessionVerify('false-secret', $proofForToken)); } + public function testSessionActive(): void + { + $user = new User([ + '$id' => ID::custom('user1'), + 'sessions' => [ + new Document([ + '$id' => ID::custom('active'), + 'secret' => 'secret', + 'provider' => SESSION_PROVIDER_EMAIL, + 'expire' => DateTime::addSeconds(new \DateTime(), 60 * 60), + ]), + new Document([ + '$id' => ID::custom('expired'), + 'secret' => 'secret', + 'provider' => SESSION_PROVIDER_EMAIL, + 'expire' => DateTime::addSeconds(new \DateTime(), -60), + ]), + new Document([ + '$id' => ID::custom('missing-expire'), + 'secret' => 'secret', + 'provider' => SESSION_PROVIDER_EMAIL, + ]), + ], + ]); + + $this->assertTrue($user->sessionActive('active')); + $this->assertFalse($user->sessionActive('expired')); + $this->assertFalse($user->sessionActive('missing-expire')); + $this->assertFalse($user->sessionActive('missing')); + } + public function testTokenVerify(): void { $proofForToken = new Token(); From ab5dbebbc077352d87d54d07f8aca8de3fb71750 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 7 Aug 2026 14:10:31 +0530 Subject: [PATCH 2/2] Annotate JWT user lookups as User for static analysis getDocument() is declared as returning Document, so the sessionActive() calls in the JWT auth paths tripped PHPStan's method.notFound. The users collection is mapped to User via setDocumentType(), matching the annotations already on the cookie-path lookups above. --- app/init/realtime/connection.php | 2 ++ app/init/resources/request.php | 2 ++ 2 files changed, 4 insertions(+) diff --git a/app/init/realtime/connection.php b/app/init/realtime/connection.php index a28b4a35dd..5b44f27946 100644 --- a/app/init/realtime/connection.php +++ b/app/init/realtime/connection.php @@ -288,8 +288,10 @@ return function (Container $container): void { $jwtUserId = $payload['userId'] ?? ''; if (!empty($jwtUserId)) { if ($mode === APP_MODE_ADMIN) { + /** @var User $user */ $user = $dbForPlatform->getDocument('users', $jwtUserId); } else { + /** @var User $user */ $user = $dbForProject->getDocument('users', $jwtUserId); } } diff --git a/app/init/resources/request.php b/app/init/resources/request.php index a07bd72597..db5d6e2460 100644 --- a/app/init/resources/request.php +++ b/app/init/resources/request.php @@ -536,8 +536,10 @@ return function (Container $context): void { $jwtUserId = $payload['userId'] ?? ''; if (! empty($jwtUserId)) { if ($mode === APP_MODE_ADMIN) { + /** @var User $user */ $user = $dbForPlatform->getDocument('users', $jwtUserId); } else { + /** @var User $user */ $user = $dbForProject->getDocument('users', $jwtUserId); } }