forked from mtusi2026/mtusi2026lab4
364 lines
9.7 KiB
TypeScript
364 lines
9.7 KiB
TypeScript
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,
|
|
} from './support/e2e.js';
|
|
import { noisePng, padPngTo, 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 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(201);
|
|
return result.body;
|
|
}
|
|
|
|
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 created = await upload(alice.token, chatId, png, {
|
|
filename: 'Снимок.png',
|
|
});
|
|
expect(created.response.status).toBe(201);
|
|
expect(created.body).toMatchObject({
|
|
chat_id: chatId,
|
|
owner_id: alice.user.id,
|
|
filename: 'Снимок.png',
|
|
content_type: 'image/png',
|
|
size_bytes: png.length,
|
|
sha256: sha256(png),
|
|
status: 'ready',
|
|
variants: [],
|
|
error_code: null,
|
|
});
|
|
const id = created.body.id;
|
|
|
|
const metadata = await http<typeof created.body>(
|
|
`/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('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),
|
|
];
|
|
for (const data of cases)
|
|
expectError(
|
|
await upload(alice.token, leftoverChatId, data),
|
|
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(201);
|
|
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<Page<Message>>(
|
|
`/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(201);
|
|
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 unknown content variants as errors, not bytes', async () => {
|
|
const created = await ready(png);
|
|
const variant = (name: string) =>
|
|
http<ApiErrorBody>(`/api/v1/attachments/${created.id}/content?variant=${name}`, {
|
|
headers: bearer(alice.token),
|
|
});
|
|
expect((await variant('original')).response.status).toBe(200);
|
|
expectError(await variant('thumbnail'), 404, 'not_found');
|
|
expectError(await variant('bogus'), 400, 'invalid_request');
|
|
});
|
|
|
|
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<void>((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<void> {
|
|
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);
|
|
}
|