From c3a70b123d1213651150330e7a853ea8fd20fc66 Mon Sep 17 00:00:00 2001 From: Francis Cao Date: Tue, 18 Aug 2026 14:16:03 -0700 Subject: [PATCH] fix(relational): prevent orphaned website_event sessions and keep event pagination stable. Closes #4462. --- src/app/api/send/route.test.ts | 52 +++++++++++++++++-- src/app/api/send/route.ts | 4 +- .../sql/events/getWebsiteEvents.test.ts | 9 ++-- src/queries/sql/events/getWebsiteEvents.ts | 2 +- 4 files changed, 58 insertions(+), 9 deletions(-) diff --git a/src/app/api/send/route.test.ts b/src/app/api/send/route.test.ts index 9733c0da2..1c45874d0 100644 --- a/src/app/api/send/route.test.ts +++ b/src/app/api/send/route.test.ts @@ -7,7 +7,7 @@ process.env.APP_SECRET = 'route-send-test-secret'; import { isbot } from 'isbot'; import clickhouse from '@/lib/clickhouse'; import { CACHE_TOKEN_TYPE, EVENT_TYPE } from '@/lib/constants'; -import { secret } from '@/lib/crypto'; +import { getSalt, secret, uuid } from '@/lib/crypto'; import { getClientInfo, hasBlockedIp } from '@/lib/detect'; import { createToken, parseToken } from '@/lib/jwt'; import { fetchWebsite } from '@/lib/load'; @@ -93,6 +93,13 @@ function callPOST( ); } +function makeComputedSessionId(sourceId: string, timestamp = 1704067200) { + const createdAt = new Date(timestamp * 1000); + const sessionSalt = getSalt(process.env.SALT_ROTATION || 'month', createdAt); + + return uuid(sourceId, defaultClientInfo.ip, defaultClientInfo.userAgent, sessionSalt); +} + beforeEach(() => { vi.clearAllMocks(); (clickhouse as any).enabled = false; @@ -540,11 +547,12 @@ describe('cache token handling', () => { ); } - test('a valid cache token skips website lookup and session creation', async () => { - const token = makeCacheToken(); + test('a valid cache token skips website lookup and session creation when it matches the computed session', async () => { + const timestamp = 1704067200; + const token = makeCacheToken({ sessionId: makeComputedSessionId(WEBSITE_ID, timestamp) }); const response = await callPOST( - { type: 'event', payload: { website: WEBSITE_ID, url: '/' } }, + { type: 'event', payload: { website: WEBSITE_ID, url: '/', timestamp } }, { headers: { 'x-umami-cache': token } }, ); @@ -553,6 +561,42 @@ describe('cache token handling', () => { await expect(response.json()).resolves.toMatchObject({ visitId: 'cached-visit' }); }); + test('a valid cache token creates the computed session before event writes when the cached session differs', async () => { + const token = makeCacheToken({ sessionId: 'cached-session' }); + + await callPOST( + { type: 'event', payload: { website: WEBSITE_ID, url: '/' } }, + { headers: { 'x-umami-cache': token } }, + ); + + expect(createSessionMock).toHaveBeenCalledTimes(1); + const createdSession = createSessionMock.mock.calls[0][0] as Record; + const savedEvent = saveEventMock.mock.calls[0][0] as Record; + + expect(createdSession.id).not.toBe('cached-session'); + expect(savedEvent.sessionId).toBe(createdSession.id); + }); + + test('a valid cache token creates the computed session before identify writes when the cached session differs', async () => { + const token = makeCacheToken({ sessionId: 'cached-session' }); + + await callPOST( + { type: 'identify', payload: { website: WEBSITE_ID, id: 'user-42', data: { plan: 'pro' } } }, + { headers: { 'x-umami-cache': token } }, + ); + + expect(createSessionMock).toHaveBeenCalledTimes(1); + const createdSession = createSessionMock.mock.calls[0][0] as Record; + const savedLink = saveSessionLinkMock.mock.calls[0][0] as Record; + const updatedSession = updateSessionMock.mock.calls[0][0] as Record; + const savedSessionData = saveSessionDataMock.mock.calls[0][0] as Record; + + expect(createdSession.id).not.toBe('cached-session'); + expect(savedLink.sessionId).toBe(createdSession.id); + expect(updatedSession.sessionId).toBe(createdSession.id); + expect(savedSessionData.sessionId).toBe(createdSession.id); + }); + test('an invalid cache token falls back to website lookup', async () => { await callPOST( { type: 'event', payload: { website: WEBSITE_ID, url: '/' } }, diff --git a/src/app/api/send/route.ts b/src/app/api/send/route.ts index cd5d42634..513cf873a 100644 --- a/src/app/api/send/route.ts +++ b/src/app/api/send/route.ts @@ -156,9 +156,11 @@ export async function POST(request: Request) { const visitSalt = hash(startOfHour(createdAt).toUTCString()); const sessionId = uuid(sourceId, ip, userAgent, sessionSalt); + const shouldEnsureSession = + !clickhouse.enabled && !!websiteId && !!cache?.sessionId && cache.sessionId !== sessionId; // Create a session if not found - if (!clickhouse.enabled && !cache?.sessionId) { + if ((!clickhouse.enabled && !cache?.sessionId) || shouldEnsureSession) { await createSession({ id: sessionId, websiteId: sourceId, diff --git a/src/queries/sql/events/getWebsiteEvents.test.ts b/src/queries/sql/events/getWebsiteEvents.test.ts index 51238327f..d957289ad 100644 --- a/src/queries/sql/events/getWebsiteEvents.test.ts +++ b/src/queries/sql/events/getWebsiteEvents.test.ts @@ -5,8 +5,7 @@ const prismaParseFiltersResult = { filterQuery: 'and website_event.url_path = {{path}}', dateQuery: 'and website_event.created_at between {{startDate}} and {{endDate}}', cohortQuery: 'join cohort on cohort.session_id = website_event.session_id', - joinSessionQuery: - 'inner join session on website_event.session_id = session.session_id and website_event.website_id = session.website_id', + joinSessionQuery: '', }; const clickhouseParseFiltersResult = { @@ -67,7 +66,7 @@ afterEach(() => { }); describe('getWebsiteEvents', () => { - test('postgres counts from the lightweight filtered event query and enriches only the paged rows', async () => { + test('postgres counts from the lightweight filtered event query and keeps paged rows even when session enrichment is missing', async () => { const { getWebsiteEvents, prismaRawQuery } = await loadModule({ mode: 'prisma' }); const result = await getWebsiteEvents('website-1', { @@ -86,6 +85,7 @@ describe('getWebsiteEvents', () => { expect(countQuery).toContain('select count(*) as num from (select 1 from ('); expect(countQuery).not.toContain('exists('); expect(countQuery).toContain('event_name ilike {{eventSearch}}'); + expect(countQuery).not.toContain('inner join session on website_event.session_id'); expect(dataQuery).toContain('with paged_events as ('); expect(dataQuery).toContain('paged_event_data as ('); expect(dataQuery).toContain( @@ -96,6 +96,9 @@ describe('getWebsiteEvents', () => { expect(dataQuery).toContain( 'left join paged_event_data on paged_event_data.event_id = website_event.event_id', ); + expect(dataQuery).toContain( + 'left join session on session.session_id = website_event.session_id', + ); expect(dataQuery).toContain('order by paged_events.created_at desc'); expect(dataQuery).toContain('(paged_event_data.event_id is not null) as "hasData"'); expect(countParams).toMatchObject({ eventSearch: '%signup%' }); diff --git a/src/queries/sql/events/getWebsiteEvents.ts b/src/queries/sql/events/getWebsiteEvents.ts index e105a0ddf..ca779c5ed 100644 --- a/src/queries/sql/events/getWebsiteEvents.ts +++ b/src/queries/sql/events/getWebsiteEvents.ts @@ -100,7 +100,7 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) { (paged_event_data.event_id is not null) as "hasData" from paged_events join website_event on website_event.event_id = paged_events.event_id - join session on session.session_id = website_event.session_id + left join session on session.session_id = website_event.session_id and session.website_id = website_event.website_id left join paged_event_data on paged_event_data.event_id = website_event.event_id order by paged_events.created_at desc