diff --git a/scripts/hooks/gateguard-fact-force.js b/scripts/hooks/gateguard-fact-force.js index 8eaf5cccd..f68ca5bbb 100644 --- a/scripts/hooks/gateguard-fact-force.js +++ b/scripts/hooks/gateguard-fact-force.js @@ -333,6 +333,48 @@ function quoteAwareSegments(input) { const SHELL_WRAPPERS = new Set(['sh', 'bash', 'zsh', 'dash', 'ksh']); +// Short shell options that take a value. Reaching one inside a cluster means +// the rest of the token is that value, so a `c` behind it is not `-c`. +const SHELL_SHORT_OPTIONS_WITH_VALUE = new Set(['o', 'O']); + +/** + * Return the command string a shell would run for `-c`, or null. + * + * `-c` reads its payload from the FOLLOWING argument (`sh -c'cmd'` is an + * invalid option, not an attached value), and short options cluster — so + * `sh -ec ''`, `sh -xc ''`, and `sh -ce ''` all run exactly + * what `sh -c ''` runs. Matching only a bare `-c` token left every + * clustered spelling unclassified. + * + * `-o`/`-O` take a value, so `c` behind one of them is that value rather + * than a flag; the shell rejects those forms instead of running the payload + * (`sh -oc 'cmd'` → "invalid option name"), so stopping there loses nothing. + * + * @param {string[]} tokens command words, wrappers already stripped + * @returns {string|null} + */ +function shellDashCPayload(tokens) { + if (tokens.length === 0) return null; + if (!SHELL_WRAPPERS.has(commandBasename(tokens[0]))) return null; + + for (let i = 1; i < tokens.length; i++) { + const token = tokens[i]; + // `--` ends the options, and a bare operand is the script name — after + // either one there is no `-c` left to find. + if (token === '--' || token === '-' || !token.startsWith('-')) return null; + if (token.startsWith('--')) continue; + + for (const flag of token.slice(1)) { + if (flag === 'c') { + const payload = tokens[i + 1]; + return typeof payload === 'string' ? payload : null; + } + if (SHELL_SHORT_OPTIONS_WITH_VALUE.has(flag)) return null; + } + } + return null; +} + /** * Quote-aware destructive check: catches quoted command words, newline * separators, quoted `find -exec`, and `sh -c`/`bash -c` wrappers that evade @@ -352,14 +394,9 @@ function isDestructiveQuoteAware(raw, depth = 0) { if (tokens.length === 0) continue; if (isDestructiveRm(tokens)) return true; if (isDestructiveGit(tokens)) return true; - if (isDestructiveFindExec(tokens.join(' '))) return true; - const base = commandBasename(tokens[0]); - if (SHELL_WRAPPERS.has(base)) { - const ci = tokens.indexOf('-c'); - if (ci !== -1 && tokens[ci + 1] && isDestructiveQuoteAware(tokens[ci + 1], depth + 1)) { - return true; - } - } + if (isDestructiveFindExecTokens(tokens, depth)) return true; + const payload = shellDashCPayload(tokens); + if (payload && isDestructiveQuoteAware(payload, depth + 1)) return true; } return false; } @@ -767,17 +804,33 @@ function collectExecutableBodies(raw) { * `-exec unlink {} \;`, `-exec git reset --hard {} \;`. * * @param {string} command + * @param {number} [depth] recursion guard shared with isDestructiveQuoteAware * @returns {boolean} */ -function isDestructiveFindExec(command) { +function isDestructiveFindExec(command, depth = 0) { const raw = String(command || ''); const trimmed = raw.trim(); if (!trimmed) { return false; } - // Tokenize the whole command line - const tokens = stripCommandWrappers(tokenize(trimmed)); + return isDestructiveFindExecTokens(tokenize(trimmed), depth); +} + +/** + * Token-taking half of `isDestructiveFindExec`. + * + * The quote-aware pass already holds correctly split tokens. Joining them + * back into one string so this check could re-tokenize it flattened a quoted + * `-exec sh -c ''` payload into separate words, which is precisely the + * command that needed classifying. + * + * @param {string[]} rawTokens + * @param {number} [depth] recursion guard shared with isDestructiveQuoteAware + * @returns {boolean} + */ +function isDestructiveFindExecTokens(rawTokens, depth = 0) { + const tokens = stripCommandWrappers(rawTokens); if (!tokens || tokens.length === 0) { return false; } @@ -814,6 +867,15 @@ function isDestructiveFindExec(command) { return false; } + // `-exec sh -c '' \;` runs just as a bare `sh -c ''` does. + // Classifying only the `sh` executable stopped at the wrapper, so the + // deletion inside it was never seen — the quote-aware pass already looks + // through the same wrapper, and this is the sibling that did not. + const execPayload = shellDashCPayload(execCommand); + if (execPayload) { + return isDestructiveQuoteAware(execPayload, depth + 1); + } + const baseCmd = commandBasename(execCommand[0]); // Directly destructive commands inside -exec diff --git a/tests/hooks/gateguard-fact-force.test.js b/tests/hooks/gateguard-fact-force.test.js index 18de33680..5a423c002 100644 --- a/tests/hooks/gateguard-fact-force.test.js +++ b/tests/hooks/gateguard-fact-force.test.js @@ -315,7 +315,26 @@ function runTests() { 'env -uSHELL rm -rf /important/data', 'env -uS rm -rf /important/data', // `-p` is not a lookup mode, so the operand still runs. - 'command -p rm -rf /important/data' + 'command -p rm -rf /important/data', + // `-exec sh -c ''` runs . Classifying only the `sh` executable + // stopped at the wrapper, so every deletion behind one was allowed while + // the bare `-exec rm` spelling was denied. + 'find . -exec sh -c "rm -rf {}" ;', + "find . -exec sh -c 'rm -rf {}' ;", + 'find . -exec bash -c "rm -rf /important/data" ;', + 'find . -exec zsh -c "rm -rf /important/data" ;', + 'find /tmp -type f -exec sh -c "rm -rf {}" ;', + // `+` terminates -exec too, and the payload keeps its own wrappers. + 'find . -exec sh -c "rm -rf {}" +', + 'find . -exec sh -c "git reset --hard" ;', + 'find . -exec sh -c "sudo rm -rf /important/data" ;', + // Short options cluster, so `-c` is reached through `-e`/`-x` as well — + // attached or not, and whether or not `c` comes first. + 'sh -ec "rm -rf /important/data"', + 'sh -xc "rm -rf /important/data"', + 'sh -ce "rm -rf /important/data"', + 'bash -ec "rm -rf /important/data"', + 'find . -exec sh -ec "rm -rf {}" ;' ]) { clearState(); if ( @@ -364,7 +383,19 @@ function runTests() { 'env -vS "npm test"', // `-uS` is `-u` unsetting a variable named S, not a split string. 'env -uS FOO', - 'env -C /tmp npm run build' + 'env -C /tmp npm run build', + // Looking inside `-c` must classify the payload, not assume the worst of + // every shell wrapper. + 'find . -exec sh -c "echo hello" ;', + 'sh -c "npm test"', + 'sh -ec "npm run build"', + 'bash --norc -c "npm test"', + // These two carry a destructive payload on purpose: with a benign one the + // assertion passes either way and pins nothing. `-o` takes a value, so `c` + // behind it is that value ("invalid option name"), and after `--` the next + // word is the script name — neither shell runs the string as a command. + 'sh -oc "rm -rf /important/data"', + 'sh -- -c "rm -rf /important/data"' ]) { clearState(); if (