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): 收紧 .cindy 权限恢复的边界与导出快照

zhanghengxd2e9f36494a2b
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 d01b7d1487..2acc3608d0 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
@@ -79,7 +79,11 @@ function goodManifest(id = 'hello'): Record<string, unknown> {
describe('installedFileModeFromZip', () => {
it('normalizes strings, strips special bits, and skips Windows or missing metadata', () => {
expect(installedFileModeFromZip('755', 'linux')).toBe(0o755);
+ expect(installedFileModeFromZip(0o644, 'linux')).toBe(0o644);
expect(installedFileModeFromZip(0o4755, 'darwin')).toBe(0o755);
+ expect(installedFileModeFromZip(0o777, 'linux')).toBe(0o755);
+ expect(installedFileModeFromZip(0o666, 'linux')).toBe(0o644);
+ expect(installedFileModeFromZip(0o700, 'linux')).toBe(0o700);
expect(installedFileModeFromZip(0o000, 'linux')).toBe(0o600);
expect(installedFileModeFromZip(0o120777, 'linux')).toBeNull();
expect(installedFileModeFromZip(null, 'linux')).toBeNull();
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/exportGhostPackage.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/exportGhostPackage.test.ts
index 55181c021e..66a04961a2 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/exportGhostPackage.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/exportGhostPackage.test.ts
@@ -256,14 +256,21 @@ describe('exportGhostPackage', () => {
await writeStatement(['ghost.json', 'locales/en.json', 'main.js']);
// 第一次读 main.js 返回陈旧字节(模拟并发更新读到旧目录):
// 哈希与 statement 不符,必须整体重读,最终包内容应为真实字节。
- const realReadFile = fs.promises.readFile;
+ const realOpen = fs.promises.open;
let staleServed = false;
- const spy = vi.spyOn(fs.promises, 'readFile').mockImplementation(async (p: any, opts: any) => {
+ const spy = vi.spyOn(fs.promises, 'open').mockImplementation(async (p: any, flags: any) => {
+ const fileHandle = await realOpen(p, flags);
if (String(p).endsWith('main.js') && !staleServed) {
- staleServed = true;
- return Buffer.from('stale-bytes');
+ const realHandleReadFile = fileHandle.readFile.bind(fileHandle);
+ fileHandle.readFile = vi.fn(async (...args: any[]) => {
+ if (!staleServed) {
+ staleServed = true;
+ return Buffer.from('stale-bytes');
+ }
+ return realHandleReadFile(...args);
+ }) as unknown as typeof fileHandle.readFile;
}
- return realReadFile(p, opts) as any;
+ return fileHandle;
});
try {
const result = await exportGhostPackage('hello', makeDeps());
diff --git a/apps/desktop/src/main/cindy-brain/exportGhostPackage.ts b/apps/desktop/src/main/cindy-brain/exportGhostPackage.ts
index 3fae769e31..f667fb5373 100644
--- a/apps/desktop/src/main/cindy-brain/exportGhostPackage.ts
+++ b/apps/desktop/src/main/cindy-brain/exportGhostPackage.ts
@@ -157,39 +157,44 @@ async function readSignedDoc(dir: string): Promise<SignedDoc | null> {
const sigPath = path.join(dir, GHOST_SIGNATURE_FILE);
// 先 stat 再读(评审 P1):超上限的签名文件反正过不了装入,不把它
// 完整读进内存;读后再查一次字节数,挡住 stat 后被改大的窗口。
- let stat: fs.Stats;
+ let fileHandle: fs.promises.FileHandle;
try {
- stat = await fs.promises.stat(sigPath);
+ fileHandle = await fs.promises.open(sigPath, 'r');
} catch (err) {
if ((err as NodeJS.ErrnoException)?.code === 'ENOENT') return null;
throw err;
}
- if (stat.size > MAX_SIGNATURE_FILE_BYTES) throw new Error('signature file too large');
- const raw = await fs.promises.readFile(sigPath);
- if (raw.byteLength > MAX_SIGNATURE_FILE_BYTES) {
- throw new Error('signature file too large');
- }
- const doc = JSON.parse(raw.toString('utf8')) as {
- statement?: { files?: Array<{ path?: unknown; sha256?: unknown; bytes?: unknown }> };
- };
- const files = doc?.statement?.files;
- if (!Array.isArray(files)) throw new Error('invalid signature statement');
- const parsed: SignedDoc['files'] = [];
- for (const item of files) {
- if (
- typeof item?.path !== 'string' ||
- typeof item?.sha256 !== 'string' ||
- typeof item?.bytes !== 'number'
- ) {
- throw new Error('invalid signature statement entry');
+ try {
+ const stat = await fileHandle.stat();
+ if (stat.size > MAX_SIGNATURE_FILE_BYTES) throw new Error('signature file too large');
+ const raw = await fileHandle.readFile();
+ if (raw.byteLength > MAX_SIGNATURE_FILE_BYTES) {
+ throw new Error('signature file too large');
+ }
+ const doc = JSON.parse(raw.toString('utf8')) as {
+ statement?: { files?: Array<{ path?: unknown; sha256?: unknown; bytes?: unknown }> };
+ };
+ const files = doc?.statement?.files;
+ if (!Array.isArray(files)) throw new Error('invalid signature statement');
+ const parsed: SignedDoc['files'] = [];
+ for (const item of files) {
+ if (
+ typeof item?.path !== 'string' ||
+ typeof item?.sha256 !== 'string' ||
+ typeof item?.bytes !== 'number'
+ ) {
+ throw new Error('invalid signature statement entry');
+ }
+ parsed.push({ path: item.path, sha256: item.sha256, bytes: item.bytes });
}
- parsed.push({ path: item.path, sha256: item.sha256, bytes: item.bytes });
+ return {
+ raw,
+ unixPermissions: unixRegularFilePermissionsForArchive(stat.mode),
+ files: parsed,
+ };
+ } finally {
+ await fileHandle.close();
}
- return {
- raw,
- unixPermissions: unixRegularFilePermissionsForArchive(stat.mode),
- files: parsed,
- };
}
/** statement 路径必须相对且不逃逸——装入侧已校验,这里防一手目录被改。 */
@@ -212,11 +217,17 @@ async function readSignedEntries(dir: string, doc: SignedDoc): Promise<PackageEn
// 先 stat 对尺寸:与 statement 不符必然哈希也不符,不必把可能超限
// 的文件读进内存(评审 P1:巨型缓存文件要先挡在分配之前)。
let data: Buffer;
- let stat: fs.Stats;
+ let unixPermissions: number;
try {
- stat = await fs.promises.stat(path.join(dir, ...item.path.split('/')));
- if (stat.size !== item.bytes) return null;
- data = await fs.promises.readFile(path.join(dir, ...item.path.split('/')));
+ const fileHandle = await fs.promises.open(path.join(dir, ...item.path.split('/')), 'r');
+ try {
+ const stat = await fileHandle.stat();
+ if (stat.size !== item.bytes) return null;
+ data = await fileHandle.readFile();
+ unixPermissions = unixRegularFilePermissionsForArchive(stat.mode);
+ } finally {
+ await fileHandle.close();
+ }
} catch {
return null;
}
@@ -225,7 +236,7 @@ async function readSignedEntries(dir: string, doc: SignedDoc): Promise<PackageEn
out.push({
rel: item.path,
data,
- unixPermissions: unixRegularFilePermissionsForArchive(stat.mode),
+ unixPermissions,
});
}
return out;
diff --git a/apps/desktop/src/main/cindy-brain/ghostZipPermissions.ts b/apps/desktop/src/main/cindy-brain/ghostZipPermissions.ts
index 5708c57840..3d5d121c55 100644
--- a/apps/desktop/src/main/cindy-brain/ghostZipPermissions.ts
+++ b/apps/desktop/src/main/cindy-brain/ghostZipPermissions.ts
@@ -80,7 +80,9 @@ export function installedFileModeFromZip(
if (platform === 'win32') return null;
const parsed = parseZipUnixPermissions(unixPermissions);
if (parsed === null || !declaresType(parsed, S_IFREG)) return null;
- return (parsed & PERMISSION_BITS) | 0o600;
+ // 篡改包或权限设置粗心的包不得把 group/world 可写文件装进插件内容目录,
+ // 否则同机其它账号可改写受害用户之后会执行的插件代码。
+ return (parsed & PERMISSION_BITS & ~0o022) | 0o600;
}
/**