Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(safety): read the command after env's lone -

DheerajPiduru529729f2734b38
diff --git a/server/safety-floor.test.ts b/server/safety-floor.test.ts
index d037a3f012..e7e7fde94d 100644
--- a/server/safety-floor.test.ts
+++ b/server/safety-floor.test.ts
@@ -109,6 +109,9 @@ const BLOCKED: Case[] = [
["FORCE=1 rm -rf /", "delete-root"],
['A=1 B="two words" rm -rf /usr', "delete-system-directory"],
["env FOO=bar rm -rf /", "delete-root"],
+ ["env - rm -rf /", "delete-root"],
+ ["env -i - PATH=/bin rm -rf /etc", "delete-system-directory"],
+ ["sudo env - rm -rf ~", "delete-home"],
// `env -S LINE` / `--split-string=LINE`: env runs what it splits LINE into
["env -S 'rm -rf /'", "delete-root"],
['env -S"rm -rf /"', "delete-root"],
@@ -375,6 +378,7 @@ const ALLOWED: Array<[command: string, on?: Platform | "posix"]> = [
["echo rm -rf /"],
["env -S 'echo rm -rf /'"],
["env -S 'node --no-warnings' script.js"],
+ ["env - PATH=/bin ls -la /"],
// env hands rm a directory named "~": nothing expands it without a shell
["env -S 'rm -rf ~'"],
["printf '%s\\n' 'rm -rf /' > notes.txt"],
diff --git a/server/safety-floor.ts b/server/safety-floor.ts
index 746e491cb7..3626f06e9e 100644
--- a/server/safety-floor.ts
+++ b/server/safety-floor.ts
@@ -338,12 +338,13 @@ function lex(source: string, windows: boolean): Reading {
}
/** Commands that run the command after them. `values`/`named`: options taking a value; `asks`: options that only
- * report on the command; `split`: the option whose value is more arguments (`env -S`). */
-const PREFIXES: Record<string, { values?: string; named?: string[]; operands?: number; asks?: string; asksNamed?: string[]; split?: { letter: string; named: string } }> = {
+ * report on the command; `split`: the option whose value is more arguments (`env -S`); `loneDash`: a `-` after the
+ * options is one more option (env's `-` is `-i`). */
+const PREFIXES: Record<string, { values?: string; named?: string[]; operands?: number; asks?: string; asksNamed?: string[]; split?: { letter: string; named: string }; loneDash?: boolean }> = {
// `sudo -l rm` lists whether rm may be run; it runs nothing.
sudo: { values: "CDghpRrtTUu", named: ["user", "group", "host", "prompt", "chdir", "chroot", "role", "type", "other-user", "close-from", "command-timeout"], asks: "l", asksNamed: ["list"] },
doas: { values: "Cu" },
- env: { values: "uCS", named: ["unset", "chdir", "split-string"], split: { letter: "S", named: "split-string" } },
+ env: { values: "uCS", named: ["unset", "chdir", "split-string"], split: { letter: "S", named: "split-string" }, loneDash: true },
nice: { values: "n", named: ["adjustment"] },
timeout: { values: "ks", named: ["kill-after", "signal"], operands: 1 },
exec: { values: "a" },
@@ -443,6 +444,7 @@ function commandIndex(words: string[], unread: () => void): number {
}
words.splice(--index, spliced, ...parts);
}
+ if (prefix.loneDash && words[index] === "-") index++;
index += prefix.operands ?? 0;
}
return -1;