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.

5.4 KB
Raw Blame
import { readFileSync } from 'fs'
import { join } from 'path'

import { parseJSON } from '~/common/utils/json-parse'

import { MAX_USER_AGENT_LENGTH, flattenProperties, parseUserAgent } from './transformation-functions'

const CHROME_UA =
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'

// The Rust HogVM binding asserts the same cases against its port, so both implementations agree.
const USER_AGENT_CASES: { label: string; userAgent: string; expected: ReturnType<typeof parseUserAgent> }[] = parseJSON(
    readFileSync(join(__dirname, '../../../../rust/common/hogvm/node/tests/static/user_agent_cases.json'), 'utf8')
)

describe('transformation-functions', () => {
    describe('parseUserAgent', () => {
        it.each(USER_AGENT_CASES)('parses $label', ({ userAgent, expected }) => {
            expect(parseUserAgent(userAgent)).toEqual(expected)
        })

        it.each([
            ['a non-string', 42],
            ['an empty string', ''],
        ])('returns null for %s', (_label, value) => {
            expect(parseUserAgent(value)).toBeNull()
        })

        it('returns null past the length bound without parsing', () => {
            // Repeated `Chrom` tokens are the adversarial shape: detect-browser backtracks
            // quadratically on them, so an unbounded value would stall the whole worker.
            const hostile = 'Chrom'.repeat((100 * 1024) / 5)

            const start = process.hrtime.bigint()
            expect(parseUserAgent(hostile)).toBeNull()
            const durationMs = Number(process.hrtime.bigint() - start) / 1e6

            // Generous next to the ~1800ms an unbounded parse of this value costs, but tight enough
            // to fail if the value ever reaches detect-browser again.
            expect(durationMs).toBeLessThan(100)
        })

        it('parses a hostile value at the accepted maximum length quickly', () => {
            // The adversarial `Chrom` pattern at exactly the accepted maximum still reaches
            // detect-browser, so the bound must be low enough that even this stays cheap.
            const hostileAtMax = 'Chrom'.repeat(Math.floor(MAX_USER_AGENT_LENGTH / 5))

            const start = process.hrtime.bigint()
            parseUserAgent(hostileAtMax)
            const durationMs = Number(process.hrtime.bigint() - start) / 1e6

            expect(durationMs).toBeLessThan(10)
        })

        it('still parses a user agent at the length bound', () => {
            const padded = CHROME_UA + ' '.repeat(MAX_USER_AGENT_LENGTH - CHROME_UA.length)

            expect(parseUserAgent(padded)?.browser).toBe('chrome')
        })
    })

    describe('flattenProperties', () => {
        it('flattens nested objects and arrays, keeping originals and matching the legacy plugin', () => {
            expect(flattenProperties({ a: { b: { c: 1 } }, w: { arr: [{ z: 2 }] }, x: 'plain' }, '__')).toEqual({
                a: { b: { c: 1 } },
                w: { arr: [{ z: 2 }] },
                x: 'plain',
                a__b__c: 1,
                w__arr__0__z: 2,
            })
        })

        it('emits a nested empty object as a leaf but not an empty array', () => {
            expect(flattenProperties({ a: { b: {} }, list: { items: [] } }, '__')).toEqual({
                a: { b: {} },
                list: { items: [] },
                a__b: {},
            })
        })

        it('flattens within $set under a fresh prefix and skips internal properties', () => {
            const result = flattenProperties(
                { $set: { user: { id: 7 } }, $elements: { keep: 'nested' } },
                '__'
            ) as Record<string, any>

            expect(result.$set.user__id).toBe(7)
            expect(result.$elements__keep).toBeUndefined()
        })

        it('stays linear for a wide payload', () => {
            const wide: Record<string, any> = {}
            for (let i = 0; i < 50_000; i++) {
                wide[`k${i}`] = { nested: i }
            }
            const start = process.hrtime.bigint()
            const result = flattenProperties({ outer: wide }, '__') as Record<string, any>
            const durationMs = Number(process.hrtime.bigint() - start) / 1e6

            expect(result.outer__k49999__nested).toBe(49999)
            expect(durationMs).toBeLessThan(500)
        })

        it('falls back to the default separator when the configured one is too long', () => {
            // A huge separator would otherwise be copied into every flattened key.
            const result = flattenProperties({ a: { b: 1 } }, 'x'.repeat(200)) as Record<string, any>

            expect(result.a__b).toBe(1)
        })

        it('bounds total key bytes when a long property name sits above many leaves', () => {
            // A ~5000-char name over 2000 leaves would produce ~10MB of key strings; the byte
            // budget truncates before every leaf is flattened.
            const longName = 'p'.repeat(5000)
            const leaves = Array.from({ length: 2000 }, (_, i) => ({ x: i }))

            const result = flattenProperties({ [longName]: leaves }, '__') as Record<string, any>
            const flattenedKeys = Object.keys(result).filter((k) => k.startsWith(`${longName}__`))

            expect(flattenedKeys.length).toBeLessThan(2000)
        })

        it('returns the input unchanged when it is not an object', () => {
            expect(flattenProperties('nope', '__')).toBe('nope')
        })
    })
})