mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 17:03:15 +00:00
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.
This commit is contained in:
@@ -168,6 +168,17 @@ describe('Avatar', () => {
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
|
||||
it('PROFILE-003b: checks the avatar bytes before storing it', async () => {
|
||||
const { user } = await makeUser(orm);
|
||||
const res = await request(app)
|
||||
.post('/api/auth/avatar')
|
||||
.set('Cookie', authCookie(user.id))
|
||||
.attach('avatar', Buffer.from('not an image'), { filename: 'avatar.png', contentType: 'image/png' });
|
||||
|
||||
expect(res.status).toBe(400);
|
||||
expect(await findRow(orm, Users, { id: user.id })).toMatchObject({ avatar: null });
|
||||
});
|
||||
|
||||
it('PROFILE-005 — DELETE /api/auth/avatar clears avatar_url', async () => {
|
||||
const { user } = await makeUser(orm);
|
||||
// Upload first
|
||||
|
||||
@@ -17,6 +17,7 @@ import { HttpException } from '@nestjs/common';
|
||||
|
||||
import type { Request, Response } from 'express';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import { join } from 'node:path';
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
|
||||
vi.mock('../../../src/nest/audit/client-ip', () => ({ getClientIp: vi.fn(() => '1.2.3.4') }));
|
||||
@@ -32,6 +33,7 @@ vi.mock('../../../src/nest/common/demo', () => ({ isDemoEmail: vi.fn(() => false
|
||||
const user = { id: 1, username: 'u', role: 'user', email: 'u@example.test' } as User;
|
||||
const req = { ip: '9.9.9.9', headers: {} } as Request;
|
||||
const res = {} as Response;
|
||||
const avatarFixture = join(__dirname, '../../fixtures/small-image.jpg');
|
||||
|
||||
function asvc(o: Partial<AuthService> = {}): AuthService {
|
||||
return {
|
||||
@@ -468,7 +470,10 @@ describe('AuthController (authenticated)', () => {
|
||||
});
|
||||
const saveAvatar = vi.fn().mockResolvedValue({ avatar: '/a.jpg' });
|
||||
expect(
|
||||
await ac(asvc({}), rl(), {}, { saveAvatar }).avatar(user, { filename: 'a.jpg' } as Express.Multer.File),
|
||||
await ac(asvc({}), rl(), {}, { saveAvatar }).avatar(user, {
|
||||
filename: 'a.jpg',
|
||||
path: avatarFixture,
|
||||
} as Express.Multer.File),
|
||||
).toEqual({ avatar: '/a.jpg' });
|
||||
});
|
||||
|
||||
@@ -972,9 +977,12 @@ describe('AuthController (authenticated)', () => {
|
||||
it('avatar saves when not in demo mode (env present but email is not a demo email)', async () => {
|
||||
process.env.DEMO_MODE = 'true';
|
||||
vi.mocked(isDemoEmail).mockReturnValue(false);
|
||||
const saveAvatar = vi.fn().mockResolvedValue({ avatar: '/b.png' });
|
||||
const saveAvatar = vi.fn().mockResolvedValue({ avatar: '/b.jpg' });
|
||||
expect(
|
||||
await ac(asvc({}), rl(), {}, { saveAvatar }).avatar(user, { filename: 'b.png' } as Express.Multer.File),
|
||||
).toEqual({ avatar: '/b.png' });
|
||||
await ac(asvc({}), rl(), {}, { saveAvatar }).avatar(user, {
|
||||
filename: 'b.jpg',
|
||||
path: avatarFixture,
|
||||
} as Express.Multer.File),
|
||||
).toEqual({ avatar: '/b.jpg' });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
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',
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -190,6 +190,13 @@ describe('renditionFor', () => {
|
||||
});
|
||||
|
||||
describe('renditionFor re-encode fallback', () => {
|
||||
it('refuses a legacy avatar whose raster dimensions cannot be read', async () => {
|
||||
write('avatars', 'incomplete.bmp', Buffer.from('BM'));
|
||||
|
||||
expect(await service.renditionFor('avatars', 'incomplete.bmp')).toBeNull();
|
||||
expect(fs.readdirSync(path.join(root, '.spool'))).toEqual([]);
|
||||
});
|
||||
|
||||
it('PMEDIA-004: an image whose metadata cannot be cleaned is re-encoded instead of refused', async () => {
|
||||
const image = new Jimp({ width: 4, height: 2, color: 0x336699ff });
|
||||
const bmp = await image.getBuffer('image/bmp');
|
||||
|
||||
Reference in New Issue
Block a user