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(auto-review): 递归审 script block 里的写目标,块没闭合按不可执行处理

Chrisc6a785f759c3
diff --git a/packages/maker-core/src/agents/shared/auto-review.test.ts b/packages/maker-core/src/agents/shared/auto-review.test.ts
index cb3d269751..696bb5f759 100644
--- a/packages/maker-core/src/agents/shared/auto-review.test.ts
+++ b/packages/maker-core/src/agents/shared/auto-review.test.ts
@@ -3118,6 +3118,61 @@ describe('参数形式的系统路径写入 / setsid 选项(第三十五批评
}
});
+ it('script block 里的写目标要递归审,块没闭合就 fail closed', () => {
+ // 块里是一段完整命令文本,块外的段判据看不到它。`& { … }` 恰好已被覆盖(`&` 是段分隔符、`{` 又被
+ // stripShellControlTokens 剥掉),真正漏的是没有分隔符可依赖的那几种(codex 报 call operator,
+ // 实测该形态修前已必问)。
+ const win = ['C:\\repo'];
+ const hosts = 'C:\\Windows\\System32\\drivers\\etc\\hosts';
+ for (const c of [
+ `. { Set-Content ${hosts} owned }`, // 点源块
+ `Invoke-Command -ScriptBlock { Set-Content ${hosts} owned }`,
+ `Start-Job -ScriptBlock { Set-Content ${hosts} owned }`,
+ `Start-Job -ScriptBlock:{ Set-Content ${hosts} owned }`, // 贴值
+ `Get-ChildItem | ForEach-Object { Set-Content ${hosts} owned }`,
+ `Get-ChildItem | % { Remove-Item ${hosts} }`, // 别名
+ `. { . { Set-Content ${hosts} owned } }`, // 嵌套
+ // 块内自带 cd:递归里 cwd 跟踪照常生效。
+ `Invoke-Command -ScriptBlock { cd C:\\Windows\\System32; Set-Content payload.txt owned }`,
+ // 块没闭合 = 看不到真实载荷,而写法明确要执行它 → fail closed。
+ `. { Set-Content ${hosts} owned`,
+ `Invoke-Command -ScriptBlock { Set-Content ${hosts} owned`,
+ // 包装成 pwsh -Command 后同样覆盖。
+ `pwsh -Command '. { Set-Content ${hosts} owned }'`,
+ // 修前已必问的形态,一并钉住防回退。
+ `& { Set-Content ${hosts} owned }`,
+ `& { & { Set-Content ${hosts} owned } }`,
+ ]) {
+ expect(classifyShellCommand(c, win, { platform: 'win32' }), c).toBe('prompt-each-time');
+ }
+
+ // 反例一:块里写的是区内路径 → 判档不变。递归用的是同一套判据,不是"见到块就升级"。
+ for (const c of [
+ '& { Set-Content C:\\repo\\a.txt x }',
+ '. { Set-Content C:\\repo\\a.txt x }',
+ 'Invoke-Command -ScriptBlock { Set-Content C:\\repo\\a.txt x }',
+ 'Get-ChildItem C:\\repo\\build | ForEach-Object { Set-Content C:\\repo\\out.txt x }',
+ 'cd C:\\repo\\build && & { Set-Content a.txt x }',
+ ]) {
+ expect(classifyShellCommand(c, win, { platform: 'win32' }), c).toBe('prompt');
+ }
+ // 反例二:引号里的大括号不是块;非"要执行"的写法即使没闭合也不升级。
+ for (const c of [
+ "Set-Content C:\\repo\\a.txt '{'",
+ "Get-Content C:\\repo\\a.txt | ForEach-Object { $_ -replace '}','' }",
+ "Get-ChildItem C:\\repo\\build | Where-Object { $_ -eq 'x'", // 没闭合但不是 &/. /-ScriptBlock
+ ]) {
+ expect(classifyShellCommand(c, win, { platform: 'win32' }), c).toBe('prompt');
+ }
+ // 反例三:POSIX 侧的大括号用法判档不变(`find … {} \;`、awk 程序体)。
+ for (const c of ['find . -name x -exec rm {} \\;', "awk '{print $1}' /ws/a.txt"]) {
+ expect(classifyShellCommand(c, ['/ws'], { platform: 'linux' }), c).toBe('prompt');
+ }
+ // POSIX 的命令组本来就必问,递归不改它。
+ expect(classifyShellCommand('{ rm -rf /etc; }', ['/ws'], { platform: 'linux' }))
+ .toBe('prompt-each-time');
+ });
+
it('PowerShell 的 *> 全流重定向与 > 是同一个写通道', () => {
// `*>` / `*>>` 把所有流一起写进目标(about_Redirection),重定向提取只认 `>`/`N>`/`&>` → 整条
// 落灰区(codex 报)。
diff --git a/packages/maker-core/src/agents/shared/auto-review.ts b/packages/maker-core/src/agents/shared/auto-review.ts
index 3cc638f926..7318d97ee3 100644
--- a/packages/maker-core/src/agents/shared/auto-review.ts
+++ b/packages/maker-core/src/agents/shared/auto-review.ts
@@ -3604,6 +3604,55 @@ function pipelineFedWriteTargetNeedsConsent(
});
}
+/**
+ * 「明确要执行这个大括号块」的写法:`&`/`.` 紧跟 `{`(call operator / 点源),或 `-ScriptBlock {`
+ * (`Invoke-Command`、`Start-Job`、`Start-Process` 等)。解析不出完整块时**这些写法**要 fail closed
+ * —— 其余场合的大括号(hashtable、通配符展开、`find … {} \;`)不因解析不完整而升级。
+ */
+const EXECUTABLE_SCRIPT_BLOCK = /(?:^|[\s;|&(])[&.]\s*\{|-scriptblock\s*[:=]?\s*\{/i;
+
+/**
+ * 抽出命令里**最外层**大括号块的内容,供递归审查。引号里的大括号不算(`-replace '}',''`)、
+ * 反引号转义的也不算。嵌套块由递归自然覆盖,所以这里只取最外层。
+ *
+ * `& { … }` 这一形态本来就已必问 —— `&` 是段分隔符,`{` 又被 stripShellControlTokens 剥掉,里面的
+ * `Set-Content` 恰好落回普通段判据。但 `. { … }`、`-ScriptBlock { … }`、`ForEach-Object { … }`
+ * 没有分隔符可依赖,块里的写目标一个都进不了判据(codex 报的是 call operator,实测该形态已必问,
+ * 真正漏的是这三种)。所以这里不按操作符挑,直接对所有块递归 —— 递归只会**增加**命中,不会放松。
+ */
+function braceBlockPayloads(command: string): { payloads: string[]; unbalanced: boolean } {
+ const payloads: string[] = [];
+ let depth = 0;
+ let start = -1;
+ let quote: "'" | '"' | null = null;
+ for (let i = 0; i < command.length; i++) {
+ const ch = command[i];
+ if (quote !== null) {
+ if (ch === quote) quote = null;
+ continue;
+ }
+ if (ch === "'" || ch === '"') {
+ quote = ch;
+ continue;
+ }
+ if (ch === '`') { // PowerShell 转义符:跳过被转义的那个字符
+ i += 1;
+ continue;
+ }
+ if (ch === '{') {
+ if (depth === 0) start = i + 1;
+ depth += 1;
+ continue;
+ }
+ if (ch === '}' && depth > 0) {
+ depth -= 1;
+ if (depth === 0 && start >= 0) payloads.push(command.slice(start, i));
+ continue;
+ }
+ }
+ return { payloads, unbalanced: depth > 0 };
+}
+
/** 系统/区外批量破坏与受保护分支强推不能只交给模型裁决。 */
function scopedDestructionNeedsConsent(
command: string,
@@ -3611,6 +3660,20 @@ function scopedDestructionNeedsConsent(
opts: ShellReviewOptions,
depth = 0,
): boolean {
+ // script block 里是一段完整命令文本,块外的段判据看不到它(`. { Set-Content <系统路径> owned }`
+ // 的 bin 是 `.`,写目标表抽不到东西 → 整条落灰区,codex 报)。递归用同一套判据审块内文本。
+ const blocks = braceBlockPayloads(command);
+ if (blocks.payloads.length > 0 || blocks.unbalanced) {
+ // 块没闭合 / 递归到上限 = 看不到真实载荷。**明确要执行它**时不能当没看见。
+ if ((blocks.unbalanced || depth >= MAX_EXEC_REVIEW_DEPTH)
+ && EXECUTABLE_SCRIPT_BLOCK.test(command)) return true;
+ if (depth < MAX_EXEC_REVIEW_DEPTH) {
+ for (const inner of blocks.payloads) {
+ if (inner.trim().length > 0
+ && scopedDestructionNeedsConsent(inner, workspaceRoots, opts, depth + 1)) return true;
+ }
+ }
+ }
let currentCwd: string | undefined = opts.cwd ?? workspaceRoots[0];
let currentCwdUnknown = opts.cwdUnknown === true;
// 上一段的位置操作数 —— 供「写 cmdlet 的目标由 pipeline 喂进来」时证明上游枚举的位置在区内。