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): 路径枚举器有上游时不兜底成当前目录;Select 展开属性算换来源

Chrisff132a621569
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 b19b4cb76b..0ee5617672 100644
--- a/packages/maker-core/src/agents/shared/auto-review.test.ts
+++ b/packages/maker-core/src/agents/shared/auto-review.test.ts
@@ -3040,6 +3040,77 @@ describe('参数形式的系统路径写入 / setsid 选项(第三十五批评
}
});
+ it('路径枚举器无实参时,有上游就不是"枚举当前目录"', () => {
+ // `Get-ChildItem` 一族的 `-Path` 都接受 pipeline 输入,所以"没给实参"在**有上游**时是"项由上游
+ // 喂进来",不是"枚举当前目录"。早先一律兜底成 `.` → `'<受保护路径>' | Resolve-Path | Remove-Item`
+ // 的上游被换成当前目录、判成区内 → 整条降级(codex 报)。
+ const win = ['C:\\repo'];
+ const hosts = 'C:\\Windows\\System32\\drivers\\etc\\hosts';
+ for (const c of [
+ `'${hosts}' | Resolve-Path | Remove-Item`,
+ `'${hosts}' | Get-Item | Remove-Item`, // 同族别名一并覆盖
+ `'${hosts}' | gi | Remove-Item`,
+ `'${hosts}' | rvpa | Remove-Item`,
+ // 上游本来就不可证时,枚举器不得把它"洗"成当前目录。
+ 'Get-Content C:\\repo\\targets.txt | Resolve-Path | Remove-Item',
+ '$env:TEMP | Resolve-Path | Remove-Item',
+ // 受保护位置经枚举器透传仍必问。
+ 'Get-ChildItem C:\\Windows\\System32\\* | Resolve-Path | Remove-Item',
+ ]) {
+ expect(classifyShellCommand(c, win, { platform: 'win32' }), c).toBe('prompt-each-time');
+ }
+
+ // 反例:**首段**没实参才是"枚举当前目录",这条不能被收紧。
+ for (const c of [
+ 'Get-ChildItem | Remove-Item',
+ 'Resolve-Path | Remove-Item',
+ 'cd C:\\repo; Get-ChildItem | Remove-Item', // 分号后仍是首段(不是 pipeline 下游)
+ // 上游可证在区内 → 枚举器原样传递,日常清理仍是灰区。
+ 'Get-ChildItem C:\\repo\\build\\* | Get-Item | Remove-Item',
+ 'Get-ChildItem C:\\repo\\build | Resolve-Path | Remove-Item',
+ // 枚举器自己显式给了区内位置 → 用它,不看上游。
+ `'${hosts}' | Get-ChildItem C:\\repo\\build | Remove-Item`,
+ ]) {
+ expect(classifyShellCommand(c, win, { platform: 'win32' }), c).toBe('prompt');
+ }
+ });
+
+ it('Select-Object -ExpandProperty 取的是属性值,来源变了', () => {
+ // `Select-Object` 只有**透传对象**的形态算"只挑选、没换来源";`-ExpandProperty` 输出属性值,
+ // 来源随之改变:`Get-Item Env:ComSpec | Select-Object -ExpandProperty Value | Remove-Item`
+ // 喂给删除段的是系统 cmd.exe 路径,而 provenance 还留着看着安全的 `Env:ComSpec`(codex 报)。
+ const win = ['C:\\repo'];
+ for (const c of [
+ 'Get-Item Env:ComSpec | Select-Object -ExpandProperty Value | Remove-Item',
+ 'Get-ChildItem C:\\repo | Select-Object -ExpandProperty Target | Remove-Item',
+ 'Get-ChildItem C:\\repo | Select-Object -Exp Target | Remove-Item', // 前缀缩写
+ 'Get-ChildItem C:\\repo | Select-Object -ExpandProperty:Target | Remove-Item', // 贴值
+ 'Get-ChildItem C:\\repo | select -ExpandProperty Target | Remove-Item', // 别名
+ // `-e` / `-ex` 真机上与 -ExcludeProperty 歧义会报错,这里按改变来源处理 = fail closed。
+ 'Get-ChildItem C:\\repo | Select-Object -ex Target | Remove-Item',
+ ]) {
+ expect(classifyShellCommand(c, win, { platform: 'win32' }), c).toBe('prompt-each-time');
+ }
+
+ // 反例:透传形态照旧传递 provenance —— 这次收窄不许把 Select-Object 整个踢出透传表。
+ for (const c of [
+ 'Get-ChildItem C:\\repo\\build | Select-Object -First 1 | Remove-Item',
+ 'Get-ChildItem C:\\repo\\build | Select-Object -Last 2 | Remove-Item',
+ 'Get-ChildItem C:\\repo\\build | Select-Object -Skip 1 | Remove-Item',
+ 'Get-ChildItem C:\\repo\\build | Select-Object Name | Remove-Item',
+ 'Get-ChildItem C:\\repo\\build | Select-Object -ExcludeProperty Name | Remove-Item',
+ 'Get-ChildItem C:\\repo\\build | Select-Object -Unique | Remove-Item',
+ ]) {
+ expect(classifyShellCommand(c, win, { platform: 'win32' }), c).toBe('prompt');
+ }
+ // 受保护位置经透传形态仍必问(收窄没有削弱原有覆盖)。
+ expect(classifyShellCommand(
+ 'Get-ChildItem C:\\Windows\\System32 | Select-Object -First 1 | Remove-Item',
+ win,
+ { platform: 'win32' },
+ )).toBe('prompt-each-time');
+ });
+
it('curl/wget 在 PowerShell 里是 iwr 别名:-OutFile 与 POSIX -o/-O 取并集', () => {
// Windows PowerShell 里 `curl` / `wget` 是 Invoke-WebRequest 的别名,落地参数写成 `-OutFile`,
// 而这两个 bin 走的是 POSIX 分支(只认 `-o`/`-O`/`--output`)→ 受保护落地漏掉(codex 报)。
diff --git a/packages/maker-core/src/agents/shared/auto-review.ts b/packages/maker-core/src/agents/shared/auto-review.ts
index 4edb7cb8a2..e7942f3d99 100644
--- a/packages/maker-core/src/agents/shared/auto-review.ts
+++ b/packages/maker-core/src/agents/shared/auto-review.ts
@@ -3285,6 +3285,8 @@ const GLOB_COMPONENT_PLACEHOLDER = '\u0000globpart';
* 是 `Where-Object` 的实参(`Name`、`hosts`),那两个按相对路径落在工作区内 → 整条降级(codex 报)。
*
* `ForEach-Object` / `%` **不在此列**:它能返回任意对象,来源无法证明 → 落到"不可证"那档。
+ * `Select-Object` 在此列,但只限**透传**形态 —— `-ExpandProperty` 由
+ * {@link selectObjectChangesProvenance} 单独摘出去。
*/
const POWERSHELL_PIPELINE_PASSTHROUGH: ReadonlySet<string> = new Set([
'where-object', 'where', '?', 'sort-object', 'sort', 'select-object', 'select',
@@ -3292,13 +3294,37 @@ const POWERSHELL_PIPELINE_PASSTHROUGH: ReadonlySet<string> = new Set([
]);
/**
- * 会**枚举路径**的 cmdlet:不给实参时枚举当前目录,所以 provenance 落到 `.`(与本文件既有的 cwd
- * 兜底同口径)。表外的阶段不给实参时 provenance 记为"不可证",不假设它产出的是区内路径。
+ * 会**枚举路径**的 cmdlet:**首段**不给实参时枚举当前目录,所以 provenance 落到 `.`(与本文件既有
+ * 的 cwd 兜底同口径)。表外的阶段不给实参时 provenance 记为"不可证",不假设它产出的是区内路径。
+ *
+ * 注意:这一族的 `-Path` 都**接受 pipeline 输入**,所以「不给实参」在**有上游**时不等于"枚举当前
+ * 目录",而是"项由上游喂进来" —— 见段循环里的 `fromPipe` 分支。
*/
const POWERSHELL_PATH_ENUMERATORS: ReadonlySet<string> = new Set([
'get-childitem', 'gci', 'dir', 'ls', 'get-item', 'gi', 'resolve-path', 'rvpa',
]);
+/** `Select-Object` 及其别名 —— 只有透传对象的形态才保留 provenance,见下。 */
+const POWERSHELL_SELECT_CMDLETS: ReadonlySet<string> = new Set(['select-object', 'select']);
+
+/**
+ * `Select-Object` 用 `-ExpandProperty <name>` 时输出的是**那个属性的值**、不再是原对象,来源随之
+ * 改变,不能算"只挑选、没换来源":
+ * `Get-Item Env:ComSpec | Select-Object -ExpandProperty Value | Remove-Item` 喂给删除段的是系统
+ * `cmd.exe` 的路径,而 provenance 还留着看着安全的 `Env:ComSpec`(codex 报)。
+ *
+ * 按**前缀**匹配识别(PowerShell 允许 `-Exp` 这类缩写)。`-e` / `-ex` 在真机上会因与
+ * `-ExcludeProperty` 歧义而报错,这里也一并按"改变来源"处理 = fail closed。
+ */
+function selectObjectChangesProvenance(bin: string, tokens: string[]): boolean {
+ if (!POWERSHELL_SELECT_CMDLETS.has(bin)) return false;
+ return tokens.slice(1).some((token) => {
+ if (!token.startsWith('-')) return false;
+ const name = token.slice(1).split(/[:=]/)[0].toLowerCase();
+ return name.length > 0 && 'expandproperty'.startsWith(name);
+ });
+}
+
function hasExplicitPathArgument(tokens: string[]): boolean {
const known = [
...POWERSHELL_TARGET_PARAMS, ...POWERSHELL_VALUE_PARAMS, ...POWERSHELL_SWITCH_PARAMS,
@@ -3510,7 +3536,12 @@ function scopedDestructionNeedsConsent(
// 留给下一段用:pipeline 到这里为止,「被传下去的对象来自哪些位置」。必须在下面那个
// `continue` 之前赋值,否则改目录的段会把它漏掉。
// · 只过滤/排序/挑选的阶段 → provenance 原样传递(它没换来源);
- // · **路径枚举器**(`Get-ChildItem` 一族)→ 它的位置实参就是产出的位置,没给实参则枚举当前目录;
+ // 唯一例外是 `Select-Object -ExpandProperty` —— 那是取属性值,来源变了,落到不可证。
+ // · **路径枚举器**(`Get-ChildItem` 一族)→ 它的位置实参就是产出的位置;没给实参时分两种:
+ // 首段 = 枚举当前目录 → `.`;**有上游**(`… | Resolve-Path | …`)= 项由 pipeline 喂进来 →
+ // provenance 原样保留(可能是 `null`)。这一族的 `-Path` 都接受 pipeline 输入,所以无实参时
+ // 一律兜底成 `.` 会把上游那个路径换成"当前目录",于是
+ // `'<受保护路径>' | Resolve-Path | Remove-Item` 被判成区内 → 整条降级(codex 报)。
// · 其余一律 `null` = 不可证,由下游 fail closed。
//
// 第三条早先写成「给了位置实参就拿它当 provenance」,那个泛化是**错的** ——
@@ -3519,12 +3550,14 @@ function scopedDestructionNeedsConsent(
// 断言"上游是 C:\repo\targets.txt,在区内,所以安全"(codex 报)。输出一个错的"安全"比没有这条
// 规则更糟,所以收窄成只对路径枚举器成立;`Get-Content`/`Import-Csv`/`Select-String`/
// `ForEach-Object` 这些一概落到不可证。
- if (!POWERSHELL_PIPELINE_PASSTHROUGH.has(bin)) {
+ if (!POWERSHELL_PIPELINE_PASSTHROUGH.has(bin) || selectObjectChangesProvenance(bin, tokens)) {
if (!POWERSHELL_PATH_ENUMERATORS.has(bin)) {
upstreamOperands = null;
} else {
const operands = positionalOperands(tokens.slice(1));
- upstreamOperands = operands.length > 0 ? operands : ['.'];
+ if (operands.length > 0) upstreamOperands = operands;
+ else if (!fromPipe) upstreamOperands = ['.'];
+ // fromPipe 且没给实参 → 项由上游喂进来,provenance 原样保留(含 `null` = 不可证)。
}
}