Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp
15.5 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-or-later

import {createHash} from 'node:crypto';
import fs from 'node:fs';
import {createTestAccount} from '@app/api/auth/tests/AuthTestUtils';
import {
	createChannel,
	createGuild,
	loadFixture,
	sendMessageWithAttachments,
} from '@app/api/channel/tests/AttachmentTestUtils';
import type {MediaProxyMetadataRequest, MediaProxyStorageObject} from '@app/api/infrastructure/IMediaService';
import {fileShaCache} from '@app/api/middleware/FileShaCache';
import {setInjectedMediaService} from '@app/api/middleware/ServiceRegistry';
import {type ApiTestHarness, createApiTestHarness} from '@app/api/test/ApiTestHarness';
import {HTTP_STATUS} from '@app/api/test/TestConstants';
import {TestMediaService} from '@app/api/test/TestMediaService';
import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';
import {beforeAll, beforeEach, describe, expect, it} from 'vitest';

class NullUploadMetadataService extends TestMediaService {
	override async getMetadata(request: MediaProxyMetadataRequest) {
		if (request.type === 'upload') {
			return null;
		}
		return super.getMetadata(request);
	}
}

const STRIPPED_BYTES = Buffer.from('stripped media bytes');

class RewritingStripMediaService extends TestMediaService {
	override async stripObjectMetadata(_source: MediaProxyStorageObject, contentType: string, outputPath: string) {
		await fs.promises.writeFile(outputPath, STRIPPED_BYTES);
		return {contentType, width: 100, height: 100};
	}
}

describe('Attachment Upload Validation', () => {
	let harness: ApiTestHarness;
	beforeAll(async () => {
		harness = await createApiTestHarness();
	});
	beforeEach(async () => {
		await harness.reset();
		setInjectedMediaService(new TestMediaService(harness.storageService));
	});
	describe('File Validation', () => {
		describe('size limits', () => {
			it('should reject files exceeding size limits', async () => {
				const account = await createTestAccount(harness);
				const guild = await createGuild(harness, account.token, 'Too Large File Test Guild');
				const channel = await createChannel(harness, account.token, guild.id, 'test-channel');
				const channelId = guild.system_channel_id ?? channel.id;
				const largeFileData = Buffer.alloc(26 * 1024 * 1024);
				const payload = {
					content: 'Large file test',
					attachments: [{id: 0, filename: 'large.bin'}],
				};
				const {response} = await sendMessageWithAttachments(harness, account.token, channelId, payload, [
					{index: 0, filename: 'large.bin', data: largeFileData},
				]);
				expect(response.status).toBe(400);
			});
			it('should store media files as plain attachments when metadata extraction fails', async () => {
				setInjectedMediaService(new NullUploadMetadataService(harness.storageService));
				const account = await createTestAccount(harness);
				const guild = await createGuild(harness, account.token, 'Invalid Media Metadata Test Guild');
				const channel = await createChannel(harness, account.token, guild.id, 'test-channel');
				const channelId = guild.system_channel_id ?? channel.id;
				const payload = {
					content: 'Invalid media metadata test',
					attachments: [{id: 0, filename: 'bad.png'}],
				};
				const {response, text} = await sendMessageWithAttachments(harness, account.token, channelId, payload, [
					{index: 0, filename: 'bad.png', data: loadFixture('yeah.png')},
				]);
				const body = JSON.parse(text) as {
					attachments?: Array<{filename?: string; width?: number | null; height?: number | null}>;
				};
				expect(response.status).toBe(200);
				expect(body.attachments?.[0]?.filename).toBe('bad.png');
				expect(body.attachments?.[0]?.width).toBeNull();
				expect(body.attachments?.[0]?.height).toBeNull();
			});
			it('should refuse media whose stripped bytes are on the file hash list', async () => {
				setInjectedMediaService(new RewritingStripMediaService(harness.storageService));
				const account = await createTestAccount(harness);
				const guild = await createGuild(harness, account.token, 'Stripped Hash Test Guild');
				const channel = await createChannel(harness, account.token, guild.id, 'test-channel');
				const channelId = guild.system_channel_id ?? channel.id;
				fileShaCache.add(createHash('sha256').update(STRIPPED_BYTES).digest('hex'));
				const {response, text} = await sendMessageWithAttachments(
					harness,
					account.token,
					channelId,
					{content: 'stripped hash test', attachments: [{id: 0, filename: 'yeah.png'}]},
					[{index: 0, filename: 'yeah.png', data: loadFixture('yeah.png')}],
				);
				const body = JSON.parse(text) as {code?: string};
				expect(response.status).toBe(HTTP_STATUS.FORBIDDEN);
				expect(body.code).toBe(APIErrorCodes.CONTENT_BLOCKED);
			});
			it('should return service unavailable when fallback multipart storage upload fails', async () => {
				const account = await createTestAccount(harness);
				const guild = await createGuild(harness, account.token, 'Storage Unavailable Test Guild');
				const channel = await createChannel(harness, account.token, guild.id, 'test-channel');
				const channelId = guild.system_channel_id ?? channel.id;
				harness.storageService.configure({shouldFailUpload: true});
				try {
					const payload = {
						content: 'storage unavailable test',
						attachments: [{id: 0, filename: 'diagnostics.txt'}],
					};
					const {response, text} = await sendMessageWithAttachments(harness, account.token, channelId, payload, [
						{index: 0, filename: 'diagnostics.txt', data: Buffer.from('diagnostics')},
					]);
					const body = JSON.parse(text) as {code?: string};
					expect(response.status).toBe(HTTP_STATUS.SERVICE_UNAVAILABLE);
					expect(body.code).toBe(APIErrorCodes.SERVICE_UNAVAILABLE);
				} finally {
					harness.storageService.configure({shouldFailUpload: false});
				}
			});
		});
		describe('filename validation', () => {
			it('should use metadata filename when different from upload filename', async () => {
				const account = await createTestAccount(harness);
				const guild = await createGuild(harness, account.token, 'Filename Mismatch Test Guild');
				const channel = await createChannel(harness, account.token, guild.id, 'test-channel');
				const channelId = guild.system_channel_id ?? channel.id;
				const fileData = Buffer.from('test content');
				const payload = {
					content: 'Filename mismatch test',
					attachments: [{id: 0, filename: 'expected.txt'}],
				};
				const {response, json} = await sendMessageWithAttachments(harness, account.token, channelId, payload, [
					{index: 0, filename: 'different.txt', data: fileData},
				]);
				expect(response.status).toBe(200);
				expect(json.attachments).toBeDefined();
				expect(json.attachments).not.toBeNull();
				expect(json.attachments!).toHaveLength(1);
				expect(json.attachments![0].filename).toBe('expected.txt');
			});
		});
		describe('special characters', () => {
			let channelId: string;
			let accountToken: string;
			beforeEach(async () => {
				const account = await createTestAccount(harness);
				accountToken = account.token;
				const guild = await createGuild(harness, account.token, 'Special Chars Test Guild');
				const channel = await createChannel(harness, account.token, guild.id, 'test-channel');
				channelId = guild.system_channel_id ?? channel.id;
			});
			const testCases = [
				{filename: 'file<script>.txt', description: 'HTML tag in filename'},
				{filename: 'file>output.txt', description: 'redirect operator'},
				{filename: 'file|pipe.txt', description: 'pipe character'},
				{filename: 'file:colon.txt', description: 'colon (Windows reserved)'},
				{filename: 'file*star.txt', description: 'asterisk (wildcard)'},
				{filename: 'file?question.txt', description: 'question mark'},
				{filename: 'file"quote.txt', description: 'double quote'},
				{filename: 'COM1.txt', description: 'Windows reserved name'},
				{filename: 'LPT1.txt', description: 'Windows reserved name'},
			];
			for (const tc of testCases) {
				it(`should handle ${tc.description}`, async () => {
					const fileData = Buffer.from('test content');
					const payload = {
						content: `Special char test: ${tc.description}`,
						attachments: [
							{
								id: 0,
								filename: tc.filename,
							},
						],
					};
					const {response, json} = await sendMessageWithAttachments(harness, accountToken, channelId, payload, [
						{index: 0, filename: 'test.txt', data: fileData},
					]);
					expect(response.status).toBe(200);
					expect(json.attachments).toBeDefined();
					expect(json.attachments).not.toBeNull();
					expect(json.attachments!).toHaveLength(1);
					const sanitized = json.attachments![0].filename;
					const dangerousChars = ['<', '>', ':', '"', '|', '?', '*'];
					for (const char of dangerousChars) {
						expect(sanitized).not.toContain(char);
					}
				});
			}
		});
		describe('path traversal', () => {
			const testCases = [
				{input: '../../../etc/passwd', expectedSuffix: 'passwd'},
				{input: '..\\\\..\\\\..\\\\windows\\\\system32\\\\config\\\\sam', expectedSuffix: 'sam'},
				{input: '....//....//....//etc/passwd', expectedSuffix: 'passwd'},
				{input: '..\\\\..\\\\..\\\\', expectedSuffix: ''},
				{input: '../../sensitive.txt', expectedSuffix: 'sensitive.txt'},
				{input: './../../etc/hosts', expectedSuffix: 'hosts'},
			];
			for (const tc of testCases) {
				it(`should sanitize path traversal in filename: ${tc.input}`, async () => {
					const account = await createTestAccount(harness);
					const guild = await createGuild(harness, account.token, 'Path Traversal Test Guild');
					const channel = await createChannel(harness, account.token, guild.id, 'test-channel');
					const channelId = guild.system_channel_id ?? channel.id;
					const fileData = Buffer.from('test content');
					const payload = {
						content: 'Path traversal test',
						attachments: [{id: 0, filename: tc.input}],
					};
					const {response, json} = await sendMessageWithAttachments(harness, account.token, channelId, payload, [
						{index: 0, filename: tc.input, data: fileData},
					]);
					expect(response.status).toBe(200);
					expect(json.attachments).toBeDefined();
					expect(json.attachments).not.toBeNull();
					expect(json.attachments!).toHaveLength(1);
					const sanitized = json.attachments![0].filename;
					expect(sanitized).not.toContain('..');
					expect(sanitized).not.toContain('/');
					expect(sanitized).not.toContain('\\');
				});
			}
		});
	});
	describe('Metadata Validation', () => {
		describe('id matching', () => {
			it('should reject negative IDs in metadata', async () => {
				const account = await createTestAccount(harness);
				const guild = await createGuild(harness, account.token, 'Negative ID Test Guild');
				const channel = await createChannel(harness, account.token, guild.id, 'test-channel');
				const channelId = guild.system_channel_id ?? channel.id;
				const fileData = Buffer.from('test content');
				const payload = {
					content: 'Negative ID test',
					attachments: [{id: -1, filename: 'test.txt'}],
				};
				const {response} = await sendMessageWithAttachments(harness, account.token, channelId, payload, [
					{index: 0, filename: 'test.txt', data: fileData},
				]);
				expect(response.status).toBe(400);
			});
			it('should reject mismatched IDs between metadata and files', async () => {
				const account = await createTestAccount(harness);
				const guild = await createGuild(harness, account.token, 'ID Mismatch Test Guild');
				const channel = await createChannel(harness, account.token, guild.id, 'test-channel');
				const channelId = guild.system_channel_id ?? channel.id;
				const fileData = Buffer.from('test content');
				const payload = {
					content: 'ID mismatch test',
					attachments: [{id: 5, filename: 'test.txt'}],
				};
				const {response} = await sendMessageWithAttachments(harness, account.token, channelId, payload, [
					{index: 0, filename: 'test.txt', data: fileData},
				]);
				expect(response.status).toBe(400);
			});
			it('should handle non-sequential IDs like 2, 5', async () => {
				const account = await createTestAccount(harness);
				const guild = await createGuild(harness, account.token, 'Sparse IDs Test Guild');
				const channel = await createChannel(harness, account.token, guild.id, 'test-channel');
				const channelId = guild.system_channel_id ?? channel.id;
				const file1Data = loadFixture('yeah.png');
				const file2Data = loadFixture('animated.gif');
				const payload = {
					content: 'Sparse IDs test',
					attachments: [
						{id: 2, filename: 'yeah.png', description: 'ID is 2', title: 'Two'},
						{id: 5, filename: 'animated.gif', description: 'ID is 5', title: 'Five'},
					],
				};
				const {response, json} = await sendMessageWithAttachments(harness, account.token, channelId, payload, [
					{index: 2, filename: 'yeah.png', data: file1Data},
					{index: 5, filename: 'animated.gif', data: file2Data},
				]);
				expect(response.status).toBe(200);
				expect(json.attachments).toBeDefined();
				expect(json.attachments).not.toBeNull();
				expect(json.attachments!).toHaveLength(2);
				expect(json.attachments![0].description).toBe('ID is 2');
				expect(json.attachments![0].title).toBe('Two');
				expect(json.attachments![1].description).toBe('ID is 5');
				expect(json.attachments![1].title).toBe('Five');
			});
		});
		describe('metadata requirements', () => {
			it('should allow file upload without attachment metadata', async () => {
				const account = await createTestAccount(harness);
				const guild = await createGuild(harness, account.token, 'Without Metadata Test Guild');
				const channel = await createChannel(harness, account.token, guild.id, 'test-channel');
				const channelId = guild.system_channel_id ?? channel.id;
				const fileData = loadFixture('yeah.png');
				const payload = {
					content: 'File without metadata',
				};
				const {response} = await sendMessageWithAttachments(harness, account.token, channelId, payload, [
					{index: 0, filename: 'yeah.png', data: fileData},
				]);
				expect(response.status).toBe(200);
			});
			it('should reject attachment metadata without corresponding file', async () => {
				const account = await createTestAccount(harness);
				const guild = await createGuild(harness, account.token, 'Metadata Without File Test Guild');
				const channel = await createChannel(harness, account.token, guild.id, 'test-channel');
				const channelId = guild.system_channel_id ?? channel.id;
				const payload = {
					content: 'Metadata without file',
					attachments: [{id: 0, filename: 'missing.png', description: 'This file does not exist'}],
				};
				const {response} = await sendMessageWithAttachments(harness, account.token, channelId, payload, []);
				expect(response.status).toBe(400);
			});
		});
	});
	describe('Flag Preservation', () => {
		it('should preserve attachment flags through upload flow', async () => {
			const account = await createTestAccount(harness);
			const guild = await createGuild(harness, account.token, 'Flags Preserved Test Guild');
			const channel = await createChannel(harness, account.token, guild.id, 'test-channel');
			const channelId = guild.system_channel_id ?? channel.id;
			const fileData = loadFixture('yeah.png');
			const payload = {
				content: 'Flags preservation test',
				attachments: [
					{
						id: 0,
						filename: 'spoiler.png',
						flags: 8,
					},
				],
			};
			const {response, json} = await sendMessageWithAttachments(harness, account.token, channelId, payload, [
				{index: 0, filename: 'spoiler.png', data: fileData},
			]);
			expect(response.status).toBe(200);
			expect(json.attachments).toBeDefined();
			expect(json.attachments).not.toBeNull();
			expect(json.attachments!).toHaveLength(1);
			expect(json.attachments![0].flags).toBe(8);
		});
	});
});