Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(chat): import local file URLs in component image replies

ylliuuu66353a16e888c9
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/chatServerReplyImages.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/chatServerReplyImages.test.ts
index 44c0acaab9..545e9cd489 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/chatServerReplyImages.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/chatServerReplyImages.test.ts
@@ -1,5 +1,6 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import path from 'node:path';
+import { pathToFileURL } from 'node:url';
const f = vi.hoisted(() => ({ realpath: vi.fn(), stat: vi.fn(), canRead: vi.fn(), resolve: vi.fn(), materialize: vi.fn(), db: {} }));
vi.mock('node:fs/promises', () => ({ default: { stat: f.stat, realpath: f.realpath } }));
vi.mock('../../cindy-media/taskImageDelivery.js', () => ({ materializeTaskImageTextResult: f.materialize }));
@@ -106,6 +107,40 @@ describe('generated reply image attachments', () => {
expect(result.attachments.map(image => image.url)).toEqual([url]);
expect(result.text).toBe(component + '\nAgain');
});
+ // #7492 P1: file URLs were skipped with a successful empty delivery. Exercise
+ // the public prepare entry and real Markdown importer; only byte/ledger boundaries are fakes.
+ it.each(['encoded', 'literal spaces', 'localhost'])('imports a component file URL with %s through the existing checks', async form => {
+ const sourcePath = path.resolve('fixture', '图片 [copy] #1 %.png');
+ const fileUrl = pathToFileURL(sourcePath).href;
+ const src = form === 'literal spaces' ? fileUrl.replace(/%20/g, ' ')
+ : form === 'localhost' ? fileUrl.replace('file:///', 'file://localhost/') : fileUrl;
+ const importImage = vi.fn().mockResolvedValue(url);
+ f.materialize.mockImplementation(async (_session, text) => materializeTaskImageMarkdown(text, { importImage }));
+ const result = await prepareChatReplyImages(rich({ kind: 'image', props: { src } }) + `\n![Again](${url})`, 'lane', vi.fn());
+ expect(importImage).toHaveBeenCalledExactlyOnceWith(sourcePath);
+ expect(result.attachments).toEqual([expect.objectContaining({ url, sourcePaths: [sourcePath], category: 'image' })]);
+ expect(f.canRead).toHaveBeenCalledExactlyOnceWith('a'.repeat(64), 'lane', f.db);
+ });
+ it.each([
+ 'file://attacker/share/image.png', 'file:////attacker/share/image.png',
+ 'file:///image%2Fescape.png', 'file:///image%ZZ.png', 'file:///image%00.png',
+ 'file://user:pass@localhost/image.png', 'file:relative.png',
+ 'file:///image.png?remoteHostId=other', 'file:///image.png#fragment',
+ ])('refuses an unsafe or malformed component file URL before any file access: %s', async src => {
+ await expect(prepareChatReplyImages(rich({ kind: 'image', props: { src } }), 'lane', vi.fn()))
+ .rejects.toThrow('ATTACHMENT_UNAVAILABLE');
+ expect(f.realpath).not.toHaveBeenCalled();
+ expect(f.materialize).not.toHaveBeenCalled();
+ expect(f.stat).not.toHaveBeenCalled();
+ });
+ it('rejects an imported component file URL when the existing task ledger denies the blob', async () => {
+ const sourcePath = path.resolve('fixture', 'private image.png');
+ f.materialize.mockImplementation(async (_session, text) => materializeTaskImageMarkdown(text, { importImage: async () => url }));
+ f.canRead.mockResolvedValue(false);
+ await expect(prepareChatReplyImages(rich({ kind: 'image', props: { src: pathToFileURL(sourcePath).href } }), 'lane', vi.fn()))
+ .rejects.toThrow('ATTACHMENT_UNAVAILABLE');
+ expect(f.stat).not.toHaveBeenCalled();
+ });
it('keeps literal path characters from injecting extra image references during import', async () => {
const sourcePath = path.resolve('fixture', 'literal &<>.png') + `\n![](${other})`;
const importImage = vi.fn().mockResolvedValue(url);
diff --git a/apps/desktop/src/main/maker-ipc/chatServerReplyImages.ts b/apps/desktop/src/main/maker-ipc/chatServerReplyImages.ts
index 9cac452fb2..61e822af61 100644
--- a/apps/desktop/src/main/maker-ipc/chatServerReplyImages.ts
+++ b/apps/desktop/src/main/maker-ipc/chatServerReplyImages.ts
@@ -1,5 +1,6 @@
import fs from 'node:fs/promises';
import path from 'node:path';
+import { fileURLToPath } from 'node:url';
import { RichAnswerStream, splitRichAnswer, richString } from '@cindy/maker-shared/rich-answer';
import { BOT_GROUP_ATTACHMENTS_MAX } from '../../shared/botGroupChat.js';
import { parseBlobUrl, resolveSafe } from '../cindy-media/blobStore.js';
@@ -15,6 +16,21 @@ const imageLabel = (image: { alt: string }) => image.alt.replace(/[\\`*_{}[\]<>#
type ReplyImageReference = { url: string; start: number; identifier?: string };
+/** Normalize only local file URLs; the existing importer still owns file authority. */
+function componentImageSource(src: string): string {
+ if (!/^file:/i.test(src)) return src;
+ try {
+ const url = new URL(src);
+ if (!/^file:\//i.test(src) || !['', 'localhost'].includes(url.hostname)
+ || url.username || url.password || url.search || url.hash) throw new Error('invalid file URL');
+ const local = fileURLToPath(url);
+ if (!path.isAbsolute(local) || isNetworkOrDevicePath(local) || local.includes('\0')) throw new Error('invalid local path');
+ return local;
+ } catch {
+ throw new Error('ATTACHMENT_UNAVAILABLE');
+ }
+}
+
/** Collect final rendered references in display order, keeping Markdown scopes independent.
* The Markdown AST still owns reference definitions and code-example exclusion; the
* component parser owns patches, moves and removed subtrees.
@@ -32,7 +48,7 @@ function replyImageReferences(text: string): ReplyImageReference[] {
if (!node) return;
if (node.kind === 'image') {
const src = richString(node, 'src');
- if (src) images.push({ url: src, start: part.key });
+ if (src) images.push({ url: componentImageSource(src), start: part.key });
}
node.children.forEach(visit);
};
diff --git a/docs/verification/issue-6892/README.md b/docs/verification/issue-6892/README.md
index 9121f8b5e6..fae43d6ffc 100644
--- a/docs/verification/issue-6892/README.md
+++ b/docs/verification/issue-6892/README.md
@@ -112,3 +112,16 @@ git diff --check
- 群正文 `body-answer`:Light / Dark 共 2 项通过,总耗时 7.7 秒,overview 已目检。
上文双账号实机验收来自 2026-10-23 的构建,未在同步主干后重做;原始限制继续有效。
+
+## 2026-10-27:PR #7492 文件 URL 评审修复
+
+Greptile 指出组件 `image.src` 为本机 `file://` URL 时被静默跳过。图片提取现在先通过
+Node `fileURLToPath` 转换为本机绝对路径,再复用原图片导入、任务账本、类型、大小和数量检查。
+远端 host、网络/设备路径、非法编码、NUL、相对地址及带查询/片段的文件 URL 在文件访问前明确拒绝。
+不修改通用协议读取器、共享图片路径解析器或授权规则。
+
+生产入口回归先复现 **13 失败 / 26 通过**,修复后单文件 **39 项通过**,总耗时 0.699 秒。
+覆盖带空格、中文、编码字符及 localhost 的本机 URL、与 Markdown 混合去重、拒绝非法来源和账本拒绝。
+图片、文件、群服务及媒体上传定向回归 **4 个文件 507 项通过**,总耗时 4.48 秒。
+Desktop 完整类型检查通过。
+本次没有重做双账号实机、Mobile 或 Windows 实机验收,不将此前结果当作本次补丁的实机结果。