:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(replay): compare the plaintext key when a session key changes (#102817)
Robbie6923f24a8f08
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/sessions/metrics.ts b/nodejs/src/ingestion/pipelines/sessionreplay/sessions/metrics.tsindex dd0145d18056..d9786eee4a6d 100644--- a/nodejs/src/ingestion/pipelines/sessionreplay/sessions/metrics.ts+++ b/nodejs/src/ingestion/pipelines/sessionreplay/sessions/metrics.ts@@ -32,6 +32,15 @@ export class SessionBatchMetrics {recordSessionsDroppedMissingRetention(count)}+ private static readonly messagesDroppedSessionKeyMismatch = new Counter({+ name: 'recording_blob_ingestion_v2_messages_dropped_session_key_mismatch_total',+ help: 'Messages refused because the session key differs from the key the open block was started with. The offset still advances, so a non-zero rate means recording data is discarded',+ })++ public static incrementMessagesDroppedSessionKeyMismatch(count: number = 1): void {+ this.messagesDroppedSessionKeyMismatch.inc(count)+ }+private static readonly sessionsFlushed = new Counter({name: 'recording_blob_ingestion_v2_sessions_flushed_total',help: 'Number of individual sessions that have been flushed',diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/sessions/session-batch-recorder.test.ts b/nodejs/src/ingestion/pipelines/sessionreplay/sessions/session-batch-recorder.test.tsindex 72b5cc95d3fa..cf5138be1e2b 100644--- a/nodejs/src/ingestion/pipelines/sessionreplay/sessions/session-batch-recorder.test.ts+++ b/nodejs/src/ingestion/pipelines/sessionreplay/sessions/session-batch-recorder.test.ts@@ -183,6 +183,7 @@ jest.mock('./metrics', () => ({incrementEventsRateLimited: jest.fn(),incrementNewSessionsDetected: jest.fn(),incrementNewSessionsRateLimited: jest.fn(),+ incrementMessagesDroppedSessionKeyMismatch: jest.fn(),observeE2eLag: jest.fn(),},}))@@ -1878,12 +1879,24 @@ describe('SessionBatchRecorder', () => {})describe('encryption key handling', () => {- it('should drop messages when the session key changes between calls for the same session', async () => {- // The key is resolved upstream; the recorder must reject a second message that arrives- // with a different key for a session already in the batch, so a block isn't corrupted by- // events encrypted for two different keys.- const keyA = createMockSessionKey({ encryptedKey: Buffer.from('key-a') })- const keyB = createMockSessionKey({ encryptedKey: Buffer.from('key-b') })+ // The key is resolved upstream; the recorder must reject a second message that arrives+ // with a different key for a session already in the batch, so a block isn't corrupted by+ // events encrypted for two different keys.+ it.each([+ [+ 'the encrypted key differs',+ { encryptedKey: Buffer.from('key-a') },+ { encryptedKey: Buffer.from('key-b') },+ ],+ // A sealed ML session key carries no KMS blob, so encryptedKey is empty on both sides and only the plaintext differs.+ [+ 'only the plaintext key differs, as it does for a sealed ML key',+ { plaintextKey: Buffer.alloc(32, 1), sessionState: 'ciphertext' as const },+ { plaintextKey: Buffer.alloc(32, 2), sessionState: 'ciphertext' as const },+ ],+ ])('should drop a message for a session already in the batch when %s', async (_label, first, second) => {+ const keyA = createMockSessionKey(first)+ const keyB = createMockSessionKey(second)const message1 = createMessage('session1',@@ -1896,11 +1909,8 @@ describe('SessionBatchRecorder', () => {{ partition: 1, offset: 1 })- const bytes1 = await record(message1, '30d', keyA)- const bytes2 = await record(message2, '30d', keyB)-- expect(bytes1).toBeGreaterThan(0)- expect(bytes2).toBe(0)+ expect(await record(message1, '30d', keyA)).toBeGreaterThan(0)+ expect(await record(message2, '30d', keyB)).toBe(0)})})})diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/sessions/session-batch-recorder.ts b/nodejs/src/ingestion/pipelines/sessionreplay/sessions/session-batch-recorder.tsindex 72b92bb89889..9a4f3a541b4f 100644--- a/nodejs/src/ingestion/pipelines/sessionreplay/sessions/session-batch-recorder.ts+++ b/nodejs/src/ingestion/pipelines/sessionreplay/sessions/session-batch-recorder.ts@@ -78,6 +78,7 @@ export class SessionBatchRecorder {// Sessions are keyed by (teamId, sessionId) across all partitions. A session is pinned to one// partition, so the key is unique. Not readonly: flush swaps it for a fresh map.private sessions = new SessionMap<SessionBatchEntry>()+ private keyMismatchReported = new SessionMap<true>()private _size: number = 0private readonly batchId: stringprivate readonly rateLimiter: SessionRateLimiter@@ -154,12 +155,21 @@ export class SessionBatchRecorder {return 0}- if (!existingSessionKey.encryptedKey.equals(sessionKey.encryptedKey)) {- logger.warn('🔁', 'session_batch_recorder_session_key_mismatch', {- sessionId,- teamId,- batchId: this.batchId,- })+ // A sealed ML session key carries no KMS blob, so encryptedKey alone compares two empty buffers on that lane.+ if (+ !existingSessionKey.encryptedKey.equals(sessionKey.encryptedKey) ||+ !existingSessionKey.plaintextKey.equals(sessionKey.plaintextKey)+ ) {+ SessionBatchMetrics.incrementMessagesDroppedSessionKeyMismatch()+ // The key keeps differing for the rest of the batch, so the counter carries the volume and the log names the session once.+ if (!this.keyMismatchReported.has(teamId, sessionId)) {+ this.keyMismatchReported.set(teamId, sessionId, true)+ logger.warn('🔁', 'session_batch_recorder_session_key_mismatch', {+ sessionId,+ teamId,+ batchId: this.batchId,+ })+ }return 0}} else {@@ -369,6 +379,7 @@ export class SessionBatchRecorder {// Clear sessions, total size, and rate limiter state after successful flushthis.sessions = new SessionMap()+ this.keyMismatchReported = new SessionMap()this._size = 0this.rateLimiter.clear()