diff --git a/app/src/attachments/image/image-inspector.spec.ts b/app/src/attachments/image/image-inspector.spec.ts new file mode 100644 index 0000000..9e74959 --- /dev/null +++ b/app/src/attachments/image/image-inspector.spec.ts @@ -0,0 +1,109 @@ +import { readFileSync } from 'node:fs'; +import { dirname, join } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { describe, expect, it } from 'vitest'; +import { noisePng, pixelBombPng } from '../../../test/support/png.js'; +import { ImageInspector, ImageRejectedError } from './image-inspector.js'; + +const here = dirname(fileURLToPath(import.meta.url)); +const png = readFileSync(join(here, '../../../../tests/fixtures/sample.png')); +const jpeg = readFileSync(join(here, '../../../test/fixtures/sample.jpg')); +const progressive = readFileSync( + join(here, '../../../test/fixtures/sample-progressive.jpg'), +); +const MAX = 40_000_000; + +function inspect(data: Buffer, step = data.length, max = MAX) { + const inspector = new ImageInspector(max); + for (let offset = 0; offset < data.length; offset += step) + inspector.push(data.subarray(offset, offset + step)); + return inspector.finish(); +} + +function rejection(action: () => unknown): ImageRejectedError { + try { + action(); + } catch (error) { + expect(error).toBeInstanceOf(ImageRejectedError); + return error as ImageRejectedError; + } + throw new Error('expected rejection'); +} + +describe('ImageInspector', () => { + it.each([ + ['png', png, 'image/png'], + ['jpeg', jpeg, 'image/jpeg'], + ['progressive jpeg', progressive, 'image/jpeg'], + ])('accepts %s and reports its size', (_name, data, contentType) => { + const expected = { contentType, width: 320, height: 200 }; + expect(inspect(data)).toEqual(expected); + expect(inspect(data, 1)).toEqual(expected); + expect(inspect(data, 7)).toEqual(expected); + }); + + it('rejects truncated files on finish', () => { + for (const data of [png, jpeg, progressive]) + for (const cut of [10, 40, data.length - 100, data.length - 1]) { + const reason = rejection(() => + inspect(data.subarray(0, cut)), + ).reason; + expect(reason).toBe('unsupported_media_type'); + } + }); + + it('rejects a flipped byte inside IDAT by checksum', () => { + const damaged = Buffer.from(png); + damaged[Math.floor(png.length / 2)] ^= 0xff; + expect(rejection(() => inspect(damaged)).reason).toBe( + 'unsupported_media_type', + ); + }); + + it('rejects a pixel bomb right after IHDR, before any IDAT', () => { + const bomb = pixelBombPng(); + const inspector = new ImageInspector(MAX); + // signature + IHDR chunk = 8 + 25 bytes + const reason = rejection(() => + inspector.push(bomb.subarray(0, 33)), + ).reason; + expect(reason).toBe('too_many_pixels'); + expect(inspector.header).toBeUndefined(); + }); + + it('applies the pixel limit to JPEG frame headers', () => { + expect(rejection(() => inspect(jpeg, jpeg.length, 63_999)).reason).toBe( + 'too_many_pixels', + ); + expect(inspect(jpeg, jpeg.length, 64_000).width).toBe(320); + }); + + it('rejects foreign formats and trailing data after IEND', () => { + for (const data of [ + Buffer.from('GIF89a' + 'x'.repeat(40)), + Buffer.from('plain text, not an image'), + Buffer.alloc(0), + ]) + expect(rejection(() => inspect(data)).reason).toBe( + 'unsupported_media_type', + ); + expect( + rejection(() => inspect(Buffer.concat([png, Buffer.from([0])]))) + .reason, + ).toBe('unsupported_media_type'); + }); + + it('accepts padded and noise PNGs of exact sizes', () => { + for (const size of [131_072, 1_048_576]) { + const data = noisePng(size); + expect(data.length).toBe(size); + expect(inspect(data).width).toBe(1024); + } + }); + + it('tolerates trailing data after JPEG EOI', () => { + expect( + inspect(Buffer.concat([jpeg, Buffer.from('MPF trailer')])).height, + ).toBe(200); + }); +}); diff --git a/app/src/attachments/image/image-inspector.ts b/app/src/attachments/image/image-inspector.ts new file mode 100644 index 0000000..eb04126 --- /dev/null +++ b/app/src/attachments/image/image-inspector.ts @@ -0,0 +1,387 @@ +import { crc32 } from 'node:zlib'; + +export const IMAGE_HEADER_MAX_BYTES = 1_048_576; + +export type ImageHeader = { + contentType: 'image/png' | 'image/jpeg'; + width: number; + height: number; +}; + +export class ImageRejectedError extends Error { + constructor( + readonly reason: 'unsupported_media_type' | 'too_many_pixels', + message: string, + ) { + super(message); + this.name = 'ImageRejectedError'; + } +} + +const PNG_SIGNATURE = Buffer.from([ + 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, +]); +const PNG_MAX_DIMENSION = 2 ** 31 - 1; +const PNG_BIT_DEPTHS: Record = { + 0: [1, 2, 4, 8, 16], + 2: [8, 16], + 3: [1, 2, 4, 8], + 4: [8, 16], + 6: [8, 16], +}; +const SOF_MARKERS = new Set([ + 0xc0, 0xc1, 0xc2, 0xc3, 0xc5, 0xc6, 0xc7, 0xc9, 0xca, 0xcb, 0xcd, 0xce, + 0xcf, +]); + +type State = + | 'start' + // PNG + | 'png_chunk_head' + | 'png_ihdr' + | 'png_skip' + | 'png_crc' + | 'png_done' + // JPEG + | 'jpeg_marker' + | 'jpeg_code' + | 'jpeg_length' + | 'jpeg_sof' + | 'jpeg_skip' + | 'jpeg_entropy' + | 'jpeg_entropy_ff' + | 'jpeg_trailer'; + +function unsupported(message: string): ImageRejectedError { + return new ImageRejectedError('unsupported_media_type', message); +} + +/** + * Streaming structural validator for PNG and JPEG. It never buffers the body: + * only fixed-size fields (chunk headers, IHDR, SOF) are accumulated, and + * nothing is decompressed. + */ +export class ImageInspector { + private state: State = 'start'; + private imageHeader: ImageHeader | undefined; + + // Accumulator for fixed-length fields. + private field = Buffer.alloc(16); + private fieldNeed = 0; + private fieldLength = 0; + + // Remaining bytes to skip in png_skip / jpeg_skip. + private skip = 0; + + // PNG + private chunkType = ''; + private chunkLength = 0; + private crc = 0; + private firstChunk = true; + private seenIdat = false; + + // JPEG + private marker = 0; + private scans = 0; + private afterSkip: State = 'jpeg_marker'; + + constructor(private readonly maxPixels: number) {} + + get header(): ImageHeader | undefined { + return this.imageHeader; + } + + push(chunk: Buffer): void { + let offset = 0; + while (offset < chunk.length) offset = this.step(chunk, offset); + } + + finish(): ImageHeader { + const done = this.state === 'png_done' || this.state === 'jpeg_trailer'; + if (!done || this.imageHeader === undefined) + throw unsupported('Image data is incomplete'); + return this.imageHeader; + } + + private collect(need: number): void { + this.fieldNeed = need; + this.fieldLength = 0; + } + + /** Copies up to fieldNeed bytes; returns new offset and whether complete. */ + private fill(chunk: Buffer, offset: number): [number, boolean] { + const take = Math.min( + this.fieldNeed - this.fieldLength, + chunk.length - offset, + ); + chunk.copy(this.field, this.fieldLength, offset, offset + take); + this.fieldLength += take; + return [offset + take, this.fieldLength === this.fieldNeed]; + } + + private step(chunk: Buffer, offset: number): number { + switch (this.state) { + case 'start': + return this.stepStart(chunk, offset); + case 'png_chunk_head': + case 'png_ihdr': + case 'png_crc': + case 'jpeg_length': + case 'jpeg_sof': { + const [next, complete] = this.fill(chunk, offset); + if (complete) this.fieldDone(); + return next; + } + case 'png_skip': + case 'jpeg_skip': { + const take = Math.min(this.skip, chunk.length - offset); + if (this.state === 'png_skip') + this.crc = crc32( + chunk.subarray(offset, offset + take), + this.crc, + ); + this.skip -= take; + if (this.skip === 0) this.skipDone(); + return offset + take; + } + case 'png_done': + throw unsupported('Data after the PNG end chunk'); + case 'jpeg_marker': + if (chunk[offset] !== 0xff) + throw unsupported('Unexpected byte between JPEG segments'); + this.state = 'jpeg_code'; + return offset + 1; + case 'jpeg_code': + this.onMarkerCode(chunk[offset]); + return offset + 1; + case 'jpeg_entropy': { + const next = chunk.indexOf(0xff, offset); + if (next === -1) return chunk.length; + this.state = 'jpeg_entropy_ff'; + return next + 1; + } + case 'jpeg_entropy_ff': { + const byte = chunk[offset]; + if (byte === 0xff) return offset + 1; + if (byte === 0x00 || (byte >= 0xd0 && byte <= 0xd7)) { + this.state = 'jpeg_entropy'; + return offset + 1; + } + this.onMarkerCode(byte); + return offset + 1; + } + case 'jpeg_trailer': + return chunk.length; + } + } + + private stepStart(chunk: Buffer, offset: number): number { + // Accumulate until the type is certain: 3 bytes for JPEG, 8 for PNG. + if (this.fieldLength === 0 && this.fieldNeed === 0) this.collect(8); + const have = this.fieldLength; + const take = Math.min(this.fieldNeed - have, chunk.length - offset); + chunk.copy(this.field, have, offset, offset + take); + this.fieldLength += take; + const first = this.field[0]; + if (first === 0xff) { + if (this.fieldLength >= 3) { + if (this.field[1] !== 0xd8 || this.field[2] !== 0xff) + throw unsupported('Not a JPEG file'); + // The third byte opens the next marker. + this.state = 'jpeg_code'; + this.fieldNeed = 0; + this.fieldLength = 0; + return offset + (3 - have); + } + return offset + take; + } + if (first === PNG_SIGNATURE[0]) { + for (let index = 0; index < this.fieldLength; index += 1) + if (this.field[index] !== PNG_SIGNATURE[index]) + throw unsupported('Not a PNG file'); + if (this.fieldLength === 8) { + this.state = 'png_chunk_head'; + this.collect(8); + } + return offset + take; + } + throw unsupported('Unsupported image format'); + } + + private fieldDone(): void { + switch (this.state) { + case 'png_chunk_head': + this.onPngChunkHead(); + return; + case 'png_ihdr': + this.crc = crc32(this.field.subarray(0, 13), this.crc); + this.parseIhdr(); + this.state = 'png_crc'; + this.collect(4); + return; + case 'png_crc': { + const expected = this.field.readUInt32BE(0); + if (this.crc >>> 0 !== expected) + throw unsupported('PNG chunk checksum mismatch'); + this.onPngChunkEnd(); + return; + } + case 'jpeg_length': + this.onJpegLength(); + return; + case 'jpeg_sof': + this.onJpegSof(); + return; + default: + return; + } + } + + private skipDone(): void { + if (this.state === 'png_skip') { + this.state = 'png_crc'; + this.collect(4); + } else { + this.state = this.afterSkip; + } + } + + // ---- PNG ---- + + private onPngChunkHead(): void { + const length = this.field.readUInt32BE(0); + const type = this.field.toString('latin1', 4, 8); + if (length > PNG_MAX_DIMENSION) + throw unsupported('PNG chunk is too long'); + if (!/^[A-Za-z]{4}$/u.test(type)) + throw unsupported('PNG chunk type is invalid'); + if (this.firstChunk) { + if (type !== 'IHDR' || length !== 13) + throw unsupported('PNG must start with IHDR'); + this.firstChunk = false; + } else if (type === 'IHDR') { + throw unsupported('Duplicate PNG IHDR'); + } + if (type === 'IEND') { + if (length !== 0 || !this.seenIdat) + throw unsupported('PNG IEND is invalid'); + } + if (type === 'IDAT') this.seenIdat = true; + this.chunkType = type; + this.chunkLength = length; + this.crc = crc32(this.field.subarray(4, 8), 0); + if (type === 'IHDR') { + this.state = 'png_ihdr'; + this.collect(13); + } else if (length === 0) { + this.state = 'png_crc'; + this.collect(4); + } else { + this.state = 'png_skip'; + this.skip = length; + } + } + + private parseIhdr(): void { + const width = this.field.readUInt32BE(0); + const height = this.field.readUInt32BE(4); + const bitDepth = this.field[8]; + const colorType = this.field[9]; + if (width < 1 || width > PNG_MAX_DIMENSION) + throw unsupported('PNG width is invalid'); + if (height < 1 || height > PNG_MAX_DIMENSION) + throw unsupported('PNG height is invalid'); + if (!PNG_BIT_DEPTHS[colorType]?.includes(bitDepth)) + throw unsupported('PNG color type / bit depth is invalid'); + if (this.field[10] !== 0 || this.field[11] !== 0) + throw unsupported('PNG compression/filter method is invalid'); + if (this.field[12] > 1) + throw unsupported('PNG interlace method is invalid'); + this.pendingHeader = { contentType: 'image/png', width, height }; + } + + private pendingHeader: ImageHeader | undefined; + + private onPngChunkEnd(): void { + if (this.chunkType === 'IHDR' && this.pendingHeader) { + const { width, height } = this.pendingHeader; + if (width * height > this.maxPixels) + throw new ImageRejectedError( + 'too_many_pixels', + 'Image has too many pixels', + ); + this.imageHeader = this.pendingHeader; + } + if (this.chunkType === 'IEND') { + this.state = 'png_done'; + return; + } + this.state = 'png_chunk_head'; + this.collect(8); + } + + // ---- JPEG ---- + + private onMarkerCode(code: number): void { + if (code === 0xff) return; // fill byte + this.marker = code; + if (code === 0x00 || code === 0xd8) + throw unsupported('Unexpected JPEG marker'); + if (code === 0x01 || (code >= 0xd0 && code <= 0xd7)) { + this.state = 'jpeg_marker'; + return; + } + if (code === 0xd9) { + if (this.imageHeader === undefined || this.scans === 0) + throw unsupported('JPEG ended before image data'); + this.state = 'jpeg_trailer'; + return; + } + if (code === 0xda && this.imageHeader === undefined) + throw unsupported('JPEG scan before frame header'); + this.state = 'jpeg_length'; + this.collect(2); + } + + private onJpegLength(): void { + const length = this.field.readUInt16BE(0); + if (length < 2) throw unsupported('JPEG segment length is invalid'); + const dataLength = length - 2; + if (SOF_MARKERS.has(this.marker)) { + if (this.imageHeader !== undefined || this.pendingHeader) + throw unsupported('Duplicate JPEG frame header'); + if (dataLength < 6) throw unsupported('JPEG frame header is short'); + this.skip = dataLength - 6; + this.afterSkip = 'jpeg_marker'; + this.state = 'jpeg_sof'; + this.collect(6); + return; + } + if (this.marker === 0xda) { + this.scans += 1; + this.afterSkip = 'jpeg_entropy'; + } else { + this.afterSkip = 'jpeg_marker'; + } + if (dataLength === 0) this.state = this.afterSkip; + else { + this.skip = dataLength; + this.state = 'jpeg_skip'; + } + } + + private onJpegSof(): void { + const height = this.field.readUInt16BE(1); + const width = this.field.readUInt16BE(3); + if (width === 0 || height === 0 || this.field[5] === 0) + throw unsupported('JPEG dimensions are invalid'); + if (width * height > this.maxPixels) + throw new ImageRejectedError( + 'too_many_pixels', + 'Image has too many pixels', + ); + this.imageHeader = { contentType: 'image/jpeg', width, height }; + if (this.skip === 0) this.state = 'jpeg_marker'; + else this.state = 'jpeg_skip'; + } +} diff --git a/app/src/database/migrations/005_attachments.sql b/app/src/database/migrations/005_attachments.sql new file mode 100644 index 0000000..a6c9d81 --- /dev/null +++ b/app/src/database/migrations/005_attachments.sql @@ -0,0 +1,42 @@ +ALTER TABLE messages ADD CONSTRAINT messages_id_chat_key UNIQUE (id, chat_id); + +CREATE TABLE attachments ( + id uuid PRIMARY KEY, + chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + owner_id uuid NOT NULL REFERENCES users(id) ON DELETE RESTRICT, + filename text NOT NULL CHECK (char_length(filename) BETWEEN 1 AND 255), + content_type text NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg')), + upload_mode text NOT NULL CHECK (upload_mode IN ('proxy', 'direct')), + status text NOT NULL CHECK (status IN ('pending', 'ready', 'deleting')), + object_key text NOT NULL UNIQUE, + size_bytes bigint CHECK (size_bytes BETWEEN 1 AND 10485760), + sha256 text CHECK (sha256 ~ '^[a-f0-9]{64}$'), + width integer CHECK (width > 0), + height integer CHECK (height > 0), + expires_at timestamptz(3), + created_at timestamptz(3) NOT NULL DEFAULT now(), + updated_at timestamptz(3) NOT NULL DEFAULT now(), + CONSTRAINT attachments_id_chat_key UNIQUE (id, chat_id), + CONSTRAINT attachments_owner_member_fk FOREIGN KEY (chat_id, owner_id) + REFERENCES chat_members (chat_id, user_id), + CONSTRAINT attachments_ready_complete CHECK (status <> 'ready' OR + (size_bytes IS NOT NULL AND sha256 IS NOT NULL AND width IS NOT NULL AND height IS NOT NULL)), + CONSTRAINT attachments_unfinished_expiry CHECK (status = 'ready' OR expires_at IS NOT NULL) +); +CREATE INDEX attachments_unfinished_idx ON attachments (expires_at) WHERE status <> 'ready'; +CREATE INDEX attachments_owner_direct_pending_idx ON attachments (owner_id) + WHERE status = 'pending' AND upload_mode = 'direct'; + +CREATE TABLE message_attachments ( + message_id uuid NOT NULL, + chat_id uuid NOT NULL, + attachment_id uuid NOT NULL, + ordinal smallint NOT NULL CHECK (ordinal BETWEEN 1 AND 10), + PRIMARY KEY (message_id, attachment_id), + CONSTRAINT message_attachments_ordinal_key UNIQUE (message_id, ordinal), + CONSTRAINT message_attachments_message_fk FOREIGN KEY (message_id, chat_id) + REFERENCES messages (id, chat_id) ON DELETE CASCADE, + CONSTRAINT message_attachments_attachment_fk FOREIGN KEY (attachment_id, chat_id) + REFERENCES attachments (id, chat_id) +); +CREATE INDEX message_attachments_attachment_idx ON message_attachments (attachment_id); diff --git a/app/test/fixtures/sample-progressive.jpg b/app/test/fixtures/sample-progressive.jpg new file mode 100644 index 0000000..eed8015 Binary files /dev/null and b/app/test/fixtures/sample-progressive.jpg differ diff --git a/app/test/fixtures/sample.jpg b/app/test/fixtures/sample.jpg new file mode 100644 index 0000000..708a5ae Binary files /dev/null and b/app/test/fixtures/sample.jpg differ diff --git a/app/test/support/png.ts b/app/test/support/png.ts new file mode 100644 index 0000000..9dd29a4 --- /dev/null +++ b/app/test/support/png.ts @@ -0,0 +1,95 @@ +import { randomBytes } from 'node:crypto'; +import { crc32, deflateSync } from 'node:zlib'; + +const SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]); + +function chunk(type: string, data: Buffer): Buffer { + const head = Buffer.alloc(8); + head.writeUInt32BE(data.length, 0); + head.write(type, 4, 'latin1'); + const crc = Buffer.alloc(4); + crc.writeUInt32BE(crc32(Buffer.concat([head.subarray(4), data])) >>> 0, 0); + return Buffer.concat([head, data, crc]); +} + +export function buildPng(options: { + width: number; + height: number; + colorType: number; + bitDepth: number; + idat: Buffer; +}): Buffer { + const ihdr = Buffer.alloc(13); + ihdr.writeUInt32BE(options.width, 0); + ihdr.writeUInt32BE(options.height, 4); + ihdr[8] = options.bitDepth; + ihdr[9] = options.colorType; + return Buffer.concat([ + SIGNATURE, + chunk('IHDR', ihdr), + chunk('IDAT', options.idat), + chunk('IEND', Buffer.alloc(0)), + ]); +} + +/** Inserts a private ancillary chunk before IEND to reach exactly `size` bytes. */ +export function padPngTo(png: Buffer, size: number): Buffer { + const extra = size - png.length; + if (extra === 0) return png; + if (extra < 12) throw new Error('PNG padding needs at least 12 bytes'); + const iend = png.length - 12; + return Buffer.concat([ + png.subarray(0, iend), + chunk('prVt', Buffer.alloc(extra - 12)), + png.subarray(iend), + ]); +} + +/** 8000x5001 grayscale 1-bit, all-zero rows: tiny file, 40 008 000 pixels. */ +export function pixelBombPng(): Buffer { + const width = 8000; + const height = 5001; + const row = Buffer.alloc(1 + Math.ceil(width / 8)); + const raw = Buffer.alloc(row.length * height); + return buildPng({ + width, + height, + colorType: 0, + bitDepth: 1, + idat: deflateSync(raw, { level: 9 }), + }); +} + +const NOISE_WIDTH = 1024; +const NOISE_ROW = 1 + NOISE_WIDTH * 3; + +function noiseOfHeight(height: number): Buffer { + const raw = randomBytes(NOISE_ROW * height); + for (let row = 0; row < height; row += 1) raw[row * NOISE_ROW] = 0; + return buildPng({ + width: NOISE_WIDTH, + height, + colorType: 2, + bitDepth: 8, + idat: deflateSync(raw, { level: 0 }), + }); +} + +/** Incompressible, structurally valid PNG of exactly `size` bytes. */ +export function noisePng(size: number): Buffer { + let height = Math.max(1, Math.floor(size / NOISE_ROW)); + for (;;) { + const png = noiseOfHeight(height); + if (png.length > size) { + height -= 1; + if (height < 1) throw new Error('size is too small for noisePng'); + continue; + } + const extra = size - png.length; + if (extra > 0 && extra < 12) { + height -= 1; + continue; + } + return padPngTo(png, size); + } +}