mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-03-15 04:06:33 +01:00
* fix: sanitize artifact filenames to prevent path traversal in code output * test: Mock sanitizeFilename function in process.spec.js to return the original filename - Added a mock implementation for the `sanitizeFilename` function in the `process.spec.js` test file to return the original filename, ensuring that tests can run without altering the filename during the testing process. * fix: use path.relative for traversal check, sanitize all filenames, add security logging - Replace startsWith with path.relative pattern in saveLocalBuffer, consistent with deleteLocalFile and getLocalFileStream in the same file - Hoist sanitizeFilename call before the image/non-image branch so both code paths store the sanitized name in MongoDB - Log a warning when sanitizeFilename mutates a filename (potential traversal) - Log a specific warning when saveLocalBuffer throws a traversal error, so security events are distinguishable from generic network errors in the catch * test: improve traversal test coverage and remove mock reimplementation - Remove partial sanitizeFilename reimplementation from process-traversal tests; use controlled mock returns to verify processCodeOutput wiring instead - Add test for image branch sanitization - Use mkdtempSync for test isolation in crud-traversal to avoid parallel worker collisions - Add prefix-collision bypass test case (../user10/evil vs user1 directory) * fix: use path.relative in isValidPath to prevent prefix-collision bypass Pre-existing startsWith check without path separator had the same class of prefix-collision vulnerability fixed in saveLocalBuffer.
69 lines
2.1 KiB
JavaScript
69 lines
2.1 KiB
JavaScript
jest.mock('@librechat/api', () => ({ deleteRagFile: jest.fn() }));
|
|
jest.mock('@librechat/data-schemas', () => ({
|
|
logger: { warn: jest.fn(), error: jest.fn() },
|
|
}));
|
|
|
|
const mockTmpBase = require('fs').mkdtempSync(
|
|
require('path').join(require('os').tmpdir(), 'crud-traversal-'),
|
|
);
|
|
|
|
jest.mock('~/config/paths', () => {
|
|
const path = require('path');
|
|
return {
|
|
publicPath: path.join(mockTmpBase, 'public'),
|
|
uploads: path.join(mockTmpBase, 'uploads'),
|
|
};
|
|
});
|
|
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const { saveLocalBuffer } = require('../crud');
|
|
|
|
describe('saveLocalBuffer path containment', () => {
|
|
beforeAll(() => {
|
|
fs.mkdirSync(path.join(mockTmpBase, 'public', 'images'), { recursive: true });
|
|
fs.mkdirSync(path.join(mockTmpBase, 'uploads'), { recursive: true });
|
|
});
|
|
|
|
afterAll(() => {
|
|
fs.rmSync(mockTmpBase, { recursive: true, force: true });
|
|
});
|
|
|
|
test('rejects filenames with path traversal sequences', async () => {
|
|
await expect(
|
|
saveLocalBuffer({
|
|
userId: 'user1',
|
|
buffer: Buffer.from('malicious'),
|
|
fileName: '../../../etc/passwd',
|
|
basePath: 'uploads',
|
|
}),
|
|
).rejects.toThrow('Path traversal detected in filename');
|
|
});
|
|
|
|
test('rejects prefix-collision traversal (startsWith bypass)', async () => {
|
|
fs.mkdirSync(path.join(mockTmpBase, 'uploads', 'user10'), { recursive: true });
|
|
await expect(
|
|
saveLocalBuffer({
|
|
userId: 'user1',
|
|
buffer: Buffer.from('malicious'),
|
|
fileName: '../user10/evil',
|
|
basePath: 'uploads',
|
|
}),
|
|
).rejects.toThrow('Path traversal detected in filename');
|
|
});
|
|
|
|
test('allows normal filenames', async () => {
|
|
const result = await saveLocalBuffer({
|
|
userId: 'user1',
|
|
buffer: Buffer.from('safe content'),
|
|
fileName: 'file-id__output.csv',
|
|
basePath: 'uploads',
|
|
});
|
|
|
|
expect(result).toBe('/uploads/user1/file-id__output.csv');
|
|
|
|
const filePath = path.join(mockTmpBase, 'uploads', 'user1', 'file-id__output.csv');
|
|
expect(fs.existsSync(filePath)).toBe(true);
|
|
fs.unlinkSync(filePath);
|
|
});
|
|
});
|