LibreChat/api/server/services/Files/Azure/crud.spec.js
Hao bd08a363c4
🔣 fix: Encode Images From Storage Keys (#15851)
* fix: Prefer storage keys for image encoding

* fix: Sanitize assistant image errors

* fix: Sanitize agent image storage errors

* fix: Move blob image encoding into API package

* fix: Complete image encoder TypeScript migration

* fix: Keep request-boundary error logs diagnosable

getSafeErrorMetadata reduces an error to { type: 'Error' } and an optional
status. That is the right bound where an error may echo submitted content, and
it stays in place at getS3FileStream, getAzureFileStream and
getFirebaseFileStream, where a signed URL reaches the log.

Applied at ResumableAgentController's initialization catch and the chatV1
generic branch it blinded the whole route: a missing key, an agent that fails to
load and a database error all logged as { type: 'Error' }, with no message,
name, code or stack.

getSafeErrorText keeps the error's own description and stack, reduces every URL
to its origin so an object path and signature cannot ride along, and strips
bearer credentials. It returns text so the two handlers log it inside the
message and pass no winston metadata, which is what stops an SDK error's own
enumerable properties (request.url, config, response.headers) from being
promoted onto the record.

---------

Co-authored-by: Danny Avila <danny@librechat.ai>
2026-09-12 12:39:26 -04:00

78 lines
2.9 KiB
JavaScript

const mockDownload = jest.fn();
const mockGetBlockBlobClient = jest.fn(() => ({ download: mockDownload }));
const mockGetAzureContainerClient = jest.fn(async () => ({
url: 'https://account.blob.core.windows.net/files',
getBlockBlobClient: mockGetBlockBlobClient,
}));
const mockGetSafeErrorMetadata = jest.fn(() => ({ type: 'Error', status: 403 }));
jest.mock('@librechat/data-schemas', () => ({
logger: { error: jest.fn() },
}));
jest.mock('@librechat/api', () => ({
deleteRagFile: jest.fn(),
assertRemoteFileURL: jest.fn((url) => url),
getSafeErrorMetadata: (...args) => mockGetSafeErrorMetadata(...args),
getAzureContainerClient: (...args) => mockGetAzureContainerClient(...args),
getRemoteFileFetchMaxBytes: jest.fn(() => 1024),
getRemoteFileFetchTimeoutMs: jest.fn(() => 1000),
assertRemoteFileContentLength: jest.fn(),
}));
const { logger } = require('@librechat/data-schemas');
const { getAzureFileStream } = require('./crud');
describe('getAzureFileStream', () => {
it('downloads private blobs through the authenticated Azure client', async () => {
const stream = { pipe: jest.fn() };
mockDownload.mockResolvedValue({ readableStreamBody: stream });
await expect(
getAzureFileStream(
{},
'https://account.blob.core.windows.net/private-files/uploads/user/report%20one.pdf',
),
).resolves.toBe(stream);
expect(mockGetAzureContainerClient).toHaveBeenCalledWith('private-files');
expect(mockGetBlockBlobClient).toHaveBeenCalledWith('uploads/user/report one.pdf');
});
it('resolves Azurite blobs relative to the configured account and container path', async () => {
const stream = { pipe: jest.fn() };
mockDownload.mockResolvedValue({ readableStreamBody: stream });
mockGetAzureContainerClient.mockResolvedValueOnce({
url: 'http://127.0.0.1:10000/devstoreaccount1/files',
getBlockBlobClient: mockGetBlockBlobClient,
});
await expect(
getAzureFileStream(
{},
'http://127.0.0.1:10000/devstoreaccount1/files/uploads/user/report%20one.pdf',
),
).resolves.toBe(stream);
expect(mockGetAzureContainerClient).toHaveBeenCalledWith();
expect(mockGetBlockBlobClient).toHaveBeenCalledWith('uploads/user/report one.pdf');
});
it('logs bounded metadata without the signed blob URL', async () => {
const signedUrl =
'https://account.blob.core.windows.net/files/uploads/user/report.pdf?sig=secret';
const failure = Object.assign(new Error(`Request failed for ${signedUrl}`), {
statusCode: 403,
});
mockDownload.mockRejectedValue(failure);
await expect(getAzureFileStream({}, signedUrl)).rejects.toBe(failure);
expect(mockGetSafeErrorMetadata).toHaveBeenCalledWith(failure);
expect(logger.error).toHaveBeenCalledWith('[getAzureFileStream] Error getting blob stream:', {
type: 'Error',
status: 403,
});
expect(JSON.stringify(logger.error.mock.calls)).not.toContain(signedUrl);
});
});