From f92e02cb929bb14a30d2c2a5f75400a102e32674 Mon Sep 17 00:00:00 2001 From: Janghoon Lee <44862514+its-janghoon@users.noreply.github.com> Date: Mon, 16 Nov 2026 13:43:05 +0000 Subject: [PATCH] fix(core): run read-only shell commands without asking in edits mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A device session in edits mode (수정 자동) asked for every bash call, so grep, cat, ls, git status or sed -n waited for approval as 'something that can change or delete'. permission/readonly.ts classifies a command as read-only only when every simple command in it is a known reader with no writing flag and nothing writes through a redirection; those now run as file reads do. Anything it does not understand still asks; a configured deny still wins. --- packages/core/src/permission/mobile.ts | 7 +- packages/core/src/permission/readonly.ts | 191 +++++++++++++++++++++++ packages/core/test/readonly.test.ts | 59 +++++++ 3 files changed, 256 insertions(+), 1 deletion(-) create mode 100644 packages/core/src/permission/readonly.ts create mode 100644 packages/core/test/readonly.test.ts diff --git a/packages/core/src/permission/mobile.ts b/packages/core/src/permission/mobile.ts index 1561b5df34..96cfc8c8b8 100644 --- a/packages/core/src/permission/mobile.ts +++ b/packages/core/src/permission/mobile.ts @@ -19,13 +19,16 @@ export * as MobileProfile from "./mobile" * session becomes mobile again when the phone prompts it. */ +import { isReadOnlyCommand } from "./readonly" + const KEY = Symbol.for("redrob.mobile.permission-profile") /** * The approval mode the device picked for a session (header `X-Redrob-Approval`): * * - `ask`: every dangerous action asks (the profile above; also what an old app without the header gets); - * - `edits`: file edits inside the project folder run right away; commands and outside folders ask; + * - `edits`: file edits inside the project folder and read-only commands run right away; other + * commands and outside folders ask; * - `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. * @@ -95,6 +98,8 @@ 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" + // 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" return configured } diff --git a/packages/core/src/permission/readonly.ts b/packages/core/src/permission/readonly.ts new file mode 100644 index 0000000000..d50fbdc089 --- /dev/null +++ b/packages/core/src/permission/readonly.ts @@ -0,0 +1,191 @@ +/** + * Shell commands that only read (`grep`, `cat`, `ls`, `git status`, `sed -n`, ...), for the + * device approval modes (permission/mobile.ts): in `edits` they run without asking, as file reads do. + * + * Conservative on purpose: anything this does not understand is not read-only. A command is + * read-only when every simple command in it (split on `&&`, `||`, `;`, `|`, newlines, and inside + * `$(...)`) is a known reader with no writing flag, and nothing writes through a redirection + * (only `>/dev/null`, `2>/dev/null`, `2>&1`, `&>/dev/null` are allowed). Backticks, `<(...)`, + * here-docs and `eval`-like commands are never read-only. + */ + +/** Readers that are read-only with any arguments. */ +const READERS = new Set([ + "cat", "head", "tail", "wc", "ls", "ll", "tree", "pwd", "echo", "printf", "true", "false", "test", "[", + "grep", "egrep", "fgrep", "rg", "ag", "file", "stat", "du", "df", "which", "type", "whereis", "basename", + "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", + // Redrob's preview browser in a cloud sandbox: looks at the page, writes only its screenshot. + "redrob-preview", +]) + +/** git subcommands that only read. */ +const GIT_READ = new Set([ + "status", "diff", "log", "show", "blame", "grep", "ls-files", "ls-tree", "rev-parse", "describe", + "shortlog", "cat-file", "rev-list", "merge-base", "name-rev", "whatchanged", "reflog", +]) + +const FIND_WRITES = /^-(delete|exec|execdir|ok|okdir|fprint|fprint0|fprintf|fls)$/ + +const SAFE_REDIRECT = /^(?:[012]?>|&>)\/dev\/null$|^[12]>&[12]$/ + +/** Splits on control operators outside quotes; `$(...)` bodies are returned as separate commands. */ +function split(command: string): string[] | null { + const parts: string[] = [] + let cur = "" + let quote: "'" | '"' | null = null + for (let i = 0; i < command.length; i++) { + const c = command[i]! + if (quote === "'") { + cur += c + if (c === "'") quote = null + continue + } + if (c === "\\") { + cur += c + (command[i + 1] ?? "") + i++ + continue + } + if (c === "`") return null + if (c === "$" && command[i + 1] === "(") { + if (command[i + 2] === "(") return null // arithmetic: keep it simple + let depth = 1 + let j = i + 2 + let q: string | null = null + for (; j < command.length && depth > 0; j++) { + const d = command[j]! + if (q) { + if (d === q) q = null + } else if (d === "'" || d === '"') q = d + else if (d === "(") depth++ + else if (d === ")") depth-- + } + if (depth !== 0) return null + const inner = split(command.slice(i + 2, j - 1)) + if (!inner) return null + parts.push(...inner) + cur += "X" + i = j - 1 + continue + } + if (quote === '"') { + cur += c + if (c === '"') quote = null + continue + } + if (c === "'" || c === '"') { + quote = c + cur += c + continue + } + if (c === "<" && (command[i + 1] === "(" || command[i + 1] === "<")) return null + if (c === ">" && command[i + 1] === "(") return null + if (c === "&" && command[i + 1] === "&") { + parts.push(cur) + cur = "" + i++ + continue + } + if (c === "|") { + parts.push(cur) + cur = "" + if (command[i + 1] === "|") i++ + continue + } + if (c === ";" || c === "\n") { + parts.push(cur) + cur = "" + continue + } + if (c === "&" && command[i + 1] !== ">" && command[i - 1] !== ">") return null // background job + cur += c + } + if (quote) return null + parts.push(cur) + return parts +} + +/** Words of one simple command with quotes removed; redirections kept as their own words. */ +function words(part: string): string[] { + const out: string[] = [] + let token: string | null = null + let quote: string | null = null + for (let i = 0; i < part.length; i++) { + const c = part[i]! + if (quote) { + if (c === quote) quote = null + else if (c === "\\" && quote === '"') token += part[++i] ?? "" + else token += c + continue + } + if (c === "'" || c === '"') { + quote = c + token ??= "" + continue + } + if (c === "\\") { + token = (token ?? "") + (part[++i] ?? "") + continue + } + if (/\s/.test(c)) { + if (token !== null) out.push(token) + token = null + continue + } + token = (token ?? "") + c + } + if (token !== null) out.push(token) + return out +} + +function simpleReadOnly(part: string): boolean { + const list = words(part.trim()) + // Redirections: only to /dev/null or between stdout/stderr. + const args: string[] = [] + for (let i = 0; i < list.length; i++) { + const w = list[i]! + if (/^(?:[012]?>>?|&>>?)/.test(w) || w.includes(">")) { + const joined = /^(?:[012]?>|&>)$/.test(w) ? w + (list[++i] ?? "") : w + if (!SAFE_REDIRECT.test(joined)) return false + continue + } + if (/^[012]? FIND_WRITES.test(a)) + if (name === "sed") { + if (rest.some((a) => a === "-i" || a.startsWith("-i") || a.startsWith("--in-place"))) return false + if (!rest.some((a) => a === "-n" || /^-[a-zA-Z]*n[a-zA-Z]*$/.test(a))) return false + // Script commands that write a file or run one. + return !rest.some((a) => /(^|[;}\s])[wWe]\s|(^|[;}\s])[wW]$/.test(a)) + } + if (name === "git") { + let i = 0 + while (i < rest.length && rest[i]!.startsWith("-")) i += rest[i] === "-C" || rest[i] === "-c" ? 2 : 1 + const sub = rest[i] + if (!sub) return false + if (GIT_READ.has(sub)) return !rest.slice(i + 1).some((a) => a === "--output" || a.startsWith("--output=")) + if (sub === "branch" || sub === "tag" || sub === "remote" || sub === "stash") + return rest.slice(i + 1).every((a) => ["-a", "-r", "-v", "-vv", "--list", "-l", "list", "show", "--show-current"].includes(a)) + return false + } + return false +} + +export function isReadOnlyCommand(command: string): boolean { + if (!command.trim() || command.length > 4000) return false + const parts = split(command) + if (!parts) return false + const simple = parts.map((p) => p.trim()).filter(Boolean) + return simple.length > 0 && simple.every(simpleReadOnly) +} diff --git a/packages/core/test/readonly.test.ts b/packages/core/test/readonly.test.ts new file mode 100644 index 0000000000..f39b63a7ae --- /dev/null +++ b/packages/core/test/readonly.test.ts @@ -0,0 +1,59 @@ +import { describe, expect, test } from "bun:test" +import { MobileProfile } from "../src/permission/mobile" +import { isReadOnlyCommand } from "../src/permission/readonly" + +describe("read-only commands", () => { + test.each([ + "grep -rn pasted apps/api --include=*.ts", + "cat apps/web/package.json", + "ls -la", + "rg useComposer apps/web | head -20", + "find . -name '*.tsx' -not -path './node_modules/*'", + "git status", + "git diff --stat", + "git -C apps/web log --oneline -5", + "sed -n 60,200p components/chat/search-form/useComposerSubmit.ts", + "head -50 a.ts && tail -n 20 b.ts", + "wc -l a b c 2>/dev/null", + 'cd apps/web; f=$(grep -rl "export function useComposerSubmit" --include=*.ts* .); echo $f; cat $f', + "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", + ])("%s", (command) => expect(isReadOnlyCommand(command)).toBe(true)) + + test.each([ + "rm -rf node_modules", + "echo hi > a.txt", + "cat a >> b", + "find . -name '*.log' -delete", + "find . -exec rm {} \\;", + "sed -i 's/a/b/' x.ts", + "sed 's/a/b/' x.ts", + "sed -n 'w out.txt' x", + "git commit -m x", + "git push", + "git checkout develop", + "git branch -D x", + "npm install", + "yarn dev &", + "cat `which node`", + "echo $(rm -rf /)", + "grep x a | tee out.txt", + "cat < expect(isReadOnlyCommand(command)).toBe(false)) + + test("edits mode runs read-only commands and still asks for others", () => { + MobileProfile.mark("s-ro", "edits") + expect(MobileProfile.decide("s-ro", "bash", "git status && grep -rn x .", "ask")).toBe("allow") + expect(MobileProfile.decide("s-ro", "bash", "rm -rf x", "ask")).toBe("ask") + expect(MobileProfile.decide("s-ro", "bash", "git status", "deny")).toBe("deny") + MobileProfile.mark("s-ro", "ask") + expect(MobileProfile.decide("s-ro", "bash", "git status", "allow")).toBe("ask") + MobileProfile.unmark("s-ro") + }) +})