diff --git a/scripts/hooks/gateguard-fact-force.js b/scripts/hooks/gateguard-fact-force.js index df79f59d8..77deb03c6 100644 --- a/scripts/hooks/gateguard-fact-force.js +++ b/scripts/hooks/gateguard-fact-force.js @@ -418,6 +418,43 @@ const COMMAND_WRAPPERS = new Map([ ], ]); +// Bound `env -S` expansion so a self-referential split string cannot spin. +const MAX_WRAPPER_EXPANSIONS = 8; + +/** + * `env -S ""`, `env --split-string=`, and the attached + * `-S` form all RUN the string. Return it so the caller can expand it + * into tokens instead of consuming it as an opaque option value. + * + * @param {string} token + * @param {string | undefined} next + * @returns {{ value: string, consumed: number } | null} + */ +function envSplitString(token, next) { + if (token === '-S' || token === '--split-string') { + return typeof next === 'string' ? { value: next, consumed: 2 } : null; + } + if (token.startsWith('--split-string=')) { + return { value: token.slice('--split-string='.length), consumed: 1 }; + } + if (token.startsWith('-S') && token.length > 2) { + return { value: token.slice(2), consumed: 1 }; + } + return null; +} + +/** + * `command -v` / `-V`, including combined forms like `-pv`, only report where a + * name resolves — the operand never runs. Every other `command` option still + * executes its operand, so `-p` alone is not a lookup. + * + * @param {string} token + * @returns {boolean} + */ +function isCommandLookupOnly(token) { + return /^-[pvV]*[vV][pvV]*$/.test(token); +} + /** * Return `tokens` with any leading wrapper commands, their options, and * `VAR=value` assignment prefixes removed, so the caller sees the command that @@ -433,13 +470,18 @@ const COMMAND_WRAPPERS = new Map([ */ function stripCommandWrappers(tokens) { if (!Array.isArray(tokens) || tokens.length === 0) return tokens; + let rest = tokens; let index = 0; let valueFlags = null; - while (index < tokens.length) { - const token = tokens[index]; - const wrapperFlags = COMMAND_WRAPPERS.get(commandBasename(token)); + let wrapper = null; + let expansions = 0; + while (index < rest.length) { + const token = rest[index]; + const base = commandBasename(token); + const wrapperFlags = COMMAND_WRAPPERS.get(base); if (wrapperFlags) { valueFlags = wrapperFlags; + wrapper = base; index += 1; continue; } @@ -450,13 +492,32 @@ function stripCommandWrappers(tokens) { continue; } if (valueFlags && token.startsWith('-')) { + // `env -S "rm -rf /"` RUNS that string. Consuming it as an opaque option + // value hid the whole command behind the option, so expand it and keep + // walking. Bounded so a self-referential string cannot spin. + if (wrapper === 'env' && expansions < MAX_WRAPPER_EXPANSIONS) { + const split = envSplitString(token, rest[index + 1]); + if (split) { + rest = tokenize(split.value).concat(rest.slice(index + split.consumed)); + index = 0; + valueFlags = null; + wrapper = null; + expansions += 1; + continue; + } + } + // `command -v git reset --hard` only reports where `git` resolves — + // nothing runs, so there is no command to classify. Returning the operand + // here denied a lookup. + if (wrapper === 'command' && isCommandLookupOnly(token)) return []; if (valueFlags.has(token)) index += 1; index += 1; continue; } break; } - return index === 0 ? tokens : tokens.slice(index); + if (rest === tokens) return index === 0 ? tokens : tokens.slice(index); + return rest.slice(index); } /** diff --git a/tests/hooks/gateguard-fact-force.test.js b/tests/hooks/gateguard-fact-force.test.js index d2aa95a17..d447ba03b 100644 --- a/tests/hooks/gateguard-fact-force.test.js +++ b/tests/hooks/gateguard-fact-force.test.js @@ -292,7 +292,16 @@ function runTests() { "sudo 'rm' -rf /important/data", 'env "rm" -rf /important/data', 'doas "rm" -rf /important/data', - 'sudo bash -c "rm -rf /important/data"' + 'sudo bash -c "rm -rf /important/data"', + // `env -S ""` RUNS the string. Consuming it as an opaque option value + // let the whole command hide behind the option, in all three spellings. + 'env -S "rm -rf /important/data"', + 'env --split-string="rm -rf /important/data"', + 'env -S"rm -rf /important/data"', + 'env -S "sudo rm -rf /important/data"', + 'env -S "git reset --hard"', + // `-p` is not a lookup mode, so the operand still runs. + 'command -p rm -rf /important/data' ]) { clearState(); if ( @@ -330,7 +339,14 @@ function runTests() { 'nice -n 10 npm run build', 'ionice -c 3 npm test', 'stdbuf -o L npm test', - 'find . -exec sudo chmod 644 {} ;' + 'find . -exec sudo chmod 644 {} ;', + // `command -v`/`-V` only report where a name resolves; the operand never + // runs, so gating it denied a lookup that changes nothing. + 'command -v git reset --hard', + 'command -V git reset --hard', + 'command -pv git reset --hard', + // A benign split string must stay benign. + 'env -S "npm run build"' ]) { clearState(); if (