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 <cursoragent@cursor.com>
This commit is contained in:
eldadfuxandCursor committed 2026-08-06 20:33:21 +01:00
1 parent 42af673af6
commit 1265e8d500
4 files changed
+52 -5

No files matched your search

+1 -1
View File
@@ -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([]);
}
}
+2 -4
View File
@@ -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([]);
}
}
@@ -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());
}
}
@@ -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();