forked from mtusi2026/mtusi2026lab6
Перенесено дерево lab5 целиком: NestJS-приложение (API + worker,
RabbitMQ, outbox, S3), Dockerfile, compose.yaml, infra/{haproxy,minio,
rabbitmq}, стендовые скрипты, scripts/lab4, scripts/lab5,
evidence/lab4, evidence/lab5 и contracts/extensions.openapi.json.
Задание lab6 оставлено своим: README, REPORT, COURSE, lab.json,
contracts/{openapi,infrastructure,jobs.schema,attachment-states,
examples,observability}, tests, compose.example.yaml, infra/otel,
.github/pull_request_template.md.
Адаптации при переносе:
- docker-проект mtusi-lab5 -> mtusi-lab6 и тег образа
mtusi-messenger:lab6 (compose.yaml, scripts/stop-one.sh, .env.example,
infra/README.md), чтобы тома и контейнеры не пересекались с lab5;
- Makefile: цели lab5 сохранены целиком (skeleton lab6 не отличается
от skeleton lab5);
- .env.example: переменные приложения и стенда lab5 + блок lab6
(OTEL_*) из skeleton.
110 lines
3.5 KiB
TypeScript
110 lines
3.5 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
|
import { dirname, join } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { describe, expect, it } from 'vitest';
|
|
import { noisePng, pixelBombPng } from '../../../test/support/png.js';
|
|
import { ImageInspector, ImageRejectedError } from './image-inspector.js';
|
|
|
|
const here = dirname(fileURLToPath(import.meta.url));
|
|
const png = readFileSync(join(here, '../../../../tests/fixtures/sample.png'));
|
|
const jpeg = readFileSync(join(here, '../../../test/fixtures/sample.jpg'));
|
|
const progressive = readFileSync(
|
|
join(here, '../../../test/fixtures/sample-progressive.jpg'),
|
|
);
|
|
const MAX = 40_000_000;
|
|
|
|
function inspect(data: Buffer, step = data.length, max = MAX) {
|
|
const inspector = new ImageInspector(max);
|
|
for (let offset = 0; offset < data.length; offset += step)
|
|
inspector.push(data.subarray(offset, offset + step));
|
|
return inspector.finish();
|
|
}
|
|
|
|
function rejection(action: () => unknown): ImageRejectedError {
|
|
try {
|
|
action();
|
|
} catch (error) {
|
|
expect(error).toBeInstanceOf(ImageRejectedError);
|
|
return error as ImageRejectedError;
|
|
}
|
|
throw new Error('expected rejection');
|
|
}
|
|
|
|
describe('ImageInspector', () => {
|
|
it.each([
|
|
['png', png, 'image/png'],
|
|
['jpeg', jpeg, 'image/jpeg'],
|
|
['progressive jpeg', progressive, 'image/jpeg'],
|
|
])('accepts %s and reports its size', (_name, data, contentType) => {
|
|
const expected = { contentType, width: 320, height: 200 };
|
|
expect(inspect(data)).toEqual(expected);
|
|
expect(inspect(data, 1)).toEqual(expected);
|
|
expect(inspect(data, 7)).toEqual(expected);
|
|
});
|
|
|
|
it('rejects truncated files on finish', () => {
|
|
for (const data of [png, jpeg, progressive])
|
|
for (const cut of [10, 40, data.length - 100, data.length - 1]) {
|
|
const reason = rejection(() =>
|
|
inspect(data.subarray(0, cut)),
|
|
).reason;
|
|
expect(reason).toBe('unsupported_media_type');
|
|
}
|
|
});
|
|
|
|
it('rejects a flipped byte inside IDAT by checksum', () => {
|
|
const damaged = Buffer.from(png);
|
|
damaged[Math.floor(png.length / 2)] ^= 0xff;
|
|
expect(rejection(() => inspect(damaged)).reason).toBe(
|
|
'unsupported_media_type',
|
|
);
|
|
});
|
|
|
|
it('rejects a pixel bomb right after IHDR, before any IDAT', () => {
|
|
const bomb = pixelBombPng();
|
|
const inspector = new ImageInspector(MAX);
|
|
// signature + IHDR chunk = 8 + 25 bytes
|
|
const reason = rejection(() =>
|
|
inspector.push(bomb.subarray(0, 33)),
|
|
).reason;
|
|
expect(reason).toBe('too_many_pixels');
|
|
expect(inspector.header).toBeUndefined();
|
|
});
|
|
|
|
it('applies the pixel limit to JPEG frame headers', () => {
|
|
expect(rejection(() => inspect(jpeg, jpeg.length, 63_999)).reason).toBe(
|
|
'too_many_pixels',
|
|
);
|
|
expect(inspect(jpeg, jpeg.length, 64_000).width).toBe(320);
|
|
});
|
|
|
|
it('rejects foreign formats and trailing data after IEND', () => {
|
|
for (const data of [
|
|
Buffer.from('GIF89a' + 'x'.repeat(40)),
|
|
Buffer.from('plain text, not an image'),
|
|
Buffer.alloc(0),
|
|
])
|
|
expect(rejection(() => inspect(data)).reason).toBe(
|
|
'unsupported_media_type',
|
|
);
|
|
expect(
|
|
rejection(() => inspect(Buffer.concat([png, Buffer.from([0])])))
|
|
.reason,
|
|
).toBe('unsupported_media_type');
|
|
});
|
|
|
|
it('accepts padded and noise PNGs of exact sizes', () => {
|
|
for (const size of [131_072, 1_048_576]) {
|
|
const data = noisePng(size);
|
|
expect(data.length).toBe(size);
|
|
expect(inspect(data).width).toBe(1024);
|
|
}
|
|
});
|
|
|
|
it('tolerates trailing data after JPEG EOI', () => {
|
|
expect(
|
|
inspect(Buffer.concat([jpeg, Buffer.from('MPF trailer')])).height,
|
|
).toBe(200);
|
|
});
|
|
});
|