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.

3.9 KB
Raw Blame
import { brotliCompressSync, deflateSync, gzipSync, zstdCompressSync } from 'node:zlib'

import {
    MAX_UNCOMPRESSED_IMAGE_BYTES,
    SUPPORTED_IMAGE_MEDIA_TYPES,
    imageBytesMatchMediaType,
    prepareFetchedImage,
} from './image-transport'

const AVIF_HEADER = Buffer.concat([
    Buffer.from([0x00, 0x00, 0x00, 0x18]),
    Buffer.from('ftypavif', 'ascii'),
    Buffer.alloc(4),
    Buffer.from('avifmif1', 'ascii'),
])

const imageHeaders = {
    'image/png': Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
    'image/jpeg': Buffer.from([0xff, 0xd8, 0xff, 0xe0]),
    'image/gif': Buffer.from('GIF89a', 'ascii'),
    'image/webp': Buffer.from('RIFF\x04\x00\x00\x00WEBP', 'binary'),
} satisfies Record<(typeof SUPPORTED_IMAGE_MEDIA_TYPES)[number], Buffer>

describe('image transport', () => {
    test.each(SUPPORTED_IMAGE_MEDIA_TYPES)('accepts %s bytes that match the Kafka content-type', (contentType) => {
        expect(imageBytesMatchMediaType(imageHeaders[contentType], contentType)).toBe(true)
    })

    test.each([
        ['gzip', gzipSync],
        ['deflate', deflateSync],
        ['br', brotliCompressSync],
        ['zstd', zstdCompressSync],
    ] as const)('decodes %s response bytes before validating the image', async (contentEncoding, compress) => {
        const png = imageHeaders['image/png']

        await expect(prepareFetchedImage(compress(png), 'image/png', contentEncoding)).resolves.toEqual(png)
    })

    it('decodes stacked content codings in reverse application order', async () => {
        const png = imageHeaders['image/png']
        const encoded = brotliCompressSync(gzipSync(png))

        await expect(prepareFetchedImage(encoded, 'image/png', 'gzip, br')).resolves.toEqual(png)
    })

    it('rejects more than four content coding layers', async () => {
        await expect(
            prepareFetchedImage(
                imageHeaders['image/png'],
                'image/png',
                'identity, identity, identity, identity, identity'
            )
        ).rejects.toMatchObject({
            reason: 'content_encoding_too_deep',
            message: 'content-encoding exceeds 4 codings',
        })
    })

    it('stops decompression when the decoded bytes cross the 20 MiB cap', async () => {
        const compressed = gzipSync(Buffer.alloc(MAX_UNCOMPRESSED_IMAGE_BYTES + 1))

        await expect(prepareFetchedImage(compressed, 'image/png', 'gzip')).rejects.toMatchObject({
            reason: 'decoded_too_large',
            message: `decoded image exceeds ${MAX_UNCOMPRESSED_IMAGE_BYTES} bytes`,
        })
    })

    it.each([
        ['an unsupported content coding', imageHeaders['image/png'], 'compress', 'unsupported_content_encoding'],
        ['a malformed content coding list', imageHeaders['image/png'], 'gzip,', 'malformed_content_encoding'],
        ['compressed bytes that are malformed', Buffer.from('not gzip'), 'gzip', 'decompression_failed'],
    ] as const)('labels %s', async (_case, bytes, contentEncoding, reason) => {
        await expect(prepareFetchedImage(bytes, 'image/png', contentEncoding)).rejects.toMatchObject({ reason })
    })

    it('rejects bytes that do not match the declared media type', async () => {
        await expect(prepareFetchedImage(imageHeaders['image/jpeg'], 'image/png', undefined)).rejects.toMatchObject({
            reason: 'content_type_mismatch',
            message: 'image bytes do not match content-type image/png',
        })
    })

    it.each([
        ['a missing content-type', imageHeaders['image/png'], undefined, 'missing_content_type'],
        ['a BMP content-type', Buffer.from('BM', 'ascii'), 'image/bmp', 'unsupported_content_type'],
        ['an AVIF content-type', AVIF_HEADER, 'image/avif', 'unsupported_content_type'],
    ] as const)('labels %s', async (_case, bytes, contentType, reason) => {
        await expect(prepareFetchedImage(bytes, contentType, undefined)).rejects.toMatchObject({ reason })
    })
})