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): realpath the ghost roots and bound migration-ledger reads

Yuewei-LL823f2f6c7c85
diff --git a/apps/desktop/src/main/cindy-brain/GhostManager.ts b/apps/desktop/src/main/cindy-brain/GhostManager.ts
index b99a14bd99..9f570c4a0b 100644
--- a/apps/desktop/src/main/cindy-brain/GhostManager.ts
+++ b/apps/desktop/src/main/cindy-brain/GhostManager.ts
@@ -527,13 +527,27 @@ export class GhostManager {
}
private resolveContentRoot(): string {
- return path.resolve(this.options.getRootDir());
+ // 返回 realpath:state/content root 可以位于 symlinked 祖先之下(重定位的
+ // Home/AppData),而 bounded 读取的 containWithin 必须是 realpath 产物,
+ // 否则合法文件会被判在根外而把已批准插件当成 invalid。
+ return this.assertManagedRootPath(
+ path.resolve(this.options.getRootDir()),
+ 'ghost content root',
+ );
}
private resolveStateRoot(): string {
- if (this.options.getStateDir) return path.resolve(this.options.getStateDir());
+ if (this.options.getStateDir) {
+ return this.assertManagedRootPath(
+ path.resolve(this.options.getStateDir()),
+ 'ghost approval state root',
+ );
+ }
const root = this.resolveContentRoot();
- return path.join(path.dirname(root), `${path.basename(root)}-install-state`);
+ return this.assertManagedRootPath(
+ path.join(path.dirname(root), `${path.basename(root)}-install-state`),
+ 'ghost approval state root',
+ );
}
private contentRootDir(): string {
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 f1a4d62e43..56a791d0c1 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
@@ -623,6 +623,33 @@ describe('GhostManager · owner 受管根路径', () => {
expect(fresh.list()).toEqual([]);
});
+ it('symlinked state root 下已批准插件不被判 invalid(P1 回归)', async () => {
+ const physicalUserData = path.join(workDir, 'physical-user-data');
+ const linkedUserData = path.join(workDir, 'linked-user-data');
+ await fs.promises.mkdir(path.join(physicalUserData, 'owners', 'owner-a'), { recursive: true });
+ try {
+ await fs.promises.symlink(
+ physicalUserData,
+ linkedUserData,
+ process.platform === 'win32' ? 'junction' : 'dir',
+ );
+ } catch {
+ return;
+ }
+
+ const relocated = new GhostManager({
+ getRootDir: () => path.join(linkedUserData, 'owners', 'owner-a', 'cindy-brain'),
+ getStateDir: () => path.join(linkedUserData, 'owners', 'owner-a', 'ghost-install-state'),
+ });
+ await relocated.install(await makeCindy('approved.cindy', goodManifest('approved')));
+
+ // receipt 落在 realpath 状态根内;containWithin 必须用 realpath 根,
+ // 否则合法 receipt 会被判在根外 → list() 降级成 invalid。
+ const listed = relocated.list();
+ expect(listed).toHaveLength(1);
+ expect(listed[0].approval.state).toBe('approved');
+ });
+
it('仍拒绝受管根本身是 symlink/junction', async () => {
const outside = path.join(workDir, 'outside-root');
const linkedRoot = path.join(workDir, 'linked-root');
@@ -1605,30 +1632,28 @@ describe('GhostManager · review 第 6 轮回归(P0/P1 修复钉住)', () => {
state: 'in-progress',
pendingIds: ['existing'],
};
- await fs.promises.writeFile(migrationLedgerPath(), JSON.stringify(originalLedger));
+ // 台账经 bounded no-follow 读取,超上限即视为不可读(不再用 unbounded
+ // readFileSync)。构造一个超限 ledger,验证门保守关死且 recovery queue 不被覆盖。
+ const oversizedLedger = {
+ ...originalLedger,
+ migratedIds: Array.from({ length: 900 }, (_, i) => `pad-${'x'.repeat(120)}-${i}`),
+ };
+ await fs.promises.writeFile(
+ migrationLedgerPath(),
+ JSON.stringify(oversizedLedger),
+ );
await writeLegacyInstall('recovered', goodManifest('recovered'));
- const realReadFileSync = fs.readFileSync;
- const readSpy = vi.spyOn(fs, 'readFileSync').mockImplementation((target, options) => {
- if (path.resolve(String(target)) === path.resolve(migrationLedgerPath())) {
- throw Object.assign(new Error('EIO: ledger temporarily unreadable'), { code: 'EIO' });
- }
- return realReadFileSync(target, options as never);
- });
- try {
- await expect(
- manager.backfillRecoveredLegacyGhosts(
- ['recovered'],
- await recoveredProjectionOptions('recovered'),
- ),
- ).rejects.toThrow(
- /ledger exists but is unreadable/,
- );
- } finally {
- readSpy.mockRestore();
- }
+ await expect(
+ manager.backfillRecoveredLegacyGhosts(
+ ['recovered'],
+ await recoveredProjectionOptions('recovered'),
+ ),
+ ).rejects.toThrow(
+ /ledger exists but is unreadable/,
+ );
expect(JSON.parse(await fs.promises.readFile(migrationLedgerPath(), 'utf8'))).toEqual(
- originalLedger,
+ oversizedLedger,
);
});
diff --git a/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts b/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
index 0157f299c9..c4fe1ab255 100644
--- a/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
+++ b/apps/desktop/src/main/cindy-brain/ghostInstallReceipt.ts
@@ -32,6 +32,7 @@ const RECEIPT_SCHEMA_VERSION = 2;
const MAX_RECEIPT_BYTES = 2 * 1024 * 1024;
const MAX_PENDING_MUTATION_BYTES = 64 * 1024;
const MAX_ICON_DATA_URL_BYTES = 768 * 1024;
+const MAX_MIGRATION_LEDGER_BYTES = 64 * 1024;
/**
* 受管 icon 快照的完整形态:声明的图片 mime + 严格 base64 载荷。载荷字符集也要
* 校验 —— 只认前缀会让被改写的 receipt 把任意字符串塞进 renderer 的 img src。
@@ -404,8 +405,17 @@ export class GhostInstallReceiptStore {
/** 读迁移台账(缺失/损坏返回 null;追加 id 时用,判定门只看 hasMigrationLedger)。 */
readMigrationLedger(): GhostLegacyMigrationLedger | null {
try {
+ // 与 receipt 同源:no-follow + 限量 + realpath 根内复核。FIFO / symlink 到
+ // 无界大文件必须在 parse 前就被拒绝,否则启动路径(migrationDoorClosed)
+ // 会阻塞或分配无界内存,而不是把台账按不可读关门。
+ const bytes = readBoundedFileNoFollowSync(
+ this.migrationLedgerPath(),
+ MAX_MIGRATION_LEDGER_BYTES,
+ { containWithin: this.rootDir() },
+ );
+ if (bytes === null) return null;
const raw = JSON.parse(
- fs.readFileSync(this.migrationLedgerPath(), 'utf8'),
+ bytes.toString('utf8'),
) as GhostLegacyMigrationLedger;
if (
!raw ||