Skip to content

Commit ae98cd2

Browse files
fix(opencode,core): close audit-v14 wave K shell classification findings
1 parent 4618e51 commit ae98cd2

2 files changed

Lines changed: 455 additions & 84 deletions

File tree

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

Lines changed: 195 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ const CMD_FILES = new Set([
8383
const FLAGS = new Set(["-destination", "-literalpath", "-path"])
8484
const SWITCHES = new Set(["-confirm", "-debug", "-force", "-nonewline", "-recurse", "-verbose", "-whatif"])
8585

86+
// Commands whose arguments are broadcast as text, never opened as local files.
87+
const DATA = new Set(["echo", "printf"])
88+
89+
// Commands whose file arguments are resolved on a remote host or inside a container,
90+
// so a local path in their argument list is not a local read.
91+
const REMOTE = new Set(["ssh", "docker", "kubectl"])
92+
93+
// Shells that evaluate a `-c` string as a nested command. They are not option-model
94+
// wrappers, but the string must be re-parsed or `sh -c 'cat /etc/hostname'` runs a read
95+
// the classifier never sees.
96+
const SHELLS = new Set(["sh", "bash", "zsh", "dash", "ksh", "ash"])
97+
8698
// Node types that become classification tokens. Expansions (`$HOME`, `${X}`,
8799
// `$(...)`) must be kept or a wrapper argument like `env -C $HOME` is dropped and
88100
// the command after it is misread as the target.
@@ -140,6 +152,11 @@ const ANSI_ESCAPES: Record<string, string> = {
140152
'"': '"',
141153
}
142154

155+
// Bash cannot round-trip an out-of-range `\U` code point (it either drops the escape or
156+
// emits raw UTF-8 bytes), so any word containing one is undecidable. This private-use
157+
// marker survives into the argument so the path scan can anchor conservatively.
158+
const UNRESOLVED = "\uE000"
159+
143160
// Bash decodes `$'…'` ANSI-C quoting (octal `\057`, hex `\x2f`, `\n`) before it runs,
144161
// so `$'\057etc\057hostname'` reads `/etc/hostname`. The raw token must not reach the
145162
// path scan intact or the file command looks argument-free.
@@ -186,7 +203,7 @@ function decodeAnsiC(text: string) {
186203
const wide = /^U([0-9A-Fa-f]{1,8})/.exec(rest)
187204
if (wide) {
188205
const code = Number.parseInt(wide[1], 16)
189-
value += String.fromCodePoint(code <= 0x10ffff ? code : 0xfffd)
206+
value += code > 0x10ffff ? UNRESOLVED : String.fromCodePoint(code)
190207
scan += wide[0].length
191208
continue
192209
}
@@ -329,85 +346,77 @@ function topComma(text: string, start: number, end: number) {
329346
return false
330347
}
331348

332-
// tree-sitter emits no `command` node for a bare brace list `{cat,/etc/hostname}`, so
333-
// the whole classifier is skipped and the command runs unprompted. Bash brace-expands
334-
// the list before execution; replacing the brace/comma characters with spaces keeps the
335-
// effective words visible to the scan. `always` is suppressed by the caller because the
336-
// offered pattern no longer matches the raw command.
337-
function flattenBraces(text: string) {
338-
let out = ""
339-
let found = false
340-
let index = 0
341-
while (index < text.length) {
342-
const char = text[index]
343-
if (char === "\\" && index + 1 < text.length) {
344-
out += char + text[index + 1]
345-
index += 2
349+
const BRACE_WORD = /[A-Za-z0-9_.\/\\~=+,'"{}@%:-]/
350+
351+
// The word a brace group is concatenated with. Quotes are part of the word
352+
// (`c'a't{,}` runs `cat`), so a scan that stops at a quote splits the command name and
353+
// loses the read.
354+
function braceWordBounds(text: string, index: number) {
355+
let start = index
356+
while (start > 0) {
357+
const char = text[start - 1]
358+
if (BRACE_WORD.test(char)) {
359+
start--
346360
continue
347361
}
348-
if (char !== "{" || (index > 0 && text[index - 1] === "$")) {
349-
out += char
350-
index++
362+
if (char === "'" || char === '"') {
363+
const open = text.lastIndexOf(char, start - 2)
364+
if (open < 0) break
365+
start = open
351366
continue
352367
}
353-
const end = braceEnd(text, index)
354-
if (end < 0 || !topComma(text, index, end)) {
355-
out += char
356-
index++
368+
break
369+
}
370+
let end = index
371+
let quote: string | undefined
372+
while (end < text.length) {
373+
const char = text[end]
374+
if (char === "\\" && end + 1 < text.length) {
375+
end += 2
357376
continue
358377
}
359-
found = true
360-
let prefixStart = index
361-
while (prefixStart > 0 && /[A-Za-z0-9_.\/\\~=+-]/.test(text[prefixStart - 1])) prefixStart--
362-
let suffixEnd = end + 1
363-
while (suffixEnd < text.length && /[A-Za-z0-9_.\/\\~=+-]/.test(text[suffixEnd])) suffixEnd++
364-
const prefix = text.slice(prefixStart, index)
365-
const suffix = text.slice(end + 1, suffixEnd)
366-
if (prefix.length > 0 || suffix.length > 0) {
367-
// Bash concatenates the surrounding word with every expanded option (`ca{t,}` runs
368-
// `cat`); emit each candidate word so the real command name and its arguments stay
369-
// visible instead of hiding behind a mangled split of the same word.
370-
out = out.slice(0, out.length - prefix.length) + " "
371-
for (const option of braceOptions(text, index, end)) out += prefix + option + suffix + " "
372-
index = suffixEnd
378+
if (quote) {
379+
end++
380+
if (char === quote) quote = undefined
373381
continue
374382
}
375-
out += " "
376-
let depth = 0
377-
let quote: "'" | '"' | undefined
378-
index++
379-
for (; index < end; index++) {
380-
const inner = text[index]
381-
if (inner === "\\" && index + 1 < end) {
382-
out += inner + text[index + 1]
383-
index++
384-
continue
385-
}
386-
if (quote) {
387-
out += inner
388-
if (inner === quote) quote = undefined
389-
continue
390-
}
391-
if (inner === "'" || inner === '"') {
392-
quote = inner
393-
out += inner
394-
continue
395-
}
396-
if (inner === "{") depth++
397-
if (inner === "}") depth--
398-
out += inner === "{" || inner === "}" || (inner === "," && depth === 0) ? " " : inner
383+
if (char === "'" || char === '"') {
384+
quote = char
385+
end++
386+
continue
387+
}
388+
if (BRACE_WORD.test(char)) {
389+
end++
390+
continue
399391
}
400-
out += " "
401-
index = end + 1
392+
break
402393
}
403-
return { text: out, found }
394+
return { start, end }
395+
}
396+
397+
function firstBrace(text: string) {
398+
for (let index = 0; index < text.length; index++) {
399+
const char = text[index]
400+
if (char === "\\") {
401+
index++
402+
continue
403+
}
404+
if (char === "$" && text[index + 1] === "{") {
405+
index++
406+
continue
407+
}
408+
if (char !== "{") continue
409+
const end = braceEnd(text, index)
410+
if (end >= 0 && topComma(text, index, end)) return index
411+
}
412+
return -1
404413
}
405414

406-
function braceOptions(text: string, start: number, end: number) {
415+
function splitBraceOptions(text: string, start: number, end: number) {
407416
const options: string[] = []
408417
let acc = ""
409418
let depth = 0
410-
let quote: "'" | '"' | undefined
419+
let quote: string | undefined
411420
for (let index = start + 1; index < end; index++) {
412421
const char = text[index]
413422
if (char === "\\" && index + 1 < end) {
@@ -438,6 +447,74 @@ function braceOptions(text: string, start: number, end: number) {
438447
return options
439448
}
440449

450+
// Bash multiplies out every brace group and concatenates it with the surrounding word,
451+
// so a group nested inside an option (`c{a{t,},}`) or a second adjacent group
452+
// (`{c,d}{at,}`) must expand too. A candidate that still contains `{…}` is not a real
453+
// command or path, so leaving it unexpanded hides the effect from the scan.
454+
function expandBraces(text: string, depth = 0): string[] {
455+
if (depth > 8) return [text]
456+
const start = firstBrace(text)
457+
if (start < 0) return [text]
458+
const end = braceEnd(text, start)
459+
if (end < 0) return [text]
460+
const heads = expandBraces(text.slice(0, start), depth + 1)
461+
const tails = expandBraces(text.slice(end + 1), depth + 1)
462+
const out: string[] = []
463+
for (const option of splitBraceOptions(text, start, end)) {
464+
for (const variant of expandBraces(option, depth + 1)) {
465+
for (const head of heads) {
466+
for (const tail of tails) out.push(head + variant + tail)
467+
}
468+
}
469+
if (out.length > 64) return [text]
470+
}
471+
return out.length > 0 ? out : [text]
472+
}
473+
474+
function flattenBraces(text: string) {
475+
let out = ""
476+
let found = false
477+
let quote: "'" | '"' | undefined
478+
let index = 0
479+
while (index < text.length) {
480+
const char = text[index]
481+
if (char === "\\" && index + 1 < text.length) {
482+
out += char + text[index + 1]
483+
index += 2
484+
continue
485+
}
486+
if (quote) {
487+
out += char
488+
if (char === quote) quote = undefined
489+
index++
490+
continue
491+
}
492+
if (char === "'" || char === '"') {
493+
quote = char
494+
out += char
495+
index++
496+
continue
497+
}
498+
if (char !== "{" || (index > 0 && text[index - 1] === "$")) {
499+
out += char
500+
index++
501+
continue
502+
}
503+
const end = braceEnd(text, index)
504+
if (end < 0 || !topComma(text, index, end)) {
505+
out += char
506+
index++
507+
continue
508+
}
509+
found = true
510+
const bounds = braceWordBounds(text, index)
511+
const words = expandBraces(text.slice(bounds.start, bounds.end))
512+
out = out.slice(0, out.length - (index - bounds.start)) + words.join(" ") + " "
513+
index = bounds.end
514+
}
515+
return { text: out, found }
516+
}
517+
441518
// Classification must see the tokens the shell actually executes. It must use the
442519
// same position-free quote/escape canonicalization the persisted-grant matcher uses
443520
// (`Wildcard.unquote`), or a command name built from quotes (`cat""`) is invisible to
@@ -629,6 +706,8 @@ const WRAPPERS: Record<string, Wrapper> = {
629706
},
630707
}
631708

709+
const SHELL_SPEC: Wrapper = { valueChars: "", valueLong: new Set(), positionals: 0, shellString: "c" }
710+
632711
type Effective = {
633712
name?: string
634713
command: Part[]
@@ -665,7 +744,11 @@ function stripWrapperOptions(parts: Part[], spec: Wrapper) {
665744
}
666745
if (!text.startsWith("-") || text === "-") break
667746
if (text.startsWith("--")) {
668-
if (text.includes("=")) {
747+
const equals = text.indexOf("=")
748+
if (equals !== -1) {
749+
// An attached `--arg-file=<path>` never reaches the space-separated path branch,
750+
// so its value must be scanned here or the read is invisible.
751+
if (spec.pathLong?.has(text.slice(0, equals))) paths.push(unquote(text.slice(equals + 1)))
669752
index++
670753
continue
671754
}
@@ -794,6 +877,18 @@ function effective(command: Part[]): Effective {
794877
parts = stripped.rest
795878
continue
796879
}
880+
if (SHELLS.has(name)) {
881+
const stripped = stripWrapperOptions(parts, SHELL_SPEC)
882+
wrapped = true
883+
if (stripped.paths?.length) extraPaths = extraPaths.concat(stripped.paths)
884+
if (stripped.unresolved) return { name: undefined, command, wrapped, unresolved: true, extraPaths }
885+
if (stripped.shell !== undefined) {
886+
return { name, command: parts, cwdTarget, evalScript: stripped.shell, wrapped: true, extraPaths }
887+
}
888+
// `sh script.sh` has no `-c` string but still reads its script argument, so keep the
889+
// shell as the effective command and let the conservative scan inspect the arguments.
890+
return { name, command: parts, cwdTarget, wrapped: true, extraPaths }
891+
}
797892
if (name === "env") {
798893
const stripped = stripEnvOptions(parts)
799894
wrapped = true
@@ -843,6 +938,13 @@ function prefix(text: string) {
843938
return text.slice(0, match.index)
844939
}
845940

941+
// `/dev/null`, the standard streams and the fd aliases are sinks/sources, not external
942+
// files: scanning them turns `cmd 2>/dev/null` — the most common redirect idiom — into a
943+
// prompt for `/dev/*`.
944+
function deviceSink(text: string) {
945+
return /^\/dev\/(null|zero|stdin|stdout|stderr|tty|fd\/[0-9]+)$/.test(text)
946+
}
947+
846948
function globAnchor(text: string) {
847949
// A leading glob has no literal prefix to resolve. Keep one placeholder segment
848950
// per glob metacharacter (so a trailing `..` traversal still cancels correctly),
@@ -1041,8 +1143,12 @@ export const ShellTool = Tool.define(
10411143
})
10421144

10431145
const argPath = Effect.fn("ShellTool.argPath")(function* (arg: string, cwd: string, ps: boolean, shell: string) {
1146+
// A word carrying an unresolvable ANSI-C escape still runs as a bash-decoded path.
1147+
// Anchor the scan conservatively instead of letting mangled text look contained.
1148+
if (!ps && arg.includes(UNRESOLVED)) return { external: true as const }
10441149
const text = expand(arg, cwd, shell)
10451150
if (!text) return
1151+
if (!ps && deviceSink(text)) return
10461152
const file = prefix(text)
10471153
// A glob at position 0 has no literal prefix, but the shell still expands it
10481154
// relative to cwd and it can traverse out of the worktree (`*/../../etc`).
@@ -1079,19 +1185,20 @@ export const ShellTool = Tool.define(
10791185
const pending: { tokens: string[]; dynamic: boolean }[] = []
10801186

10811187
const addPath = Effect.fnUntraced(function* (resolved: string | { external: true } | undefined) {
1082-
if (!resolved) return
1188+
if (!resolved) return false
10831189
// An unresolvable expansion or `~name` leaves the worktree; anchor the scan at
10841190
// the filesystem root so the external_directory prompt still fires.
10851191
if (typeof resolved !== "string") {
10861192
scan.dirs.add(path.parse(cwd).root)
1087-
return
1193+
return true
10881194
}
10891195
// Lexical containment is not enough: a symlink inside the worktree can point
10901196
// outside it. Require both the lexical path and its resolved target.
10911197
const real = FSUtil.resolveExistingFrom(cwd, resolved)
1092-
if (containsPath(resolved, instance) && containsPath(real, instance)) return
1198+
if (containsPath(resolved, instance) && containsPath(real, instance)) return false
10931199
const dir = (yield* fs.isDir(real)) ? real : path.dirname(real)
10941200
scan.dirs.add(dir)
1201+
return true
10951202
})
10961203

10971204
// A `cd`/`pushd`/`popd` whose destination is not a plain literal moves the shell
@@ -1130,7 +1237,7 @@ export const ShellTool = Tool.define(
11301237
if (info.extraPaths?.length) {
11311238
for (const item of info.extraPaths) yield* addPath(yield* argPath(item, cwd, ps, shell))
11321239
}
1133-
let conservative = false
1240+
let conservative = info.command.some((item) => item.text.includes(UNRESOLVED))
11341241
if (name && (FILES.has(name) || (shellKind === "cmd" && CMD_FILES.has(name)))) {
11351242
for (const arg of pathArgs(info.command, ps, shellKind === "cmd")) {
11361243
yield* addPath(yield* argPath(arg, cwd, ps, shell))
@@ -1142,22 +1249,26 @@ export const ShellTool = Tool.define(
11421249
for (const item of info.command.slice(1)) {
11431250
yield* addPath(yield* argPath(item.text, cwd, ps, shell))
11441251
}
1145-
} else if (
1146-
name &&
1147-
!info.wrapped &&
1148-
info.command.slice(1).some((item) => {
1149-
if (item.text.startsWith("-")) return false
1150-
const inner = bareName(commandName(item.text))
1151-
return FILES.has(inner) || (shellKind === "cmd" && CMD_FILES.has(inner))
1152-
})
1153-
) {
1154-
// An unmodelled exec wrapper (`parallel cat x`) runs the file command named in
1155-
// its arguments; scan every token and suppress `always` so the wrapper name
1156-
// cannot absorb a future read.
1252+
} else if (name && DATA.has(name)) {
1253+
// Arguments are broadcast as text (`echo cat /etc/hostname`), never opened.
1254+
} else if (name && REMOTE.has(name)) {
1255+
// The inner command runs on another host or inside a container; its file
1256+
// arguments are not local paths.
1257+
} else {
1258+
// Any command the classifier cannot model fails closed. Scan every argument
1259+
// — including the value of an `if=`/`of=` style operand — and withdraw `always`
1260+
// when an external path is present, so neither the offered grant nor a stored
1261+
// `<cmd> *` rule can absorb a future external effect. This is the class close
1262+
// for read-capable commands that are not in `FILES`.
1263+
let external = false
11571264
for (const item of info.command.slice(1)) {
1158-
yield* addPath(yield* argPath(item.text, cwd, ps, shell))
1265+
if (item.text.startsWith("-") && item.text !== "-") continue
1266+
const operand = /^[A-Za-z_][A-Za-z0-9_]*=(.*)$/.exec(item.text)
1267+
for (const target of operand ? [item.text, operand[1]] : [item.text]) {
1268+
if (yield* addPath(yield* argPath(target, cwd, ps, shell))) external = true
1269+
}
11591270
}
1160-
conservative = true
1271+
conservative = conservative || external
11611272
}
11621273
if (info.evalScript && depth < 8) {
11631274
const exit = yield* parse(info.evalScript, ps).pipe(Effect.exit)

0 commit comments

Comments
 (0)