mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-20 15:02:02 +00:00
🔤 fix: Encode Unicode Filenames in Project File Uploads (#17878)
* fix: Encode Unicode Filenames in Project File Uploads Project uploads sent the raw filename, which multer reads as latin1 and the upload route then decodes with decodeURIComponent. Non-ASCII names were garbled, a stray percent sign failed with a 400, and a valid escape such as %20 was silently rewritten. Percent-encode the name the same way chat uploads do. * test: Cover Unicode and Percent Project Upload Filenames
This commit is contained in:
parent
44eb8fea36
commit
fb1ec5ff3f
3 changed files with 65 additions and 1 deletions
|
|
@ -251,6 +251,20 @@ describe('ProjectResources', () => {
|
|||
);
|
||||
});
|
||||
|
||||
it.each(['Описание.md', '日本語.md', '100% done.md', 'a%20b.md'])(
|
||||
'percent-encodes the multipart filename of %s for the upload route to decode',
|
||||
async (filename) => {
|
||||
mockUploadMutateAsync.mockResolvedValue(uploadedFile);
|
||||
renderResources();
|
||||
fireEvent.change(screen.getByTestId('project-upload-input'), {
|
||||
target: { files: [new File(['x'], filename)] },
|
||||
});
|
||||
await waitFor(() => expect(mockUploadMutateAsync).toHaveBeenCalledTimes(1));
|
||||
const formData = mockUploadMutateAsync.mock.calls[0][0] as FormData;
|
||||
expect((formData.get('file') as File).name).toBe(encodeURIComponent(filename));
|
||||
},
|
||||
);
|
||||
|
||||
it('retries a failed association without uploading another binary', async () => {
|
||||
mockUploadMutateAsync.mockResolvedValue(uploadedFile);
|
||||
mockAddMutateAsync.mockRejectedValueOnce({
|
||||
|
|
|
|||
|
|
@ -259,7 +259,7 @@ export default function ProjectResources({ project }: ProjectResourcesProps) {
|
|||
let fileId = item.fileId;
|
||||
if (!fileId) {
|
||||
const formData = new FormData();
|
||||
formData.append('file', item.file);
|
||||
formData.append('file', item.file, encodeURIComponent(item.file.name));
|
||||
formData.append('file_id', item.id);
|
||||
formData.append('endpoint', 'agents');
|
||||
formData.append('message_file', 'true');
|
||||
|
|
|
|||
|
|
@ -187,6 +187,38 @@ async function uploadFromDevice(page: Page, files: Array<{ name: string; content
|
|||
return uploadResponse;
|
||||
}
|
||||
|
||||
/** Reloads before asserting so the row shows the stored name, not the browser's local File. */
|
||||
async function expectUploadStoredAs(
|
||||
page: Page,
|
||||
request: APIRequestContext,
|
||||
sentName: string,
|
||||
storedName: string,
|
||||
): Promise<void> {
|
||||
let projectId: string | undefined;
|
||||
const fileIds: string[] = [];
|
||||
try {
|
||||
await enableProjectFiles(page);
|
||||
projectId = await createProject(page, uniqueName('Filename project'));
|
||||
await page.reload({ waitUntil: 'domcontentloaded' });
|
||||
|
||||
const uploadResponse = await uploadFromDevice(page, [
|
||||
{ name: sentName, content: 'project file with a non-trivial name' },
|
||||
]);
|
||||
expect(uploadResponse.ok()).toBeTruthy();
|
||||
const uploaded = (await uploadResponse.json()) as UploadedFile;
|
||||
if (uploaded.file_id) {
|
||||
fileIds.push(uploaded.file_id);
|
||||
}
|
||||
|
||||
const row = page.getByRole('listitem').filter({ hasText: storedName });
|
||||
await expect(row).toContainText('Ready', { timeout: 30000 });
|
||||
await page.reload({ waitUntil: 'domcontentloaded' });
|
||||
await expect(row).toContainText('Ready', { timeout: 30000 });
|
||||
} finally {
|
||||
await cleanupProjectAndFiles(page, request, projectId, fileIds, [storedName]);
|
||||
}
|
||||
}
|
||||
|
||||
test.describe('project file workspace UI', () => {
|
||||
test('selecting several files at once queues every file up to the project limit and reports the excess @scenario:multi-file-upload-queues-to-capacity-and-reports-excess', async ({
|
||||
page,
|
||||
|
|
@ -406,4 +438,22 @@ test.describe('project file workspace UI', () => {
|
|||
await cleanupProjectAndFiles(page, request, projectId, fileIds, filenames);
|
||||
}
|
||||
});
|
||||
|
||||
test('a project file with a non-Latin name keeps that name after upload @scenario:project-upload-keeps-non-latin-filename', async ({
|
||||
page,
|
||||
request,
|
||||
}) => {
|
||||
test.setTimeout(120000);
|
||||
const name = `Описание-日本語-${randomUUID().slice(0, 8)}.txt`;
|
||||
await expectUploadStoredAs(page, request, name, name);
|
||||
});
|
||||
|
||||
test('a project file with a percent sign in its name uploads instead of being rejected @scenario:project-upload-accepts-percent-filename', async ({
|
||||
page,
|
||||
request,
|
||||
}) => {
|
||||
test.setTimeout(120000);
|
||||
const stem = randomUUID().slice(0, 8);
|
||||
await expectUploadStoredAs(page, request, `100% ${stem}.txt`, `100__${stem}.txt`);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue