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(sharing): require explicit tool file deliveries

david9f6e85a14618
diff --git a/apps/desktop/src/main/device-link/__tests__/sharedTaskMediaAccess.test.ts b/apps/desktop/src/main/device-link/__tests__/sharedTaskMediaAccess.test.ts
index d44c344412..2b812b4d32 100644
--- a/apps/desktop/src/main/device-link/__tests__/sharedTaskMediaAccess.test.ts
+++ b/apps/desktop/src/main/device-link/__tests__/sharedTaskMediaAccess.test.ts
@@ -40,8 +40,8 @@ describe('shared task media access', () => {
if (role === 'tool_use' || role === 'tool_result') {
insert.run('task', 'tool_use', JSON.stringify(role === 'tool_use'
? { toolName: 'Write', input: { file_path: file, content: 'report' } }
- : { toolName: 'Bash', input: { command: 'generate-report' } }), 'run-' + time, time);
- insert.run('task', 'tool_result', JSON.stringify(role === 'tool_result' ? url : 'File written'), 'run-' + time, time);
+ : { toolName: 'mcp:reports:generate', input: {} }), 'run-' + time, time);
+ insert.run('task', 'tool_result', JSON.stringify(role === 'tool_result' ? { xdt_media_produced: [url] } : 'File written'), 'run-' + time, time);
} else insert.run('task', role, JSON.stringify(url), null, time);
};
deliver(100);
@@ -64,8 +64,8 @@ describe('shared task media access', () => {
const insert = db.prepare("INSERT INTO messages (session_id, role, content, tool_use_id, created_at) VALUES ('task', ?, ?, 'run', ?)");
insert.run('tool_use', JSON.stringify(role === 'tool_use'
? { toolName: 'Write', input: { file_path: file, content: 'report' } }
- : { toolName: 'Bash', input: { command: 'generate-report' } }), role === 'tool_use' ? 101 : 100);
- insert.run('tool_result', JSON.stringify(role === 'tool_result' ? url : 'File written'), role === 'tool_result' ? 101 : 100);
+ : { toolName: 'mcp:reports:generate', input: {} }), role === 'tool_use' ? 101 : 100);
+ insert.run('tool_result', JSON.stringify(role === 'tool_result' ? { xdt_media_produced: [url] } : 'File written'), role === 'tool_result' ? 101 : 100);
await expect(assertSharedTaskMedia(url, capture)).rejects.toThrow('PERMISSION_DENIED');
db.exec('UPDATE messages SET created_at = 101');
await expect(assertSharedTaskMedia(url, capture)).resolves.toEqual({ file });
@@ -141,12 +141,12 @@ describe('shared task media access', () => {
it.each(['ok', 'structured-error', 'text-error', 'missing-use', 'missing-id', 'revoked'])('checks direct tool-result provenance: %s', async (kind) => {
const file = path.resolve('deliveries/report.pdf');
const url = 'xdt-file://local?path=' + encodeURIComponent(file);
- const content = kind === 'structured-error' ? { ok: false, error: 'failed to open ' + url }
- : kind === 'text-error' ? 'Error: failed to open ' + url : url;
+ const content = { xdt_media_produced: [url], ...(kind === 'structured-error' ? { ok: false, error: 'failed to open ' + url }
+ : kind === 'text-error' ? { message: 'Error: failed to open ' + url } : {}) };
deps.query.mockResolvedValueOnce([{ rowid: 2, role: 'tool_result', toolUseId: kind === 'missing-id' ? null : 'run-1', content: JSON.stringify(content) }]);
deps.query.mockImplementationOnce(async () => {
if (kind === 'revoked') current = false;
- return kind === 'missing-use' ? [] : [{ content: JSON.stringify({ toolName: 'Bash', input: { command: 'generate-report' } }) }];
+ return kind === 'missing-use' ? [] : [{ content: JSON.stringify({ toolName: 'mcp:reports:generate', input: {} }) }];
});
if (kind === 'ok') await expect(assertSharedTaskMedia(url, capture)).resolves.toEqual({ file });
else await expect(assertSharedTaskMedia(url, capture)).rejects.toThrow('PERMISSION_DENIED');
@@ -155,6 +155,37 @@ describe('shared task media access', () => {
expect(deps.query.mock.calls[1][1]).toEqual(['task', 'run-1', 2]);
}
});
+ // Successful reading/echoing of guest-controlled content is not delivery.
+ // Even JSON that resembles delivery metadata stays content for native readers.
+ it.each([
+ { toolName: 'Read', input: { file_path: 'workspace/guest.txt' } },
+ { toolName: 'Grep', input: { pattern: 'xdt-file', path: 'workspace' } },
+ { toolName: 'WebFetch', input: { url: 'https://example.com' } },
+ { toolName: 'Bash', input: { command: 'cat workspace/guest.txt' } },
+ { toolName: 'mcp:reports:read', input: {} },
+ ])('does not authorize links echoed by $toolName', async (tool) => {
+ const file = path.resolve('other/private.pdf');
+ const url = 'xdt-file://local?path=' + encodeURIComponent(file);
+ const results: unknown[] = [url];
+ if (tool.toolName !== 'mcp:reports:read') results.push({ xdt_media_produced: [url] });
+ for (const result of results) {
+ deps.query.mockReset().mockResolvedValue([])
+ .mockResolvedValueOnce([{ rowid: 2, role: 'tool_result', toolUseId: 'read-1', content: JSON.stringify(result) }])
+ .mockResolvedValueOnce([{ content: JSON.stringify(tool) }]);
+ await expect(assertSharedTaskMedia(url, capture)).rejects.toThrow('PERMISSION_DENIED');
+ }
+ });
+ it('accepts explicit model-file deliveries without granting unrelated payload links', async () => {
+ const file = path.resolve('deliveries/model.glb');
+ const url = 'xdt-file://local?path=' + encodeURIComponent(file);
+ const other = path.resolve('other/private.pdf');
+ const content = { ok: true, result: { _xdt_model_files: [{ url, name: 'model' }], text: 'xdt-file://local?path=' + encodeURIComponent(other) } };
+ const row = { rowid: 2, role: 'tool_result', toolUseId: 'generate-1', content: JSON.stringify(content) };
+ deps.query.mockResolvedValueOnce([row]).mockResolvedValueOnce([{ content: JSON.stringify({ toolName: 'mcp:models:generate', input: {} }) }]);
+ await expect(assertSharedTaskMedia(url, capture)).resolves.toEqual({ file });
+ deps.query.mockResolvedValue([row]);
+ await expect(assertSharedTaskMedia('xdt-file://local?path=' + encodeURIComponent(other), capture)).rejects.toThrow('PERMISSION_DENIED');
+ });
it('does not cache file grants and scopes the history query to live messages in the current task', async () => {
const file = path.resolve('deliveries/report.pdf');
const url = 'xdt-file://local?path=' + encodeURIComponent(file);
diff --git a/apps/desktop/src/main/device-link/sharedTaskFileReferences.ts b/apps/desktop/src/main/device-link/sharedTaskFileReferences.ts
index 79fffbc41d..c95afa1788 100644
--- a/apps/desktop/src/main/device-link/sharedTaskFileReferences.ts
+++ b/apps/desktop/src/main/device-link/sharedTaskFileReferences.ts
@@ -6,7 +6,7 @@ import { visit } from 'unist-util-visit';
import { describeToolUse, createdPathsFromDescriptor } from '@cindy/maker-shared/tool-use-descriptor';
import { parseMessageToolUse } from '@cindy/maker-shared/message-normalize';
import { isToolResultFailureLike } from '@cindy/maker-shared/message-presentation';
-import { extractPayloadToolResultFiles } from '@cindy/maker-shared/payload-summary';
+import { parseToolResultPayload } from '@cindy/maker-shared/payload-summary';
import { extractCommandOutputPathCandidates } from '../../shared/commandOutputPaths.js';
import { getDbClient } from '../localDb/client/current.js';
import type { SharedTaskPeerCapture } from './sharedTaskDispatch.js';
@@ -34,7 +34,13 @@ function references(content: unknown, role: string, includeText: boolean): strin
return createdPathsFromDescriptor(descriptor);
}
if (role === 'tool_result') {
- return extractPayloadToolResultFiles(typeof content === 'string' ? content : JSON.stringify(content)).map((file) => file.url);
+ // Display extractors also find URLs inside read/search/log output. Those
+ // URLs are content, not authority; only explicit delivery fields count here.
+ const payload = parseToolResultPayload(typeof content === 'string' ? content : JSON.stringify(content));
+ if (payload?._xdt_render_image === false) return [];
+ const produced = Array.isArray(payload?.xdt_media_produced) ? payload.xdt_media_produced : [];
+ const models = Array.isArray(payload?._xdt_model_files) ? payload._xdt_model_files.map((file) => record(file)?.url) : [];
+ return [...produced, ...models].filter((url): url is string => typeof url === 'string');
}
const result: string[] = [];
// Persisted attachments have passed the host's input-reference admission and
@@ -137,7 +143,12 @@ export async function sharedTaskReferencesFile(
if (!capture.isCurrent()) return false;
if (!uses.length) continue;
const tool = parseMessageToolUse({ role: 'tool_use', content: decode(uses[0].content) });
- if (isToolResultFailureLike(describeToolUse(tool.toolName, tool.input), typeof content === 'string' ? content : JSON.stringify(content))) continue;
+ const descriptor = describeToolUse(tool.toolName, tool.input);
+ // Native readers and shell commands can echo JSON-shaped file contents.
+ // Their actual outputs are authorized via invocation output paths below;
+ // delivery metadata is a contract of MCP/dynamic tool responses only.
+ if (descriptor.kind !== 'mcp' && descriptor.kind !== 'dynamic') continue;
+ if (isToolResultFailureLike(descriptor, typeof content === 'string' ? content : JSON.stringify(content))) continue;
}
if (row.role === 'tool_use') {
// A queued/denied tool invocation is not delivery. Only completed output
diff --git a/docs/product-rules/shared-task-mode.md b/docs/product-rules/shared-task-mode.md
index 7d9a8dd573..0205ae275e 100644
--- a/docs/product-rules/shared-task-mode.md
+++ b/docs/product-rules/shared-task-mode.md
@@ -41,6 +41,7 @@
- 访客通过客户端接口只能直接访问共享任务及其资源,不能调用全局凭证、其他任务或任意主机文件接口。Agent 原生工具不承诺工作目录硬隔离。
- 文件浏览器(2026-10-10 用户裁决):访客可在文件浏览器中浏览和编辑本任务工作目录,读写权限与房主一致:列目录、读取、搜索、缩略图、保存、新建、重命名、删除,以及文件变化监听。主机只认本机记录的任务工作目录与执行端点(本地或任务所属 SSH 主机),访客传入的路径只用于比对;读写前复核成员资格。文件导出/文件夹下载这类导出任务仍只限同账号(它们走房主的同账号中转路径),工作目录本身的管理(更换目录)仍归房主。实现见 `sharedTaskDispatch.ts`(通道与操作)、`sharedTaskFileAccess.ts`(目录绑定)与 `file-browser/device-op.ts`。
- 任务关联的本机文件即使位于工作目录外,访客也可经原有取件通道只读访问(2026-10-19 修复):归属由房主端当前未回退且未被 `/clear` 隐藏的消息记录确认,包括主机已校验并接收的结构化附件(含访客上传)、房主/Agent 明确交付的文件链接及已完成且未报错的工具产出。文件引用及配对的工具调用、结果均须晚于任务的清空边界;清空前的记录即使仍在数据库中,也不再提供授权。访客自行粘贴的路径、代码示例、其他任务记录或仅前缀相同的路径不构成授权;仅放行关联的具体文件,不开放其父目录浏览或写入。取件继续检查成员资格、敏感路径和解析后的文件身份,不缓存历史授权结论。Desktop 与 Mobile 共用房主校验,无新增 wire 字段;SSH 的工作目录限界保持原规则,不以本机路径例外绕过。实现与回归见 `sharedTaskFileReferences.ts`、`sharedTaskMediaAccess.ts` 及 `sharedTaskMediaAccess.test.ts`、`mediaFetch.test.ts`。
+- 工具结果中的普通文本链接不构成外部文件交付,包括读取、搜索、网页或 shell 返回的内容。原生工具产出沿用调用中明确声明的输出路径;MCP/动态工具的结果只认既有结构化交付字段 `xdt_media_produced`、`_xdt_model_files`,并继续核对调用配对、成功状态及当前历史边界。原生读取和 shell 输出即使形似这些字段,也不按交付解释。房主/Agent 在消息中明确交付的链接仍按上述规则处理。
- 消息按主机现有输入队列顺序处理,保留 clientId 幂等。访客仅编辑/撤回自己未处理消息,房主可处理全部未处理消息;直接更新内容,不显示“已编辑”。
- 发言者身份来自经过验证的账号和成员关系,不能信任消息正文或客户端自报的身份。