Skip to content

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

fix(safety): read a substitution inside ${…} or an array

DheerajPiduru52dfe3b0fee3db
diff --git a/server/safety-floor.test.ts b/server/safety-floor.test.ts
index d037a3f012..8031841f1d 100644
--- a/server/safety-floor.test.ts
+++ b/server/safety-floor.test.ts
@@ -148,6 +148,11 @@ const BLOCKED: Case[] = [
["if true; then rm -rf /; fi", "delete-root"],
["echo $(rm -rf /)", "delete-root"],
["echo `rm -rf ~`", "delete-home"],
+ // a substitution inside `${…}` or an array runs
+ ["echo ${X:-$(rm -rf /)}", "delete-root"],
+ ['echo "${X:-"$(rm -rf /)"}"', "delete-root"],
+ [': "${DIR:=`rm -rf ~`}"', "delete-home"],
+ ["files=( $(mkfs.ext4 /dev/sda1) )", "format-disk"],
["rm -rf \\\n /", "delete-root"],
// formatting or overwriting a disk
["mkfs.ext4 /dev/sda1", "format-disk"],
@@ -452,6 +457,8 @@ const ALLOWED: Array<[command: string, on?: Platform | "posix"]> = [
["rm -rf $BUILD_DIR/"],
['rm -rf "${TMPDIR:-/tmp}/work"'],
['rm -rf "$(mktemp -d)"'],
+ ['DIR="${DIR:-$(mktemp -d)}"'],
+ ["files=( $(ls) )"],
["rm -rf /$NAME"],
["rm -rf ~/../shared-scratch"],
["rm -rf ~other"],
diff --git a/server/safety-floor.ts b/server/safety-floor.ts
index 746e491cb7..0a7e5816e5 100644
--- a/server/safety-floor.ts
+++ b/server/safety-floor.ts
@@ -104,7 +104,25 @@ function lex(source: string, windows: boolean): Reading {
return key.startsWith("env:") ? WINDOWS_VARIABLES[key.slice(4)] ?? DYNAMIC : DYNAMIC;
};
- /** Returns the text up to the matching `shut`. Inside `"${…}"` double quotes still quote but `'` is literal. */
+ const nested = (shut: string) => {
+ if (++nesting > MAX_NESTING) {
+ at = source.length;
+ synced = false;
+ } else if (!script(shut)) synced = false;
+ nesting--;
+ };
+ /** Reads a POSIX `$(…)` or backtick substitution at `at` as commands, and leaves `at` on its last character. */
+ const substitution = (): boolean => {
+ const tick = source[at] === "`";
+ if (windows || !(tick || source.startsWith("$(", at))) return false;
+ at += tick ? 1 : 2;
+ nested(tick ? "`" : ")");
+ at--;
+ return true;
+ };
+
+ /** Returns the text up to the matching `shut`. Inside `"${…}"` double quotes still quote but `'` is literal.
+ * A substitution inside (`${X:-$(…)}`, `name=( $(…) )`) runs, so its commands are read. */
const balanced = (open: string, shut: string, inDoubleQuotes: boolean): string => {
const from = at;
for (let depth = 1; at < source.length; at++) {
@@ -112,8 +130,9 @@ function lex(source: string, windows: boolean): Reading {
if (c === "\\" && !windows) at++;
else if (c === "'" && !inDoubleQuotes) at = endOf("'", at + 1);
else if (c === '"') {
- for (at++; at < source.length && source[at] !== '"'; at++) if (source[at] === "\\" && !windows) at++;
- } else if (c === open) depth++;
+ for (at++; at < source.length && source[at] !== '"'; at++) if (source[at] === "\\" && !windows) at++; else substitution();
+ } else if (substitution()) continue;
+ else if (c === open) depth++;
else if (c === shut && --depth === 0) return source.slice(from, at++);
}
synced = false;
@@ -187,13 +206,6 @@ function lex(source: string, windows: boolean): Reading {
at += skip;
command = { words: [], starts: [], writes: [], from: at, to: at };
};
- const nested = (shut: string) => {
- if (++nesting > MAX_NESTING) {
- at = source.length;
- synced = false;
- } else if (!script(shut)) synced = false;
- nesting--;
- };
const quoted = () => {
while (at < source.length) {
const c = source[at];