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。

refactor(desktop): 技能快照的目录遍历改用逐条 lstat,不依赖 Dirent 类型位

Yuewei-LL0d627c0cb652
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
index 246c55dba0..ea70bc5679 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
@@ -729,6 +729,44 @@ describe('GhostManager · Host approval receipt', () => {
expect(fs.existsSync(snapshotRoot)).toBe(false);
});
+ it('refuses to follow a link planted inside the skill directory when rebuilding', async () => {
+ await manager.install(await makeCindy('skill.cindy', skillManifest(), skillFiles()));
+ const snapshotRoot = manager.list()[0].approvedSkillRoot!;
+ const outside = path.join(workDir, 'outside-skill');
+ await fs.promises.mkdir(outside, { recursive: true });
+ await fs.promises.writeFile(path.join(outside, 'leak.txt'), 'bytes from outside the skill dir');
+ // Windows junction 不需要管理员权限即可创建,是本平台成本最低的一条"把技能目录
+ // 之外的字节拉进批准快照"的路子。判据不能建立在 Dirent 类型位的实现细节上,
+ // 所以这条用例把行为钉住:planted link 一律拒,快照不落地。
+ try {
+ await fs.promises.symlink(
+ outside,
+ path.join(rootDir, 'skilled', 'skills', 'demo', 'linked'),
+ process.platform === 'win32' ? 'junction' : 'dir',
+ );
+ } catch {
+ return; // 该环境建不了链接(无权限),跳过;判定逻辑与其他平台同源。
+ }
+ await fs.promises.rm(snapshotRoot, { recursive: true, force: true });
+ expect(await manager.setEnabled('skilled', false)).toEqual({ ok: true });
+
+ await expectRejection(await manager.setEnabled('skilled', true), 'io');
+ expect(manager.list()[0].enabled).toBe(false);
+ expect(fs.existsSync(snapshotRoot)).toBe(false);
+ // 状态根里不该出现任何来自技能目录之外的字节(含崩溃残留的 .tmp)。
+ const stateRoot = manager.approvalStateRoot();
+ const leaked: string[] = [];
+ const walk = (dir: string): void => {
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
+ const child = path.join(dir, entry.name);
+ if (entry.isDirectory()) walk(child);
+ else if (entry.name === 'leak.txt') leaked.push(child);
+ }
+ };
+ if (fs.existsSync(stateRoot)) walk(stateRoot);
+ expect(leaked).toEqual([]);
+ });
+
it('still heals a deleted snapshot when the installed skill bytes are untouched', async () => {
await manager.install(await makeCindy('skill.cindy', skillManifest(), skillFiles()));
const snapshotRoot = manager.list()[0].approvedSkillRoot!;
diff --git a/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts b/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
index 4745eaa324..9c7a625190 100644
--- a/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
+++ b/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
@@ -302,9 +302,17 @@ export async function hashApprovedSkillContent(
});
for (const entry of entries) {
const relativePath = relativeDir ? `${relativeDir}/${entry.name}` : entry.name;
- if (entry.isDirectory()) {
+ // 逐条 lstat 而不是信 Dirent 的类型位:当前 libuv 把 reparse point(软链与
+ // Windows junction)都报成 link,但那是实现细节、Node 公开契约没保证。判据
+ // 自己拿 lstat 说话,链接一律显式拒 —— 否则哪天类型位把 junction 报成
+ // directory,这里就会跟进去把技能目录之外的字节算进批准指纹。
+ const entryStat = await fs.promises.lstat(path.join(itemRoot, relativeDir, entry.name));
+ if (entryStat.isSymbolicLink()) {
+ throw new Error(`approved skill rejects link entry: ${item.dir}/${relativePath}`);
+ }
+ if (entryStat.isDirectory()) {
await collect(relativePath);
- } else if (entry.isFile()) {
+ } else if (entryStat.isFile()) {
files.push(relativePath);
} else {
throw new Error(`approved skill rejects non-regular entry: ${item.dir}/${relativePath}`);
@@ -455,9 +463,15 @@ async function copyRegularDirectory(source: string, target: string): Promise<voi
for (const entry of entries) {
const from = path.join(source, entry.name);
const to = path.join(target, entry.name);
- if (entry.isDirectory()) {
+ // 判据与 hashApprovedSkillContent 同形:逐条 lstat、链接显式拒,不依赖 Dirent
+ // 的类型位。两侧必须同形,否则指纹算的和快照拷的可能不是同一组字节。
+ const entryStat = await fs.promises.lstat(from);
+ if (entryStat.isSymbolicLink()) {
+ throw new Error(`skill snapshot rejects link entry: ${from}`);
+ }
+ if (entryStat.isDirectory()) {
await copyRegularDirectory(from, to);
- } else if (entry.isFile()) {
+ } else if (entryStat.isFile()) {
await fs.promises.copyFile(from, to, fs.constants.COPYFILE_EXCL);
} else {
throw new Error(`skill snapshot rejects non-regular entry: ${from}`);