import { INestApplication } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import Ajv2020 from 'ajv/dist/2020';
import { createHash, randomUUID } from 'crypto';
import { mkdtempSync, readdirSync, rmSync } from 'fs';
import { connect } from 'net';
import { tmpdir } from 'os';
import { join } from 'path';
import request from 'supertest';
import { PrismaService } from '../src/db/prisma.service';
import { loadDocument } from '../src/docs/swagger';
import { S3Service } from '../src/storage/s3.service';
import { bearer, createTestApp, registerUser, TestUser } from './helpers';
import {
corruptedJpegs,
corruptedPngs,
jpeg,
png,
pngHeaderOnly,
pngOfExactSize,
SAMPLE_PNG,
} from './images';
const MAX_BYTES = 10 * 1024 * 1024;
describe('Загрузка вложений (e2e)', () => {
let app: INestApplication;
let server: any;
let alice: TestUser;
let eve: TestUser;
let chatId: string;
const upload = (
body: Buffer | undefined,
{
type = 'image/png',
filename = 'sample.png',
user = alice,
chat = chatId,
}: {
type?: string | null;
filename?: string | null;
user?: TestUser | null;
chat?: string;
} = {},
) => {
const query = filename === null ? '' : `?filename=${encodeURIComponent(filename)}`;
const req = request(server).post(`/api/v1/chats/${chat}/attachments${query}`);
if (user) {
req.set('Authorization', bearer(user));
}
if (type) {
req.set('Content-Type', type);
}
return body ? req.send(body) : req;
};
const expectError = (res: request.Response, status: number, code: string) => {
expect({ status: res.status, code: res.body.error?.code }).toEqual({ status, code });
expect(res.headers['x-request-id']).toBeTruthy();
expect(res.headers['x-instance-id']).toBeTruthy();
expect(res.body.error.request_id).toBe(res.headers['x-request-id']);
};
const uploadTmp = mkdtempSync(join(tmpdir(), 'attachments-test-'));
const activeTempDirs = (): string[] =>
readdirSync(uploadTmp).filter((name) => name.startsWith('messenger-upload-'));
beforeAll(async () => {
({ app, server } = await createTestApp({ UPLOAD_TMP_DIR: uploadTmp }));
alice = await registerUser(server, 'att_alice');
eve = await registerUser(server, 'att_eve');
const chat = await request(server)
.post('/api/v1/chats')
.set('Authorization', bearer(alice))
.send({ title: 'Files', member_ids: [alice.id] })
.expect(201);
chatId = chat.body.id;
});
afterAll(async () => {
await app.close();
rmSync(uploadTmp, { recursive: true, force: true });
});
describe('успешная загрузка', () => {
it('возвращает 202 Attachment queued по контракту, staging и задание сохранены до ответа', async () => {
const res = await upload(SAMPLE_PNG).expect(202);
const document: any = loadDocument();
const ajv = new Ajv2020({ strict: false, validateFormats: false });
ajv.addSchema(document, 'contract');
const validate = ajv.compile({ $ref: 'contract#/components/schemas/Attachment' });
expect({ valid: validate(res.body), errors: validate.errors }).toEqual({
valid: true,
errors: null,
});
expect(res.body).toMatchObject({
chat_id: chatId,
owner_id: alice.id,
filename: 'sample.png',
content_type: 'image/png',
size_bytes: SAMPLE_PNG.length,
sha256: createHash('sha256').update(SAMPLE_PNG).digest('hex'),
status: 'queued',
variants: [],
error_code: null,
});
const prisma = app.get(PrismaService);
const row = await prisma.attachment.findUnique({
where: { id: res.body.id },
include: { jobs: { include: { outbox: true } } },
});
expect(row).toMatchObject({
status: 'queued',
objectKey: `attachments/${res.body.id}/original`,
stagingKey: `staging/${res.body.id}`,
});
expect(row!.jobs).toHaveLength(1);
expect(row!.jobs[0]).toMatchObject({ status: 'queued', attempt: 1, pipelineVersion: 1 });
expect(row!.jobs[0].outbox).toHaveLength(1);
expect(row!.jobs[0].outbox[0].payload).toEqual({
schema_version: 1,
job_id: row!.jobs[0].id,
type: 'attachment.process.v1',
attachment_id: res.body.id,
pipeline_version: 1,
attempt: 1,
created_at: expect.any(String),
});
const staged = await app.get(S3Service).getBuffer(`staging/${res.body.id}`, MAX_BYTES);
expect(Buffer.compare(staged, SAMPLE_PNG)).toBe(0);
expect(JSON.stringify(res.body)).not.toMatch(/attachments\/|staging\//);
});
it('принимает реальный JPEG', async () => {
const res = await upload(await jpeg(), {
type: 'image/jpeg',
filename: 'фото 1.jpg',
}).expect(202);
expect(res.body).toMatchObject({
content_type: 'image/jpeg',
filename: 'фото 1.jpg',
});
});
it('игнорирует параметры и регистр Content-Type', async () => {
await upload(SAMPLE_PNG, { type: 'IMAGE/PNG; charset=binary' }).expect(202);
});
it('принимает файл ровно 10 MiB, filename ровно 255 символов и PNG с байтами после IEND', async () => {
const exact = pngOfExactSize(MAX_BYTES);
expect(exact.length).toBe(MAX_BYTES);
const res = await upload(exact).expect(202);
expect(res.body.size_bytes).toBe(MAX_BYTES);
await upload(SAMPLE_PNG, { filename: `${'ф'.repeat(251)}.png` }).expect(202);
await upload(Buffer.concat([png(4, 4), Buffer.from('tail')])).expect(202);
});
it('принимает ровно 40 млн пикселей', async () => {
const res = await upload(await bigPng(8000, 5000));
expect(res.status).toBe(202);
});
});
describe('порядок проверок и доступ', () => {
it('без токена — 401 раньше любых проверок содержимого', async () => {
const cases: [string | null, string | null][] = [
['image/png', 'a.png'],
['text/plain', 'a.png'],
['application/json', 'a.png'],
['image/png', '../a.png'],
[null, null],
];
for (const [type, filename] of cases) {
const res = await upload(Buffer.from('{bad'), { type, filename, user: null });
expectError(res, 401, 'unauthorized');
expect(res.headers['www-authenticate']).toBe('Bearer realm="messenger"');
}
});
it('посторонний и несуществующий чат — одинаковый 404, чужой файл не сохраняется', async () => {
const prisma = app.get(PrismaService);
const before = await prisma.attachment.count({ where: { chatId } });
expectError(await upload(SAMPLE_PNG, { user: eve }), 404, 'not_found');
expectError(await upload(SAMPLE_PNG, { chat: randomUUID() }), 404, 'not_found');
expect(await prisma.attachment.count({ where: { chatId } })).toBe(before);
});
it('неверный UUID чата — 400', async () => {
expectError(await upload(SAMPLE_PNG, { chat: 'not-a-uuid' }), 400, 'invalid_request');
});
});
describe('filename', () => {
const invalid = {
'отсутствует': null,
'пустой': '',
'только пробелы': ' ',
'256 символов': 'x'.repeat(256),
'путь через /': '../x.png',
'путь через \\': 'a\\b.png',
'NUL': 'a\u0000b.png',
'перевод строки': 'a\nb.png',
'DEL': 'bad\u007f.png',
'C1 U+0085': 'c1\u0085.png',
'разделитель строк U+2028': 'a
b.png',
};
it.each(Object.entries(invalid))('%s — 400', async (_name, filename) => {
expectError(await upload(SAMPLE_PNG, { filename }), 400, 'invalid_request');
});
it('повторённый параметр — 400', async () => {
const res = await request(server)
.post(`/api/v1/chats/${chatId}/attachments?filename=a.png&filename=b.png`)
.set('Authorization', bearer(alice))
.set('Content-Type', 'image/png')
.send(SAMPLE_PNG);
expectError(res, 400, 'invalid_request');
});
});
describe('тип содержимого', () => {
it.each(['text/plain', 'application/json', 'application/octet-stream', 'multipart/form-data; boundary=x', 'image/gif', 'image/webp'])(
'Content-Type %s — 415',
async (type) => {
expectError(await upload(SAMPLE_PNG, { type }), 415, 'unsupported_media_type');
},
);
it('без Content-Type — 415', async () => {
expectError(await upload(SAMPLE_PNG, { type: null }), 415, 'unsupported_media_type');
});
it('подмена MIME в обе стороны и чужие форматы — 415', async () => {
const realJpeg = await jpeg();
expectError(await upload(SAMPLE_PNG, { type: 'image/jpeg' }), 415, 'unsupported_media_type');
expectError(await upload(realJpeg, { type: 'image/png' }), 415, 'unsupported_media_type');
for (const body of [
Buffer.from('просто текст'),
Buffer.from('GIF89a\u0001\u0000\u0001\u0000'),
Buffer.from(''),
]) {
expectError(await upload(body), 415, 'unsupported_media_type');
}
});
it('пустое тело — 400', async () => {
expectError(await upload(undefined), 400, 'invalid_request');
expectError(await upload(Buffer.alloc(0)), 400, 'invalid_request');
});
});
describe('повреждённые изображения', () => {
it.each(Object.entries(corruptedPngs()))('PNG: %s — 415', async (_name, body) => {
expectError(await upload(body), 415, 'unsupported_media_type');
});
it('JPEG: все повреждения — 415', async () => {
for (const body of Object.values(corruptedJpegs(await jpeg()))) {
expectError(await upload(body, { type: 'image/jpeg' }), 415, 'unsupported_media_type');
}
});
});
describe('лимиты', () => {
it('10 MiB + 1 байт — 413 и без чтения объявленного тела', async () => {
expectError(await upload(pngOfExactSize(MAX_BYTES + 1)), 413, 'payload_too_large');
});
it('превышение при chunked без Content-Length — 413', async () => {
const res = await new Promise((resolve, reject) => {
const req = upload(undefined);
req.set('Transfer-Encoding', 'chunked');
req.write(pngOfExactSize(MAX_BYTES));
req.write(Buffer.alloc(1024));
req.end((error, response) => (response ? resolve(response) : reject(error)));
});
expectError(res, 413, 'payload_too_large');
});
it('превышение лимита пикселей — 413 до декодирования', async () => {
for (const body of [pngHeaderOnly(8001, 5000), pngHeaderOnly(20000, 10000)]) {
expectError(await upload(body), 413, 'payload_too_large');
}
});
it('оборванная загрузка не оставляет временных файлов и строк в БД', async () => {
const prisma = app.get(PrismaService);
const before = await prisma.attachment.count({ where: { chatId } });
const { port } = server.address();
await new Promise((resolve, reject) => {
const socket = connect(port, '127.0.0.1', () => {
socket.write(
`POST /api/v1/chats/${chatId}/attachments?filename=a.png HTTP/1.1\r\n` +
`Host: test\r\nAuthorization: ${bearer(alice)}\r\n` +
`Content-Type: image/png\r\nContent-Length: ${SAMPLE_PNG.length}\r\n\r\n`,
);
socket.write(SAMPLE_PNG.subarray(0, 1000));
setTimeout(() => {
socket.destroy();
resolve();
}, 300);
});
socket.on('error', reject);
});
await new Promise((resolve) => setTimeout(resolve, 500));
expect(await prisma.attachment.count({ where: { chatId } })).toBe(before);
expect(activeTempDirs()).toEqual([]);
});
});
describe('отказы', () => {
it('S3 недоступен — 503 с заголовками, метаданные не создаются', async () => {
const { app: down, server: downServer } = await createTestApp({
S3_ENDPOINT: 'http://127.0.0.1:9',
});
try {
const prisma = app.get(PrismaService);
const before = await prisma.attachment.count({ where: { chatId } });
const started = Date.now();
const res = await request(downServer)
.post(`/api/v1/chats/${chatId}/attachments?filename=a.png`)
.set('Authorization', bearer(alice))
.set('Content-Type', 'image/png')
.send(SAMPLE_PNG);
expectError(res, 503, 'unavailable');
expect(Date.now() - started).toBeLessThan(3000);
expect(await prisma.attachment.count({ where: { chatId } })).toBe(before);
} finally {
await down.close();
}
});
it('явный отказ транзакции после PUT удаляет объект staging', async () => {
const prisma = app.get(PrismaService);
const s3 = app.get(S3Service);
const remove = jest.spyOn(s3, 'delete');
const create = jest.spyOn(prisma, '$transaction').mockRejectedValueOnce(
new Prisma.PrismaClientKnownRequestError('rejected', {
code: 'P2003',
clientVersion: 'test',
}),
);
try {
const res = await upload(SAMPLE_PNG);
expect(res.status).toBe(500);
expect(remove).toHaveBeenCalledTimes(1);
expect(remove.mock.calls[0][0]).toMatch(/^staging\//);
} finally {
create.mockRestore();
remove.mockRestore();
}
});
it('необработанная ошибка PostgreSQL при INSERT — 503, объект не удаляется', async () => {
const prisma = app.get(PrismaService);
const s3 = app.get(S3Service);
const remove = jest.spyOn(s3, 'delete');
const create = jest.spyOn(prisma, '$transaction').mockRejectedValueOnce(
new Prisma.PrismaClientUnknownRequestError('server closed the connection', {
clientVersion: 'test',
}),
);
try {
expectError(await upload(SAMPLE_PNG), 503, 'unavailable');
expect(remove).not.toHaveBeenCalled();
} finally {
create.mockRestore();
remove.mockRestore();
}
});
it('при неясном исходе COMMIT объект не удаляется', async () => {
const prisma = app.get(PrismaService);
const s3 = app.get(S3Service);
const remove = jest.spyOn(s3, 'delete');
const create = jest.spyOn(prisma, '$transaction').mockRejectedValueOnce(
new Prisma.PrismaClientKnownRequestError('connection lost', {
code: 'P1017',
clientVersion: 'test',
}),
);
try {
expectError(await upload(SAMPLE_PNG), 503, 'unavailable');
expect(remove).not.toHaveBeenCalled();
} finally {
create.mockRestore();
remove.mockRestore();
}
});
});
});
async function bigPng(width: number, height: number): Promise {
const { default: sharp } = await import('sharp');
return sharp({ create: { width, height, channels: 3, background: '#336699' } })
.png({ compressionLevel: 9 })
.toBuffer();
}