diff --git a/api/server/services/Files/Local/__tests__/cache-bust.spec.js b/api/server/services/Files/Local/__tests__/cache-bust.spec.js new file mode 100644 index 0000000000..cf66a53e6e --- /dev/null +++ b/api/server/services/Files/Local/__tests__/cache-bust.spec.js @@ -0,0 +1,97 @@ +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(), 'local-cache-bust-'), +); + +jest.mock('~/config/paths', () => { + const path = require('path'); + return { + publicPath: path.join(mockTmpBase, 'public'), + uploads: path.join(mockTmpBase, 'uploads'), + imageOutput: path.join(mockTmpBase, 'public', 'images'), + }; +}); + +const fs = require('fs'); +const path = require('path'); +const { stripCacheBust } = require('../paths'); +const { getLocalFileStream } = require('../crud'); + +const imageOutput = path.join(mockTmpBase, 'public', 'images'); +const uploads = path.join(mockTmpBase, 'uploads'); + +const makeReq = () => ({ + user: { id: 'user-1' }, + config: { paths: { publicPath: path.join(mockTmpBase, 'public'), uploads, imageOutput } }, +}); + +const readStream = (stream) => + new Promise((resolve, reject) => { + const chunks = []; + stream.on('data', (chunk) => chunks.push(chunk)); + stream.on('error', reject); + stream.on('end', () => resolve(Buffer.concat(chunks).toString())); + }); + +describe('stripCacheBust', () => { + it('removes a cache-busting query string', () => { + expect(stripCacheBust('/images/user-1/chart.png?v=1789460622697')).toBe( + '/images/user-1/chart.png', + ); + }); + + it('removes everything from the first question mark onward', () => { + expect(stripCacheBust('/uploads/user-1/doc.pdf?manual=true&v=2')).toBe( + '/uploads/user-1/doc.pdf', + ); + }); + + it('leaves a filepath without a query string untouched', () => { + expect(stripCacheBust('/images/user-1/chart.png')).toBe('/images/user-1/chart.png'); + }); + + it('passes through non-string input', () => { + expect(stripCacheBust(undefined)).toBeUndefined(); + }); +}); + +describe('getLocalFileStream cache-busted filepaths', () => { + beforeAll(() => { + fs.mkdirSync(path.join(imageOutput, 'user-1'), { recursive: true }); + fs.mkdirSync(path.join(uploads, 'user-1'), { recursive: true }); + fs.writeFileSync(path.join(imageOutput, 'user-1', 'chart.png'), 'image-bytes'); + fs.writeFileSync(path.join(uploads, 'user-1', 'doc.pdf'), 'upload-bytes'); + }); + + afterAll(() => { + fs.rmSync(mockTmpBase, { recursive: true, force: true }); + }); + + it('streams a reused code-output image whose filepath carries `?v=`', async () => { + const stream = await getLocalFileStream(makeReq(), '/images/user-1/chart.png?v=1789460622697'); + + await expect(readStream(stream)).resolves.toBe('image-bytes'); + }); + + it('streams an upload whose filepath carries a query string', async () => { + const stream = await getLocalFileStream(makeReq(), '/uploads/user-1/doc.pdf?manual=true'); + + await expect(readStream(stream)).resolves.toBe('upload-bytes'); + }); + + it('still streams a filepath without a query string', async () => { + const stream = await getLocalFileStream(makeReq(), '/images/user-1/chart.png'); + + await expect(readStream(stream)).resolves.toBe('image-bytes'); + }); + + it('still rejects traversal hidden behind a query string', async () => { + await expect(getLocalFileStream(makeReq(), '/images/../../../etc/passwd?v=1')).rejects.toThrow( + 'Invalid file path', + ); + }); +}); diff --git a/api/server/services/Files/Local/crud.js b/api/server/services/Files/Local/crud.js index 799bdfc3f7..5c69193407 100644 --- a/api/server/services/Files/Local/crud.js +++ b/api/server/services/Files/Local/crud.js @@ -13,6 +13,7 @@ const { EModelEndpoint } = require('librechat-data-provider'); const { resizeImageBuffer } = require('~/server/services/Files/images/resize'); const { getBufferMetadata } = require('~/server/utils'); const paths = require('~/config/paths'); +const { stripCacheBust } = require('./paths'); /** * Saves a file to a specified output path with a new filename. @@ -231,8 +232,8 @@ const deleteLocalFile = async (req, file) => { const appConfig = req.config; const { publicPath, uploads } = appConfig.paths; - /** Filepath stripped of query parameters (e.g., ?manual=true) */ - const cleanFilepath = file.filepath.split('?')[0]; + /** Filepath stripped of query parameters (e.g., ?manual=true, ?v=) */ + const cleanFilepath = stripCacheBust(file.filepath); await deleteRagFile({ userId: req.user.id, file }); @@ -321,12 +322,14 @@ async function uploadLocalFile({ req, file, file_id }) { * Retrieves a readable stream for a file from local storage. * * @param {ServerRequest} req - The request object from Express - * @param {string} filepath - The filepath. + * @param {string} requestedFilepath - The filepath, which may carry a cache-busting query string. * @returns {ReadableStream} A readable stream of the file. */ -async function getLocalFileStream(req, filepath) { +async function getLocalFileStream(req, requestedFilepath) { try { const appConfig = req.config; + /** Reused code outputs persist a `?v=` suffix that no file on disk carries */ + const filepath = stripCacheBust(requestedFilepath); if (filepath.includes('/uploads/')) { const basePath = filepath.split('/uploads/')[1]; diff --git a/api/server/services/Files/Local/images.js b/api/server/services/Files/Local/images.js index 7b1a9bc7eb..c91889fb70 100644 --- a/api/server/services/Files/Local/images.js +++ b/api/server/services/Files/Local/images.js @@ -2,6 +2,7 @@ const fs = require('fs'); const path = require('path'); const sharp = require('sharp'); const { resizeImageBuffer } = require('../images/resize'); +const { stripCacheBust } = require('./paths'); const { updateUser, updateFile } = require('~/models'); /** @@ -97,9 +98,7 @@ async function prepareImagesLocal(req, file) { if (!fs.existsSync(userPath)) { fs.mkdirSync(userPath, { recursive: true }); } - // Strip cache-busting query strings (e.g. reused code-output images add `?v=...`) - // so encodeImage reads the real file, not a literal `*.png?v=...` path. - const filepath = path.join(publicPath, file.filepath.split('?')[0]); + const filepath = path.join(publicPath, stripCacheBust(file.filepath)); const promises = []; promises.push(updateFile({ file_id: file.file_id })); diff --git a/api/server/services/Files/Local/paths.js b/api/server/services/Files/Local/paths.js new file mode 100644 index 0000000000..ecb6749f6e --- /dev/null +++ b/api/server/services/Files/Local/paths.js @@ -0,0 +1,26 @@ +/** + * Strips the query string from a stored `filepath` before it is resolved on disk. + * + * A regenerated code-interpreter output persists a cache-busting `?v=` suffix on its + * file document's `filepath` (`processCodeOutput`), and shared-link cache validators fold that + * field in on purpose (`buildShareFileEtag`), so the suffix cannot be dropped at write time. + * Local storage is the only strategy that turns `filepath` into a filesystem path, so it is the + * only one that has to remove the suffix before reading or deleting: remote strategies must keep + * it, since their `filepath` is a URL whose query string can carry a presigned signature. + * + * Stripping is unambiguous here because `sanitizeFilename` replaces `?` with `_` in every stored + * name, so no legitimate local path contains one. + * + * @param {string} filepath - The stored filepath, possibly suffixed with `?v=`. + * @returns {string} The filepath without its query string. + */ +function stripCacheBust(filepath) { + if (typeof filepath !== 'string') { + return filepath; + } + + const queryIndex = filepath.indexOf('?'); + return queryIndex === -1 ? filepath : filepath.slice(0, queryIndex); +} + +module.exports = { stripCacheBust };