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(desktop): guard owner skill and snapshot cleanup

Yuewei-LLb480f2245c3d
diff --git a/apps/desktop/src/main/cindy-brain/ghostSnapshotWorkerProcess.ts b/apps/desktop/src/main/cindy-brain/ghostSnapshotWorkerProcess.ts
index d21bdcda34..b0e8fa0983 100644
--- a/apps/desktop/src/main/cindy-brain/ghostSnapshotWorkerProcess.ts
+++ b/apps/desktop/src/main/cindy-brain/ghostSnapshotWorkerProcess.ts
@@ -78,6 +78,38 @@ async function verifyDirectory(workingDir: string, name: string): Promise<void>
const kind = await classifyGhostDirEntry(target);
if (kind !== 'directory') throw new Error('snapshot id parent changed');
}
+async function removeVerifiedDirectory(
+ expectedParent: GhostSnapshotParentIdentity,
+ workingDir: string,
+ targetPath: string,
+ parentName: string,
+): Promise<void> {
+ const targetStat = await fs.promises.lstat(targetPath, { bigint: true });
+ if (!targetStat.isDirectory() || targetStat.isSymbolicLink()) {
+ throw new Error('snapshot target is not a real directory');
+ }
+ const targetRealPath = await fs.promises.realpath(targetPath);
+ const quarantinePath = `${targetPath}.remove-${process.pid}-${Date.now()}`;
+ await fs.promises.rename(targetPath, quarantinePath);
+ const movedStat = await fs.promises.lstat(quarantinePath, { bigint: true });
+ if (
+ !movedStat.isDirectory()
+ || movedStat.isSymbolicLink()
+ || movedStat.dev !== targetStat.dev
+ || movedStat.ino !== targetStat.ino
+ || !samePath(await fs.promises.realpath(quarantinePath), targetRealPath)
+ ) {
+ // Never recursively delete an unverified path. Leave the quarantined
+ // directory for a later owner-checked cleanup pass.
+ throw new Error('snapshot target identity changed during removal');
+ }
+ // The pathname was renamed through a mutable parent. Revalidate the
+ // owner-bound parent before recursive deletion; on failure the isolated
+ // directory is intentionally left for a later guarded cleanup pass.
+ await verifyParent(expectedParent, workingDir);
+ await verifyDirectory(workingDir, parentName);
+ await fs.promises.rm(quarantinePath, { recursive: true, force: true });
+}
async function copyDirectory(source: string, target: string): Promise<void> {
if ((await classifyGhostDirEntry(source)) !== 'directory') throw new Error(`skill source is not a directory: ${source}`);
await fs.promises.mkdir(target, { recursive: true });
@@ -120,8 +152,9 @@ export async function runGhostSnapshotWorkerRequest(
await verifyParent(request.expectedParent, workingDir);
if (request.operation === 'remove') {
await verifyParent(request.expectedParent, workingDir);
- if (parts.length === 1) await verifyDirectory(workingDir, parts[0]);
- await fs.promises.rm(targetPath, { recursive: true, force: true });
+ if (parts.length !== 1) throw new Error('invalid snapshot removal target');
+ await verifyDirectory(workingDir, parts[0]);
+ await removeVerifiedDirectory(request.expectedParent, workingDir, targetPath, parts[0]);
send({ ok: true }); return;
}
if (!request.receipt || !request.sourceDir) throw new Error('approved skill snapshot is missing');
@@ -189,7 +222,7 @@ export async function runGhostSnapshotWorkerRequest(
await verifyParent(request.expectedParent, workingDir);
await verifyDirectory(workingDir, parts[0]);
try {
- const recheck = await fs.promises.lstat(targetPath);
+ await fs.promises.lstat(targetPath);
throw new Error('snapshot target recreated before publish');
} catch (error) {
if (!hasCode(error, 'ENOENT')) throw error;
diff --git a/apps/desktop/src/main/cindy-brain/skillSlot.ts b/apps/desktop/src/main/cindy-brain/skillSlot.ts
index ae06480491..3bf1c73330 100644
--- a/apps/desktop/src/main/cindy-brain/skillSlot.ts
+++ b/apps/desktop/src/main/cindy-brain/skillSlot.ts
@@ -195,6 +195,19 @@ export async function removeGhostSkillLinksForRoots(
blockers.push(message);
continue;
}
+ // The link may have been replaced after the ownership read. Re-read
+ // the raw target immediately before unlinking so a user-owned link
+ // cannot be removed merely because the old target was managed.
+ const finalRawTarget = await fsp.readlink(linkPath);
+ const finalLexicalTarget = normalizeForCompare(
+ path.resolve(path.dirname(linkPath), finalRawTarget),
+ );
+ if (finalLexicalTarget !== lexicalTarget) {
+ const message = `Skipped owner skill link whose target changed before removal: ${linkPath}`;
+ warnings.push(message);
+ blockers.push(message);
+ continue;
+ }
await fsp.unlink(linkPath);
changed = true;
} catch (err) {