fix(gateguard): run env's split string through classification, not past it

Review feedback on #2832 — CodeRabbit and Greptile independently flagged the
same bypass, and it reproduces.

`env` declares `-S` / `--split-string` as value-consuming, so
`stripCommandWrappers` skipped the option AND its value. But that value is the
command: `env -S "rm -rf /important/data"` runs the deletion, and the hook saw
no `rm` token at all. Measured against the built hook, all three spellings were
allowed while the bare `rm -rf` was denied:

    deny    rm -rf /important/data
    (none)  env -S "rm -rf /important/data"
    (none)  env --split-string="rm -rf /important/data"
    (none)  env -S"rm -rf /important/data"

Expand the split string into tokens and keep walking, so a wrapper nested inside
it (`env -S "sudo rm -rf /"`) is stripped too. Bounded by
MAX_WRAPPER_EXPANSIONS so a self-referential string cannot spin.

The second half is the mirror image — a false positive.
`command -v git reset --hard` only reports where `git` resolves; nothing runs.
The wrapper walk dropped `-v` and classified the operand, so a lookup was
denied. `-v`, `-V`, and combined forms like `-pv` now yield no executable
tokens, while `-p` alone still does, because it executes.

`env -S "npm run build"` stays allowed and `command -p rm -rf /` still denies.

192 passed, 0 failed. Mutation-tested: dropping the expansion fails 5, dropping
the lookup guard fails 3.
This commit is contained in:
Nguyen Thanh Dat committed 2026-08-25 18:45:10 +07:00
1 parent 1e2aa0705a
commit 89c1bf4fff
2 files changed
+83 -6

No files matched your search

+65 -4
View File
@@ -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 "<command>"`, `env --split-string=<command>`, and the attached
* `-S<command>` 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);
}
/**
+18 -2
View File
@@ -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 "<cmd>"` 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 (