Skip to content

: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.

21.4 KB
Raw Blame
import fs from 'fs'
import path from 'path'
import { gzipSync, zstdDecompressSync } from 'zlib'

import { parseJSON } from '~/common/utils/json-parse'
import { logger } from '~/common/utils/logger'
import {
    hashImageBytes,
    imageRef,
    isImageRef,
    parseImageRef,
    urlRef,
} from '~/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub/content-ref'
import {
    PSEUDONYM_IMAGE_URL_GLOBAL_VALUE,
    PSEUDONYM_IMAGE_URL_KEY,
    pseudonymize,
} from '~/ingestion/pipelines/sessionreplay/ml-mirror/pseudonymize'

// Shared fixtures pin the addon's behavior through the FFI; the pure-Rust side of the same
// fixtures is covered by rust/replay-anonymizer/tests/parity.rs.
const FIXTURE_DIR = path.resolve(__dirname, '../../../../../../rust/replay-anonymizer/tests/fixtures')

interface AllowSpec {
    text: string[]
    url: string[]
}
interface EventCase {
    name: string
    allow: AllowSpec
    event: Record<string, unknown>
    expected: Record<string, unknown>
}
interface MessageCase {
    name: string
    allow: AllowSpec
    message: Record<string, unknown[]>
    expected: Record<string, unknown>
}
interface JsonLdContract {
    schemaVersion: 1
    typeSets: Array<{
        name: string
        types: string[]
    }>
    rejectedTypes: string[]
    cases: Array<{
        name: string
        input: unknown
        expected: unknown
    }>
}

function loadDocument<T>(name: string): T {
    return parseJSON(fs.readFileSync(path.join(FIXTURE_DIR, name), 'utf8'))
}

function load<T>(name: string): T[] {
    return loadDocument<T[]>(name)
}

let rustAddon: typeof import('@posthog/replay-anonymizer') | null = null
try {
    rustAddon = require('@posthog/replay-anonymizer')
} catch (e) {
    if (process.env.CI) {
        throw new Error(`replay-anonymizer addon failed to load; native parity cannot run in CI: ${String(e)}`)
    }
    logger.warn('🙈', 'replay_anonymizer_addon_not_built_skipping_native_parity')
}

const describeAddon = rustAddon ? describe : describe.skip
describeAddon('native rust addon matches the shared fixtures', () => {
    const eventCases = load<EventCase>('events.json')
    const messageCases = load<MessageCase>('messages.json')
    const jsonLdContract = loadDocument<JsonLdContract>('json-ld-sanitization-v1.json')

    const TS0 = 1_700_000_000_000

    // Wrap fixture events in the Kafka payload shape
    function payloadOf(windowId: string, events: unknown[]): Buffer {
        const items = structuredClone(events)
        items.forEach((ev, i) => {
            if (isRecord(ev)) {
                ev.timestamp ??= TS0 + i
            }
        })
        const inner = JSON.stringify({
            event: '$snapshot_items',
            properties: { $snapshot_items: items, $session_id: 's-1', $window_id: windowId, $snapshot_source: 'web' },
        })
        return Buffer.from(JSON.stringify({ distinct_id: 'd-1', data: inner }))
    }

    function isRecord(v: unknown): v is Record<string, unknown> {
        return typeof v === 'object' && v !== null && !Array.isArray(v)
    }

    function expectedLines(windowId: string, expected: unknown[]): unknown[] {
        const events = structuredClone(expected)
        events.forEach((ev, i) => {
            if (isRecord(ev)) {
                ev.timestamp ??= TS0 + i
            }
        })
        return events.filter(isRecord).map((ev) => [windowId, ev])
    }

    function parseLines(lines: Buffer): unknown[] {
        return lines
            .toString()
            .split('\n')
            .filter((l) => l.length > 0)
            .map((l) => parseJSON(l))
    }

    test('uses the supported JSON-LD contract version', () => {
        expect(jsonLdContract.schemaVersion).toBe(1)
    })

    describe('events', () => {
        test.each(eventCases.map((c) => [c.name, c] as const))('event: %s', async (_name, c) => {
            // --runInBand is required to ensure cases are sequential
            rustAddon!.initAnonymizer(c.allow)
            const result = await rustAddon!.anonymizeKafkaPayload(payloadOf('w', [c.event]))
            expect(result.failed).toBe(false)
            expect(parseLines(result.lines!)).toEqual(expectedLines('w', [c.expected]))
        })
    })

    describe('JSON-LD contract', () => {
        test.each(jsonLdContract.cases.map((c) => [c.name, c] as const))('JSON-LD: %s', async (_name, c) => {
            rustAddon!.initAnonymizer({ text: [], url: [] })
            const event = { type: 5, data: { tag: '$json_ld', payload: c.input } }
            const expectedData = c.expected ? { tag: '$json_ld', payload: c.expected } : { tag: '$json_ld' }
            const result = await rustAddon!.anonymizeKafkaPayload(payloadOf('w', [event]))
            expect(result.failed).toBe(false)
            expect(parseLines(result.lines!)).toEqual(expectedLines('w', [{ type: 5, data: expectedData }]))
        })

        test.each(jsonLdContract.typeSets.map((typeSet) => [typeSet.name, typeSet] as const))(
            'JSON-LD types: %s',
            async (_name, typeSet) => {
                rustAddon!.initAnonymizer({ text: [], url: [] })
                for (const type of typeSet.types) {
                    const payload = { '@context': 'https://schema.org', '@type': type }
                    const event = { type: 5, data: { tag: '$json_ld', payload } }
                    const result = await rustAddon!.anonymizeKafkaPayload(payloadOf('w', [event]))
                    expect(result.failed).toBe(false)
                    expect(parseLines(result.lines!)).toEqual(
                        expectedLines('w', [{ type: 5, data: { tag: '$json_ld', payload } }])
                    )
                }
            }
        )

        test('JSON-LD rejected types', async () => {
            rustAddon!.initAnonymizer({ text: [], url: [] })
            for (const type of jsonLdContract.rejectedTypes) {
                const payload = { '@context': 'https://schema.org', '@type': type }
                const event = { type: 5, data: { tag: '$json_ld', payload } }
                const result = await rustAddon!.anonymizeKafkaPayload(payloadOf('w', [event]))
                expect(result.failed).toBe(false)
                expect(parseLines(result.lines!)).toEqual(expectedLines('w', [{ type: 5, data: { tag: '$json_ld' } }]))
            }
        })
    })

    test.each(messageCases.map((c) => [c.name, c] as const))('message: %s', async (_name, c) => {
        rustAddon!.initAnonymizer(c.allow)
        for (const [windowId, events] of Object.entries(c.message)) {
            const expected = expectedLines(windowId, (c.expected as Record<string, unknown[]>)[windowId])
            const result = await rustAddon!.anonymizeKafkaPayload(payloadOf(windowId, events))
            if (expected.length === 0) {
                // A window with only invalid events: the fused step drops the whole message.
                expect(result.failed).toBe(true)
                expect(result.reason).toBe('message_contained_no_valid_rrweb_events')
                continue
            }
            expect(result.failed).toBe(false)
            expect(parseLines(result.lines!)).toEqual(expected)
        }
    })

    // fixtures are plain text, convert to gzip bytes for this test
    describe('cv-compressed events through the production entry', () => {
        // latin-1: each compressed byte is a U+00XX codepoint, matching the SDK wire format.
        const gzipLatin1 = (json: string): string => Buffer.from(gzipSync(Buffer.from(json))).toString('latin1')
        const unzstdLatin1 = (s: string): unknown => {
            const raw = Buffer.from(s, 'latin1')
            // Magic-byte dispatch is the loader contract; pin the zstd frame magic on decode.
            expect([...raw.subarray(0, 4)]).toEqual([0x28, 0xb5, 0x2f, 0xfd])
            return parseJSON(zstdDecompressSync(raw).toString())
        }

        const fullSnapshot = {
            type: 2,
            cv: '2024-10',
            data: gzipLatin1(
                JSON.stringify({
                    node: { type: 0, id: 1, childNodes: [{ type: 3, id: 5, textContent: 'keep secret' }] },
                    initialOffset: { top: 0, left: 0 },
                })
            ),
        }
        const mutation = {
            type: 3,
            cv: '2024-10',
            data: { source: 0, texts: gzipLatin1(JSON.stringify([{ id: 5, value: 'keep secret' }])) },
        }

        it('scrubs a cv full snapshot and re-emits a decodable zstd payload', async () => {
            rustAddon!.initAnonymizer({ text: ['keep'], url: [] })
            const result = await rustAddon!.anonymizeKafkaPayload(payloadOf('w', [fullSnapshot]))
            expect(result.failed).toBe(false)
            const line = parseLines(result.lines!)[0] as [string, { data: string }]
            const decoded = unzstdLatin1(line[1].data) as { node: { childNodes: { textContent: string }[] } }
            expect(decoded.node.childNodes[0].textContent).toBe('keep ******')
        })

        it('media-scrubs an rr_src attribute inside a cv mutation sub-field', async () => {
            const attrMutation = {
                type: 3,
                cv: '2024-10',
                data: {
                    source: 0,
                    attributes: gzipLatin1(
                        JSON.stringify([
                            {
                                id: 7,
                                attributes: {
                                    rr_src: 'https://widget.example-vendor.co/v2/app/?refreshToken=FakeRefreshTok3nValue000',
                                },
                            },
                        ])
                    ),
                },
            }
            rustAddon!.initAnonymizer({ text: [], url: [] })
            const result = await rustAddon!.anonymizeKafkaPayload(payloadOf('w', [attrMutation]))
            expect(result.failed).toBe(false)
            const line = parseLines(result.lines!)[0] as [string, { data: { attributes: string } }]
            const decoded = unzstdLatin1(line[1].data.attributes) as { attributes: Record<string, string> }[]
            expect(decoded[0].attributes.rr_src).toMatch(/^data:image\/svg\+xml/)
            expect(decoded[0].attributes['data-anon-original-rr_src']).toBe(
                'https://widget.example-vendor.co/[redacted]/[redacted]/'
            )
        })

        it('scrubs a cv mutation sub-field and re-emits a decodable zstd payload', async () => {
            rustAddon!.initAnonymizer({ text: ['keep'], url: [] })
            const result = await rustAddon!.anonymizeKafkaPayload(payloadOf('w', [mutation]))
            expect(result.failed).toBe(false)
            const line = parseLines(result.lines!)[0] as [string, { data: { texts: string } }]
            const decoded = unzstdLatin1(line[1].data.texts) as { value: string }[]
            expect(decoded[0].value).toBe('keep ******')
        })

        it('reports ordered phase timings with cv op totals on success', async () => {
            rustAddon!.initAnonymizer({ text: ['keep'], url: [] })
            const result = await rustAddon!.anonymizeKafkaPayload(payloadOf('w', [fullSnapshot]))
            expect(result.failed).toBe(false)
            const t = result.timings!
            expect(t.taskStartNs).not.toBeNull()
            expect(t.decompressStartNs).toBeGreaterThanOrEqual(t.taskStartNs!)
            expect(t.decompressEndNs).toBeGreaterThanOrEqual(t.decompressStartNs!)
            expect(t.scrubStartNs).toBeGreaterThanOrEqual(t.decompressEndNs!)
            expect(t.scrubEndNs).toBeGreaterThanOrEqual(t.scrubStartNs!)
            // The cv full snapshot forces at least one timed de/recompression op.
            expect(t.cvCount).toBeGreaterThanOrEqual(1)
            expect(t.lastOp).toBe('done')
        })

        it('still reports timings when the payload fails, naming the phase that died', async () => {
            rustAddon!.initAnonymizer({ text: [], url: [] })
            // Gzip magic bytes followed by garbage: decompression starts, then fails.
            const result = await rustAddon!.anonymizeKafkaPayload(Buffer.from([0x1f, 0x8b, 0x08, 0x00, 0xde, 0xad]))
            expect(result.failed).toBe(true)
            const t = result.timings!
            expect(t.decompressStartNs).not.toBeNull()
            expect(t.scrubStartNs).toBeNull()
            expect(t.lastOp).toBe('decompress')
        })
    })
})

describe('image refs match the shared fixtures', () => {
    // Pins the keyed hashImageBytes to the Rust collector's HMAC (tests/parity.rs runs the same
    // fixture): the consumer trusts the producer, so this is the only cross-implementation check.
    interface HashCase {
        name: string
        keyAscii: string
        bytesBase64: string
        hash: string
    }
    test.each(load<HashCase>('image-hash.json').map((c) => [c.name, c] as const))('hash: %s', (_name, c) => {
        expect(hashImageBytes(c.keyAscii, Buffer.from(c.bytesBase64, 'base64'))).toBe(c.hash)
    })

    interface UrlRefCase {
        name: string
        pseudonymSecret: string
        globalUrlKey: string
        hash: string
        ref: string
    }
    test.each(load<UrlRefCase>('image-url-ref.json').map((c) => [c.name, c] as const))(
        'global URL ref: %s',
        (_name, c) => {
            expect(pseudonymize(c.pseudonymSecret, PSEUDONYM_IMAGE_URL_KEY, PSEUDONYM_IMAGE_URL_GLOBAL_VALUE)).toBe(
                c.globalUrlKey
            )
            expect(urlRef(c.hash)).toBe(c.ref)
            expect(parseImageRef(c.ref)).toEqual({ hash: c.hash, source: 'url' })
        }
    )
})

describeAddon('native image collection', () => {
    // A 16x16 PNG; any valid raster image works, the assertions only rely on byte identity.
    const PNG_B64 =
        'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAACXBIWXMAAAPoAAAD6AG1e1JrAAAAJUlEQVQokWN4plEBRyInbOAIlzjDINRAjCJk8cGoYRAG60iMBwA8H08Qor0ygQAAAABJRU5ErkJggg=='
    const PSEUDO_TEAM = '0123456789abcdef0123456789abcdef'
    const CONTENT_KEY = 'fedcba9876543210fedcba9876543210'

    function imagePayload(remoteImageUrls: string[] = []): Buffer {
        const inner = JSON.stringify({
            event: '$snapshot_items',
            properties: {
                $snapshot_items: [
                    {
                        type: 2,
                        timestamp: 1_700_000_000_000,
                        data: {
                            node: {
                                type: 0,
                                childNodes: [
                                    {
                                        type: 2,
                                        tagName: 'img',
                                        attributes: { src: `data:image/png;base64,${PNG_B64}` },
                                        childNodes: [],
                                    },
                                    ...remoteImageUrls.map((src) => ({
                                        type: 2,
                                        tagName: 'img',
                                        attributes: { src },
                                        childNodes: [],
                                    })),
                                ],
                            },
                            initialOffset: { top: 0, left: 0 },
                        },
                    },
                ],
                $session_id: 's-1',
                $window_id: 'w',
                $snapshot_source: 'web',
            },
        })
        return Buffer.from(JSON.stringify({ distinct_id: 'd-1', data: inner }))
    }

    it.each([PSEUDO_TEAM, '42'])(
        'emits a consumer-parseable ref for team %s and returns the original bytes',
        async (team) => {
            rustAddon!.initAnonymizer({ text: [], url: [] })
            const result = await rustAddon!.anonymizeKafkaPayload(imagePayload(), undefined, team, CONTENT_KEY)
            expect(result.failed).toBe(false)

            const png = Buffer.from(PNG_B64, 'base64')
            const expectedRef = imageRef(team, hashImageBytes(CONTENT_KEY, png))
            expect(isImageRef(expectedRef)).toBe(true)
            expect(result.lines!.toString()).toContain(expectedRef)
            expect(result.lines!.toString()).not.toContain(PNG_B64)

            const meta = parseJSON(result.meta!) as { images?: { hash: string; offset: number; len: number }[] }
            expect(meta.images).toHaveLength(1)
            const entry = meta.images![0]
            const bytes = result.images!.subarray(entry.offset, entry.offset + entry.len)
            expect(Buffer.from(bytes)).toEqual(png)
            // The Rust-emitted hash must be the keyed HMAC of the returned bytes.
            expect(hashImageBytes(CONTENT_KEY, Buffer.from(bytes))).toBe(entry.hash)
            // `source` is what tells a reader whether the hash names the bytes or only the URL they
            // came from. An inlined image is content-addressed, so it must read as `bytes`.
            expect(parseImageRef(expectedRef)).toEqual({
                ...(team === PSEUDO_TEAM ? { pseudoTeam: team } : { teamId: team }),
                hash: entry.hash,
                source: 'bytes',
            })
        }
    )

    it('leaves out what an earlier message produced, and still writes those refs into the lines', async () => {
        // The produce steps claim with refs that JS builds, and the addon drops with refs it builds
        // itself. If the two ever disagree, nothing is dropped and every duplicate crosses into JS.
        rustAddon!.initAnonymizer({ text: [], url: [] })
        const namespace = 'v3:42:2026-09'
        const producedRefDedup = {
            images: new rustAddon!.RefDedupCache(10),
            urls: new rustAddon!.RefDedupCache(10),
            urlTimeBucket: 7,
        }
        const anonymize = () =>
            rustAddon!.anonymizeKafkaPayload(
                imagePayload(['https://cdn.example.com/a.png']),
                undefined,
                namespace,
                CONTENT_KEY,
                'url-key',
                namespace,
                producedRefDedup
            )

        const first = await anonymize()
        const firstMeta = parseJSON(first.meta!) as { urls?: { hash: string; url: string }[] }
        expect(firstMeta.urls).toHaveLength(1)
        const imageRefs = [imageRef(namespace, hashImageBytes(CONTENT_KEY, Buffer.from(PNG_B64, 'base64')))]
        const urlRefs = firstMeta.urls!.map(({ hash }) => `imageurl:${namespace}:${hash}`)
        const urls = firstMeta.urls!.map(({ url }) => url)
        // The anonymize call only reads the caches, so the first message's refs are still unclaimed.
        expect(producedRefDedup.images.claimRefs(imageRefs)).toEqual([true])
        expect(producedRefDedup.urls.claimTransportUrls(urlRefs, urls, 7)).toEqual([true])

        const second = await anonymize()
        expect(second).toMatchObject({
            failed: false,
            images: null,
            dedupedImageCount: 1,
            dedupedUrlCount: 1,
            collectedUrlDomainCount: 1,
        })
        expect((parseJSON(second.meta!) as { urls?: unknown[] }).urls).toBeUndefined()
        expect(second.lines!.toString()).toContain(imageRefs[0])
        expect(second.lines!.toString()).toContain(urlRefs[0])
    })

    it.each([
        ['without the collection keys', () => rustAddon!.anonymizeKafkaPayload(imagePayload())],
        [
            'over maxImageBytes',
            () =>
                rustAddon!.anonymizeKafkaPayload(
                    imagePayload(),
                    undefined,
                    PSEUDO_TEAM,
                    CONTENT_KEY,
                    null,
                    null,
                    undefined,
                    16
                ),
        ],
    ])('collects nothing %s and blanks the image instead', async (_case, anonymize) => {
        rustAddon!.initAnonymizer({ text: [], url: [] })
        const result = await anonymize()
        expect(result.failed).toBe(false)
        expect(result.images).toBeNull()
        expect(result.lines!.toString()).not.toContain(PNG_B64)
        expect(result.lines!.toString()).not.toContain('image:')
    })

    it('requires a team ID only for the inline image key', async () => {
        rustAddon!.initAnonymizer({ text: [], url: [] })
        await expect(
            rustAddon!.anonymizeKafkaPayload(imagePayload(), undefined, undefined, CONTENT_KEY)
        ).rejects.toThrow('contentKey requires teamId')
        await expect(
            rustAddon!.anonymizeKafkaPayload(imagePayload(), undefined, undefined, undefined, CONTENT_KEY)
        ).resolves.toMatchObject({ failed: false })
        await expect(
            rustAddon!.anonymizeKafkaPayload(
                imagePayload(),
                undefined,
                PSEUDO_TEAM,
                CONTENT_KEY,
                null,
                null,
                undefined,
                0
            )
        ).rejects.toThrow('maxImageBytes')
    })

    it('runs either collection lane without the other', async () => {
        // The URL lane measures before any fetch topic exists, so it must not need the image lane.
        rustAddon!.initAnonymizer({ text: [], url: [] })
        const imagesOnly = await rustAddon!.anonymizeKafkaPayload(imagePayload(), undefined, PSEUDO_TEAM, CONTENT_KEY)
        expect(imagesOnly.failed).toBe(false)
        // A pseudonym with neither key collects nothing and is not an error.
        const neither = await rustAddon!.anonymizeKafkaPayload(imagePayload(), undefined, PSEUDO_TEAM, undefined)
        expect(neither.failed).toBe(false)
        expect(neither.lines!.toString()).not.toContain('image:')
    })
})