Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(download): honor language tags in extended filenames (#4517)

fengyue-xved5771334c434
diff --git a/frontend/src/utils/protectedResource.ts b/frontend/src/utils/protectedResource.ts
index 24e0b495b..91dccf5d3 100644
--- a/frontend/src/utils/protectedResource.ts
+++ b/frontend/src/utils/protectedResource.ts
@@ -7,7 +7,7 @@ export interface LoadedProtectedFile {
export const RESOURCE_PREVIEW_EVENT = 'weknora:resource-preview';
export function responseFileName(disposition: string | null, source: string): string {
- const encoded = disposition?.match(/filename\*=UTF-8''([^;]+)/i)?.[1];
+ const encoded = disposition?.match(/(?:^|;)\s*filename\*\s*=\s*UTF-8'[^']*'([^;]+)/i)?.[1]?.trim();
let name = disposition?.match(/filename="([^"]+)"|filename=([^;]+)/i)?.slice(1).find(Boolean);
if (encoded) {
try { name = decodeURIComponent(encoded); } catch { /* use plain filename */ }
diff --git a/frontend/src/utils/protectedResourceFilename.test.ts b/frontend/src/utils/protectedResourceFilename.test.ts
new file mode 100644
index 000000000..b086dd430
--- /dev/null
+++ b/frontend/src/utils/protectedResourceFilename.test.ts
@@ -0,0 +1,27 @@
+import assert from 'node:assert/strict'
+import test from 'node:test'
+
+import { responseFileName } from './protectedResource.ts'
+
+for (const language of ['en', 'zh-CN', '']) {
+ test(`extended UTF-8 download filename supports language '${language}'`, () => {
+ const header = `attachment; filename="fallback.pptx"; filename*=UTF-8'${language}'%E6%8A%A5%E5%91%8A.pptx`
+ assert.equal(responseFileName(header, 'resource://opaque-id'), '报告.pptx')
+ })
+}
+
+test('extended filenames tolerate parameter whitespace and case', () => {
+ assert.equal(responseFileName("inline; FILENAME* = utf-8'en'%E6%8A%A5%E5%91%8A.pdf ", ''), '报告.pdf')
+})
+
+test('malformed extended filename retains the ordinary filename', () => {
+ assert.equal(responseFileName("attachment; filename=report.pdf; filename*=UTF-8'en'%ZZ", ''), 'report.pdf')
+})
+
+test('extended filename still strips unsafe path components and control bytes', () => {
+ assert.equal(responseFileName("attachment; filename*=UTF-8'en'..%2F..%2Freport%00.pdf", ''), 'report.pdf')
+})
+
+test('source filename remains the fallback without a disposition', () => {
+ assert.equal(responseFileName(null, 'local://1/exports/report.pdf'), 'report.pdf')
+})