lab4: исправления второго ревью (40 Мп в tmpfs, таймауты S3, параллельный complete, auth до Content-Type, лимит на пользователя, бюджет памяти декода)

This commit is contained in:
2026-10-23 17:09:49 +03:00
parent a06471a6c6
commit 4b53e03fd6
27 changed files with 642 additions and 141 deletions
+103 -1
View File
@@ -21,8 +21,10 @@ import {
upload,
uploadRaw,
} from './support/e2e.js';
import { jpegWithoutScanData } from './support/jpeg.js';
import sharp from 'sharp';
import { jpegWithMetadata, jpegWithoutScanData } from './support/jpeg.js';
import {
maxPixelsRgba16Png,
noisePng,
padPngTo,
pixelBombPng,
@@ -146,6 +148,106 @@ describe('attachments (proxy upload)', () => {
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<ApiErrorBody>(
`/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<ApiErrorBody>(
`/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(201);
} 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);