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): 修包装转义吃掉双引号路径,并按 cmdlet 区分 -Path 是源还是目标

Chris76cdd67d5394
diff --git a/packages/maker-core/src/agents/claude-code/__tests__/auto-review-policy.test.ts b/packages/maker-core/src/agents/claude-code/__tests__/auto-review-policy.test.ts
index c6034c5087..96341c5666 100644
--- a/packages/maker-core/src/agents/claude-code/__tests__/auto-review-policy.test.ts
+++ b/packages/maker-core/src/agents/claude-code/__tests__/auto-review-policy.test.ts
@@ -317,39 +317,52 @@ describe('工具映射漏项不得变成静默拒绝', () => {
// POWERSHELL_DANGER_PATTERNS 一条都匹配不上,红线形同虚设;而落到兜底 other
// 又会因缺 description 在调模型前被直接 block(静默拒绝)。
expect(normalizeBuiltinToolForAutoReview('PowerShell', { command: 'Get-ChildItem' }))
- .toEqual({ kind: 'exec', command: 'pwsh -Command "Get-ChildItem"' });
- // **用双引号包装、内层单引号原样保留**:PowerShell 路径很常见地写成单引号
- // (`Set-Content 'C:\Program Files\x' …`)。早先用单引号包装并把内层 `'` 转成 POSIX 的
- // `'\''`,core 的 tokenizer 还原不回原路径 —— 单引号写的系统路径因此取不到写目标、
- // 掉进灰区(而同一条命令交 Bash 入口是必问)。下一条用例锁住修复效果。
+ .toEqual({ kind: 'exec', command: "pwsh -Command 'Get-ChildItem'" });
+ // 包装的唯一判据是「core 的 tokenizer 能不能把这一个 token 还原成原文」,所以用的是
+ // **tokenizer 自己那套规则**,不是 PowerShell 的。两种引号都得能过 —— 这里各锁一条:
+ // · 内层**双**引号:PowerShell 的重复引号规则(`"` → `""`)在 POSIX 规则下是"闭引号紧接
+ // 开引号"= 字符串拼接,引号会被吃掉、路径按空格拆散(见下一条用例);
+ // · 内层**单**引号:POSIX 的 `\'` 转义也不行 —— tokenizer 为了 Windows 路径分隔符会
+ // 连反斜杠一起保留,还原成 `\'C:\…\'`。
+ // 只有单引号包装 + 内层 `'` → `'"'"'` 两者都对。
expect(normalizeBuiltinToolForAutoReview('PowerShell', { command: "echo 'x'" }))
- .toEqual({ kind: 'exec', command: 'pwsh -Command "echo \'x\'"' });
- // 内层双引号按 PowerShell 自己的重复引号规则转义(仍是单射 → 身份无损)。
+ .toEqual({ kind: 'exec', command: `pwsh -Command 'echo '"'"'x'"'"''` });
+ // 内层双引号原样保留(不转义 → 也不会被 tokenizer 当成拼接)。
expect(normalizeBuiltinToolForAutoReview('PowerShell', { command: 'echo "x"' }))
- .toEqual({ kind: 'exec', command: 'pwsh -Command "echo ""x"""' });
+ .toEqual({ kind: 'exec', command: `pwsh -Command 'echo "x"'` });
// 空命令仍按证据不足处理,不拼出一个只有前缀的假命令。
expect(normalizeBuiltinToolForAutoReview('PowerShell', { command: ' ' }))
.toEqual({ kind: 'exec', command: '' });
// 非解释器目标 / 引号未闭合都不算解释器调用 → 照常整条包装。
expect(normalizeBuiltinToolForAutoReview('PowerShell', { command: "& 'C:\\tools\\my.exe' -x" }))
- .toEqual({ kind: 'exec', command: 'pwsh -Command "& \'C:\\tools\\my.exe\' -x"' });
+ .toEqual({ kind: 'exec', command: `pwsh -Command '& '"'"'C:\\tools\\my.exe'"'"' -x'` });
expect(normalizeBuiltinToolForAutoReview('PowerShell', { command: "& 'C:\\PF\\pwsh.exe -enc X" }))
- .toEqual({ kind: 'exec', command: 'pwsh -Command "& \'C:\\PF\\pwsh.exe -enc X"' });
+ .toEqual({ kind: 'exec', command: `pwsh -Command '& '"'"'C:\\PF\\pwsh.exe -enc X'` });
// `.\script.ps1` / `./script.ps1` 是相对路径调用,开头的 `.` 不是点源运算符。
expect(normalizeBuiltinToolForAutoReview('PowerShell', { command: '.\\script.ps1 -enc X' }))
- .toEqual({ kind: 'exec', command: 'pwsh -Command ".\\script.ps1 -enc X"' });
+ .toEqual({ kind: 'exec', command: `pwsh -Command '.\\script.ps1 -enc X'` });
expect(normalizeBuiltinToolForAutoReview('PowerShell', { command: './script.ps1 -enc X' }))
- .toEqual({ kind: 'exec', command: 'pwsh -Command "./script.ps1 -enc X"' });
+ .toEqual({ kind: 'exec', command: `pwsh -Command './script.ps1 -enc X'` });
});
- it('单引号写的系统路径也要取到写目标(包装用双引号的原因)', () => {
+ it('带引号的系统路径三种写法都要取到写目标(包装转义的回归锁)', () => {
const hosts = 'C:\\Windows\\System32\\drivers\\etc\\hosts';
- // 三种引号写法都必须与 Bash 入口结论一致 —— 早先单引号形态在 PowerShell 工具入口
- // 掉进灰区,而 Bash 入口是必问,那个差异纯粹由包装的转义造成。
+ // **必须用带空格的路径**才能测出包装转义的问题:早先这条用例只用了 `…\etc\hosts`,
+ // 它不含空格,所以即使引号被 tokenizer 吃掉、路径按空格拆散,拆出来的第一段仍是整条路径,
+ // 判据照旧命中 —— 双引号形态的缺陷因此被这条用例漏过(codex 报)。
+ const defender = 'C:\\Program Files\\Windows Defender\\x';
for (const command of [
+ // 单引号:早先用 POSIX `\'` 转义,tokenizer 连反斜杠一起保留 → 还原不回原路径。
+ `Set-Content '${defender}' owned`,
+ `Copy-Item payload '${defender}'`,
+ // 双引号:早先按 PowerShell 的重复引号规则转成 `""`,在 POSIX 规则下等于字符串拼接 →
+ // 引号被吃掉,`C:\Program Files\…` 被拆成 `C:\Program` + `Files\Windows` + …,判据失效。
+ `Set-Content "${defender}" owned`,
+ `Remove-Item "${defender}"`,
+ `Copy-Item C:\\repo\\payload "${defender}"`,
+ // 裸路径(无空格)与带引号的无空格路径:回归基线。
`Set-Content '${hosts}' owned`,
- `Copy-Item payload '${hosts}'`,
`Set-Content "${hosts}" owned`,
`Set-Content ${hosts} owned`,
]) {
@@ -357,8 +370,9 @@ describe('工具映射漏项不得变成静默拒绝', () => {
expect(verdict('PowerShell', { command }), `${command} 与 Bash 入口一致`)
.toBe(verdict('Bash', { command }));
}
- // 区内的单引号路径不受影响,仍是灰区。
+ // 区内的带引号路径不受影响,仍是灰区(转义修好不等于顺手升级)。
expect(verdict('PowerShell', { command: "Set-Content 'C:\\repo\\a.txt' hi" })).toBe('prompt');
+ expect(verdict('PowerShell', { command: 'Set-Content "C:\\repo\\my notes.txt" hi' })).toBe('prompt');
});
it('PowerShell 判档实测表:哪些必问、哪些留灰区、上限在哪', () => {
diff --git a/packages/maker-core/src/agents/claude-code/auto-review-policy.ts b/packages/maker-core/src/agents/claude-code/auto-review-policy.ts
index 6879fec2d5..039b6a0c51 100644
--- a/packages/maker-core/src/agents/claude-code/auto-review-policy.ts
+++ b/packages/maker-core/src/agents/claude-code/auto-review-policy.ts
@@ -98,16 +98,26 @@ function powerShellExecCommand(command: string): string {
/**
* 把任意命令文本包成**一个** token,供审查判据取整条载荷。
*
- * **用双引号 + 重复引号转义(`"` → `""`),不用单引号**:载荷是 PowerShell 语句,里面的路径
- * 很常见地写成单引号(`Set-Content 'C:\Program Files\x' …`)。早先用单引号包装并把内层 `'`
- * 转成 POSIX 的 `'\''`,core 的 tokenizer 还原不回原路径 —— 于是单引号写的系统路径取不到
- * 写目标、掉进灰区(实测:同一条命令交 `Bash` 入口是必问)。改成双引号后内层单引号原样保留,
- * 双引号按 PowerShell 自己的重复引号规则转义,core 两边都认。
+ * 判据是"core 的 tokenizer 能不能把这一个 token 还原成原文"。它的两条相关行为决定了写法:
+ * 1. 单引号内**一切原样**(连反斜杠都保留)—— Windows 路径需要这个;
+ * 2. 引号外的 `\X` **连反斜杠一起保留**(为了 Windows 路径分隔符),所以 POSIX 的 `\'`
+ * 转义还原不回一个裸单引号。
+ *
+ * 于是:**单引号包装,内层 `'` 转成 `'"'"'`**(闭单引号 → 双引号裹一个单引号 → 重开单引号,
+ * 相邻片段被 tokenizer 拼回同一个 token)。这样内层的 `"`、`\`、空格全部逐字还原。
+ *
+ * 两条都是实测踩过的坑,不要再换回去:
+ * - 用 `'\''` 转义 → tokenizer 保留反斜杠,单引号写的路径还原成 `\'C:\…\'`,取不到写目标;
+ * - 改用双引号 + PowerShell 的重复引号规则(`"` → `""`)→ 单引号路径修好了,但**双引号**
+ * 写的路径反而坏掉:`""` 在 POSIX 规则下是"闭引号紧接开引号"= 字符串拼接,于是
+ * `Set-Content "C:\Program Files\Windows Defender\x" owned` 的引号被吃掉、路径按空格
+ * 拆成 `C:\Program` + `Files\Windows` + …,系统路径判据整条失效(codex 报,已实测)。
+ * 两种引号都得能过,所以只能用 tokenizer 自己那套规则,不能用 PowerShell 的。
*
* 只服务静态审查、不用于真实执行;转义单射,所以包装对缓存身份无损:不同原文必得不同结果。
*/
function quoteAsSingleShellToken(text: string): string {
- return `"${text.replaceAll('"', '""')}"`;
+ return `'${text.replaceAll("'", `'"'"'`)}'`;
}
/**
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 d167457b55..3bbbaa0d75 100644
--- a/packages/maker-core/src/agents/shared/auto-review.test.ts
+++ b/packages/maker-core/src/agents/shared/auto-review.test.ts
@@ -2122,6 +2122,59 @@ describe('参数形式的系统路径写入 / setsid 选项(第三十五批评
}
});
+ it('copy/move 的 -Path 是源不是目标;省略 -Destination 时目标默认 cwd', () => {
+ // `-Path` 的语义**按 cmdlet 变**:`Set-Content -Path` 是写目标,`Copy-Item -Path` 是读源。
+ // 一律当目标会同时造成两个方向的错判(codex 报,都已实测)。
+ const win = ['C:\\repo'];
+ const hosts = 'C:\\Windows\\System32\\drivers\\etc\\hosts';
+
+ // 一、把源当目标 → 省略 -Destination 时目标其实是 cwd,隐式写进系统目录会**漏成灰区**。
+ for (const c of [
+ 'cd C:\\Windows\\System32; Copy-Item -Path C:\\repo\\payload',
+ 'cd C:\\Windows\\System32; Copy-Item -LiteralPath C:\\repo\\payload',
+ 'cd C:\\Windows\\System32; Copy-Item C:\\repo\\payload',
+ 'cd C:\\Windows\\System32; Move-Item -Path C:\\repo\\payload',
+ 'cd C:\\Windows\\System32; cpi -Path C:\\repo\\payload',
+ 'cd "C:\\Program Files\\Windows Defender"; Copy-Item -Path C:\\repo\\payload',
+ ]) {
+ expect(classifyShellCommand(c, win, { platform: 'win32' }), c).toBe('prompt-each-time');
+ }
+
+ // 二、反方向:从系统路径**读**、写到工作区内,不该被误升成硬弹窗。
+ for (const c of [
+ `Copy-Item -Path ${hosts} -Destination C:\\repo\\bak`,
+ `Copy-Item -LiteralPath ${hosts} -Destination C:\\repo\\bak`,
+ `Copy-Item ${hosts} C:\\repo\\bak`,
+ `cpi -Path ${hosts} -Destination C:\\repo\\bak`,
+ // cwd 在区内时,省略 -Destination 的复制也只是写区内。
+ 'cd C:\\repo; Copy-Item -Path C:\\repo\\payload',
+ 'Copy-Item -Path C:\\repo\\a -Destination C:\\repo\\b',
+ ]) {
+ expect(classifyShellCommand(c, win, { platform: 'win32' }), c).toBe('prompt');
+ }
+
+ // 三、具名 -Destination 指向系统路径仍必问(修 -Path 语义没有削弱目标侧判定)。
+ for (const c of [
+ `Copy-Item -Path C:\\repo\\payload -Destination ${hosts}`,
+ `Copy-Item -Destination ${hosts} -Path C:\\repo\\payload`,
+ `Move-Item -Path C:\\repo\\payload -Destination ${hosts}`,
+ `Copy-Item C:\\repo\\payload ${hosts}`,
+ ]) {
+ expect(classifyShellCommand(c, win, { platform: 'win32' }), c).toBe('prompt-each-time');
+ }
+
+ // 四、`Move-Item` 的源也被销毁 → 搬走系统文件仍必问,哪怕目标在区内。
+ expect(classifyShellCommand(`Move-Item -Path ${hosts} -Destination C:\\repo\\bak`, win, { platform: 'win32' }))
+ .toBe('prompt-each-time');
+ // 五、`Set-Content` 这类 cmdlet 的 `-Path` 仍是写目标 —— 本改动按 cmdlet 区分,不是全局改语义。
+ expect(classifyShellCommand(`Set-Content -Path ${hosts} -Value owned`, win, { platform: 'win32' }))
+ .toBe('prompt-each-time');
+ expect(classifyShellCommand(`Rename-Item -Path ${hosts} -NewName x`, win, { platform: 'win32' }))
+ .toBe('prompt-each-time');
+ expect(classifyShellCommand(`Remove-Item -Path ${hosts}`, win, { platform: 'win32' }))
+ .toBe('prompt-each-time');
+ });
+
it('setsid 的选项不遮蔽内层破坏命令', () => {
for (const c of [
'setsid -f rm -rf /outside',
diff --git a/packages/maker-core/src/agents/shared/auto-review.ts b/packages/maker-core/src/agents/shared/auto-review.ts
index 6adeeaa327..1232b13147 100644
--- a/packages/maker-core/src/agents/shared/auto-review.ts
+++ b/packages/maker-core/src/agents/shared/auto-review.ts
@@ -311,8 +311,18 @@ function shortClusterOption(
*
* `sources: true` = 源操作数同样被销毁(搬走/改名系统文件等于改掉它),源与目标都算写目标。
* `targets: 'all'` = 每个操作数都是被写/被删的目标(删除类 cmdlet 的 `-Path` 收数组)。
- */
-const POWERSHELL_WRITE_CMDLETS: ReadonlyMap<string, { targets: 'first' | 'last' | 'all'; sources?: boolean }> =
+ * `pathIsSource: true` = 这个 cmdlet 的 `-Path`/`-LiteralPath` 指的是**读**的源,写目标由
+ * `-Destination` 或末位操作数给出。`-Path` 的语义按 cmdlet 变:`Set-Content -Path` 是写目标,
+ * `Copy-Item -Path` 是源 —— 一律当目标会同时造成两个错判(codex 报,都已实测):
+ * · `cd C:\Windows\System32; Copy-Item -Path C:\repo\payload` 把源当目标 → 隐式写进系统
+ * 目录**漏成灰区**(省略 -Destination 时目标是 cwd);
+ * · `Copy-Item -Path <系统路径> -Destination C:\repo\bak`(从系统路径读、写区内)反被
+ * **误升成硬弹窗**。
+ */
+const POWERSHELL_WRITE_CMDLETS: ReadonlyMap<
+ string,
+ { targets: 'first' | 'last' | 'all'; sources?: boolean; pathIsSource?: boolean }
+> =
new Map([
// 写内容到 -Path(位置 0),值是第二个操作数。
['set-content', { targets: 'first' }],
@@ -344,15 +354,15 @@ const POWERSHELL_WRITE_CMDLETS: ReadonlyMap<string, { targets: 'first' | 'last'
['mp', { targets: 'all' }],
['rename-itemproperty', { targets: 'all' }],
['rnp', { targets: 'all' }],
- // 复制:末位操作数是 -Destination,源是只读的。
+ // 复制:末位操作数是 -Destination,源是只读的 → `-Path` 在这里是**源**。
// `copy` 既是 Copy-Item 的别名,也是 cmd.exe 的 copy —— 两者都是「末位是目标」,可共用。
- ['copy-item', { targets: 'last' }],
- ['cpi', { targets: 'last' }],
- ['copy', { targets: 'last' }],
+ ['copy-item', { targets: 'last', pathIsSource: true }],
+ ['cpi', { targets: 'last', pathIsSource: true }],
+ ['copy', { targets: 'last', pathIsSource: true }],
// 移动/改名:源也被销毁 → 两端都算。`move` 同理兼作 cmd.exe 的 move。
- ['move-item', { targets: 'last', sources: true }],
- ['mi', { targets: 'last', sources: true }],
- ['move', { targets: 'last', sources: true }],
+ ['move-item', { targets: 'last', sources: true, pathIsSource: true }],
+ ['mi', { targets: 'last', sources: true, pathIsSource: true }],
+ ['move', { targets: 'last', sources: true, pathIsSource: true }],
['rename-item', { targets: 'first', sources: true }],
['rni', { targets: 'first', sources: true }],
['ren', { targets: 'first', sources: true }],
@@ -384,6 +394,12 @@ const POWERSHELL_TARGET_PARAMS: readonly string[] = [
'-path', '-literalpath', '-lp', '-pspath', '-destination', '-filepath', '-newname',
];
+/**
+ * `-Path` 这一族(含 `-LiteralPath` 的文档别名)—— 它指目标还是指源**由 cmdlet 决定**:
+ * `Set-Content -Path` 是写目标,`Copy-Item -Path` 是读源。见 `pathIsSource`。
+ */
+const POWERSHELL_PATH_PARAMS: readonly string[] = ['-path', '-literalpath', '-lp', '-pspath'];
+
/**
* 写 cmdlet 上**带值**的非目标参数 —— 必须把值一并消费,否则值会被当成位置操作数、顶掉真正的
* 写目标:`Set-Content -ErrorVariable errs C:\Windows\…\hosts owned` 会把 `errs` 当目标,系统
@@ -460,48 +476,57 @@ function powerShellWriteTargets(bin: string, args: string[]): string[] | null {
continue;
}
if (POWERSHELL_SWITCH_PARAMS.includes(candidates[0])) continue; // 已知开关:确定不吃值
- const isTarget = POWERSHELL_TARGET_PARAMS.includes(candidates[0]);
+ // `-Path`/`-LiteralPath` 在 copy/move 上指的是**源**(见 pathIsSource):它的值要按位置
+ // 操作数处理 —— 既不能当写目标,也不能丢掉(`sources: true` 的 cmdlet 源本身也被销毁,
+ // 而且操作数个数决定了"有没有给出目标"、要不要落回 cwd)。
+ const pathIsSourceHere = spec.pathIsSource === true
+ && POWERSHELL_PATH_PARAMS.includes(candidates[0]);
+ const isTarget = POWERSHELL_TARGET_PARAMS.includes(candidates[0]) && !pathIsSourceHere;
if (attached) {
- // 贴在参数上的值:目标参数取它,带值参数直接丢掉。
- if (isTarget) named.push(token.slice(name.length + 1));
+ // 贴在参数上的值:目标参数取它,源路径按操作数收,其它带值参数直接丢掉。
+ const value = token.slice(name.length + 1);
+ if (isTarget) named.push(...splitPowerShellPathList(value));
+ else if (pathIsSourceHere) operands.push(...splitPowerShellPathList(value));
continue;
}
const value = args[i + 1];
- if (!isTarget) {
+ if (!isTarget && !pathIsSourceHere) {
// **带值的非目标参数必须把值一并消费**,否则值会被当操作数、顶掉真正的写目标。
if (value !== undefined && !value.startsWith('-')) i++;
continue;
}
- // 目标参数缺值 = 写通道在、目标不可证 → 哨兵(与 `cp --target-directory` 缺值同口径)。
- named.push(...(value !== undefined && !value.startsWith('-')
- ? splitPowerShellPathList(value)
- : [UNPROVABLE_WRITE_TARGET]));
+ if (value === undefined || value.startsWith('-')) {
+ // 目标参数缺值 = 写通道在、目标不可证 → 哨兵(与 `cp --target-directory` 缺值同口径)。
+ // 源参数缺值 = 没给出源,不虚构操作数。两种情况都**没有**消费下一个 token,不能 i++。
+ if (isTarget) named.push(UNPROVABLE_WRITE_TARGET);
+ continue;
+ }
+ (isTarget ? named : operands).push(...splitPowerShellPathList(value));
i++;
continue;
}
- operands.push(token);
+ operands.push(...splitPowerShellPathList(token));
}
if (named.length > 0) {
// 具名给出目标时,`sources: true` 的 cmdlet 仍要把位置源算进来(`Move-Item src -Dest /etc`);
// `targets: 'all'` 同理 —— 它的语义就是"每个操作数都是目标",具名参数只是**追加**一个,
// 不能因为出现了具名目标就把操作数丢掉(`Rename-ItemProperty <系统路径> -Name a -NewName b`
// 里被改的是位置操作数那个路径,`-NewName` 只是新属性名)。
- return spec.sources || spec.targets === 'all'
- ? [...named, ...operands.flatMap(splitPowerShellPathList)]
- : named;
+ return spec.sources || spec.targets === 'all' ? [...named, ...operands] : named;
}
// 一个操作数都没有 = 命令本身不完整(`Set-Content` 单独一条会报错)。与 coreutils 的
// `cp payload`(操作数不足)同口径返回空,不虚构目标 —— 真正的"写通道存在但目标不可证"
// 只有具名参数缺值那种,已在上面返回哨兵(与 `cp --target-directory` 缺值一致)。
if (operands.length === 0) return [];
- if (spec.targets === 'all' || spec.sources) return operands.flatMap(splitPowerShellPathList);
// 操作数顺序不可证(见 POWERSHELL_SWITCH_PARAMS)→ 全部当目标:只会多问,不会漏掉真目标。
- if (operandOrderUnprovable) return operands.flatMap(splitPowerShellPathList);
- if (spec.targets === 'first') return splitPowerShellPathList(operands[0]);
- // 末位是目标;只给一个操作数时 PowerShell 的 -Destination 默认当前位置(`Copy-Item payload`
- // 合法且常用)→ 目标就是 cwd,交给调用方按有效 cwd 解析(cwd 未知时那边会 fail-closed),
- // **不能**当成不可证而硬弹卡:那会把日常复制打成必问。
- return operands.length >= 2 ? splitPowerShellPathList(operands[operands.length - 1]) : ['.'];
+ if (spec.targets === 'all' || operandOrderUnprovable) return operands;
+ if (spec.targets === 'first') return spec.sources ? operands : [operands[0]];
+ // 末位是目标;只给一个操作数(含 `-Path <源>` 这种只给了源的写法)时,PowerShell 的
+ // -Destination 默认**当前位置**(`Copy-Item payload` 合法且常用)→ 目标就是 cwd,交给调用方
+ // 按有效 cwd 解析(`cd C:\Windows\System32; Copy-Item -Path payload` 由此命中系统写红线;
+ // cwd 未知时那边会 fail-closed)。**不能**当成不可证而硬弹卡:那会把日常复制打成必问。
+ if (operands.length >= 2) return spec.sources ? operands : [operands[operands.length - 1]];
+ return spec.sources ? [...operands, '.'] : ['.'];
}
/**