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.
7 changes: 4 additions & 3 deletions packages/kap-server/src/routes/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -963,8 +963,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 base = segs.at(-1) ?? rel;
const sanitized = base.replaceAll(/[\u0000-\u001F\u007F"\\]/g, '_');
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 @@ -8,6 +8,7 @@ import { FakeRuntime } from '@moonshot-ai/agent-core-v2/runtime/fakeRuntime';
import { ErrorCode } from '../src/protocol/error-codes';
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest';

import { sanitizeFilename } from '../src/routes/fs';
import { type RunningServer, startServer } from '../src/start';
import { TEST_HOST_IDENTITY } from './helpers/hostIdentity';
import { authHeaders } from './helpers/auth';
Expand Down Expand Up @@ -354,6 +355,10 @@ describe('server-v2 /api/v1 fs routes', () => {
}
});

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

async function postWorkspaceSearch<T>(body: unknown): Promise<Envelope<T>> {
const res = await fetch(`${base}/api/v1/workspace/fs:search`, {
method: 'POST',
Expand Down