Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(chat): recognize relative and canonical reply file links
ylliuuu66d5d270dad472
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/chatServerReplyFiles.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/chatServerReplyFiles.test.tsindex acd593cb89..361854cd8c 100644--- a/apps/desktop/src/main/maker-ipc/__tests__/chatServerReplyFiles.test.ts+++ b/apps/desktop/src/main/maker-ipc/__tests__/chatServerReplyFiles.test.ts@@ -61,6 +61,57 @@ describe('group reply file delivery', () => {expect(await prepareChatReplyFiles('First Second', 'lane', () => {}, 'reply-client')).toEqual({ text: 'First Second', attachments: [] });});+ it.each(['pdf', 'html', 'md', 'json'])('delivers bare relative %s links through the production parser', async ext => {+ await fs.mkdir(path.join(work, 'documents'), { recursive: true });+ const name = `documents/报告.${ext}`;+ await fs.writeFile(path.join(work, name), 'report bytes');+ const result = await prepareChatReplyFiles(`[报告](${name})`, 'lane', () => {});+ expect(result.text).toBe('报告');+ expect(result.attachments).toHaveLength(1);+ expect((await result.attachments[0]!.readBytes!()).toString()).toBe('report bytes');+ });++ it.each(['local/', 'open'])('accepts canonical xdt-file %s query paths', async host => {+ const result = await prepareChatReplyFiles(`[报告](xdt-file://${host}?path=${encodeURIComponent(pdf)})`, 'lane', () => {});+ expect((await result.attachments[0]!.readBytes!()).toString()).toContain('%PDF');+ });++ it('accepts sandbox absolute paths within the task', async () => {+ const result = await prepareChatReplyFiles(`[报告](<sandbox:/${pdf.replaceAll('\\', '/')}>)`, 'lane', () => {});+ expect(result.attachments).toHaveLength(1);+ expect((await result.attachments[0]!.readBytes!()).toString()).toContain('%PDF');+ });++ it('accepts canonical paths when the task workdir is an alias and deduplicates spellings', async () => {+ const alias = path.join(root, 'alias');+ await fs.symlink(work, alias, process.platform === 'win32' ? 'junction' : 'dir');+ try {+ scope.row.workingDir = alias;+ const result = await prepareChatReplyFiles(`[real](<${pdf}>) [alias](<${path.join(alias, path.basename(pdf))}>)`, 'lane', () => {});+ expect(result.attachments).toHaveLength(1);+ expect((await result.attachments[0]!.readBytes!()).toString()).toContain('%PDF');+ } finally { await fs.unlink(alias); }+ });++ it('keeps remote protocols and anchors out of local file delivery', async () => {+ scope.snapshot = null;+ const text = '[anchor](#report) [mail](mailto:[email protected]) [media](cindy-media://blobs/a) [web](https://example.test/report.pdf)';+ expect(await prepareChatReplyFiles(text, 'lane', () => {})).toEqual({ text, attachments: [] });+ });++ it('rejects remote xdt-file metadata even when its path exists locally', async () => {+ const url = `xdt-file://local/?path=${encodeURIComponent(pdf)}&remoteHostId=other-host`;+ await expect(prepareChatReplyFiles(`[report](${url})`, 'lane', () => {})).rejects.toThrow('ATTACHMENT_UNAVAILABLE');+ });++ it('handles escaped relative links but still rejects traversal and missing files', async () => {+ const result = await prepareChatReplyFiles('[report](<report 中文.pdf>)', 'lane', () => {});+ expect((await result.attachments[0]!.readBytes!()).toString()).toContain('%PDF');+ await expect(prepareChatReplyFiles('[private](documents/../../private.pdf)', 'lane', () => {})).rejects.toThrow('ATTACHMENT_UNAVAILABLE');+ const missing = await prepareChatReplyFiles('[missing](documents/missing.pdf)', 'lane', () => {});+ await expect(missing.attachments[0]!.readBytes!()).rejects.toThrow();+ });+it('does not access session scope for ordinary text, remote links, fenced examples or images', async () => {scope.snapshot = null;const text = '```md\n[example](./x.pdf)\n```\n\n[web](https://example.test)';diff --git a/apps/desktop/src/main/maker-ipc/chatServerReplyFiles.ts b/apps/desktop/src/main/maker-ipc/chatServerReplyFiles.tsindex dd517a8fe4..c7afc9940f 100644--- a/apps/desktop/src/main/maker-ipc/chatServerReplyFiles.ts+++ b/apps/desktop/src/main/maker-ipc/chatServerReplyFiles.ts@@ -20,6 +20,13 @@ const parser = unified().use(remarkParse).use(remarkGfm);const MAX_BYTES = 100 * 1024 * 1024;const unavailable = () => new Error('ATTACHMENT_UNAVAILABLE');+// Match local references, not arbitrary URI schemes or in-message anchors.+function isFileTarget(value: string): boolean {+ if (!value || /^[#?]/.test(value)) return false;+ if (/^(?:file|xdt-file|sandbox):/i.test(value) || /^[A-Za-z]:[\\/]/.test(value)) return true;+ return !/^[A-Za-z][A-Za-z0-9+.-]*:/.test(value);+}+/** Only actual Markdown file links are deliveries; code, images and remote URLs are not. */export function replyFileLinks(text: string) {// The reported reply HTML-escaped the angle brackets around paths with spaces.@@ -29,7 +36,8 @@ export function replyFileLinks(text: string) {const start = node.position?.start.offset, end = node.position?.end.offset;if (start === undefined || end === undefined) return;const raw = text.slice(start, end);- const normalized = raw.replace(/(\[[^\]\n]+\]\()<((?:\/|[A-Za-z]:[\\/]|\.{1,2}\/|xdt-file:\/\/|file:\/\/)[^\n]*?)>(\))/g, '$1<$2>$3');+ const normalized = raw.replace(/(\[[^\]\n]+\]\()<([^\n]*?)>(\))/g,+ (match, before, target, after) => isFileTarget(target) ? `${before}<${target}>${after}` : match);if (normalized !== raw) escaped.push({ start, end, text: normalized });});for (const edit of escaped.sort((a, b) => b.start - a.start))@@ -46,7 +54,7 @@ export function replyFileLinks(text: string) {visit(tree, node => {if (node.type !== 'link' && node.type !== 'linkReference') return;const target = node.type === 'link' ? node : definitions.get(node.identifier);- if (!target || !/^(?:\/|[A-Za-z]:[\\/]|\.{1,2}\/|xdt-file:\/\/|file:\/\/)/.test(target.url)) return;+ if (!target || !isFileTarget(target.url)) return;const start = node.position?.start.offset, end = node.position?.end.offset;const first = node.children[0]?.position?.start.offset, last = node.children.at(-1)?.position?.end.offset;// A linked image belongs to image delivery, not this file path.@@ -69,9 +77,22 @@ export function replyFileLinks(text: string) {function localPath(url: string, workDir: string): string {let value = url;- if (/^(?:xdt-file|file):/.test(url)) {- try { value = fileURLToPath(url.replace(/^xdt-file:/, 'file:')); } catch { throw unavailable(); }- }+ try {+ if (/^sandbox:/i.test(url)) {+ const match = /^sandbox:\/*(?:([a-z]:[\\/][^\n]*)|(\/[^\n]*))$/i.exec(url);+ if (!match) throw unavailable();+ value = match[1] ?? match[2]!;+ } else if (/^xdt-file:/i.test(url)) {+ const parsed = new URL(url);+ // A remote reference must never be interpreted as a path on this machine.+ if (parsed.searchParams.has('remoteHostId') || parsed.username || parsed.password) throw unavailable();+ if (parsed.searchParams.has('path')) {+ if (!['', 'local', 'open'].includes(parsed.hostname)) throw unavailable();+ value = parsed.searchParams.get('path')!;+ if (!path.isAbsolute(value) && !/^[A-Za-z]:[\\/]/.test(value)) throw unavailable();+ } else value = fileURLToPath(url.replace(/^xdt-file:/i, 'file:'));+ } else if (/^file:/i.test(url)) value = fileURLToPath(url);+ } catch { throw unavailable(); }// Never resolve a network/device path, including on another host platform.if (/^[\\/]{2}|^[\\/]\?\?[\\/]/.test(value) || value.includes('\0')) throw unavailable();if (/^[A-Za-z]:/.test(value) && process.platform !== 'win32') throw unavailable();@@ -143,8 +164,9 @@ export async function prepareChatReplyFiles(text: string, sessionId: string, assfor (const link of parsed.links) {const source = localPath(link.url, scope.workingDir!);// /tmp can alias /private/tmp; normalize the trusted workdir prefix without following the file.- if (!isPathWithin(path.resolve(scope.workingDir!), source)) throw unavailable();- const canonicalSource = path.resolve(root, path.relative(scope.workingDir!, source));+ const lexicalRoot = path.resolve(scope.workingDir!);+ if (!isPathWithin(lexicalRoot, source) && !isPathWithin(root, source)) throw unavailable();+ const canonicalSource = isPathWithin(root, source) ? source : path.resolve(root, path.relative(lexicalRoot, source));if (seen.has(canonicalSource)) continue;if (attachments.length >= BOT_GROUP_ATTACHMENTS_MAX) throw unavailable();seen.add(canonicalSource);