import { createHash, randomBytes, randomUUID } from 'node:crypto'; import { readFileSync } from 'node:fs'; import net from 'node:net'; import { setTimeout as sleep } from 'node:timers/promises'; import { afterAll, beforeAll, describe, expect, it } from 'vitest'; import type { Pool } from 'pg'; import type { ApiErrorBody } from '../src/common/errors/api-error.js'; import type { Page } from '../src/common/http/page.js'; import type { Message } from '../src/messages/message.js'; import { Actor, actor, baseUrl, bearer, createChat, database, download, expectError, http, postMessage, upload, uploadRaw, waitReady, } from './support/e2e.js'; import sharp from 'sharp'; import { jpegWithMetadata, jpegWithoutScanData } from './support/jpeg.js'; import { maxPixelsRgba16Png, noisePng, padPngTo, pixelBombPng, pngWithGarbageIdat, } from './support/png.js'; const png = readFileSync( new URL('../../tests/fixtures/sample.png', import.meta.url), ); const jpeg = readFileSync(new URL('./fixtures/sample.jpg', import.meta.url)); const progressive = readFileSync( new URL('./fixtures/sample-progressive.jpg', import.meta.url), ); const MAX = 10_485_760; let db: Pool; let alice: Actor; let bob: Actor; let eve: Actor; let chatId: string; let eveChatId: string; let leftoverChatId: string; const sha256 = (data: Uint8Array) => createHash('sha256').update(data).digest('hex'); async function ready(data: Buffer, type = 'image/png', chat = chatId) { const result = await upload(alice.token, chat, data, { contentType: type, }); expect(result.response.status).toBe(202); expect(result.body.status).toBe('queued'); return waitReady(alice.token, result.body.id); } describe('attachments (proxy upload)', () => { beforeAll(async () => { db = database(); alice = await actor(); bob = await actor(); eve = await actor(); chatId = (await createChat(alice, [bob])).id; eveChatId = (await createChat(eve, [])).id; // Chat for rejected uploads: it must end up without any row. leftoverChatId = (await createChat(alice, [])).id; }); afterAll(async () => { await db.end(); }); it('stores a PNG and serves it only to chat members', async () => { const uploaded = await upload(alice.token, chatId, png, { filename: 'Снимок.png', }); expect(uploaded.response.status).toBe(202); expect(uploaded.body).toMatchObject({ chat_id: chatId, owner_id: alice.user.id, filename: 'Снимок.png', content_type: 'image/png', size_bytes: png.length, sha256: sha256(png), status: 'queued', variants: [], error_code: null, }); const id = uploaded.body.id; const ready = await waitReady(bob.token, id); expect(ready).toMatchObject({ filename: 'Снимок.png', size_bytes: png.length, sha256: sha256(png), status: 'ready', error_code: null, }); expect(ready.variants.map((variant) => variant.name)).toEqual([ 'thumbnail', 'crop', 'optimized', ]); const metadata = await http(`/api/v1/attachments/${id}`, { headers: bearer(bob.token), }); expect(metadata.response.status).toBe(200); expect(metadata.body.sha256).toBe(sha256(png)); const content = await download(bob.token, id); expect(content.response.status).toBe(200); expect(content.response.headers.get('content-type')).toBe('image/png'); expect(content.response.headers.get('x-content-type-options')).toBe( 'nosniff', ); expect(content.response.headers.get('content-length')).toBe( String(png.length), ); expect(content.response.headers.get('content-disposition')).toContain( "filename*=UTF-8''%D0%A1", ); expect(content.bytes.equals(png)).toBe(true); for (const path of [`/${id}`, `/${id}/content`]) { expectError( await http(`/api/v1/attachments${path}`, { headers: bearer(eve.token), }), 404, 'not_found', ); } expectError( await http(`/api/v1/attachments/${id}/content`), 401, 'unauthorized', ); expectError(await upload(eve.token, chatId, png), 404, 'not_found'); expectError( await http(`/api/v1/attachments/${randomUUID()}`, { headers: bearer(bob.token), }), 404, 'not_found', ); }); it.each([ ['JPEG', jpeg], ['progressive JPEG', progressive], ])('accepts a %s', async (_name, data) => { const created = await ready(data, 'image/jpeg'); expect(created.content_type).toBe('image/jpeg'); const content = await download(alice.token, created.id); expect(content.bytes.equals(data)).toBe(true); }); it('accepts camera-size images up to the pixel limit', async () => { // 7728x5152 = 39.8 MP, like a 40 MP camera; decoded size far above the // replica's 16 MiB /tmp, so libvips must not spill to a temp file. const camera = sharp({ create: { width: 7728, height: 5152, channels: 3, background: { r: 120, g: 80, b: 40 }, }, }); const baseline = await camera.clone().jpeg({ quality: 85 }).toBuffer(); const progressiveCamera = await camera .clone() .jpeg({ quality: 85, progressive: true }) .toBuffer(); for (const data of [baseline, progressiveCamera]) expect((await ready(data, 'image/jpeg')).size_bytes).toBe( data.length, ); const rgba16 = await maxPixelsRgba16Png(); expect((await ready(rgba16)).size_bytes).toBe(rgba16.length); }, 120_000); it('accepts a JPEG with megabytes of metadata before the frame header', async () => { const data = jpegWithMetadata(jpeg, 20); expect(data.length).toBeGreaterThan(1_048_576); expect((await ready(data, 'image/jpeg')).sha256).toBe(sha256(data)); }); it('authenticates before judging the content type', async () => { const zip = await http( `/api/v1/chats/${chatId}/attachments?filename=a.zip`, { method: 'POST', headers: { 'content-type': 'application/zip' }, body: png as unknown as BodyInit, }, ); expectError(zip, 401, 'unauthorized'); expectError( await upload(alice.token, leftoverChatId, png, { contentType: 'application/json', }), 415, 'unsupported_media_type', ); }); it('answers 413 with the error envelope to an oversized text body on JSON routes', async () => { const result = await http( `/api/v1/chats/${chatId}/messages`, { method: 'POST', headers: { ...bearer(alice.token), 'content-type': 'text/plain', 'idempotency-key': randomUUID().replaceAll('-', ''), }, body: 'x'.repeat(2_100_000), }, ); expectError(result, 413, 'payload_too_large'); }); it('keeps one slow client from taking every upload slot', async () => { const slow = await actor(); const slowChat = (await createChat(slow, [])).id; const url = new URL(baseUrl); const sockets: net.Socket[] = []; try { for (let index = 0; index < 12; index += 1) { const socket = net.connect( Number(url.port || 80), url.hostname, ); socket.on('error', () => {}); socket.write( `POST /api/v1/chats/${slowChat}/attachments?filename=slow.png HTTP/1.1\r\n` + `Host: ${url.host}\r\nAuthorization: Bearer ${slow.token}\r\n` + `Content-Type: image/png\r\nContent-Length: ${png.length}\r\n\r\n`, ); socket.write(png.subarray(0, 4)); sockets.push(socket); } await sleep(1000); const extra = await upload(slow.token, slowChat, png); expectError(extra, 429, 'rate_limited'); expect(extra.response.headers.get('retry-after')).toBe('1'); // Another user is not affected by the slow client. for (let index = 0; index < 4; index += 1) expect( (await upload(bob.token, chatId, png)).response.status, ).toBe(202); } finally { for (const socket of sockets) socket.destroy(); } await settled(slowChat); }); it('enforces the 10 MiB limit exactly', async () => { const exact = padPngTo(png, MAX); const created = await ready(exact); expect(created.size_bytes).toBe(MAX); expectError( await upload(alice.token, leftoverChatId, padPngTo(png, MAX + 1)), 413, 'payload_too_large', ); }); it('rejects a pixel bomb before storing anything', async () => { expectError( await upload(alice.token, leftoverChatId, pixelBombPng()), 413, 'payload_too_large', ); }); it('rejects damaged images', async () => { const flipped = Buffer.from(png); flipped[Math.floor(png.length / 2)] ^= 0xff; const cases: Buffer[] = [ png.subarray(0, png.length - 100), flipped, Buffer.concat([png.subarray(0, 8), randomBytes(2048)]), Buffer.alloc(0), // Structure and CRCs are fine; the pixel data does not decode. pngWithGarbageIdat(), ]; for (const data of cases) expectError( await upload(alice.token, leftoverChatId, data), 415, 'unsupported_media_type', ); }); it.each([ ['baseline', jpeg], ['progressive', progressive], ])('rejects a %s JPEG without scan data', async (_name, data) => { expectError( await upload( alice.token, leftoverChatId, jpegWithoutScanData(data), { contentType: 'image/jpeg', }, ), 415, 'unsupported_media_type', ); }); it('rejects a content type that does not match the bytes', async () => { const gif = Buffer.from('GIF89a' + 'x'.repeat(64)); const cases: [Buffer, string][] = [ [png, 'image/jpeg'], [jpeg, 'image/png'], [gif, 'image/png'], [png, 'text/plain'], [png, 'application/octet-stream'], ]; for (const [data, type] of cases) expectError( await upload(alice.token, leftoverChatId, data, { contentType: type, }), 415, 'unsupported_media_type', ); }); it('validates filenames by code points and forbidden characters', async () => { const bad = [ '?filename=a%2Fb.png', '?filename=a%5Cb.png', '?filename=a%00b.png', '?filename=a%07b.png', '?filename=%E2%80%AEgnp.exe', '?filename=%20', '?filename=', `?filename=${'a'.repeat(256)}`, '', '?filename=a.png&filename=b.png', ]; for (const query of bad) expectError( await uploadRaw(alice.token, leftoverChatId, query, png), 400, 'invalid_request', ); const long = '😀'.repeat(255); const created = await upload(alice.token, chatId, png, { filename: long, }); expect(created.response.status).toBe(202); expect(created.body.filename).toBe(long); }); it('links attachments to messages in the given order', async () => { const first = await ready(png); const second = await ready(jpeg, 'image/jpeg'); const order = [second.id, first.id]; const key = randomUUID().replaceAll('-', ''); const sent = await postMessage( alice.token, chatId, { text: '', attachment_ids: order }, key, ); expect(sent.response.status).toBe(201); expect(sent.body.attachment_ids).toEqual(order); const history = await http>( `/api/v1/chats/${chatId}/messages?limit=100`, { headers: bearer(bob.token) }, ); const stored = history.body.items.find((m) => m.id === sent.body.id); expect(stored?.attachment_ids).toEqual(order); const replay = await postMessage( alice.token, chatId, { text: '', attachment_ids: order }, key, ); expect(replay.response.status).toBe(201); expect(replay.body).toEqual(sent.body); expectError( await postMessage( alice.token, chatId, { text: '', attachment_ids: [first.id] }, key, ), 409, 'conflict', ); const textOnly = await postMessage(alice.token, chatId, { text: 'hello', }); expect(textOnly.response.status).toBe(201); expect(textOnly.body.attachment_ids).toEqual([]); }); it('rejects invalid attachment references', async () => { const mine = await ready(png); const foreign = await upload(eve.token, eveChatId, png); expect(foreign.response.status).toBe(202); expectError( await postMessage(alice.token, chatId, { text: '' }), 400, 'invalid_request', ); expectError( await postMessage(alice.token, chatId, { text: '', attachment_ids: [foreign.body.id], }), 404, 'not_found', ); expectError( await postMessage(alice.token, chatId, { text: 'x', attachment_ids: [randomUUID()], }), 404, 'not_found', ); for (const ids of [ [mine.id, mine.id], [mine.id, mine.id.toUpperCase()], Array.from({ length: 11 }, () => randomUUID()), null, ['not-a-uuid'], ]) expectError( await postMessage(alice.token, chatId, { text: 'x', attachment_ids: ids, }), 400, 'invalid_request', ); }); it('serves the promised variants only after ready', async () => { const uploaded = await upload(alice.token, chatId, png); expect(uploaded.response.status).toBe(202); expectError( await http( `/api/v1/attachments/${uploaded.body.id}/content`, { headers: bearer(alice.token) }, ), 409, 'conflict', ); const created = await waitReady(alice.token, uploaded.body.id); const variant = (name: string) => http( `/api/v1/attachments/${created.id}/content?variant=${name}`, { headers: bearer(alice.token), }, ); expect((await variant('original')).response.status).toBe(200); for (const [name, type] of [ ['thumbnail', 'image/jpeg'], ['crop', 'image/jpeg'], ['optimized', 'image/webp'], ] as const) { const response = await variant(name); expect(response.response.status).toBe(200); expect(response.response.headers.get('content-type')).toBe(type); } expectError(await variant('bogus'), 400, 'invalid_request'); }); it('answers 4xx, not 500, for raw image or text bodies on JSON routes', async () => { for (const [type, status, code] of [ ['image/png', 415, 'unsupported_media_type'], ['image/jpeg', 415, 'unsupported_media_type'], ['text/plain', 400, 'invalid_request'], ] as const) { const result = await http('/api/v1/users', { method: 'POST', headers: { 'content-type': type }, body: 'x', }); expectError(result, status, code); } }); it('removes the pending row when the client drops the connection', async () => { const data = noisePng(2 * 1024 * 1024); const url = new URL(baseUrl); const socket = net.connect(Number(url.port || 80), url.hostname); await new Promise((resolve, reject) => { socket.once('connect', resolve); socket.once('error', reject); }); socket.on('error', () => {}); socket.write( `POST /api/v1/chats/${leftoverChatId}/attachments?filename=cut.png HTTP/1.1\r\n` + `Host: ${url.host}\r\nAuthorization: Bearer ${alice.token}\r\n` + `Content-Type: image/png\r\nContent-Length: ${data.length}\r\n\r\n`, ); socket.write(data.subarray(0, 512 * 1024)); await sleep(500); socket.destroy(); await settled(leftoverChatId); }); it('leaves no rows behind after rejected uploads', async () => { await settled(leftoverChatId); }); }); async function settled(chat: string): Promise { const deadline = Date.now() + 10_000; let count = -1; while (Date.now() < deadline) { const result = await db.query<{ count: string }>( 'SELECT count(*) FROM attachments WHERE chat_id = $1', [chat], ); count = Number(result.rows[0].count); if (count === 0) return; await sleep(250); } expect(count).toBe(0); }