Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): reject malformed media URLs (#2128)
mengfanyu9410-hub67a54f435889
diff --git a/apps/desktop/src/main/__tests__/imageCacheStoreResolve.test.ts b/apps/desktop/src/main/__tests__/imageCacheStoreResolve.test.tsindex 47819eee34..b76aa01de1 100644--- a/apps/desktop/src/main/__tests__/imageCacheStoreResolve.test.ts+++ b/apps/desktop/src/main/__tests__/imageCacheStoreResolve.test.ts@@ -26,7 +26,7 @@ vi.mock('electron', () => ({},}));-const { resolveSafe, resolveSessionImageLenient } = await import('../imageCacheStore');+const { resolveSafe, resolveSessionImageLenient, collectSessionImageUrls } = await import('../imageCacheStore');describe('resolveSafe — reserved hosts', () => {it('feishu-media-images host routes to feishu-media/images/', () => {@@ -148,6 +148,12 @@ describe('resolveSafe — session-id path (regression)', () => {it('non-xdt-image URL is rejected', () => {expect(() => resolveSafe('http://example.com/x.png')).toThrow(/invalid url/);});++ it('rejects malformed percent-encoding as a malformed URL', () => {+ expect(() => resolveSafe('xdt-image://sess-abc/%E0%A4%A.png')).toThrow(+ /malformed url/,+ );+ });});describe('resolveSessionImageLenient — ghost attachment grant forms', () => {@@ -207,4 +213,8 @@ describe('resolveSessionImageLenient — ghost attachment grant forms', () => {resolveSessionImageLenient('xdt-image://no-such-file-1700000000000.png'),).toThrow();});++ it('ignores malformed percent-encoding while collecting session URLs', () => {+ expect(collectSessionImageUrls('see xdt-image://sess-lenient/%E0%A4%A.png')).toEqual([]);+ });});diff --git a/apps/desktop/src/main/__tests__/mediaProtocolMalformedUrl.test.ts b/apps/desktop/src/main/__tests__/mediaProtocolMalformedUrl.test.tsnew file mode 100644index 0000000000..369f4d1f7b--- /dev/null+++ b/apps/desktop/src/main/__tests__/mediaProtocolMalformedUrl.test.ts@@ -0,0 +1,31 @@+import { beforeEach, describe, expect, it, vi } from 'vitest';++type ProtocolHandler = (request: Request) => Promise<Response>;++const handle = vi.fn<(scheme: string, handler: ProtocolHandler) => void>();+vi.mock('electron', () => ({+ app: { getPath: vi.fn(() => '/tmp/cindy-media-protocol-test') },+ protocol: { handle },+}));+vi.mock('../logger', () => ({ createLogger: () => ({ error: vi.fn() }) }));++const { registerImageProtocolHandler } = await import('../imageProtocol');+const { registerVideoProtocolHandler } = await import('../videoProtocol');+const { registerModelProtocolHandler } = await import('../modelProtocol');++describe('media protocol malformed URLs', () => {+ beforeEach(() => vi.clearAllMocks());++ it('returns 403 instead of 500 for malformed percent encoding', async () => {+ registerImageProtocolHandler();+ registerVideoProtocolHandler();+ registerModelProtocolHandler();+ const handlers = new Map<string, ProtocolHandler>(handle.mock.calls);+ for (const scheme of ['xdt-image', 'xdt-video', 'xdt-model']) {+ const handler = handlers.get(scheme);+ expect(handler).toBeTypeOf('function');+ if (!handler) throw new Error(`missing protocol handler for ${scheme}`);+ await expect(handler(new Request(`${scheme}://session/%E0%A4%A`))).resolves.toMatchObject({ status: 403 });+ }+ });+});diff --git a/apps/desktop/src/main/__tests__/modelCacheStoreResolve.test.ts b/apps/desktop/src/main/__tests__/modelCacheStoreResolve.test.tsnew file mode 100644index 0000000000..2c71d115e3--- /dev/null+++ b/apps/desktop/src/main/__tests__/modelCacheStoreResolve.test.ts@@ -0,0 +1,17 @@+import { describe, expect, it, vi } from 'vitest';+import os from 'node:os';+import path from 'node:path';++vi.mock('electron', () => ({+ app: { getPath: () => path.join(os.tmpdir(), 'model-cache-resolve') },+}));++const { resolveSafe } = await import('../modelCacheStore');++describe('modelCacheStore.resolveSafe', () => {+ it('rejects malformed percent-encoding as a malformed URL', () => {+ expect(() => resolveSafe('xdt-model://mivo-3d-cache/%E0%A4%A.glb')).toThrow(+ /malformed url/i,+ );+ });+});diff --git a/apps/desktop/src/main/__tests__/videoCacheStoreResolve.test.ts b/apps/desktop/src/main/__tests__/videoCacheStoreResolve.test.tsindex 15501e9b03..e6032c2986 100644--- a/apps/desktop/src/main/__tests__/videoCacheStoreResolve.test.ts+++ b/apps/desktop/src/main/__tests__/videoCacheStoreResolve.test.ts@@ -59,4 +59,10 @@ describe('videoCacheStore.resolveSafe', () => {/invalid url/i,);});++ it('rejects malformed percent-encoding as a malformed URL', () => {+ expect(() => resolveSafe('xdt-video://lizi-art-media-videos/%E0%A4%A.mp4')).toThrow(+ /malformed url/i,+ );+ });});diff --git a/apps/desktop/src/main/imageCacheStore.ts b/apps/desktop/src/main/imageCacheStore.tsindex d1a470fe45..5c82cfdab5 100644--- a/apps/desktop/src/main/imageCacheStore.ts+++ b/apps/desktop/src/main/imageCacheStore.ts@@ -200,10 +200,16 @@ function parseSessionImageUrl(url: string): { sessionId: string; filename: strinreturn null;}- const host = decodeURIComponent(parsed.hostname);- if (RESERVED_HOSTS[host]) return null;- const pathnameRaw = parsed.pathname.startsWith('/') ? parsed.pathname.slice(1) : parsed.pathname;- const filename = decodeURIComponent(pathnameRaw);+ let host: string;+ let filename: string;+ try {+ host = decodeURIComponent(parsed.hostname);+ if (RESERVED_HOSTS[host]) return null;+ const pathnameRaw = parsed.pathname.startsWith('/') ? parsed.pathname.slice(1) : parsed.pathname;+ filename = decodeURIComponent(pathnameRaw);+ } catch {+ return null;+ }try {assertSafeSessionId(host);@@ -550,10 +556,16 @@ export function resolveSafe(url: string): { absPath: string; mimeType: string }} catch {throw new Error('xdt-image: malformed url');}- const host = decodeURIComponent(parsed.hostname);- // pathname is e.g. "/abc-123.png" → strip leading slash- const pathnameRaw = parsed.pathname.startsWith('/') ? parsed.pathname.slice(1) : parsed.pathname;- const filename = decodeURIComponent(pathnameRaw);+ let host: string;+ let filename: string;+ try {+ host = decodeURIComponent(parsed.hostname);+ // pathname is e.g. "/abc-123.png" → strip leading slash+ const pathnameRaw = parsed.pathname.startsWith('/') ? parsed.pathname.slice(1) : parsed.pathname;+ filename = decodeURIComponent(pathnameRaw);+ } catch {+ throw new Error('xdt-image: malformed url');+ }if (!host ||diff --git a/apps/desktop/src/main/modelCacheStore.ts b/apps/desktop/src/main/modelCacheStore.tsindex 0325cd90d0..9bb53bc2e1 100644--- a/apps/desktop/src/main/modelCacheStore.ts+++ b/apps/desktop/src/main/modelCacheStore.ts@@ -43,11 +43,17 @@ export function resolveSafe(url: string): { absPath: string; mimeType: string }} catch {throw new Error('xdt-model: malformed url');}- const host = decodeURIComponent(parsed.hostname);- const pathnameRaw = parsed.pathname.startsWith('/')- ? parsed.pathname.slice(1)- : parsed.pathname;- const filename = decodeURIComponent(pathnameRaw);+ let host: string;+ let filename: string;+ try {+ host = decodeURIComponent(parsed.hostname);+ const pathnameRaw = parsed.pathname.startsWith('/')+ ? parsed.pathname.slice(1)+ : parsed.pathname;+ filename = decodeURIComponent(pathnameRaw);+ } catch {+ throw new Error('xdt-model: malformed url');+ }if (!host ||diff --git a/apps/desktop/src/main/videoCacheStore.ts b/apps/desktop/src/main/videoCacheStore.tsindex cd861d8f58..c5905dc6d4 100644--- a/apps/desktop/src/main/videoCacheStore.ts+++ b/apps/desktop/src/main/videoCacheStore.ts@@ -60,11 +60,17 @@ export function resolveSafe(url: string): { absPath: string; mimeType: string }} catch {throw new Error('xdt-video: malformed url');}- const host = decodeURIComponent(parsed.hostname);- const pathnameRaw = parsed.pathname.startsWith('/')- ? parsed.pathname.slice(1)- : parsed.pathname;- const filename = decodeURIComponent(pathnameRaw);+ let host: string;+ let filename: string;+ try {+ host = decodeURIComponent(parsed.hostname);+ const pathnameRaw = parsed.pathname.startsWith('/')+ ? parsed.pathname.slice(1)+ : parsed.pathname;+ filename = decodeURIComponent(pathnameRaw);+ } catch {+ throw new Error('xdt-video: malformed url');+ }if (!host ||