LibreChat/api/server/services/Files/Local/__tests__/images.spec.js
lia-by-librechat[bot] be54587d14
🪒 fix: Strip Cache-Bust Suffixes From Every Local File Read (#15991)
* 🧼 fix: Strip Cache-Bust Query Before Local Vision Encode

Reused code-interpreter images persist filepath with a ?v= suffix.
prepareImagesLocal now strips it before disk encode, matching crud/share.

Co-authored-by: Zsanz3 <Zsanz3@users.noreply.github.com>

* 🧪 test: Isolate prepareImagesLocal query-strip coverage

Mock sharp and resize so the local vision-encode test does not load
the rest of the file-strategy graph.

Co-authored-by: Zsanz3 <Zsanz3@users.noreply.github.com>

* 🪒 fix: Strip Cache-Bust Suffixes From Every Local File Read

Reused code-interpreter outputs persist a `?v=<timestamp>` suffix on the file
document's filepath, and local storage resolves that field into a filesystem path.
prepareImagesLocal already handled it after the cherry-picked commits; getLocalFileStream
did not, so every download-stream consumer (the download route, provisioning, agent and
skill file reads) still hit ENOENT for a regenerated image.

Move the strip into one documented helper and call it from all three local read paths.

* 🧱 refactor: Move Cache-Bust Stripping Into the Storage Package

`/api` holds wiring, not behavior, so `stripCacheBust` moves out of a new CJS helper and
into `packages/api/src/storage/path.ts`, beside `resolveDownloadPath` — the function that
already decides what a stored filepath means to a download. Its doc comment now states why
the two differ: a remote strategy's query string can carry a presigned signature, so only
the local paths may strip.

The local storage modules keep just the call into it. Semantics are covered by the storage
package's own test; the `/api` specs pin the wiring.

Addresses the Codex P1 finding on 5f8dbf3cf.

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Zsanz3 <Zsanz3@users.noreply.github.com>
Co-authored-by: Lia <lia@librechat.ai>
2026-09-16 08:28:32 -04:00

64 lines
2.1 KiB
JavaScript

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'));
});
});