Files
TREK/server/tests/unit/nest/bounded-raster.test.ts
T
Maurice 28eea68559 fix(media): bound image work before public rendition
Check avatar uploads and raster fallbacks before decoding. Limit compressed bytes, decoded dimensions, GIF frames and the combined TIFF page budget.
2026-12-01 11:56:24 +01:00

119 lines
4.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { boundedRasterHeader, readRasterFile } from '../../../src/nest/common/bounded-raster';
import { Jimp } from 'jimp';
import { join } from 'node:path';
import { describe, expect, it } from 'vitest';
function pngHeader(width: number, height: number): Buffer {
const buffer = Buffer.alloc(24);
buffer.writeUInt32BE(0x89504e47, 0);
buffer.write('IHDR', 12);
buffer.writeUInt32BE(width, 16);
buffer.writeUInt32BE(height, 20);
return buffer;
}
function tiffDirectories(...pages: ReadonlyArray<ReadonlyArray<readonly [number, number]>>): Buffer {
const data = Buffer.alloc(8 + pages.reduce((size, tags) => size + 6 + tags.length * 12, 0));
data.write('II');
data.writeUInt16LE(42, 2);
data.writeUInt32LE(8, 4);
let directory = 8;
for (const [page, tags] of pages.entries()) {
data.writeUInt16LE(tags.length, directory);
let entry = directory + 2;
for (const [tag, value] of tags) {
data.writeUInt16LE(tag, entry);
data.writeUInt16LE(4, entry + 2);
data.writeUInt32LE(1, entry + 4);
data.writeUInt32LE(value, entry + 8);
entry += 12;
}
directory = entry + 4;
data.writeUInt32LE(page === pages.length - 1 ? 0 : directory, entry);
}
return data;
}
function gifFrameHeader(frameWidth: number, frameHeight: number): Buffer {
const data = Buffer.alloc(26);
data.write('GIF87a');
data.writeUInt16LE(4, 6);
data.writeUInt16LE(2, 8);
data[13] = 0x2c;
data.writeUInt16LE(frameWidth, 18);
data.writeUInt16LE(frameHeight, 20);
data[25] = 0x3b;
return data;
}
describe('boundedRasterHeader', () => {
it.each(['image/png', 'image/jpeg', 'image/bmp', 'image/gif', 'image/tiff'] as const)(
'reads %s dimensions without decoding',
async (mimeType) => {
const image = new Jimp({ width: 4, height: 2, color: 0x336699ff });
expect(boundedRasterHeader(await image.getBuffer(mimeType))).toEqual({ mimeType, width: 4, height: 2 });
},
);
it.each([
[0, 20],
[20, 0],
[20_000, 20],
[20, 20_000],
[8_000, 6_000],
])('refuses dimensions %d × %d', (width, height) => {
expect(boundedRasterHeader(pngHeader(width, height))).toBeNull();
});
it('refuses unsupported and incomplete headers', () => {
expect(boundedRasterHeader(Buffer.from('not an image'))).toBeNull();
expect(boundedRasterHeader(Buffer.from('II\x2a\x00\x08\x00\x00\x00', 'latin1'))).toBeNull();
expect(boundedRasterHeader(Buffer.from('GIF89a'))).toBeNull();
});
it('checks individual GIF frames independently of the canvas', () => {
expect(boundedRasterHeader(gifFrameHeader(4, 2))).toMatchObject({ mimeType: 'image/gif', width: 4, height: 2 });
expect(boundedRasterHeader(gifFrameHeader(0, 2))).toBeNull();
expect(boundedRasterHeader(gifFrameHeader(4, 20_000))).toBeNull();
});
it('counts the decoded space used by every TIFF page', () => {
const small = [
[256, 4],
[257, 2],
] as const;
expect(boundedRasterHeader(tiffDirectories(small, small))).toMatchObject({
mimeType: 'image/tiff',
width: 4,
height: 2,
});
const large = [
[256, 5_000],
[257, 5_000],
] as const;
expect(boundedRasterHeader(tiffDirectories(large, large))).toBeNull();
});
it('refuses TIFF pages with unbounded samples or tile geometry', () => {
const size = [
[256, 4],
[257, 2],
] as const;
expect(boundedRasterHeader(tiffDirectories([...size, [277, 5]]))).toBeNull();
expect(boundedRasterHeader(tiffDirectories([...size, [322, 20_000], [323, 2]]))).toBeNull();
expect(boundedRasterHeader(tiffDirectories([...size, [330, 8]]))).toBeNull();
});
});
describe('readRasterFile', () => {
const fixture = join(__dirname, '../../fixtures/small-image.jpg');
it('reads only input within its byte limit', async () => {
expect(await readRasterFile(fixture, 4)).toBeNull();
expect(boundedRasterHeader((await readRasterFile(fixture, 5 * 1024 * 1024)) ?? Buffer.alloc(0))).toMatchObject({
mimeType: 'image/jpeg',
});
});
});