Skip to content

Commit ce5a3fd

Browse files
fix: strip inline env var prefixes from bash permission patterns
Commands like 'GITHUB_TOKEN=x git push --force' were matching '*': allow instead of the intended deny rule because variable_assignment nodes were included in the pattern string used for permission matching. Build the permission pattern from parts() tokens (which already exclude variable_assignment children) rather than from the raw node text, then re-attach any redirection suffix from the parent redirected_statement so 'echo test > output.txt' still produces the full pattern as before. Also adds integration tests verifying the corrected behavior. Based on test groundwork from PR anomalyco#16086. Fixes issue anomalyco#16075
1 parent f70afbe commit ce5a3fd

2 files changed

Lines changed: 77 additions & 5 deletions

File tree

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,6 @@ function parts(node: Node) {
119119
return out
120120
}
121121

122-
function source(node: Node) {
123-
return (node.parent?.type === "redirected_statement" ? node.parent.text : node.text).trim()
124-
}
125-
126122
function commands(node: Node) {
127123
return node.descendantsOfType("command").filter((child): child is Node => Boolean(child))
128124
}
@@ -401,7 +397,20 @@ export const ShellTool = Tool.define(
401397
}
402398

403399
if (tokens.length && (!cmd || !CWD.has(cmd))) {
404-
scan.patterns.add(source(node))
400+
// Build the pattern from parts() tokens, which already exclude variable_assignment
401+
// nodes (e.g. GITHUB_TOKEN=x), so the pattern only contains the actual command.
402+
let pattern = tokens.join(" ")
403+
if (node.parent?.type === "redirected_statement") {
404+
// Re-attach any redirection suffix (e.g. "2>&1", "> file") that lives on
405+
// the parent but outside the command node itself.
406+
// Guard with startsWith: tree-sitter guarantees child ranges are within
407+
// parent ranges, but .text is a string slice so we verify before slicing.
408+
if (node.parent.text.startsWith(node.text)) {
409+
const redirectSuffix = node.parent.text.slice(node.text.length).trim()
410+
if (redirectSuffix) pattern = pattern + " " + redirectSuffix
411+
}
412+
}
413+
scan.patterns.add(pattern)
405414
scan.always.add(BashArity.prefix(tokens).join(" ") + " *")
406415
}
407416
}

‎packages/opencode/test/tool/shell.test.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,69 @@ describe("tool.shell permissions", () => {
10231023
)
10241024
}),
10251025
)
1026+
1027+
each("strips inline env var prefix from permission pattern", () =>
1028+
Effect.gen(function* () {
1029+
const tmp = yield* tmpdirScoped()
1030+
yield* runIn(
1031+
tmp,
1032+
Effect.gen(function* () {
1033+
const err = new Error("stop after permission")
1034+
const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
1035+
yield* fail(
1036+
{ command: 'CI=true git commit -m "test"', description: "Commit with env prefix" },
1037+
capture(requests, err),
1038+
)
1039+
const bashReq = requests.find((r) => r.permission === "bash")
1040+
expect(bashReq).toBeDefined()
1041+
expect(bashReq!.patterns).toContain('git commit -m "test"')
1042+
expect(bashReq!.patterns).not.toContain('CI=true git commit -m "test"')
1043+
}),
1044+
)
1045+
}),
1046+
)
1047+
1048+
each("strips multiple inline env var prefixes from permission pattern", () =>
1049+
Effect.gen(function* () {
1050+
const tmp = yield* tmpdirScoped()
1051+
yield* runIn(
1052+
tmp,
1053+
Effect.gen(function* () {
1054+
const err = new Error("stop after permission")
1055+
const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
1056+
yield* fail(
1057+
{ command: "FOO=1 BAR=2 echo hello", description: "Echo with multiple env prefixes" },
1058+
capture(requests, err),
1059+
)
1060+
const bashReq = requests.find((r) => r.permission === "bash")
1061+
expect(bashReq).toBeDefined()
1062+
expect(bashReq!.patterns).toContain("echo hello")
1063+
expect(bashReq!.patterns).not.toContain("FOO=1")
1064+
}),
1065+
)
1066+
}),
1067+
)
1068+
1069+
each("strips env var prefix and preserves redirection in permission pattern", () =>
1070+
Effect.gen(function* () {
1071+
const tmp = yield* tmpdirScoped()
1072+
yield* runIn(
1073+
tmp,
1074+
Effect.gen(function* () {
1075+
const err = new Error("stop after permission")
1076+
const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
1077+
yield* fail(
1078+
{ command: "CI=true echo hello > output.txt", description: "Redirect with env prefix" },
1079+
capture(requests, err),
1080+
)
1081+
const bashReq = requests.find((r) => r.permission === "bash")
1082+
expect(bashReq).toBeDefined()
1083+
expect(bashReq!.patterns).toContain("echo hello > output.txt")
1084+
expect(bashReq!.patterns).not.toContain("CI=true")
1085+
}),
1086+
)
1087+
}),
1088+
)
10261089
})
10271090

10281091
describe("tool.shell abort", () => {

0 commit comments

Comments
 (0)