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.
This commit is contained in:
Chris Scott committed 2026-10-21 15:18:50 +00:00
1 parent eb3937ef7e
commit 993a635ce0
2 files changed
+28 -4

No files matched your search

-4
View File
@@ -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: {
+28
View File
@@ -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)