import { createHash } from 'node:crypto'; import { readFileSync } from 'node:fs'; import { setTimeout as sleep } from 'node:timers/promises'; import { afterAll, beforeAll, describe, expect, it } from 'vitest'; import type { Pool } from 'pg'; import type { Attachment } from '../src/attachments/attachment.js'; import type { DirectUpload } from '../src/attachments/direct-uploads.service.js'; import type { ApiErrorBody } from '../src/common/errors/api-error.js'; import { Actor, actor, bearer, createChat, database, download, expectError, http, json, postMessage, waitReady, } from './support/e2e.js'; import { noisePng, pixelBombPng } 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 MAX = 10_485_760; const shortTtl = process.env.E2E_DIRECT_TTL_SHORT === '1'; let db: Pool; let alice: Actor; let bob: Actor; let eve: Actor; let chatId: string; const sha256 = (data: Uint8Array) => createHash('sha256').update(data).digest('hex'); function init(token: string, chat: string, body: Record) { return http( `/api/v1/chats/${chat}/attachments/direct-uploads`, json(body, bearer(token)), ); } function declaration(data: Buffer, contentType = 'image/png') { return { filename: 'direct.png', content_type: contentType, size_bytes: data.length, sha256: sha256(data), }; } async function initOk(data: Buffer, contentType = 'image/png') { const result = await init( alice.token, chatId, declaration(data, contentType), ); expect(result.response.status).toBe(201); return result.body; } // The PUT goes straight to the public S3 endpoint, not through the API. async function put(ticket: DirectUpload, data: Buffer): Promise { return fetch(ticket.url, { method: ticket.method, headers: ticket.headers, body: data as unknown as BodyInit, }); } function complete(token: string, id: string) { return http( `/api/v1/attachments/${id}/complete`, { method: 'POST', headers: bearer(token), }, ); } describe('direct upload (presigned PUT)', () => { beforeAll(async () => { db = database(); alice = await actor(); bob = await actor(); eve = await actor(); chatId = (await createChat(alice, [bob])).id; }); afterAll(async () => { await db.end(); }); it('uploads around the API and exposes the attachment after complete', async () => { const ticket = await initOk(png); expect(ticket.method).toBe('PUT'); expect(ticket.headers['x-amz-checksum-sha256']).toBe( Buffer.from(sha256(png), 'hex').toString('base64'), ); expect(Date.parse(ticket.expires_at)).toBeGreaterThan(Date.now()); expect(new URL(ticket.url).pathname).toBe( `/messenger-attachments/staging/${ticket.attachment_id}/original`, ); const hidden = await http( `/api/v1/attachments/${ticket.attachment_id}`, { headers: bearer(bob.token) }, ); expectError(hidden, 404, 'not_found'); expectError( await postMessage(alice.token, chatId, { text: '', attachment_ids: [ticket.attachment_id], }), 404, 'not_found', ); expectError( await complete(alice.token, ticket.attachment_id), 409, 'conflict', ); expect((await put(ticket, png)).status).toBe(200); expectError( await complete(bob.token, ticket.attachment_id), 404, 'not_found', ); expectError( await complete(eve.token, ticket.attachment_id), 404, 'not_found', ); const done = await complete(alice.token, ticket.attachment_id); expect(done.response.status).toBe(200); expect(done.body).toMatchObject({ id: ticket.attachment_id, owner_id: alice.user.id, content_type: 'image/png', size_bytes: png.length, sha256: sha256(png), status: 'queued', }); const ready = await waitReady(alice.token, ticket.attachment_id); expect(ready.status).toBe('ready'); expect(ready.variants).toHaveLength(3); const again = await complete(alice.token, ticket.attachment_id); expect(again.response.status).toBe(200); expect(again.body.status).toBe('ready'); const content = await download(bob.token, ticket.attachment_id); expect(content.response.status).toBe(200); expect(content.bytes.equals(png)).toBe(true); const sent = await postMessage(alice.token, chatId, { text: '', attachment_ids: [ticket.attachment_id], }); expect(sent.response.status).toBe(201); expect(sent.body.attachment_ids).toEqual([ticket.attachment_id]); }); it('answers 200 to every parallel complete of one upload', async () => { const ticket = await initOk(png); expect((await put(ticket, png)).status).toBe(200); const results = await Promise.all( Array.from({ length: 4 }, () => complete(alice.token, ticket.attachment_id), ), ); expect(results.map((result) => result.response.status)).toEqual([ 200, 200, 200, 200, ]); for (const result of results) expect(result.body.id).toBe(ticket.attachment_id); }); it('rejects invalid declarations', async () => { const base = declaration(png); expectError(await init(eve.token, chatId, base), 404, 'not_found'); expectError( await init(alice.token, chatId, { ...base, size_bytes: MAX + 1 }), 413, 'payload_too_large', ); expectError( await init(alice.token, chatId, { ...base, content_type: 'image/gif', }), 415, 'unsupported_media_type', ); for (const bad of [ { ...base, extra: true }, { ...base, sha256: 'ABC' }, { ...base, size_bytes: 0 }, { ...base, filename: 'a/b.png' }, ]) expectError( await init(alice.token, chatId, bad), 400, 'invalid_request', ); }); it('lets the storage refuse bytes that differ from the declaration', async () => { const ticket = await initOk(png); const tampered = Buffer.from(png); tampered[1000] ^= 1; const refused = await put(ticket, tampered); expect(refused.status).toBe(400); expect(await refused.text()).toContain('XAmzContentChecksumMismatch'); // No object was created, so the upload can still be finished. expectError( await complete(alice.token, ticket.attachment_id), 409, 'conflict', ); expect((await put(ticket, png)).status).toBe(200); expect( (await complete(alice.token, ticket.attachment_id)).response.status, ).toBe(200); const longer = await initOk(png); const wrongLength = await put( longer, Buffer.concat([png, Buffer.from('x')]), ); expect(wrongLength.status).toBe(403); }); it('rejects a declared PNG that is really a JPEG, then forgets the claim', async () => { const ticket = await initOk(jpeg, 'image/png'); expect((await put(ticket, jpeg)).status).toBe(200); expectError( await complete(alice.token, ticket.attachment_id), 415, 'unsupported_media_type', ); expectError( await complete(alice.token, ticket.attachment_id), 404, 'not_found', ); // The URL is still valid: a re-PUT must stay tracked so the sweeper // removes the new object too. expect((await put(ticket, jpeg)).status).toBe(200); const row = await db.query<{ status: string }>( 'SELECT status FROM attachments WHERE id = $1', [ticket.attachment_id], ); expect(row.rows[0]?.status).toBe('rejected'); expectError( await complete(alice.token, ticket.attachment_id), 404, 'not_found', ); }); it.each([ ['truncated PNG', () => png.subarray(0, png.length - 100)], [ 'PNG damaged after the first 64 KiB', () => { const data = noisePng(1_048_576); data[700_000] ^= 0xff; return data; }, ], ])('rejects a %s even with a matching checksum', async (_name, make) => { // The declaration and the signed checksum match the bytes exactly: // only a check of the whole image can tell they are not an image. const data = make(); const ticket = await initOk(data); expect((await put(ticket, data)).status).toBe(200); expectError( await complete(alice.token, ticket.attachment_id), 415, 'unsupported_media_type', ); expectError( await complete(alice.token, ticket.attachment_id), 404, 'not_found', ); }); it('rejects a pixel bomb on complete', async () => { const bomb = pixelBombPng(); const ticket = await initOk(bomb); expect((await put(ticket, bomb)).status).toBe(200); expectError( await complete(alice.token, ticket.attachment_id), 413, 'payload_too_large', ); expectError( await complete(alice.token, ticket.attachment_id), 404, 'not_found', ); }); it('limits unfinished direct uploads per user', async () => { const spammer = await actor(); const chat = (await createChat(spammer, [])).id; for (let index = 0; index < 10; index += 1) { const ok = await init(spammer.token, chat, declaration(png)); expect(ok.response.status).toBe(201); } const limited = await init(spammer.token, chat, declaration(png)); expectError(limited, 429, 'rate_limited'); expect( Number(limited.response.headers.get('retry-after')), ).toBeGreaterThan(0); }); it.runIf(shortTtl)( 'refuses an expired URL and removes the claim', async () => { const ticket = await initOk(png); const expiresAt = Date.parse(ticket.expires_at); await sleep(Math.max(0, expiresAt - Date.now()) + 2000); expect((await put(ticket, png)).status).toBe(403); const deadline = Date.now() + 15_000; for (;;) { const rows = await db.query( 'SELECT 1 FROM attachments WHERE id = $1', [ticket.attachment_id], ); if (rows.rowCount === 0) break; expect(Date.now()).toBeLessThan(deadline); await sleep(250); } expectError( await complete(alice.token, ticket.attachment_id), 404, 'not_found', ); }, 60_000, ); });