LibreChat/api/server/services/Files/Code/__tests__/process-traversal.spec.js
lia-by-librechat[bot] a51c45bbf3
🌳 fix: Preserve Primary Files Until Secondary Cleanup Succeeds (#17228)
* fix: Preserve primary files until secondary cleanup succeeds

* test: Use a flat deletion-strategy lookup

* fix: Preserve erasure retries and reconcile removed embeddings

* test: Match deletion regression formatting

* fix: Preserve search intent after vector cleanup

* fix: Repair retained project search files before tool execution

* fix: Normalize Retained Project File Runtime Types

* test: Use the Storage Source Enum in Project Repair Coverage

* fix: Confirm Secondary Cleanup and Preserve Repair Policy

* test: Correct Repair Fixture Types and Cleanup Retry Assertions

* fix: Reconcile Code Copies Compatibly and Charge Shared File Budgets

* fix: Reserve Admitted File Budgets Before Deferred Screening

* fix: Retain Concurrent File Copies and Scope Cleanup by Tenant

* fix: Bound Erasure File Checks and Correct Cleanup Fixtures

* fix: Preserve Provider-Only Assistant File Deletion

* fix: Reconcile Primary Vector Deletions Before Finalization

* fix: Finalize Successful Files in Mixed Cleanup Batches

* test: Load Real File Finalizer in Cleanup Integration Tests

* fix: Preserve Project Routing MIME and Fence File Storage Operations

* fix: Validate Provisioning Principal and Type Project Repair Fixtures

* fix: Recover Provisioning Claims and Retain Uncertain Storage Outcomes

* fix: Normalize File Recovery Results and Reconcile Transport Attempts

* test: Preserve Deployment Routes in Host Provisioning Fixtures

* fix: Recover Deletion Claims and Fence Generated Output Writes

* fix: Preserve Generated Output Commit Snapshots

* test: Use Persisted Owner Type in Generated File Fixture

* test: Type Generated Output Fixture with Its Commit Contract

* fix: Preserve Publication Error Handling and Cleanup Test Boundaries

---------

Co-authored-by: Lia <lia@librechat.ai>
2026-10-23 07:00:20 +02:00

191 lines
7.5 KiB
JavaScript

jest.mock('uuid', () => ({ v4: jest.fn(() => 'mock-uuid') }));
jest.mock('@librechat/data-schemas', () => ({
logger: { warn: jest.fn(), debug: jest.fn(), error: jest.fn() },
}));
jest.mock('@librechat/agents', () => ({
...jest.requireActual('@librechat/agents'),
getCodeBaseURL: jest.fn(() => 'http://localhost:8000'),
}));
const mockSanitizeArtifactPath = jest.fn();
const mockFlattenArtifactPath = jest.fn((name) => name.replace(/\//g, '__'));
const mockAxios = jest.fn().mockResolvedValue({
data: Buffer.from('file-content'),
});
mockAxios.post = jest.fn();
jest.mock('@librechat/api', () => {
const http = require('http');
const https = require('https');
return {
processCodeOutput: jest.requireActual('@librechat/api').processCodeOutput,
createFileProvisionRecovery: jest.requireActual('@librechat/api').createFileProvisionRecovery,
createSandboxTextReader: jest.requireActual('@librechat/api').createSandboxTextReader,
resolveDownloadPath: (file) => file.storageKey || file.filepath,
logAxiosError: jest.fn(),
getBasePath: jest.fn(() => ''),
sanitizeArtifactPath: mockSanitizeArtifactPath,
flattenArtifactPath: mockFlattenArtifactPath,
createAxiosInstance: jest.fn(() => mockAxios),
getCodeApiAuthHeaders: jest.fn(async () => ({})),
codeExecutionHeaders: jest.fn(() => ({})),
getCodeExecutionBaseUrl: jest.fn(() => 'http://localhost:8000'),
CODE_API_EXPECTED_PROFILE_HEADER: 'X-CodeAPI-Expected-Profile',
classifyCodeArtifact: jest.fn(() => 'other'),
extractCodeArtifactText: jest.fn(async () => null),
getBoundedCodeOutputByteLimit: (configured) =>
typeof configured === 'number' && Number.isFinite(configured) && configured > 0
? Math.min(configured, 64 * 1024 * 1024)
: 64 * 1024 * 1024,
/* `processCodeOutput` calls this to derive the trust flag persisted
* on `IMongoFile.textFormat` — Codex P1 review on PR #12934. The
* mock returns null in lockstep with the null `text` above so
* downstream consumers don't see a phantom format. */
getExtractedTextFormat: jest.fn(() => null),
getStorageMetadata: jest.fn(() => ({})),
/* Pass-through `withTimeout`: this suite asserts traversal sanitization,
* not deferred preview timing. */
withTimeout: async (promise) => promise,
/* These traversal cases all use non-office filenames — keep the
* inline (non-finalize) path so existing assertions on a single
* commitCodeFile call hold. */
hasOfficeHtmlPath: jest.fn(() => false),
/* Identity-helper stub mirroring `packages/api/src/files/code/identity.ts`.
* `processCodeOutput` calls this for every output download URL;
* traversal cases don't care about the query shape, just that it
* returns something concatable. */
buildCodeEnvDownloadQuery: jest.fn(({ kind, id, version }) => {
const params = new URLSearchParams({ kind, id });
if (version != null) params.set('version', String(version));
return `?${params.toString()}`;
}),
codeServerHttpAgent: new http.Agent({ keepAlive: false }),
codeServerHttpsAgent: new https.Agent({ keepAlive: false }),
};
});
jest.mock('librechat-data-provider', () => ({
...jest.requireActual('librechat-data-provider'),
mergeFileConfig: jest.fn(() => ({ serverFileSizeLimit: 100 * 1024 * 1024 })),
getEndpointFileConfig: jest.fn(() => ({
fileSizeLimit: 100 * 1024 * 1024,
supportedMimeTypes: ['*/*'],
})),
fileConfig: { checkType: jest.fn(() => true) },
}));
jest.mock('~/models', () => ({
createFile: jest.fn().mockResolvedValue({}),
getFiles: jest.fn().mockResolvedValue([]),
updateFile: jest.fn(),
claimCodeFile: jest.fn().mockResolvedValue({ file_id: 'mock-uuid', usage: 0 }),
commitCodeFile: jest.fn().mockResolvedValue(true),
acquireFileProvision: jest.fn().mockResolvedValue(true),
releaseFileProvision: jest.fn().mockResolvedValue(true),
commitFileProvision: jest.fn(async ({ output, releaseOnly }) => {
if (releaseOnly) return true;
return jest.requireMock('~/models').commitCodeFile(output.file, output.sourceDispatchedAt);
}),
}));
const mockSaveBuffer = jest.fn().mockResolvedValue('/uploads/user123/mock-uuid__output.csv');
jest.mock('~/server/services/Files/strategies', () => ({
getStrategyFunctions: jest.fn(() => ({
saveBuffer: mockSaveBuffer,
})),
}));
jest.mock('~/server/services/Files/permissions', () => ({
filterFilesByAgentAccess: jest.fn().mockResolvedValue([]),
}));
jest.mock('~/server/services/Files/images/convert', () => ({
convertImage: jest.fn(),
}));
jest.mock('~/server/utils', () => ({
determineFileType: jest.fn().mockResolvedValue({ mime: 'text/csv' }),
}));
jest.mock('~/server/services/Files/retention', () => ({
getRetentionExpiry: jest.fn(() => ({})),
}));
const { getRetentionExpiry } = require('~/server/services/Files/retention');
const { commitCodeFile } = require('~/models');
const { processCodeOutput } = require('../process');
const baseParams = {
req: {
user: { id: 'user123', tenantId: 'tenantA' },
config: {
fileStrategy: 'local',
imageOutputType: 'webp',
fileConfig: {},
},
},
id: 'code-file-id',
apiKey: 'test-key',
toolCallId: 'tool-1',
conversationId: 'conv-1',
messageId: 'msg-1',
session_id: 'session-1',
};
describe('processCodeOutput path traversal protection', () => {
beforeEach(() => {
jest.clearAllMocks();
});
test('sanitizeArtifactPath is called with the raw artifact name', async () => {
mockSanitizeArtifactPath.mockReturnValueOnce('output.csv');
await processCodeOutput({ ...baseParams, name: 'output.csv' });
expect(mockSanitizeArtifactPath).toHaveBeenCalledWith('output.csv');
});
test('sanitized name is used in saveBuffer fileName (and flattened to a single component)', async () => {
mockSanitizeArtifactPath.mockReturnValueOnce('sanitized-name.txt');
await processCodeOutput({ ...baseParams, name: '../../../tmp/poc.txt' });
expect(mockSanitizeArtifactPath).toHaveBeenCalledWith('../../../tmp/poc.txt');
const call = mockSaveBuffer.mock.calls[0][0];
/* `flattenArtifactPath` is identity for already-flat names; the assert
* is against the storage-key composition (`<file_id>__<flat>`). */
expect(call.fileName).toBe('mock-uuid__sanitized-name.txt');
});
test('sanitized name is stored as filename in the file record', async () => {
mockSanitizeArtifactPath.mockReturnValueOnce('safe-output.csv');
await processCodeOutput({ ...baseParams, name: 'unsafe/../../output.csv' });
const fileArg = commitCodeFile.mock.calls[0][0];
expect(fileArg.filename).toBe('safe-output.csv');
expect(fileArg.tenantId).toBe('tenantA');
});
test('getRetentionExpiry is called with the request object', async () => {
mockSanitizeArtifactPath.mockReturnValueOnce('output.csv');
await processCodeOutput({ ...baseParams, name: 'output.csv' });
expect(getRetentionExpiry).toHaveBeenCalledWith(baseParams.req);
});
test('sanitized name is used for image file records', async () => {
const { convertImage } = require('~/server/services/Files/images/convert');
convertImage.mockResolvedValueOnce({
filepath: '/images/user123/mock-uuid.webp',
bytes: 100,
});
mockSanitizeArtifactPath.mockReturnValueOnce('safe-chart.png');
await processCodeOutput({ ...baseParams, name: '../../../chart.png' });
expect(mockSanitizeArtifactPath).toHaveBeenCalledWith('../../../chart.png');
const fileArg = commitCodeFile.mock.calls[0][0];
expect(fileArg.filename).toBe('safe-chart.png');
expect(fileArg.tenantId).toBe('tenantA');
});
});