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(dir-dist): reject files outside the manifest (exact-set match), not just verify listed ones

Chris48e342128678
diff --git a/scripts/__tests__/ensure-agent-binaries.test.mjs b/scripts/__tests__/ensure-agent-binaries.test.mjs
index f33e8acb69..b0a5123c77 100644
--- a/scripts/__tests__/ensure-agent-binaries.test.mjs
+++ b/scripts/__tests__/ensure-agent-binaries.test.mjs
@@ -116,6 +116,20 @@ test('verifyDirDistManifest: rejects size drift, empty manifests, and malformed
fs.writeFileSync(path.join(dir, 'photon.wasm'), Buffer.alloc(8, 2));
assert.equal(verifyDirDistManifest(dir), false);
+ // 清单之外的多余文件(旧构建残留 / 本地污染)→ 拒绝(不能作为未验证资产打进安装包)
+ fs.writeFileSync(path.join(dir, 'photon.wasm'), Buffer.alloc(512, 2)); // 复原
+ writeDirDistManifest(dir);
+ assert.equal(verifyDirDistManifest(dir), true);
+ fs.writeFileSync(path.join(dir, 'stray-residue.bin'), Buffer.alloc(16, 3));
+ assert.equal(verifyDirDistManifest(dir), false);
+ fs.rmSync(path.join(dir, 'stray-residue.bin'));
+ assert.equal(verifyDirDistManifest(dir), true); // 移除多余文件后恢复
+ // 嵌套子目录里的多余文件同样拒绝
+ fs.mkdirSync(path.join(dir, 'nested'), { recursive: true });
+ fs.writeFileSync(path.join(dir, 'nested', 'extra.json'), '{}');
+ assert.equal(verifyDirDistManifest(dir), false);
+ fs.rmSync(path.join(dir, 'nested'), { recursive: true, force: true });
+
// 空清单 / 非法结构 → 拒绝
fs.writeFileSync(path.join(dir, '.manifest'), JSON.stringify({ files: [] }));
assert.equal(verifyDirDistManifest(dir), false);
diff --git a/tools/shared/dir-dist-manifest.mjs b/tools/shared/dir-dist-manifest.mjs
index 5f0d5bfb43..bf32ebf809 100644
--- a/tools/shared/dir-dist-manifest.mjs
+++ b/tools/shared/dir-dist-manifest.mjs
@@ -84,5 +84,39 @@ export function verifyDirDistManifest(destDir) {
}
if (actual !== entry.sha256) return false;
}
+ // 反向精确匹配:目录里不得有清单之外的条目。逐文件哈希只覆盖清单内条目,清单外的
+ // 字节(旧构建残留 / 本地污染)完全不参与校验,却会被 ensure-agent-binaries 跳过刷新、
+ // 被 stagePi 原样打进安装包 —— 成为未验证资产(codex review P1)。故递归枚举实际集合,
+ // 要求与清单精确一致:多出普通文件、或出现任何 symlink/非普通文件(污染向量)即 false。
+ const manifestPaths = new Set(files.map((entry) => entry.path));
+ const actualPaths = [];
+ let sawUnexpectedEntry = false;
+ const walk = (dir) => {
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
+ const abs = path.join(dir, entry.name);
+ const rel = path.relative(destDir, abs).split(path.sep).join('/');
+ if (entry.isDirectory()) {
+ walk(abs);
+ continue;
+ }
+ if (MARKER_FILES.has(rel)) continue;
+ // symlink / 设备文件等非普通文件:writeDirDistManifest 从不收录,出现即视为污染。
+ if (!entry.isFile()) {
+ sawUnexpectedEntry = true;
+ continue;
+ }
+ actualPaths.push(rel);
+ }
+ };
+ try {
+ walk(destDir);
+ } catch {
+ return false;
+ }
+ if (sawUnexpectedEntry) return false;
+ if (actualPaths.length !== manifestPaths.size) return false;
+ for (const rel of actualPaths) {
+ if (!manifestPaths.has(rel)) return false;
+ }
return true;
}