Skip to content

armgit / ECC

Public Imported from GitHub
0

The agent harness performance optimization system. Skills, instincts, memory, security, and research-first development for Claude Code, Codex, Opencode, Cursor and beyond.

hooks: parse sed options through the whole argv — later -e scripts still execute

chenhz0134a7f41539e3
diff --git a/scripts/hooks/block-no-verify.js b/scripts/hooks/block-no-verify.js
index 2464223e5..0a37e212e 100644
--- a/scripts/hooks/block-no-verify.js
+++ b/scripts/hooks/block-no-verify.js
@@ -614,15 +614,20 @@ function sedExecParts(script, budget) {
}
// Model sed's argv: which operands are scripts, which are file names (data).
+// GNU sed collects -e/--expression scripts from ANY argv position — a later -e
+// after positional operands still executes — so options are parsed through the
+// whole argv. When any script option is given, every positional is a file;
+// otherwise the first positional is the script (verified against GNU sed 4.9).
// Returns the script strings, or null when the invocation shape is unmodeled —
// the caller then keeps today's opaque default instead of guessing.
function sedScriptParts(words, budget) {
const scripts = [];
+ const positionals = [];
let i = 1;
while (i < words.length) {
const raw = words[i].value;
budget.spend(raw.length + 1);
- if (raw === '--') { i++; break; }
+ if (raw === '--') break; // remaining operands are files (data)
if (raw === '--expression') {
if (!words[i + 1]) return null;
scripts.push(words[i + 1].value); i += 2; continue;
@@ -640,15 +645,16 @@ function sedScriptParts(words, budget) {
if (raw === '-f') return null; // script comes from a file we cannot read
if (/^-l[0-9]+$/.test(raw)) { i++; continue; }
if (/^-[nsEruzb]+$/.test(raw)) { i++; continue; }
- return null; // -i suffix ambiguity and unknown short options
+ return null; // -i suffix ambiguity, unknown/attached short options
}
- break; // first positional operand
+ positionals.push(raw); i++; // operands: first becomes the script only when no script option was given
}
if (!scripts.length) {
- if (i >= words.length) return null; // no script operand at all
- scripts.push(words[i].value);
+ if (!positionals.length) return null; // no script operand at all
+ scripts.push(positionals[0]);
}
- // Remaining operands are file names: data, deliberately left uninspected.
+ // With any -e/--expression present, every positional is a file: data,
+ // deliberately left uninspected.
return scripts;
}
diff --git a/tests/hooks/block-no-verify.test.js b/tests/hooks/block-no-verify.test.js
index 4c714f20b..29740d1bc 100644
--- a/tests/hooks/block-no-verify.test.js
+++ b/tests/hooks/block-no-verify.test.js
@@ -2165,7 +2165,13 @@ const sedCases = [
// Litmus anchors from the #3051 direction check (existing behavior pinned).
['quoted-as-data through echo stays allowed', 'echo "git commit --no-verify"', 0],
['direct --no-verify stays blocked', 'git commit --no-verify -m x', 2],
- ['mixed quoting smuggling a real flag stays blocked', "git commit '--no-verify' -m x", 2]
+ ['mixed quoting smuggling a real flag stays blocked', "git commit '--no-verify' -m x", 2],
+ // GNU sed collects -e scripts from any argv position (CodeRabbit finding on
+ // this PR): an execution construct after positional operands still executes.
+ ['later -e script after positional operands is scanned', "printf x | sed -e 's/x/y/' - -e 's/y/git commit --no-verify/e'", 2],
+ ['later -e after a positional script operand is scanned', "sed 's/x/y/' -e 'e git commit --no-verify'", 2],
+ ['later --expression= after positionals is scanned', "sed 's/x/y/' --expression='s/x/git commit --no-verify/e'", 2],
+ ['-- ends options: later tokens are file operands', "sed -e 's/x/y/' -- 'git commit --no-verify'", 0]
];
for (const [name, command, expected] of sedCases) {
if (test(`sed model ${name}`, () => {