From 993a635ce0d5515d3b15fe319a01070690a4023a Mon Sep 17 00:00:00 2001 From: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:18:50 +0000 Subject: [PATCH] fix(auth): make session revocation immediate Deleting session rows did not end a session while the 5 minute cookie cache served the signed cookie without reading the database, so an admin password reset left the old session usable. Drop the cache so session state is always authoritative, and cover revocation with a test. --- backend/src/auth/index.ts | 4 ---- backend/test/auth/index.test.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/backend/src/auth/index.ts b/backend/src/auth/index.ts index fb9463b9..161527f2 100644 --- a/backend/src/auth/index.ts +++ b/backend/src/auth/index.ts @@ -76,10 +76,6 @@ export function createAuth(db: Database) { session: { expiresIn: 60 * 60 * 24 * 7, updateAge: 60 * 60 * 24, - cookieCache: { - enabled: true, - maxAge: 60 * 5, - }, }, user: { additionalFields: { diff --git a/backend/test/auth/index.test.ts b/backend/test/auth/index.test.ts index e42f48b9..70af8bf3 100644 --- a/backend/test/auth/index.test.ts +++ b/backend/test/auth/index.test.ts @@ -119,6 +119,34 @@ describe('createAuth', () => { } }) + it('rejects a session cookie immediately after its session row is deleted', async () => { + const db = createAuthDatabase() + const auth = createAuth(db) + + try { + const signUpResponse = await auth.handler(new Request('http://localhost:5173/api/auth/sign-up/email', { + method: 'POST', + headers: { 'content-type': 'application/json', origin: 'http://localhost:5173' }, + body: JSON.stringify({ email: 'revoked@example.com', password: 'test-password', name: 'Revoked' }), + })) + expect(signUpResponse.status).toBe(200) + + const setCookies = signUpResponse.headers.getSetCookie() + expect(setCookies.length).toBeGreaterThan(0) + const headers = new Headers({ cookie: setCookies.map((cookie) => cookie.split(';')[0]).join('; ') }) + + const session = await auth.api.getSession({ headers }) + expect(session?.user.email).toBe('revoked@example.com') + + db.prepare('DELETE FROM "session" WHERE "userId" = ?').run(session!.user.id) + expect(db.prepare('SELECT COUNT(*) AS count FROM "session"').get()).toEqual({ count: 0 }) + + expect(await auth.api.getSession({ headers })).toBeNull() + } finally { + db.close() + } + }, 30000) + it('creates an auth instance without social providers and responds to requests', async () => { const db = createAuthDatabase() const auth = createAuth(db)