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.

10.3 KB
Raw Blame
import { Server, createServer, request as httpRequest } from 'http'
import { AddressInfo } from 'net'
import { register } from 'prom-client'
import { gzipSync } from 'zlib'

import { FetchResponse, internalFetch } from '~/common/utils/request'

import { createPushSubscriptionsHandler } from './push-subscriptions-http'
import { PushHandlerResult, PushSubscriptionsService } from './push-subscriptions.service'

describe('push subscriptions http', () => {
    let server: Server
    let base: string
    let seen: {
        method: string
        body: string
        contentType?: string
        compression?: string | null
        mirrored?: boolean
    }[]
    let answer: PushHandlerResult
    let failure: Error | null
    let regionBlocked: boolean

    beforeEach(async () => {
        seen = []
        answer = { status: 200, body: { distinct_id: 'user-1' } }
        failure = null
        regionBlocked = false
        const service = {
            handle: (request: any) => {
                seen.push({
                    method: request.method,
                    body: request.body.toString('utf8'),
                    contentType: request.contentType,
                    compression: request.query?.get('compression') ?? null,
                    mirrored: request.mirrored,
                })
                return failure ? Promise.reject(failure) : Promise.resolve(answer)
            },
        } as unknown as PushSubscriptionsService

        const handler = createPushSubscriptionsHandler(service, () => regionBlocked)
        server = createServer(
            (req, res) =>
                void handler(req, res).catch(() => {
                    res.writeHead(500).end()
                })
        )
        await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve))
        base = `http://127.0.0.1:${(server.address() as AddressInfo).port}`
    })

    afterEach(async () => {
        await new Promise((resolve) => server.close(resolve))
    })

    const post = (path: string, init: Record<string, any> = {}): Promise<FetchResponse> =>
        internalFetch(`${base}${path}`, { method: 'POST', ...init })

    it.each([
        ['us.i.posthog.com-shadow', true],
        ['us.i.posthog.com', false],
        ['shadow.example.com', false],
    ])('marks a request with Host %s as mirrored: %s', async (host, mirrored) => {
        await new Promise<void>((resolve, reject) => {
            const req = httpRequest(
                `${base}/api/push_subscriptions/`,
                { method: 'POST', headers: { Host: host, 'Content-Type': 'application/json' } },
                (res) => res.resume().on('end', resolve)
            )
            req.on('error', reject)
            req.end('{}')
        })

        expect(seen[0].mirrored).toEqual(mirrored)
    })

    it.each([
        ['POST', false],
        ['OPTIONS', false],
        ['DELETE', true],
    ])(
        'answers %s from a blocked region with 403, and reaches the service only to unregister: %s',
        async (method, reaches) => {
            regionBlocked = true

            const response = await internalFetch(`${base}/api/push_subscriptions/`, {
                method,
                headers: { 'Content-Type': 'application/json' },
                body: method === 'OPTIONS' ? undefined : JSON.stringify({ api_key: 'phc_x' }),
            })

            expect(response.status).toEqual(403)
            expect(await response.text()).toContain('not available in your region')
            expect(seen.map((request) => request.method)).toEqual(reaches ? [method] : [])
        }
    )

    it('delivers a DELETE body to the service', async () => {
        // The reason this endpoint is not on the plugin server's express framework: that one drops
        // the body on DELETE, and DELETE with a body is how every released SDK unregisters a device.
        const body = JSON.stringify({ api_key: 'phc_x', app_id: 'a' })

        const response = await internalFetch(`${base}/api/push_subscriptions/`, {
            method: 'DELETE',
            headers: { 'Content-Type': 'application/json' },
            body,
        })

        expect(response.status).toEqual(200)
        expect(seen).toEqual([
            { method: 'DELETE', body, contentType: 'application/json', compression: null, mirrored: false },
        ])
    })

    it('passes the content type and query through untouched', async () => {
        await post('/api/push_subscriptions/?compression=gzip', {
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            body: 'data=x',
        })

        expect(seen[0]).toMatchObject({
            contentType: 'application/x-www-form-urlencoded',
            compression: 'gzip',
        })
    })

    it('hands the gzipped bytes over without decoding them itself', async () => {
        const gzipped = gzipSync(Buffer.from('{"a":1}'))

        await post('/api/push_subscriptions/', {
            headers: { 'Content-Encoding': 'gzip', 'Content-Type': 'application/json' },
            body: gzipped,
        })

        expect(Buffer.from(seen[0].body, 'utf8').length).toBeGreaterThan(0)
    })

    it.each([['/api/push_subscriptions'], ['/api/push_subscriptions/']])('serves %s', async (path) => {
        const response = await post(path, { body: '{}' })

        expect(response.status).toEqual(200)
    })

    it.each([['/api/push_subscriptions/extra'], ['//']])('does not serve %s', async (path) => {
        const response = await post(path, { body: '{}' })

        expect(response.status).toEqual(404)
        expect(seen).toEqual([])
    })

    it('answers a preflight without calling the service', async () => {
        const response = await internalFetch(`${base}/api/push_subscriptions/`, {
            method: 'OPTIONS',
            headers: { Origin: 'https://app.example.com' },
        })

        expect(response.status).toEqual(200)
        expect(response.headers['access-control-allow-origin']).toEqual('*')
        expect(seen).toEqual([])
    })

    it('advertises the verbs it serves, including the one django omits', async () => {
        // A browser preflight for the unregister call is refused when DELETE is missing here.
        const response = await internalFetch(`${base}/api/push_subscriptions/`, {
            method: 'OPTIONS',
            headers: { Origin: 'https://app.example.com' },
        })

        expect(response.headers['access-control-allow-methods']).toEqual('GET, POST, DELETE, OPTIONS')
    })

    it('never lets another site make a credentialed call', async () => {
        const response = await post('/api/push_subscriptions/', {
            headers: { Origin: 'https://evil.example' },
            body: '{}',
        })

        expect(response.headers['access-control-allow-origin']).toEqual('*')
        expect(response.headers['access-control-allow-credentials']).toBeUndefined()
    })

    it('stops reading once a body passes the limit, and still answers', async () => {
        answer = { status: 413, body: { type: 'validation_error', code: 'request_too_large', detail: 'x', attr: null } }
        const oversized = Buffer.alloc(64 * 1024, 'a')

        const response = await post('/api/push_subscriptions/', { body: oversized })

        expect(response.status).toEqual(413)
        // One byte past the limit is enough for the service to see it is over; nothing larger is held.
        expect(seen[0].body.length).toEqual(16 * 1024 + 1)
    })

    it('returns the rejection body the service produced', async () => {
        answer = {
            status: 401,
            body: {
                type: 'authentication_error',
                code: 'invalid_api_key',
                detail: 'Invalid project token.',
                attr: null,
            },
            rejection: { code: 'invalid_api_key', apiKeyFingerprint: 'abc123' },
        }

        const response = await post('/api/push_subscriptions/', { body: '{}' })

        expect(response.status).toEqual(401)
        expect(await response.json()).toEqual({
            type: 'authentication_error',
            code: 'invalid_api_key',
            detail: 'Invalid project token.',
            attr: null,
        })
    })

    it('sends the headers the service set', async () => {
        answer = {
            status: 429,
            body: { type: 'throttled_error', code: 'capture_billing_limited', detail: 'Failed.', attr: null },
            headers: { 'Retry-After': '3600' },
        }

        const response = await post('/api/push_subscriptions/', { body: '{}' })

        expect(response.status).toEqual(429)
        expect(response.headers['retry-after']).toEqual('3600')
        expect(response.headers['content-type']).toEqual('application/json')
    })

    it('counts a request the service throws on as a 500', async () => {
        const failedRequests = async (): Promise<number> => {
            const metric = await register.getSingleMetric('push_subscription_request_duration_seconds')!.get()
            return metric.values
                .filter(
                    (v) => 'metricName' in v && String(v.metricName).endsWith('_count') && v.labels.status === '500'
                )
                .reduce((sum, v) => sum + v.value, 0)
        }
        const before = await failedRequests()
        failure = new Error('postgres is down')

        const response = await post('/api/push_subscriptions/', { body: '{}' })

        expect(response.status).toEqual(500)
        expect((await failedRequests()) - before).toEqual(1)
    })

    it('does not hang when the client disconnects mid-body', async () => {
        // Announce a body and then drop the socket. Without a close handler the read never settles
        // and the handler is retained for the life of the process.
        await new Promise<void>((resolve) => {
            const req = httpRequest(
                {
                    host: '127.0.0.1',
                    port: (server.address() as AddressInfo).port,
                    path: '/api/push_subscriptions/',
                    method: 'POST',
                },
                () => undefined
            )
            req.setHeader('Content-Length', '1000')
            req.on('error', () => resolve())
            req.write('x'.repeat(10))
            req.destroy()
            setTimeout(resolve, 100)
        })

        const response = await post('/api/push_subscriptions/', { body: '{}' })
        expect(response.status).toEqual(200)
    })
})