Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/safe-download-filenames.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@moonshot-ai/kimi-code": patch
---

Sanitize filenames in session file download response headers.
5 changes: 3 additions & 2 deletions packages/kap-server/src/routes/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,9 @@ function buildValidationEnvelope(
};
}

function sanitizeFilename(rel: string): string {
export function sanitizeFilename(rel: string): string {
const segs = rel.split('/');
const base = segs[segs.length - 1] ?? rel;
return base.replace(/"/g, '\\"');
const sanitized = base.replace(/[\u0000-\u001f\u007f"\\]/g, '_');

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Normalize non-Latin-1 filenames before setting headers

When a workspace file has a name such as 报告.txt or an emoji filename, this sanitizer leaves those characters unchanged and the download handler interpolates the result into Content-Disposition. On the Node 24 server, header values containing code points outside Latin-1 throw ERR_INVALID_CHAR, so these downloads fail with a server error instead of streaming the file. Since this change is meant to make response-header filenames safe, sanitize to ASCII or use a properly encoded filename* fallback before calling reply.header.

Useful? React with 👍 / 👎.

return sanitized.length > 0 ? sanitized : 'download';
}
5 changes: 5 additions & 0 deletions packages/kap-server/test/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { IModelCatalog } from '@moonshot-ai/agent-core-v2';
import { ErrorCode } from '../src/protocol/error-codes';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';

import { sanitizeFilename } from '../src/routes/fs';
import { type RunningServer, startServer } from '../src/start';
import { authHeaders } from './helpers/auth';

Expand Down Expand Up @@ -303,4 +304,8 @@ describe('server-v2 /api/v1/sessions/{sid}/fs:*', () => {
} as never);
expect(cached.status).toBe(304);
});

it('sanitizeFilename strips control characters and path separators', async () => {
expect(sanitizeFilename('bad"name\\.txt')).toBe('bad_name_.txt');
});
});