Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
🪒 fix: Strip Cache-Bust Suffixes From Every Local File Read (#15991)
lia-by-librechat[bot]be54587d14da
diff --git a/api/server/services/Files/Local/__tests__/cache-bust.spec.js b/api/server/services/Files/Local/__tests__/cache-bust.spec.jsnew file mode 100644index 0000000000..5004b15097--- /dev/null+++ b/api/server/services/Files/Local/__tests__/cache-bust.spec.js@@ -0,0 +1,89 @@+/**+ * Wiring coverage: `stripCacheBust`'s own behavior is covered in+ * `packages/api/src/storage/__tests__/path.test.ts`. These tests assert that the local read paths+ * hand their raw filepath to it, so a reused code output resolves to the file that exists on disk.+ */+jest.mock('@librechat/api', () => ({+ deleteRagFile: jest.fn(),+ stripCacheBust: jest.fn((filepath) => filepath.split('?')[0]),+}));+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('@librechat/api');+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('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 requested = '/images/user-1/chart.png?v=1789460622697';++ const stream = await getLocalFileStream(makeReq(), requested);++ await expect(readStream(stream)).resolves.toBe('image-bytes');+ expect(stripCacheBust).toHaveBeenCalledWith(requested);+ });++ it('streams an upload whose filepath carries a query string', async () => {+ const requested = '/uploads/user-1/doc.pdf?manual=true';++ const stream = await getLocalFileStream(makeReq(), requested);++ await expect(readStream(stream)).resolves.toBe('upload-bytes');+ expect(stripCacheBust).toHaveBeenCalledWith(requested);+ });++ 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/__tests__/images.spec.js b/api/server/services/Files/Local/__tests__/images.spec.jsnew file mode 100644index 0000000000..f64d547765--- /dev/null+++ b/api/server/services/Files/Local/__tests__/images.spec.js@@ -0,0 +1,64 @@+jest.mock('sharp', () => ({}));+jest.mock('@librechat/api', () => ({+ stripCacheBust: jest.fn((filepath) => filepath.split('?')[0]),+}));+jest.mock('../../images/resize', () => ({ resizeImageBuffer: jest.fn() }));+jest.mock('~/models', () => ({+ updateUser: jest.fn(),+ updateFile: jest.fn(async (doc) => doc),+}));++const fs = require('fs');+const os = require('os');+const path = require('path');+const { updateFile } = require('~/models');+const { prepareImagesLocal } = require('../images');++describe('prepareImagesLocal', () => {+ let tmpDir;+ let publicPath;+ let imageOutput;++ beforeEach(() => {+ jest.clearAllMocks();+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'prepare-images-local-'));+ publicPath = path.join(tmpDir, 'public');+ imageOutput = path.join(tmpDir, 'images');+ fs.mkdirSync(path.join(publicPath, 'images', 'user-1'), { recursive: true });+ });++ afterEach(() => {+ fs.rmSync(tmpDir, { recursive: true, force: true });+ });++ const makeReq = () => ({+ user: { id: 'user-1' },+ config: { paths: { publicPath, imageOutput } },+ });++ it('strips a cache-busting query string before encoding from disk', async () => {+ const relativePath = '/images/user-1/chart.png';+ fs.writeFileSync(path.join(publicPath, relativePath), Buffer.from('fake-png-bytes'));++ const [updated, encoded] = await prepareImagesLocal(makeReq(), {+ file_id: 'file-1',+ filepath: `${relativePath}?v=1789460622697`,+ });++ expect(updateFile).toHaveBeenCalledWith({ file_id: 'file-1' });+ expect(updated).toEqual({ file_id: 'file-1' });+ expect(encoded).toBe(Buffer.from('fake-png-bytes').toString('base64'));+ });++ it('encodes a filepath without a query string', async () => {+ const relativePath = '/images/user-1/chart.png';+ fs.writeFileSync(path.join(publicPath, relativePath), Buffer.from('plain-png-bytes'));++ const [, encoded] = await prepareImagesLocal(makeReq(), {+ file_id: 'file-2',+ filepath: relativePath,+ });++ expect(encoded).toBe(Buffer.from('plain-png-bytes').toString('base64'));+ });+});diff --git a/api/server/services/Files/Local/crud.js b/api/server/services/Files/Local/crud.jsindex 799bdfc3f7..fe52455098 100644--- a/api/server/services/Files/Local/crud.js+++ b/api/server/services/Files/Local/crud.js@@ -3,6 +3,7 @@ const path = require('path');const axios = require('axios');const {deleteRagFile,+ stripCacheBust,assertRemoteFileURL,getRemoteFileFetchMaxBytes,getRemoteFileFetchTimeoutMs,@@ -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=<timestamp>) */+ 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=<timestamp>` 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.jsindex d46f4779a6..ebfaf1e4c4 100644--- a/api/server/services/Files/Local/images.js+++ b/api/server/services/Files/Local/images.js@@ -1,6 +1,7 @@const fs = require('fs');const path = require('path');const sharp = require('sharp');+const { stripCacheBust } = require('@librechat/api');const { resizeImageBuffer } = require('../images/resize');const { updateUser, updateFile } = require('~/models');@@ -97,7 +98,7 @@ async function prepareImagesLocal(req, file) {if (!fs.existsSync(userPath)) {fs.mkdirSync(userPath, { recursive: true });}- const filepath = path.join(publicPath, file.filepath);+ const filepath = path.join(publicPath, stripCacheBust(file.filepath));const promises = [];promises.push(updateFile({ file_id: file.file_id }));diff --git a/packages/api/src/storage/__tests__/path.test.ts b/packages/api/src/storage/__tests__/path.test.tsindex 3ffaa5e41e..09179bb04b 100644--- a/packages/api/src/storage/__tests__/path.test.ts+++ b/packages/api/src/storage/__tests__/path.test.ts@@ -1,4 +1,4 @@-import { resolveDownloadPath } from '../path';+import { resolveDownloadPath, stripCacheBust } from '../path';describe('resolveDownloadPath', () => {it('prefers the recorded object key over the stored URL', () => {@@ -28,3 +28,21 @@ describe('resolveDownloadPath', () => {expect(resolveDownloadPath({ filepath: url })).toBe(url);});});++describe('stripCacheBust', () => {+ it('removes the cache-buster a regenerated code output persists', () => {+ expect(stripCacheBust('/images/u1/chart.png?v=1789460622697')).toBe('/images/u1/chart.png');+ });++ it('removes everything from the first question mark onward', () => {+ expect(stripCacheBust('/uploads/u1/doc.pdf?manual=true&v=2')).toBe('/uploads/u1/doc.pdf');+ });++ it('leaves a path without a query string untouched', () => {+ expect(stripCacheBust('/images/u1/chart.png')).toBe('/images/u1/chart.png');+ });++ it('keeps a name whose sanitized form cannot contain a question mark', () => {+ expect(stripCacheBust('/uploads/u1/what_is_this_.pdf')).toBe('/uploads/u1/what_is_this_.pdf');+ });+});diff --git a/packages/api/src/storage/path.ts b/packages/api/src/storage/path.tsindex 36ef64a030..3a201c0166 100644--- a/packages/api/src/storage/path.ts+++ b/packages/api/src/storage/path.ts@@ -12,3 +12,22 @@ export type StoredFileRef = Pick<TFile, 'filepath'> & { storageKey?: string | nuexport function resolveDownloadPath(file: StoredFileRef): string {return file.storageKey || file.filepath;}++/**+ * Strips the query string from a stored `filepath` before it is resolved on disk.+ *+ * A regenerated code-interpreter output persists a cache-busting `?v=<timestamp>` suffix on its+ * file document's `filepath` (`processCodeOutput`), and shared-link cache validators fold that+ * field in deliberately (`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. A remote strategy receives a+ * URL whose query string can carry a presigned signature, which is why `resolveDownloadPath`+ * hands its result over untouched.+ *+ * Unambiguous for local paths because `sanitizeFilename` replaces `?` with `_` in every stored+ * name, so no file on disk carries one.+ */+export function stripCacheBust(filepath: string): string {+ const queryIndex = filepath.indexOf('?');+ return queryIndex === -1 ? filepath : filepath.slice(0, queryIndex);+}