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(plugins): verify configured directory before ordinary task creation

Dash2addd2af2218
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/pluginTaskCreateDirectory.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/pluginTaskCreateDirectory.test.ts
new file mode 100644
index 0000000000..e3d6fdc1f8
--- /dev/null
+++ b/apps/desktop/src/main/maker-ipc/__tests__/pluginTaskCreateDirectory.test.ts
@@ -0,0 +1,91 @@
+import { readFileSync } from 'node:fs';
+import { mkdtemp, mkdir, realpath, rename, rm, symlink } from 'node:fs/promises';
+import os from 'node:os';
+import path from 'node:path';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+import ts from 'typescript';
+import { resolvePluginWorkerDirectory } from '../pluginWorkerDirectory.js';
+import { PluginTaskError } from '../pluginTaskService.js';
+
+const source = readFileSync(new URL('../register.ts', import.meta.url), 'utf8');
+const start = source.indexOf(' createSession: async (pluginId, taskId, title, route, isolatedWorkspace) => {');
+const branch = source.slice(start, source.indexOf(' readSession:', start)).trim().replace(/,$/, '');
+const js = ts.transpileModule(`return ({ ${branch} }).createSession;`, { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText;
+const roots: string[] = [];
+afterEach(async () => { await Promise.all(roots.splice(0).map(root => rm(root, { recursive: true, force: true }))); });
+async function directory() {
+ const root = await realpath(await mkdtemp(path.join(os.tmpdir(), 'plugin-create-directory-')));
+ roots.push(root);
+ const selected = path.join(root, 'selected');
+ await mkdir(selected);
+ return { root, selected };
+}
+function fixture(workingDir?: string, afterResolve?: () => void) {
+ const snapshot = {};
+ let owner = snapshot;
+ let authorized = true;
+ const cfg = { workingDir, permissionMode: 'plan' };
+ const create = vi.fn(async (_args: { shouldContinue: () => boolean }) => 'task');
+ const resolve = vi.fn(async (input: Parameters<typeof resolvePluginWorkerDirectory>[0]) => {
+ const result = await resolvePluginWorkerDirectory(input);
+ afterResolve?.();
+ return result;
+ });
+ const deps = {
+ snapshot, PluginTaskError,
+ assertPlugin: () => { if (owner !== snapshot || !authorized) throw new PluginTaskError('PERMISSION_DENIED', 'Owner changed'); },
+ readGhostErrandConfig: () => ({ ...cfg }),
+ resolvePluginWorkerDirectory: resolve,
+ createGhostErrandSession: create,
+ clampErrandPermissionMode: (mode: string) => mode,
+ getCurrentDbClientSnapshot: () => owner,
+ isPluginTaskAuthorized: () => authorized,
+ notifyGhostSessionEvent: vi.fn(), broadcastSessionCreated: vi.fn(),
+ };
+ const run = new Function(...Object.keys(deps), js)(...Object.values(deps));
+ return { cfg, create, resolve, switchOwner: () => { owner = {}; }, revoke: () => { authorized = false; }, run: (isolated = false) => run('plugin', 'task', 'title', {}, isolated) };
+}
+describe('ordinary plugin task configured-directory admission', () => {
+ it('creates in the unchanged real directory', async () => {
+ const { selected } = await directory();
+ const f = fixture(selected);
+ await f.run();
+ expect(f.create).toHaveBeenCalledWith(expect.objectContaining({ workingDir: selected }));
+ });
+ it('rejects a saved root replaced by a link before any creator side effects', async () => {
+ const { root, selected } = await directory();
+ await rename(selected, path.join(root, 'original'));
+ const other = path.join(root, 'other');
+ await mkdir(other);
+ await symlink(other, selected, 'junction');
+ const f = fixture(selected);
+ await expect(f.run()).rejects.toMatchObject({ code: 'PERMISSION_DENIED' });
+ expect(f.create).not.toHaveBeenCalled();
+ });
+ it.each([true, false])('keeps managed directory creation without resolving a project root (isolated=%s)', async isolated => {
+ const f = fixture(isolated ? '/missing-project' : undefined);
+ await f.run(isolated);
+ expect(f.resolve).not.toHaveBeenCalled();
+ expect(f.create.mock.calls[0][0]).not.toHaveProperty('workingDir');
+ });
+ it.each(['directory', 'permission', 'account', 'revocation'])('rejects %s changes after async resolution', async change => {
+ const { selected } = await directory();
+ const f = fixture(selected, () => {
+ if (change === 'directory') f.cfg.workingDir = '/changed';
+ if (change === 'permission') f.cfg.permissionMode = 'auto';
+ if (change === 'account') f.switchOwner();
+ if (change === 'revocation') f.revoke();
+ });
+ await expect(f.run()).rejects.toMatchObject({ code: 'PERMISSION_DENIED' });
+ expect(f.create).not.toHaveBeenCalled();
+ });
+ it('keeps configuration checks in the creator continuation predicate', async () => {
+ const { selected } = await directory();
+ const f = fixture(selected);
+ await f.run();
+ const check = f.create.mock.calls[0][0].shouldContinue;
+ expect(check()).toBe(true);
+ f.cfg.workingDir = '/changed';
+ expect(check()).toBe(false);
+ });
+});
diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.ts
index d027261210..bb2ca83f1c 100644
--- a/apps/desktop/src/main/maker-ipc/register.ts
+++ b/apps/desktop/src/main/maker-ipc/register.ts
@@ -10172,11 +10172,27 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
createSession: async (pluginId, taskId, title, route, isolatedWorkspace) => {
assertPlugin(pluginId);
const cfg = readGhostErrandConfig(pluginId);
+ const configurationIsCurrent = () => {
+ const current = readGhostErrandConfig(pluginId);
+ return current.workingDir === cfg.workingDir && current.permissionMode === cfg.permissionMode;
+ };
+ const workingDir = !isolatedWorkspace && cfg.workingDir
+ ? await resolvePluginWorkerDirectory({
+ requested: cfg.workingDir, configuredDirectory: cfg.workingDir,
+ isPickedDirectory: () => false,
+ assertCurrent: () => {
+ assertPlugin(pluginId);
+ if (!configurationIsCurrent()) throw new PluginTaskError('PERMISSION_DENIED', 'Plugin task configuration changed');
+ },
+ })
+ : undefined;
+ assertPlugin(pluginId);
+ if (!configurationIsCurrent()) throw new PluginTaskError('PERMISSION_DENIED', 'Plugin task configuration changed');
await createGhostErrandSession({
ghostId: pluginId, sessionId: taskId, title, ...route,
permissionMode: clampErrandPermissionMode(cfg.permissionMode),
- ...(!isolatedWorkspace && cfg.workingDir ? { workingDir: cfg.workingDir } : {}),
- shouldContinue: () => getCurrentDbClientSnapshot() === snapshot && isPluginTaskAuthorized(pluginId),
+ ...(workingDir ? { workingDir } : {}),
+ shouldContinue: () => getCurrentDbClientSnapshot() === snapshot && isPluginTaskAuthorized(pluginId) && configurationIsCurrent(),
notifySessionCreated: ({ sessionId, workdir }) => {
notifyGhostSessionEvent('created', { sessionId, workdir });
broadcastSessionCreated(sessionId);
diff --git a/docs/dev-rules/plugin-task-api-implementation.md b/docs/dev-rules/plugin-task-api-implementation.md
index c1370b8442..2c6dec5f06 100644
--- a/docs/dev-rules/plugin-task-api-implementation.md
+++ b/docs/dev-rules/plugin-task-api-implementation.md
@@ -6,7 +6,7 @@
- `capabilities/create/list/get/send/getRun/listRuns/readMessages/cancel`。
- create 可传 isolatedWorkspace:true,由宿主分配独立空作答目录;返回本任务 workingDir 与 permissionMode,插件不能覆盖权限。
-- 创建用户可见的普通本地 Session;工作目录及权限来自用户配置,不接受插件传入绕过权限、任意目录或另一插件身份。
+- 创建用户可见的普通本地 Session;工作目录及权限来自用户配置,不接受插件传入绕过权限、任意目录或另一插件身份。普通任务创建前复用 Worker 的目录身份判据:配置路径必须仍为原规范路径,改链、缺失或非目录拒绝;异步解析后复核账号、tasks 批准及配置。缺省/isolatedWorkspace 仍由宿主分配目录,旧 errand 路径不变。旧别名配置没有原目标证据,不自动授权当前目标;这不解决校验后的目录置换窗口。
- 发送走普通输入协调器,使用持久 requestKey 去重和稳定输入 ID;重复请求不会再次启动模型。
- 指定 provider、model、effort、Fast 必须通过当前目录核验;派发前再次核验,不静默 fallback。
- 回执独立落库,区分任务、输入执行及原生执行代次;原生终态按 instanceId + generation 结算,不以 idle 或最后一条 assistant 消息猜测完成。