From ec4e9fb6086f9d61667f4731b5b91a12d037a118 Mon Sep 17 00:00:00 2001 From: Ilya Date: Thu, 22 Oct 2026 22:26:22 +0300 Subject: [PATCH] =?UTF-8?q?lab4:=20=D1=81=D1=85=D0=B5=D0=BC=D0=B0=20=D0=B2?= =?UTF-8?q?=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8=D0=B9,=20=D0=BF=D0=BE?= =?UTF-8?q?=D1=82=D0=BE=D0=BA=D0=BE=D0=B2=D1=8B=D0=B9=20=D0=B2=D0=B0=D0=BB?= =?UTF-8?q?=D0=B8=D0=B4=D0=B0=D1=82=D0=BE=D1=80=20=D0=B8=D0=B7=D0=BE=D0=B1?= =?UTF-8?q?=D1=80=D0=B0=D0=B6=D0=B5=D0=BD=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../attachments/image/image-inspector.spec.ts | 109 +++++ app/src/attachments/image/image-inspector.ts | 387 ++++++++++++++++++ .../database/migrations/005_attachments.sql | 42 ++ app/test/fixtures/sample-progressive.jpg | Bin 0 -> 4891 bytes app/test/fixtures/sample.jpg | Bin 0 -> 4726 bytes app/test/support/png.ts | 95 +++++ 6 files changed, 633 insertions(+) create mode 100644 app/src/attachments/image/image-inspector.spec.ts create mode 100644 app/src/attachments/image/image-inspector.ts create mode 100644 app/src/database/migrations/005_attachments.sql create mode 100644 app/test/fixtures/sample-progressive.jpg create mode 100644 app/test/fixtures/sample.jpg create mode 100644 app/test/support/png.ts 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 0000000000000000000000000000000000000000..eed80152996e3a0dca2a2178deb7e430065cd816 GIT binary patch literal 4891 zcmbVP3n0|l8lUl+sVU8bX&UmFbR!Kt4CB$25k;3w8S*wOkMSJBhT1L}B;=85CuwI0 zJ2g^5Atf!ci#(c4Elct$ZA`mLTe;^yUU9cw?fIYi&-Z=jobUX;*Eutjos&H{!QkM5O+JB{!K5Xnq@*OJ!An|NS_ZKUi2!W{3bkYzX2r^tm=zd|99C(SoIG9u zgTblc@N3qnsHn)TBB&FT)s>W0lp!T>uoNMKkVPV8mE|$=%3s_j@4_%hSOa{!B%A<~ zz`!Lj@X32HJb)^0u$d+Smz09ZAi$U+93~+N24_ZK5Gy4k4MR*mhAoo>qi9Jqu=733 zNVv-y6=v;QrA6u175H^CI6>vRxiMkfZYzObX9X4FMr~>%wa;t~aMdWc6C`c5HapaQ z^Bi55*hAi}KEdYUdGAx`F<>#QWjumFeMXjGMXM zXLO0pzjsug{=JiNlN)quf6&)OoS(~!YOR_xbsal{8|+JuDkyM**ZFS^u;)~CX(vgy zotWBqqrTm*Cu$6d&uj()<3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M~z2VU!8q}0RBDCw3Tu)rBU-i{U$Pb;B zkYK`(2U?P5S>VFYNW4m1`g1c;-pRcx6lfBUf4W)lqmRE=N$EA=V_`tKbAeJK$zcjPPg5=i7lq|{DCEk z)0WI#w!o6~)-g`+>^+P2cM_EHr(cgY_Ma~)3vFKg#K=cXGMgPCvKL6_&D-ht@t6U5 zk7!A&;AmmX=#r17CJ%hxB~QZ8+1cygB@ewYAlul;4RU2y1?)L6g3`Bp*7q7;xH$gJ8R3_ZGvGs~z)zVZwxfTZ`a2)S51Je~f8O{) z`m@1znnp z(&_KD60&M*VpzwHp-_?tt(cf&r)q0i#}XuM7)=HR#M3`|l`N7fZxH9x+>6WOae3_& zw)}&9R`%t7A1#A@|A3hgHqO9hqY`;PZvTE9 znOV&2@$l%WY4q@*gHHG8seS8!5Iv-Uy+EVV9O~S*jGalY>K$A`j;#F$Og*S~bA>77 z%+a3SJGHejF)UV0LW1N9tpwok6*4l)N=js$i9`?GgWhvz88MC;!c(ez&x}Uk}8q@(RIL U;KSoBC2*5A6n@7`Iyx!*AA%PG5dZ)H literal 0 HcmV?d00001 diff --git a/app/test/fixtures/sample.jpg b/app/test/fixtures/sample.jpg new file mode 100644 index 0000000000000000000000000000000000000000..708a5aeef83f6f6e4dc9df6944d19622739dabf0 GIT binary patch literal 4726 zcmb7I4LH>479U2~TTIgwF_;WH*{!tAPgd#!|A|3+|VPOH*Nx6Pfrg&0X`;7n4oVk$lMfOA3WTGNsy5t!yo?I1OK(bCCVYi3S&6h~0DWoO`+PmmPY~828JTZO-wjP{Ru}R@M zx;0^B>trg|kIEh1x>_K?wj$If>EwmxiTnRUW9^`^G8eig?gN4Z%s7m?Z3GpcEmwqK zP+!~RD6~|_xWf=RnFP1zVg8CgfEEC#p_~;!W07GiUze%KKaH!~ExqYzH8Xv}!F+cI z(V-m)(z4dpH2lU!9xTOmtXY^LS2Gno3~^1`!0#7=`7yQgvzm9mpLclqo#RbC^x3ao z6-F@LJ-HS)Af~V#KIvyj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EY^8Ls zsH=-^-3n2(C`gcgc~;LlM|t^X*-&4O1<==!2yZ` zG$O`}%8)lWFA%kif3K5?*g6$EUL)7WVIJ9{7E4`G=%(x zgg)0l$5Fl2C{ZAZ?l#JnpW0~%`@gmc?IN3%!D3vVgVAC5&w%KjR7c=Ui=g(jIsySy g64*8iZ6j$-0nRU3Z4Wm+nv;=@q@iuT9z*K?0+^xq^#A|> literal 0 HcmV?d00001 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); + } +}