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): 补 iwr/irm 的 -OutFile 写通道与反引号转义路径

Chris53f9e47ffa48
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 91177e9293..56512cd257 100644
--- a/packages/maker-core/src/agents/shared/auto-review.test.ts
+++ b/packages/maker-core/src/agents/shared/auto-review.test.ts
@@ -2765,6 +2765,88 @@ describe('参数形式的系统路径写入 / setsid 选项(第三十五批评
.toBe('prompt');
});
+ it('iwr/irm 的 -OutFile 是写通道;位置 0 是 URL,不做位置推断', () => {
+ // `iwr <url> -OutFile <path>` 与 `curl -o <path> <url>` 是同一个写通道 —— 后者早就被 POSIX
+ // 分支覆盖、已必问,PowerShell 形态一直漏(codex 报)。
+ const win = ['C:\\repo'];
+ const hosts = 'C:\\Windows\\System32\\drivers\\etc\\hosts';
+ for (const c of [
+ `Invoke-WebRequest https://e.test/x -OutFile ${hosts}`,
+ `iwr https://e.test/x -OutFile ${hosts}`,
+ `Invoke-RestMethod https://e.test/x -OutFile ${hosts}`,
+ `irm https://e.test/x -OutFile ${hosts}`,
+ `iwr -Uri https://e.test/x -OutFile ${hosts}`,
+ `iwr https://e.test/x -OutFile:${hosts}`, // 贴值形态
+ `iwr https://e.test/x -Headers $h -OutFile ${hosts}`, // 带值参数在前
+ `pwsh -Command iwr https://e.test/x -OutFile ${hosts}`, // 载荷下探
+ ]) {
+ expect(classifyShellCommand(c, win, { platform: 'win32' }), c).toBe('prompt-each-time');
+ }
+
+ // 反例一:**不带 `-OutFile` 就是不落盘** —— 位置 0 是 URL,不是路径。所以这一档只认具名参数,
+ // 不像 copy 那样把缺失的目标落回 cwd:那等于凭空造出一次写入,会把每个 `iwr <url>` 都打成
+ // 写工作区。
+ for (const c of [
+ 'iwr https://e.test/x',
+ 'Invoke-WebRequest https://e.test/x',
+ 'iwr https://e.test/x -UseBasicParsing',
+ 'iwr https://e.test/x -Method POST -Body $b',
+ 'iwr https://e.test/x -UseBasicParsing -TimeoutSec 30',
+ 'iwr https://e.test/x -SkipCertificateCheck',
+ 'iwr https://e.test/x -Headers $h',
+ 'iwr https://e.test/x -OutFile C:\\repo\\a.zip', // 区内落地
+ 'iwr https://e.test/x -OutFile C:\\repo\\a.zip -Headers $h',
+ ]) {
+ expect(classifyShellCommand(c, win, { platform: 'win32' }), c).toBe('prompt');
+ }
+
+ // 反例二(**回归**):不登记 `curl` / `wget`。它们在 Windows PowerShell 里也是 iwr 的别名,
+ // 但已落在 POSIX 的 curl/wget 分支(认 `-o`/`-O`/`--output-dir` 一整套)。把它们改走这条更窄的
+ // 规则等于把既有覆盖面改小 —— 和 `tee` 同一个道理。
+ expect(classifyShellCommand(`curl -o ${hosts} https://e.test/x`, win, { platform: 'win32' }))
+ .toBe('prompt-each-time');
+ expect(classifyShellCommand(`wget -O ${hosts} https://e.test/x`, win, { platform: 'win32' }))
+ .toBe('prompt-each-time');
+ expect(classifyShellCommand('curl -o C:\\repo\\a https://e.test/x', win, { platform: 'win32' }))
+ .toBe('prompt');
+
+ // 反例三:歧义缩写 fail closed —— `-Out` 同时像 -OutFile / -OutVariable / -OutBuffer,
+ // 无法证明它不是落盘参数,所以要求同意(不靠"真 PowerShell 会报错"兜底)。
+ expect(classifyShellCommand(`iwr https://e.test/x -Out ${hosts}`, win, { platform: 'win32' }))
+ .toBe('prompt-each-time');
+ });
+
+ it('PowerShell 反引号转义的受保护路径要能命中', () => {
+ // PowerShell 里 `` ` `` 转义下一个字符,所以 ``C:\Win`dows\…\hosts`` 运行时就是 hosts;
+ // 判据要匹配字面 `Windows`,带着反引号一条都不命中(codex 报)。
+ // 修法是**多加一个候选形态**(去掉反引号),与既有那条"去 POSIX `\` 转义"变体完全同构,
+ // 所以 PowerShell 工具与 Bash 原样串两个入口自动一致。
+ const win = ['C:\\repo'];
+ for (const c of [
+ 'Set-Content C:\\Win`dows\\System32\\drivers\\etc\\hosts owned',
+ 'Set-Content -Path C:\\Win`dows\\System32\\drivers\\etc\\hosts owned',
+ 'Remove-Item C:\\Win`dows\\System32\\drivers\\etc\\hosts',
+ 'Copy-Item C:\\repo\\p C:\\Win`dows\\x',
+ 'Export-Csv -Path C:\\Win`dows\\System32\\x',
+ 'pwsh -Command Set-Content C:\\Win`dows\\System32\\x owned',
+ // 带空格的受保护根必须加引号(不加引号 PowerShell 自己也会把它拆成两个实参)。
+ 'Set-Content "C:\\P`rogram Files\\x" owned',
+ // 反引号在受保护根之后,原本就命中 —— 回归基线。
+ 'Set-Content C:\\Windows\\Sys`tem32\\drivers\\etc\\hosts owned',
+ ]) {
+ expect(classifyShellCommand(c, win, { platform: 'win32' }), c).toBe('prompt-each-time');
+ }
+
+ // 反例:反引号出现在真实文件名里只是多一个候选形态,判档不变。
+ expect(classifyShellCommand('Set-Content C:\\repo\\a`b.txt hi', win, { platform: 'win32' }))
+ .toBe('prompt');
+ expect(classifyShellCommand('Set-Content C:\\repo\\a.txt hi', win, { platform: 'win32' }))
+ .toBe('prompt');
+ // 动态目标仍走不可证那条(反引号解码不影响它)。
+ expect(classifyShellCommand('Set-Content $t owned', win, { platform: 'win32' }))
+ .toBe('prompt-each-time');
+ });
+
it('iex 是把 stdin 当程序的执行器:`| iex` 落在外层 shell 也命中下载即执行', () => {
// `pwsh -Command 'iwr https://…/a.ps1' | iex`:`| iex` 在**外层**,顶层分段把它切成独立一段。
// 于是两段各自都不红 —— payload 那段只有 `iwr`(单纯下载不是红线),`iex` 那段 tokens[0] 不是
diff --git a/packages/maker-core/src/agents/shared/auto-review.ts b/packages/maker-core/src/agents/shared/auto-review.ts
index 2bb09a4efc..a7b18caa28 100644
--- a/packages/maker-core/src/agents/shared/auto-review.ts
+++ b/packages/maker-core/src/agents/shared/auto-review.ts
@@ -321,7 +321,7 @@ function shortClusterOption(
*/
const POWERSHELL_WRITE_CMDLETS: ReadonlyMap<
string,
- { targets: 'first' | 'last' | 'all'; sources?: boolean; pathIsSource?: boolean }
+ { targets: 'first' | 'last' | 'all' | 'named'; sources?: boolean; pathIsSource?: boolean }
> =
new Map([
// 写内容到 -Path(位置 0),值是第二个操作数。
@@ -355,6 +355,18 @@ const POWERSHELL_WRITE_CMDLETS: ReadonlyMap<
['expand-archive', { targets: 'last', pathIsSource: true }],
['export-certificate', { targets: 'last', pathIsSource: true }],
['export-pfxcertificate', { targets: 'last', pathIsSource: true }],
+ // 下载落盘:`iwr <url> -OutFile <path>` 与 `curl -o <path> <url>` 是同一个写通道
+ // (后者早就被 POSIX 分支覆盖、已必问,PowerShell 形态一直漏,codex 报)。
+ // **`targets: 'named'`**:位置 0 是 `-Uri`,不是路径 —— 既不能当写目标,也不能像 copy 那样
+ // 落回 cwd(不带 `-OutFile` 的 `iwr <url>` 只返回对象、根本不落盘,给它编一个 cwd 目标就是
+ // 凭空造出一次写入)。所以这一档只认具名 `-OutFile`,不做任何位置推断。
+ // **不登记 `curl` / `wget`**:它们在 Windows PowerShell 里也是 Invoke-WebRequest 的别名,但
+ // 已经落在 POSIX 的 curl/wget 分支(那条认 `-o`/`-O`/`--output-dir` 等更完整的一套)。
+ // 加进这张表会把它们改走这条更窄的规则 —— 那是把既有覆盖面改小,和 `tee` 同一个道理。
+ ['invoke-webrequest', { targets: 'named' }],
+ ['iwr', { targets: 'named' }],
+ ['invoke-restmethod', { targets: 'named' }],
+ ['irm', { targets: 'named' }],
['clear-content', { targets: 'first' }],
['set-itemproperty', { targets: 'first' }],
['set-item', { targets: 'first' }],
@@ -425,6 +437,8 @@ const POWERSHELL_TARGET_PARAMS: readonly string[] = [
'-path', '-literalpath', '-lp', '-pspath', '-destination', '-filepath', '-newname',
// 归档 / 转录 / 帮助下载各自的落地位置参数(Compress-Archive、Start-Transcript、Save-Help…)。
'-destinationpath', '-outputdirectory',
+ // 下载落盘位置(Invoke-WebRequest / Invoke-RestMethod)。
+ '-outfile',
];
/**
@@ -504,6 +518,14 @@ const POWERSHELL_VALUE_PARAMS: readonly string[] = [
// `-Encoding` 与 `-AclObject`,所以登记新 cmdlet 时一并登记它的带值参数已是固定动作)。
'-inputobject', '-cert', '-certificate', '-module', '-compressionlevel', '-password',
'-usequotes', '-quotefields', '-usiculture', '-fullyqualifiedmodule',
+ // Invoke-WebRequest / Invoke-RestMethod 的带值参数。这一组必须齐,否则未知参数会触发下面
+ // 「操作数顺序不可证」的 fail closed,把 `iwr <url> -Headers $h` 这种日常调用打成硬弹窗。
+ '-uri', '-method', '-headers', '-body', '-contenttype', '-useragent', '-timeoutsec',
+ '-maximumredirection', '-maximumretrycount', '-retryintervalsec', '-proxy',
+ '-proxycredential', '-sessionvariable', '-websession', '-form', '-infile',
+ '-transferencoding', '-authentication', '-token', '-certificatethumbprint',
+ '-statuscodevariable', '-responseheadersvariable', '-connectiontimeoutseconds',
+ '-operationtimeoutseconds', '-httpversion',
];
/**
@@ -523,6 +545,10 @@ const POWERSHELL_SWITCH_PARAMS: readonly string[] = [
'-force', '-recurse', '-passthru', '-nonewline', '-noclobber', '-append', '-container',
'-usetransaction', '-asbytestream', '-raw', '-wait', '-followsymlink', '-nooverwrite',
'-notypeinformation', '-nonewwindow',
+ // Invoke-WebRequest / Invoke-RestMethod 的开关(同上:列出来才能把「未知参数」缩小到真的未知)。
+ '-usebasicparsing', '-usedefaultcredentials', '-skipcertificatecheck', '-skiphttperrorcheck',
+ '-skipheadervalidation', '-allowunencryptedauthentication', '-noproxy', '-resume',
+ '-preserveauthorizationonredirect', '-disablekeepalive', '-allowinsecureredirect',
];
/**
@@ -702,6 +728,11 @@ function powerShellWriteTargetOperands(bin: string, args: string[]): string[] |
? [...named, ...operands.flatMap(expand)]
: named;
}
+ // 只认具名目标的 cmdlet(下载类:位置 0 是 URL,不是路径)—— 没给具名落地参数就是不落盘,
+ // 不能像 copy 那样落回 cwd,那等于凭空造出一次写入。
+ // 但**有歧义/未知参数吃了值**时不能就此判"没写目标":那个参数可能正是 `-OutFile`
+ // (`iwr <url> -Out <系统路径>` 里 `-Out` 同时像 -OutFile/-OutVariable/-OutBuffer)→ fail closed。
+ if (spec.targets === 'named') return operandOrderUnprovable ? [UNPROVABLE_WRITE_TARGET] : [];
// 一个操作数都没有 = 命令本身不完整(`Set-Content` 单独一条会报错)。与 coreutils 的
// `cp payload`(操作数不足)同口径返回空,不虚构目标 —— 真正的"写通道存在但目标不可证"
// 只有具名参数缺值那种,已在上面返回哨兵(与 `cp --target-directory` 缺值一致)。
@@ -3155,8 +3186,13 @@ function systemWriteTargetsInSegment(
if (isProtectedProviderPath(resolved) || isProtectedSystemPath(resolved)) return true;
return !isInsideWorkspace(resolved, workspaceRoots, aliasFirmlinks);
}
- // 每个目标查两种形态:原样(保留 Windows `\` 分隔符)与去 POSIX `\` 转义(`/e\tc`→`/etc`)。
- return [t, t.replace(/\\(.)/g, '$1')].some((v) => {
+ // 每个目标查三种形态:原样(保留 Windows `\` 分隔符)、去 POSIX `\` 转义(`/e\tc`→`/etc`)、
+ // 去 PowerShell 反引号转义。后者是 codex 报的绕过:PowerShell 里 `` ` `` 转义下一个字符,
+ // 所以 ``C:\Win`dows\System32\drivers\etc\hosts`` 运行时就是 hosts,但判据要匹配字面
+ // `Windows`,带着反引号一条都不命中。**只多加一个候选形态,不改原判据** —— 与既有那条
+ // POSIX 去转义变体完全同构,所以两个入口(PowerShell 工具与 Bash 原样串)自动一致。
+ // 反引号出现在真实文件名里(``C:\repo\a`b.txt``)只会多出一个候选,判档不变(已断言)。
+ return [t, t.replace(/\\(.)/g, '$1'), t.replace(/`/g, '')].some((v) => {
const forward = toForwardSlashes(v);
// cwd 未知 + 相对目标 → 无法证明它没落进系统目录,fail-closed。
if (opts.cwdUnknown && !isAbsolutePath(forward)) return true;