From 635b1edca32ca6f70e35446a173c45a97b6db637 Mon Sep 17 00:00:00 2001 From: Janghoon Lee <44862514+its-janghoon@users.noreply.github.com> Date: Mon, 16 Nov 2026 16:27:03 +0000 Subject: [PATCH] fix(permission): fewer approvals in edits mode, none in a disposable sandbox - Read-only detection also covers loops (for/do/done), xargs with a reader, awk that only prints, identify/xxd/date/sleep; chains with && ; | were already read-only part by part. - REDROB_SANDBOX=1 (set by Redrob's cloud project runs on the daemon): in edits mode bash, edits and outside folders run without asking, because the VM is thrown away and only reviewed changes leave it. Still asks for clearly destructive commands: deleting the machine, home, system folders or the sandbox's own tools, download-piped-to-shell, disk/power commands, killing everything or the engine. A configured deny still wins; PCs are unchanged. --- packages/core/src/permission/mobile.ts | 20 +++++++- packages/core/src/permission/readonly.ts | 59 +++++++++++++++++++++++- packages/core/test/readonly.test.ts | 31 +++++++++++++ 3 files changed, 106 insertions(+), 4 deletions(-) diff --git a/packages/core/src/permission/mobile.ts b/packages/core/src/permission/mobile.ts index 96cfc8c8b8..81a0dcabab 100644 --- a/packages/core/src/permission/mobile.ts +++ b/packages/core/src/permission/mobile.ts @@ -19,7 +19,7 @@ export * as MobileProfile from "./mobile" * session becomes mobile again when the phone prompts it. */ -import { isReadOnlyCommand } from "./readonly" +import { isReadOnlyCommand, isSandboxDestructive } from "./readonly" const KEY = Symbol.for("redrob.mobile.permission-profile") @@ -28,7 +28,8 @@ const KEY = Symbol.for("redrob.mobile.permission-profile") * * - `ask`: every dangerous action asks (the profile above; also what an old app without the header gets); * - `edits`: file edits inside the project folder and read-only commands run right away; other - * commands and outside folders ask; + * commands and outside folders ask. In a disposable cloud sandbox (`REDROB_SANDBOX=1`) commands, + * edits and outside folders run too, except clearly destructive commands (`isSandboxDestructive`); * - `all`: nothing asks except the rules in `KEEP_ASKING`. The forwarder only grants it while the * launcher's switch (`~/.redrob/desk/settings.json` `allowAutoAll`) is not turned off. * @@ -37,6 +38,17 @@ const KEY = Symbol.for("redrob.mobile.permission-profile") export type ApprovalMode = "ask" | "edits" | "all" export const APPROVAL_MODES: readonly ApprovalMode[] = ["ask", "edits", "all"] +/** What a disposable sandbox's `edits` mode lets through. */ +const SANDBOX_ACTIONS: readonly string[] = ["bash", "edit", "external_directory", "read", "write"] + +/** + * The engine runs in a disposable cloud sandbox (Redrob's project preview sets `REDROB_SANDBOX=1` + * on the daemon): the VM is thrown away with the run. Never set on a PC. + */ +export function disposable(): boolean { + return process.env.REDROB_SANDBOX === "1" +} + /** Rules that ask in every mode, even `all`. */ export const KEEP_ASKING: readonly string[] = ["doom_loop"] @@ -98,6 +110,10 @@ export function decide(sessionID: string, action: string, resource: string, conf const current = mode(sessionID) if (current === "all") return "allow" if (current === "edits" && action === "edit" && insideProject(resource)) return "allow" + // A disposable cloud sandbox (REDROB_SANDBOX=1): nothing in it outlives the run and only the + // reviewed changes leave it, so edits, commands and outside folders run; clear wreckage still asks. + if (current === "edits" && disposable() && SANDBOX_ACTIONS.includes(action)) + return action === "bash" && isSandboxDestructive(resource) ? "ask" : "allow" // Commands that only read (grep, cat, git status, ...) run as reads do (permission/readonly.ts). if (current === "edits" && action === "bash" && isReadOnlyCommand(resource)) return "allow" if (isDangerous(action)) return "ask" diff --git a/packages/core/src/permission/readonly.ts b/packages/core/src/permission/readonly.ts index d50fbdc089..deed2c6335 100644 --- a/packages/core/src/permission/readonly.ts +++ b/packages/core/src/permission/readonly.ts @@ -16,6 +16,7 @@ const READERS = new Set([ "dirname", "realpath", "readlink", "sort", "uniq", "cut", "tr", "nl", "column", "diff", "cmp", "jq", "whoami", "id", "uname", "printenv", "cd", "pushd", "popd", "seq", "md5sum", "sha1sum", "sha256sum", "od", "hexdump", "strings", "comm", "fold", "rev", "tac", "expand", "paste", + "identify", "xxd", "nproc", "date", "sleep", // Redrob's preview browser in a cloud sandbox: looks at the page, writes only its screenshot. "redrob-preview", ]) @@ -156,12 +157,37 @@ function simpleReadOnly(part: string): boolean { } args.push(w) } - // Leading VAR=value assignments. - while (args.length && /^[A-Za-z_][A-Za-z0-9_]*=/.test(args[0]!)) args.shift() + return argsReadOnly(args) +} + +/** Shell keywords around a loop or test body: they run nothing themselves. */ +const KEYWORDS = new Set(["do", "then", "else", "elif", "if", "while", "until", "!", "{", "}", "(", ")"]) +const ENDS = new Set(["done", "fi", "esac"]) + +/** One simple command's words (redirections already checked and taken out). */ +function argsReadOnly(input: string[]): boolean { + const args = [...input] + // Leading VAR=value assignments and loop/test keywords. + for (;;) { + if (args.length && /^[A-Za-z_][A-Za-z0-9_]*=/.test(args[0]!)) args.shift() + else if (args.length && KEYWORDS.has(args[0]!)) args.shift() + else break + } if (!args.length) return true + // `for f in a b`: the list only names things (substitutions in it are checked on their own). + if (args[0] === "for") return true + if (ENDS.has(args[0]!)) return args.length === 1 const [cmd, ...rest] = args const name = cmd!.split("/").pop()! if (READERS.has(name)) return true + if (name === "xargs") { + let i = 0 + while (i < rest.length && rest[i]!.startsWith("-")) i += /^-(n|I|L|P|d|s|E)$/.test(rest[i]!) ? 2 : 1 + return i < rest.length && argsReadOnly(rest.slice(i)) + } + // awk that only prints: no system(), pipes, redirections or getline in its program. + if (name === "awk" || name === "gawk" || name === "mawk") + return !rest.some((a) => /system\s*\(|getline|[|>]|\bfflush\b/.test(a)) if (name === "find") return !rest.some((a) => FIND_WRITES.test(a)) if (name === "sed") { if (rest.some((a) => a === "-i" || a.startsWith("-i") || a.startsWith("--in-place"))) return false @@ -189,3 +215,32 @@ export function isReadOnlyCommand(command: string): boolean { const simple = parts.map((p) => p.trim()).filter(Boolean) return simple.length > 0 && simple.every(simpleReadOnly) } + +/** System folders a sandbox command must not delete from. */ +const SYSTEM_DIRS = /^\/(bin|boot|dev|etc|lib|lib64|opt|proc|root|sbin|sys|usr|var|home)(\/|$)/ + +/** Is one delete target the machine, home, a system folder or the sandbox's own tools? */ +function wreckTarget(raw: string): boolean { + const t = raw.replace(/^["']|["']$/g, "") + if (t === "/" || t === "/*" || t === "~" || t === "~/" || t === "$HOME" || t === "$HOME/" || t === "/." || t === "/..") return true + if (SYSTEM_DIRS.test(t)) return true + if (t.startsWith("/vercel/sandbox") && !t.startsWith("/vercel/sandbox/repo")) return true + return /(^|\/)\.redrob(\/|$)/.test(t) +} + +/** + * In a disposable sandbox almost anything may run; these still ask: deleting the machine, home, + * system folders or the sandbox's own tools (outside the repository), piping a download into a + * shell, disk and power commands, and killing everything or the engine. + */ +export function isSandboxDestructive(command: string): boolean { + const c = command.replace(/\s+/g, " ") + for (const m of c.matchAll(/(?:^|[;&|(\s])(?:sudo\s+)?rm\s+([^;&|]*)/g)) { + if ((m[1] ?? "").split(" ").some((t) => t && !t.startsWith("-") && wreckTarget(t))) return true + } + if (/\b(curl|wget)\b[^|;&]*\|\s*(sudo\s+)?(ba|z|da)?sh\b/.test(c)) return true + if (/\b(mkfs(\.\w+)?|fdisk|parted|shutdown|reboot|poweroff|halt)\b/.test(c)) return true + if (/\bdd\b[^;&|]*\bof=\/dev\//.test(c)) return true + if (/\bkill\s+(-9\s+)?-1\b|\bpkill\b[^;&|]*\bredrob\b|\bkillall\b/.test(c)) return true + return false +} diff --git a/packages/core/test/readonly.test.ts b/packages/core/test/readonly.test.ts index f39b63a7ae..e713961774 100644 --- a/packages/core/test/readonly.test.ts +++ b/packages/core/test/readonly.test.ts @@ -19,6 +19,11 @@ describe("read-only commands", () => { "cd /vercel/sandbox/repo; grep -rn \"pasted\" apps/api --include=*.ts 2>&1 | head", "git branch --show-current", "/vercel/sandbox/bin/redrob-preview shot /settings --width 390", + "cd /vercel/sandbox/repo && wc -l a.ts && grep -n x b.ts", + "for f in a b; do wc -l $f; done", + "git ls-files | xargs grep -l x", + "awk '{print $1}' a.ts", + "ls -la /tmp/rp/ && identify /tmp/rp/a.png", ])("%s", (command) => expect(isReadOnlyCommand(command)).toBe(true)) test.each([ @@ -44,6 +49,9 @@ describe("read-only commands", () => { "node -e 'require(\"fs\").rmSync(\"a\")'", "env rm -rf x", "ls & rm x", + "git ls-files | xargs rm", + "awk '{print > \"out\"}' a", + "awk 'BEGIN{system(\"rm x\")}'", "", ])("not: %s", (command) => expect(isReadOnlyCommand(command)).toBe(false)) @@ -56,4 +64,27 @@ describe("read-only commands", () => { expect(MobileProfile.decide("s-ro", "bash", "git status", "allow")).toBe("ask") MobileProfile.unmark("s-ro") }) + + test("a disposable sandbox runs commands, edits and outside folders in edits mode", () => { + const before = process.env.REDROB_SANDBOX + MobileProfile.mark("s-sb", "edits") + try { + expect(MobileProfile.decide("s-sb", "bash", "npx tsc --noEmit", "ask")).toBe("ask") + process.env.REDROB_SANDBOX = "1" + expect(MobileProfile.decide("s-sb", "bash", "cd apps/web && npx tsc --noEmit", "ask")).toBe("allow") + expect(MobileProfile.decide("s-sb", "bash", "cat > /tmp/s.js <