fix(gateguard): classify the command a find -exec shell wrapper runs

`find . -exec sh -c 'rm -rf {}' \;` was allowed while both `find . -exec rm
{} \;` and a bare `sh -c 'rm -rf ...'` were denied. `isDestructiveFindExec`
classified only the executable named after `-exec`, so a shell wrapper ended
the inspection exactly where the destructive command began. The quote-aware
pass already looks through `sh -c`; this was the sibling that did not.

Two things had to change for that recursion to see the payload:

- `isDestructiveQuoteAware` handed `isDestructiveFindExec` a re-joined
  string, which flattened the quoted payload back into separate words. It
  now passes the tokens it already holds, through a token-taking half of the
  same check.

- `-c` was matched only as a bare token. Short options cluster, so
  `sh -ec '<cmd>'`, `sh -xc '<cmd>'`, and `sh -ce '<cmd>'` run the same
  string `sh -c '<cmd>'` runs — verified against the shell, as was the fact
  that the payload always comes from the next argument (`sh -c'cmd'` is an
  invalid option, not an attached value). That gap was reachable without
  `find` at all: `sh -ec 'rm -rf /important/data'` was allowed.

`-o`/`-O` take a value, so a `c` behind one of them is that value, and `--`
ends the options. The shell rejects both spellings instead of running the
string, so the scan stops there rather than reporting a deletion that cannot
happen.

Tests cover each spelling in both directions. The two allow-list cases carry
a destructive payload deliberately: with a benign one they pass whether or
not the arity and `--` stops are present, and pin nothing.
This commit is contained in:
Nguyen Thanh Dat committed 2026-08-25 20:22:44 +07:00
1 parent f48abf7f22
commit d29d8a3503
2 files changed
+106 -13

No files matched your search

+73 -11
View File
@@ -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 '<cmd>'`, `sh -xc '<cmd>'`, and `sh -ce '<cmd>'` all run exactly
* what `sh -c '<cmd>'` 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 '<cmd>'` 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 '<cmd>' \;` runs <cmd> just as a bare `sh -c '<cmd>'` 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
+33 -2
View File
@@ -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 '<cmd>'` runs <cmd>. 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 (