Skip to content

Commit 0783a1b

Browse files
Cheticclaude
andcommitted
feat(permission): extract inner commands from wrapper commands for permission matching
When commands are wrapped in xargs, timeout, sudo, nice, nohup, etc., the permission system now extracts and checks the inner command against permission rules in addition to the wrapper command itself. This prevents bypassing deny rules like "rm *": "deny" by running "sudo rm -rf /". Closes anomalyco#9516 Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 18fb19d commit 0783a1b

6 files changed

Lines changed: 406 additions & 1 deletion

File tree

‎packages/opencode/src/permission/arity.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,31 @@ This dictionary is used to identify the "human-understandable command" from an i
3232
env: 1, // env
3333
export: 1, // export PATH=/usr/bin
3434
grep: 1, // grep pattern file.txt
35+
ionice: 1, // ionice -c 2 command
3536
kill: 1, // kill 1234
3637
killall: 1, // killall process
3738
ln: 1, // ln -s source target
3839
ls: 1, // ls -la
3940
mkdir: 1, // mkdir new-dir
4041
mv: 1, // mv old.txt new.txt
42+
nice: 1, // nice -n 5 command
43+
nohup: 1, // nohup command args
4144
ps: 1, // ps aux
4245
pwd: 1, // pwd
4346
rm: 1, // rm file.txt
4447
rmdir: 1, // rmdir empty-dir
4548
sleep: 1, // sleep 5
4649
source: 1, // source ~/.bashrc
50+
stdbuf: 1, // stdbuf -oL command
51+
sudo: 1, // sudo command
4752
tail: 1, // tail -f log.txt
53+
time: 1, // time command
54+
timeout: 1, // timeout 5 command
4855
touch: 1, // touch file.txt
4956
unset: 1, // unset VAR
57+
watch: 1, // watch -n 1 command
5058
which: 1, // which node
59+
xargs: 1, // xargs -I{} command
5160
aws: 3, // aws s3 ls
5261
az: 3, // az storage blob list
5362
bazel: 2, // bazel build
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
interface WrapperConfig {
2+
readonly flagsWithArgs: string
3+
readonly positionalArgs: number
4+
readonly skipAssignments?: boolean
5+
}
6+
7+
const WRAPPERS: Record<string, WrapperConfig> = {
8+
env: { flagsWithArgs: "u", positionalArgs: 0, skipAssignments: true },
9+
ionice: { flagsWithArgs: "cnp", positionalArgs: 0 },
10+
nice: { flagsWithArgs: "n", positionalArgs: 0 },
11+
nohup: { flagsWithArgs: "", positionalArgs: 0 },
12+
stdbuf: { flagsWithArgs: "ioe", positionalArgs: 0 },
13+
sudo: { flagsWithArgs: "ugCDhprtU", positionalArgs: 0 },
14+
time: { flagsWithArgs: "", positionalArgs: 0 },
15+
timeout: { flagsWithArgs: "ks", positionalArgs: 1 },
16+
watch: { flagsWithArgs: "n", positionalArgs: 0 },
17+
xargs: { flagsWithArgs: "ILnPsEd", positionalArgs: 0 },
18+
}
19+
20+
function skip(
21+
tokens: ReadonlyArray<string>,
22+
index: number,
23+
config: WrapperConfig,
24+
): number {
25+
const token = tokens[index]
26+
if (token === "--") return -(index + 1)
27+
28+
if (config.skipAssignments && /^[A-Za-z_][A-Za-z0-9_]*=/.test(token))
29+
return index + 1
30+
31+
if (token.startsWith("--")) {
32+
if (token.includes("=")) return index + 1
33+
if (config.flagsWithArgs.length > 0) return index + 2
34+
return index + 1
35+
}
36+
37+
if (token.startsWith("-") && token.length > 1) {
38+
const flag = token[1]
39+
if (config.flagsWithArgs.includes(flag)) {
40+
if (token.length > 2) return index + 1
41+
return index + 2
42+
}
43+
return index + 1
44+
}
45+
46+
return 0
47+
}
48+
49+
function extractFind(tokens: ReadonlyArray<string>): string[][] {
50+
const results: string[][] = []
51+
for (let i = 0; i < tokens.length; i++) {
52+
if (tokens[i] !== "-exec" && tokens[i] !== "-execdir") continue
53+
const inner: string[] = []
54+
for (let j = i + 1; j < tokens.length; j++) {
55+
const t = tokens[j]
56+
if (t === ";" || t === "\\;" || t === "+") {
57+
i = j
58+
break
59+
}
60+
if (t !== "{}") inner.push(t)
61+
}
62+
if (inner.length > 0) results.push(inner)
63+
}
64+
return results
65+
}
66+
67+
export namespace BashWrapper {
68+
export function extract(tokens: ReadonlyArray<string>): string[][] {
69+
if (tokens.length === 0) return []
70+
const cmd = tokens[0]
71+
72+
if (cmd === "find") return extractFind(tokens)
73+
74+
const config = WRAPPERS[cmd]
75+
if (!config) return []
76+
77+
let i = 1
78+
let positionalRemaining = config.positionalArgs
79+
while (i < tokens.length) {
80+
const result = skip(tokens, i, config)
81+
if (result < 0) {
82+
const inner = tokens.slice(-result)
83+
if (inner.length === 0) return []
84+
const nested = extract(inner)
85+
return nested.length > 0 ? nested : [Array.from(inner)]
86+
}
87+
if (result > 0) {
88+
i = result
89+
continue
90+
}
91+
if (positionalRemaining > 0) {
92+
positionalRemaining--
93+
i++
94+
continue
95+
}
96+
const inner = tokens.slice(i)
97+
const nested = extract(inner)
98+
return nested.length > 0 ? nested : [Array.from(inner)]
99+
}
100+
return []
101+
}
102+
}

‎packages/opencode/src/tool/bash.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { Flag } from "@/flag/flag.ts"
1515
import { Shell } from "@/shell/shell"
1616

1717
import { BashArity } from "@/permission/arity"
18+
import { BashWrapper } from "@/permission/wrapper"
1819
import { Truncate } from "./truncation"
1920
import { Plugin } from "@/plugin"
2021

@@ -138,6 +139,12 @@ export const BashTool = Tool.define("bash", async () => {
138139
if (command.length && command[0] !== "cd") {
139140
patterns.add(commandText)
140141
always.add(BashArity.prefix(command).join(" ") + " *")
142+
for (const inner of BashWrapper.extract(command)) {
143+
if (inner.length > 0 && inner[0] !== "cd") {
144+
patterns.add(inner.join(" "))
145+
always.add(BashArity.prefix(inner).join(" ") + " *")
146+
}
147+
}
141148
}
142149
}
143150

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
import { describe, expect, test } from "bun:test"
2+
import { BashWrapper } from "../../src/permission/wrapper"
3+
4+
describe("BashWrapper.extract", () => {
5+
describe("each wrapper type", () => {
6+
test("nohup", () => {
7+
expect(BashWrapper.extract(["nohup", "git", "push"])).toEqual([["git", "push"]])
8+
})
9+
10+
test("time", () => {
11+
expect(BashWrapper.extract(["time", "make", "build"])).toEqual([["make", "build"]])
12+
})
13+
14+
test("timeout", () => {
15+
expect(BashWrapper.extract(["timeout", "5", "curl", "http://example.com"])).toEqual([
16+
["curl", "http://example.com"],
17+
])
18+
})
19+
20+
test("nice", () => {
21+
expect(BashWrapper.extract(["nice", "-n", "5", "make"])).toEqual([["make"]])
22+
})
23+
24+
test("ionice", () => {
25+
expect(BashWrapper.extract(["ionice", "-c", "2", "-n", "7", "dd", "if=/dev/zero"])).toEqual([
26+
["dd", "if=/dev/zero"],
27+
])
28+
})
29+
30+
test("stdbuf", () => {
31+
expect(BashWrapper.extract(["stdbuf", "-oL", "tail", "-f", "log.txt"])).toEqual([["tail", "-f", "log.txt"]])
32+
})
33+
34+
test("watch", () => {
35+
expect(BashWrapper.extract(["watch", "-n", "1", "kubectl", "get", "pods"])).toEqual([
36+
["kubectl", "get", "pods"],
37+
])
38+
})
39+
40+
test("sudo", () => {
41+
expect(BashWrapper.extract(["sudo", "rm", "-rf", "/"])).toEqual([["rm", "-rf", "/"]])
42+
})
43+
44+
test("sudo with flags", () => {
45+
expect(BashWrapper.extract(["sudo", "-u", "root", "systemctl", "restart", "nginx"])).toEqual([
46+
["systemctl", "restart", "nginx"],
47+
])
48+
})
49+
50+
test("env", () => {
51+
expect(BashWrapper.extract(["env", "node", "server.js"])).toEqual([["node", "server.js"]])
52+
})
53+
54+
test("xargs", () => {
55+
expect(BashWrapper.extract(["xargs", "-I{}", "gh", "api", "-X", "DELETE"])).toEqual([
56+
["gh", "api", "-X", "DELETE"],
57+
])
58+
})
59+
60+
test("xargs with split flag and arg", () => {
61+
expect(BashWrapper.extract(["xargs", "-I", "{}", "rm"])).toEqual([["rm"]])
62+
})
63+
})
64+
65+
describe("flag handling", () => {
66+
test("combined flag+arg", () => {
67+
expect(BashWrapper.extract(["xargs", "-I{}", "echo"])).toEqual([["echo"]])
68+
})
69+
70+
test("split flag and arg", () => {
71+
expect(BashWrapper.extract(["xargs", "-I", "{}", "echo"])).toEqual([["echo"]])
72+
})
73+
74+
test("boolean flag", () => {
75+
expect(BashWrapper.extract(["sudo", "-n", "ls"])).toEqual([["ls"]])
76+
})
77+
78+
test("long flag with =", () => {
79+
expect(BashWrapper.extract(["timeout", "--signal=KILL", "5", "sleep", "10"])).toEqual([["sleep", "10"]])
80+
})
81+
82+
test("-- terminator", () => {
83+
expect(BashWrapper.extract(["sudo", "--", "rm", "-rf", "/tmp/x"])).toEqual([["rm", "-rf", "/tmp/x"]])
84+
})
85+
})
86+
87+
describe("env specifics", () => {
88+
test("VAR=val skipping", () => {
89+
expect(BashWrapper.extract(["env", "FOO=bar", "BAZ=qux", "node", "app.js"])).toEqual([["node", "app.js"]])
90+
})
91+
92+
test("mixed flags and assignments", () => {
93+
expect(BashWrapper.extract(["env", "-u", "HOME", "PATH=/usr/bin", "sh", "-c", "echo"])).toEqual([
94+
["sh", "-c", "echo"],
95+
])
96+
})
97+
})
98+
99+
describe("timeout positional", () => {
100+
test("duration arg skipped before inner command", () => {
101+
expect(BashWrapper.extract(["timeout", "30s", "npm", "test"])).toEqual([["npm", "test"]])
102+
})
103+
104+
test("with flags and duration", () => {
105+
expect(BashWrapper.extract(["timeout", "--signal=KILL", "10", "make"])).toEqual([["make"]])
106+
})
107+
})
108+
109+
describe("find -exec", () => {
110+
test("single -exec", () => {
111+
expect(BashWrapper.extract(["find", ".", "-name", "*.log", "-exec", "rm", "{}", ";"])).toEqual([["rm"]])
112+
})
113+
114+
test("-execdir", () => {
115+
expect(BashWrapper.extract(["find", "/tmp", "-execdir", "chmod", "644", "{}", ";"])).toEqual([["chmod", "644"]])
116+
})
117+
118+
test("multiple -exec clauses", () => {
119+
expect(
120+
BashWrapper.extract([
121+
"find",
122+
".",
123+
"-exec",
124+
"echo",
125+
"{}",
126+
";",
127+
"-exec",
128+
"rm",
129+
"{}",
130+
"+",
131+
]),
132+
).toEqual([["echo"], ["rm"]])
133+
})
134+
135+
test("escaped terminator", () => {
136+
expect(BashWrapper.extract(["find", ".", "-exec", "cat", "{}", "\\;"])).toEqual([["cat"]])
137+
})
138+
})
139+
140+
describe("nested wrappers", () => {
141+
test("timeout + sudo", () => {
142+
expect(BashWrapper.extract(["timeout", "5", "sudo", "gh", "api"])).toEqual([["gh", "api"]])
143+
})
144+
145+
test("nohup + nice + make", () => {
146+
expect(BashWrapper.extract(["nohup", "nice", "-n", "10", "make", "build"])).toEqual([["make", "build"]])
147+
})
148+
149+
test("sudo + env + node", () => {
150+
expect(BashWrapper.extract(["sudo", "env", "NODE_ENV=prod", "node", "app.js"])).toEqual([["node", "app.js"]])
151+
})
152+
})
153+
154+
describe("non-wrappers", () => {
155+
test("regular command returns empty", () => {
156+
expect(BashWrapper.extract(["git", "checkout", "main"])).toEqual([])
157+
})
158+
159+
test("unknown command returns empty", () => {
160+
expect(BashWrapper.extract(["myapp", "--verbose"])).toEqual([])
161+
})
162+
})
163+
164+
describe("edge cases", () => {
165+
test("empty tokens", () => {
166+
expect(BashWrapper.extract([])).toEqual([])
167+
})
168+
169+
test("wrapper with no args", () => {
170+
expect(BashWrapper.extract(["nohup"])).toEqual([])
171+
})
172+
173+
test("wrapper with only flags", () => {
174+
expect(BashWrapper.extract(["sudo", "-n"])).toEqual([])
175+
})
176+
177+
test("timeout with only duration", () => {
178+
expect(BashWrapper.extract(["timeout", "5"])).toEqual([])
179+
})
180+
})
181+
})

0 commit comments

Comments
 (0)