OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
fix(api): a database error no longer signs everyone out
Carl-Gerhard Lindesvärd98c09cfdc34a
diff --git a/packages/core/src/http/session.test.ts b/packages/core/src/http/session.test.tsnew file mode 100644index 000000000..493f16303--- /dev/null+++ b/packages/core/src/http/session.test.ts@@ -0,0 +1,44 @@+import { expect, test } from 'bun:test';+import { stubAppDeps } from '../../test/http-fixtures';+import { type AppDeps, createCtx } from '../context';+import { resolveSession } from './session';++const NO_HEADERS = new Headers();++function ctxWithSessionLookup(findUnique: () => Promise<unknown>) {+ const { deps, logger } = stubAppDeps();+ const db = { session: { findUnique } } as unknown as AppDeps['db'];+ return createCtx({ ...deps, db }, { requestId: 'req-1', logger });+}++function cookieJar(token: string | undefined) {+ return { get: (name: string) => (name === 'session' ? token : undefined) };+}++test('a cookie that matches no session resolves to nobody signed in', async () => {+ const ctx = ctxWithSessionLookup(() => Promise.resolve(null));++ expect(+ await resolveSession(ctx, cookieJar('not-a-real-token'), NO_HEADERS)+ ).toBeNull();+});++test('no cookie resolves to nobody signed in without touching the database', async () => {+ const ctx = ctxWithSessionLookup(() =>+ Promise.reject(new Error('should not be called'))+ );++ expect(+ await resolveSession(ctx, cookieJar(undefined), NO_HEADERS)+ ).toBeNull();+});++test('a failing session lookup propagates instead of signing the user out', async () => {+ const ctx = ctxWithSessionLookup(() =>+ Promise.reject(new Error("Can't reach database server"))+ );++ await expect(+ resolveSession(ctx, cookieJar('some-token'), NO_HEADERS)+ ).rejects.toThrow("Can't reach database server");+});diff --git a/packages/core/src/http/session.ts b/packages/core/src/http/session.tsindex 882392b19..2d6e08d34 100644--- a/packages/core/src/http/session.ts+++ b/packages/core/src/http/session.ts@@ -37,14 +37,13 @@ export async function resolveSession(const token = cookies.get(SESSION_COOKIE_NAME);const auth = ctx.services.auth;- try {- const result = await runWithAlsSession(- token ? auth.decodeSessionToken(token) : DEMO_ALS_SESSION_ID,- () => auth.validateSessionToken(token ?? null)- );- return result.userId === null ? null : result;- } catch {- // A malformed cookie resolves to null (nobody signed in) rather than a 500.- return null;- }+ // No catch: decoding is a hash and cannot fail on any cookie value, so the+ // only thing that throws here is the database. Answering `null` for that+ // would be a 401 and the dashboard would sign every user out during a blip;+ // a 500 makes the client retry instead.+ const result = await runWithAlsSession(+ token ? auth.decodeSessionToken(token) : DEMO_ALS_SESSION_ID,+ () => auth.validateSessionToken(token ?? null)+ );+ return result.userId === null ? null : result;}