@@ -42,6 +42,18 @@ const FILES = new Set([ |
42 | 42 | "chmod", |
43 | 43 | "chown", |
44 | 44 | "cat", |
| 45 | + "head", |
| 46 | + "tail", |
| 47 | + "grep", |
| 48 | + "egrep", |
| 49 | + "fgrep", |
| 50 | + "awk", |
| 51 | + "gawk", |
| 52 | + "sed", |
| 53 | + "sort", |
| 54 | + "uniq", |
| 55 | + "source", |
| 56 | + ".", |
45 | 57 | // Leave PowerShell aliases out for now. Common ones like cat/cp/mv/rm/mkdir |
46 | 58 | // already hit the entries above, and alias normalization should happen in one |
47 | 59 | // place later so we do not risk double-prompting. |
@@ -171,6 +183,13 @@ function decodeAnsiC(text: string) { |
171 | 183 | scan += unicode[0].length |
172 | 184 | continue |
173 | 185 | } |
| 186 | + const wide = /^U([0-9A-Fa-f]{1,8})/.exec(rest) |
| 187 | + if (wide) { |
| 188 | + const code = Number.parseInt(wide[1], 16) |
| 189 | + value += String.fromCodePoint(code <= 0x10ffff ? code : 0xfffd) |
| 190 | + scan += wide[0].length |
| 191 | + continue |
| 192 | + } |
174 | 193 | const next = rest[0] |
175 | 194 | if (next === undefined) break |
176 | 195 | value += ANSI_ESCAPES[next] ?? next |
@@ -338,6 +357,21 @@ function flattenBraces(text: string) { |
338 | 357 | continue |
339 | 358 | } |
340 | 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 |
| 373 | + continue |
| 374 | + } |
341 | 375 | out += " " |
342 | 376 | let depth = 0 |
343 | 377 | let quote: "'" | '"' | undefined |
@@ -369,6 +403,41 @@ function flattenBraces(text: string) { |
369 | 403 | return { text: out, found } |
370 | 404 | } |
371 | 405 |
|
| 406 | +function braceOptions(text: string, start: number, end: number) { |
| 407 | + const options: string[] = [] |
| 408 | + let acc = "" |
| 409 | + let depth = 0 |
| 410 | + let quote: "'" | '"' | undefined |
| 411 | + for (let index = start + 1; index < end; index++) { |
| 412 | + const char = text[index] |
| 413 | + if (char === "\\" && index + 1 < end) { |
| 414 | + acc += char + text[index + 1] |
| 415 | + index++ |
| 416 | + continue |
| 417 | + } |
| 418 | + if (quote) { |
| 419 | + acc += char |
| 420 | + if (char === quote) quote = undefined |
| 421 | + continue |
| 422 | + } |
| 423 | + if (char === "'" || char === '"') { |
| 424 | + quote = char |
| 425 | + acc += char |
| 426 | + continue |
| 427 | + } |
| 428 | + if (char === "{") depth++ |
| 429 | + if (char === "}") depth-- |
| 430 | + if (char === "," && depth === 0) { |
| 431 | + options.push(acc) |
| 432 | + acc = "" |
| 433 | + continue |
| 434 | + } |
| 435 | + acc += char |
| 436 | + } |
| 437 | + options.push(acc) |
| 438 | + return options |
| 439 | +} |
| 440 | + |
372 | 441 | // Classification must see the tokens the shell actually executes. It must use the |
373 | 442 | // same position-free quote/escape canonicalization the persisted-grant matcher uses |
374 | 443 | // (`Wildcard.unquote`), or a command name built from quotes (`cat""`) is invisible to |
@@ -484,6 +553,10 @@ type Wrapper = { |
484 | 553 | valueLong: Set<string> |
485 | 554 | positionals: number |
486 | 555 | shellString?: string |
| 556 | + // Options that consume an input file path (`xargs -a`, `parallel --arg-file`) must be |
| 557 | + // scanned: a stored wrapper grant would otherwise absorb that read. |
| 558 | + pathChars?: string |
| 559 | + pathLong?: Set<string> |
487 | 560 | } |
488 | 561 |
|
489 | 562 | const WRAPPERS: Record<string, Wrapper> = { |
@@ -496,6 +569,7 @@ const WRAPPERS: Record<string, Wrapper> = { |
496 | 569 | stdbuf: { valueChars: "ioe", valueLong: new Set(["--input", "--output", "--error"]), positionals: 0 }, |
497 | 570 | xargs: { |
498 | 571 | valueChars: "InPsaEdL", |
| 572 | + pathChars: "a", |
499 | 573 | valueLong: new Set([ |
500 | 574 | "--arg-file", |
501 | 575 | "--delimiter", |
@@ -507,6 +581,7 @@ const WRAPPERS: Record<string, Wrapper> = { |
507 | 581 | "--process-slot-var", |
508 | 582 | "--replace", |
509 | 583 | ]), |
| 584 | + pathLong: new Set(["--arg-file"]), |
510 | 585 | positionals: 0, |
511 | 586 | }, |
512 | 587 | watch: { valueChars: "n", valueLong: new Set(["--interval"]), positionals: 0 }, |
@@ -535,6 +610,23 @@ const WRAPPERS: Record<string, Wrapper> = { |
535 | 610 | }, |
536 | 611 | doas: { valueChars: "u", valueLong: new Set(), positionals: 0 }, |
537 | 612 | script: { valueChars: "", valueLong: new Set(), positionals: 0, shellString: "c" }, |
| 613 | + parallel: { |
| 614 | + valueChars: "ajNS", |
| 615 | + pathChars: "a", |
| 616 | + valueLong: new Set(["--jobs", "--max-args", "--max-lines", "--arg-file", "--files"]), |
| 617 | + pathLong: new Set(["--arg-file"]), |
| 618 | + positionals: 0, |
| 619 | + }, |
| 620 | + busybox: { valueChars: "", valueLong: new Set(), positionals: 0 }, |
| 621 | + fakeroot: { valueChars: "", valueLong: new Set(), positionals: 0 }, |
| 622 | + unshare: { valueChars: "", valueLong: new Set(), positionals: 0 }, |
| 623 | + "ssh-agent": { valueChars: "at", valueLong: new Set(), positionals: 0 }, |
| 624 | + "dbus-run-session": { valueChars: "", valueLong: new Set(), positionals: 0 }, |
| 625 | + "systemd-inhibit": { |
| 626 | + valueChars: "", |
| 627 | + valueLong: new Set(["--what", "--who", "--why"]), |
| 628 | + positionals: 0, |
| 629 | + }, |
538 | 630 | } |
539 | 631 |
|
540 | 632 | type Effective = { |
@@ -544,17 +636,26 @@ type Effective = { |
544 | 636 | evalScript?: string |
545 | 637 | wrapped?: boolean |
546 | 638 | unresolved?: boolean |
| 639 | + extraPaths?: string[] |
547 | 640 | } |
548 | 641 |
|
549 | 642 | function commandName(text: string) { |
550 | 643 | const name = Wildcard.unquote(text) |
551 | 644 | return name.startsWith("\\") ? name.slice(1) : name |
552 | 645 | } |
553 | 646 |
|
| 647 | +// A path-qualified command (`/usr/bin/cat`, `.\bin\type`) runs the same binary as its |
| 648 | +// bare name; lookups must normalize so an absolute path cannot dodge the FILES scan. |
| 649 | +function bareName(text: string) { |
| 650 | + const slash = Math.max(text.lastIndexOf("/"), text.lastIndexOf("\\")) |
| 651 | + return slash === -1 ? text : text.slice(slash + 1) |
| 652 | +} |
| 653 | + |
554 | 654 | // Strip one wrapper's options and positional prefixes so the next token is the command |
555 | 655 | // it runs. An option shape the spec does not model returns `unresolved` instead of |
556 | 656 | // guessing: a wrong guess would skip the real file command and hide the read. |
557 | 657 | function stripWrapperOptions(parts: Part[], spec: Wrapper) { |
| 658 | + const paths: string[] = [] |
558 | 659 | let index = 1 |
559 | 660 | while (index < parts.length) { |
560 | 661 | const text = parts[index].text |
@@ -569,14 +670,18 @@ function stripWrapperOptions(parts: Part[], spec: Wrapper) { |
569 | 670 | continue |
570 | 671 | } |
571 | 672 | if (spec.valueLong.has(text)) { |
| 673 | + if (spec.pathLong?.has(text)) { |
| 674 | + const value = parts[index + 1]?.text |
| 675 | + if (value !== undefined) paths.push(unquote(value)) |
| 676 | + } |
572 | 677 | index += 2 |
573 | 678 | continue |
574 | 679 | } |
575 | 680 | if (spec.shellString && text === "--command") { |
576 | 681 | const value = parts[index + 1]?.text |
577 | | - return { rest: parts.slice(index + 2), shell: value === undefined ? "" : unquote(value), wrapped: true } |
| 682 | + return { rest: parts.slice(index + 2), shell: value === undefined ? "" : unquote(value), wrapped: true, paths } |
578 | 683 | } |
579 | | - return { rest: parts, wrapped: true, unresolved: true } |
| 684 | + return { rest: parts, wrapped: true, unresolved: true, paths } |
580 | 685 | } |
581 | 686 | const body = text.slice(1) |
582 | 687 | let consumed = false |
@@ -589,18 +694,24 @@ function stripWrapperOptions(parts: Part[], spec: Wrapper) { |
589 | 694 | rest: parts.slice(attached ? index + 1 : index + 2), |
590 | 695 | shell: value === undefined ? "" : unquote(value), |
591 | 696 | wrapped: true, |
| 697 | + paths, |
592 | 698 | } |
593 | 699 | } |
594 | 700 | if (!spec.valueChars.includes(flag)) continue |
595 | 701 | consumed = true |
| 702 | + if (spec.pathChars?.includes(flag)) { |
| 703 | + const attached = body.slice(position + 1) |
| 704 | + const value = attached || parts[index + 1]?.text |
| 705 | + if (value !== undefined) paths.push(unquote(value)) |
| 706 | + } |
596 | 707 | index += position === body.length - 1 ? 2 : 1 |
597 | 708 | break |
598 | 709 | } |
599 | 710 | if (!consumed) index += 1 |
600 | 711 | } |
601 | 712 | let rest = parts.slice(index) |
602 | 713 | for (let skip = 0; skip < spec.positionals && rest.length > 0; skip++) rest = rest.slice(1) |
603 | | - return { rest, wrapped: true } |
| 714 | + return { rest, wrapped: true, paths } |
604 | 715 | } |
605 | 716 |
|
606 | 717 | function stripEnvOptions(parts: Part[]) { |
@@ -670,12 +781,14 @@ function effective(command: Part[]): Effective { |
670 | 781 | let cwdTarget: Part | undefined |
671 | 782 | let evalScript: string | undefined |
672 | 783 | let wrapped = false |
| 784 | + let extraPaths: string[] = [] |
673 | 785 | for (let depth = 0; depth < 64 && parts.length > 0; depth++) { |
674 | | - const name = commandName(parts[0].text) |
| 786 | + const name = bareName(commandName(parts[0].text)) |
675 | 787 | const spec = WRAPPERS[name] |
676 | 788 | if (spec) { |
677 | 789 | const stripped = stripWrapperOptions(parts, spec) |
678 | 790 | wrapped = true |
| 791 | + if (stripped.paths?.length) extraPaths = extraPaths.concat(stripped.paths) |
679 | 792 | if (stripped.unresolved) return { name: undefined, command, wrapped, unresolved: true } |
680 | 793 | if (stripped.shell !== undefined) evalScript = stripped.shell |
681 | 794 | parts = stripped.rest |
@@ -695,14 +808,21 @@ function effective(command: Part[]): Effective { |
695 | 808 | .map((item) => Wildcard.unquote(item.text)) |
696 | 809 | .join(" ") |
697 | 810 | .trim() |
698 | | - return { name, command: parts, cwdTarget, evalScript: script, wrapped: true } |
| 811 | + return { name, command: parts, cwdTarget, evalScript: script, wrapped: true, extraPaths } |
699 | 812 | } |
700 | 813 | break |
701 | 814 | } |
702 | | - if (parts.length > 0 && WRAPPERS[commandName(parts[0].text)]) { |
703 | | - return { name: undefined, command, wrapped, unresolved: true } |
| 815 | + if (parts.length > 0 && WRAPPERS[bareName(commandName(parts[0].text))]) { |
| 816 | + return { name: undefined, command, wrapped, unresolved: true, extraPaths } |
| 817 | + } |
| 818 | + return { |
| 819 | + name: parts.length > 0 ? bareName(commandName(parts[0].text)) : undefined, |
| 820 | + command: parts, |
| 821 | + cwdTarget, |
| 822 | + evalScript, |
| 823 | + wrapped, |
| 824 | + extraPaths, |
704 | 825 | } |
705 | | - return { name: parts.length > 0 ? commandName(parts[0].text) : undefined, command: parts, cwdTarget, evalScript, wrapped } |
706 | 826 | } |
707 | 827 |
|
708 | 828 | // A dynamic argument cannot be resolved to a real path, but if it still carries a |
@@ -990,7 +1110,7 @@ export const ShellTool = Tool.define( |
990 | 1110 | const classify: ( |
991 | 1111 | command: Part[], |
992 | 1112 | depth?: number, |
993 | | - ) => Effect.Effect<{ name?: string; info: Effective }, never, never> = Effect.fnUntraced(function* ( |
| 1113 | + ) => Effect.Effect<{ name?: string; info: Effective; conservative?: boolean }, never, never> = Effect.fnUntraced(function* ( |
994 | 1114 | command: Part[], |
995 | 1115 | depth = 0, |
996 | 1116 | ) { |
@@ -1007,6 +1127,10 @@ export const ShellTool = Tool.define( |
1007 | 1127 | ) |
1008 | 1128 | if (info.cwdTarget && dynamic(unquote(info.cwdTarget.text), ps)) state.dynamic = true |
1009 | 1129 | if (info.cwdTarget) yield* addPath(yield* argPath(info.cwdTarget.text, cwd, ps, shell)) |
| 1130 | + if (info.extraPaths?.length) { |
| 1131 | + for (const item of info.extraPaths) yield* addPath(yield* argPath(item, cwd, ps, shell)) |
| 1132 | + } |
| 1133 | + let conservative = false |
1010 | 1134 | if (name && (FILES.has(name) || (shellKind === "cmd" && CMD_FILES.has(name)))) { |
1011 | 1135 | for (const arg of pathArgs(info.command, ps, shellKind === "cmd")) { |
1012 | 1136 | yield* addPath(yield* argPath(arg, cwd, ps, shell)) |
@@ -1018,6 +1142,22 @@ export const ShellTool = Tool.define( |
1018 | 1142 | for (const item of info.command.slice(1)) { |
1019 | 1143 | yield* addPath(yield* argPath(item.text, cwd, ps, shell)) |
1020 | 1144 | } |
| 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. |
| 1157 | + for (const item of info.command.slice(1)) { |
| 1158 | + yield* addPath(yield* argPath(item.text, cwd, ps, shell)) |
| 1159 | + } |
| 1160 | + conservative = true |
1021 | 1161 | } |
1022 | 1162 | if (info.evalScript && depth < 8) { |
1023 | 1163 | const exit = yield* parse(info.evalScript, ps).pipe(Effect.exit) |
@@ -1035,14 +1175,23 @@ export const ShellTool = Tool.define( |
1035 | 1175 | scan.dirs.add(path.parse(cwd).root) |
1036 | 1176 | } |
1037 | 1177 | } |
1038 | | - return { name, info } |
| 1178 | + return { name, info, conservative } |
1039 | 1179 | }) |
1040 | 1180 |
|
1041 | 1181 | const entries = commands(root).map((node) => { |
1042 | 1182 | const command = parts(node) |
1043 | 1183 | return { node, command, tokens: command.map((item) => item.text) } |
1044 | 1184 | }) |
1045 | 1185 |
|
| 1186 | + // Redirection targets (`cat < /etc/hostname`, `cmd > /tmp/out`) are dropped from |
| 1187 | + // the token list, so a stored grant could absorb that read or write; scan each |
| 1188 | + // target path through the same resolution as other arguments. |
| 1189 | + for (const redirect of root.descendantsOfType("file_redirect")) { |
| 1190 | + if (!redirect) continue |
| 1191 | + const destination = redirect.childForFieldName("destination") |
| 1192 | + if (destination) yield* addPath(yield* argPath(destination.text, cwd, ps, shell)) |
| 1193 | + } |
| 1194 | + |
1046 | 1195 | // A non-empty command the parser could not turn into a `command` node (a bare |
1047 | 1196 | // brace list, any future parse blind spot) must not run unprompted. Anchor the |
1048 | 1197 | // scan at the filesystem root and prompt; `always` stays empty. |
@@ -1055,7 +1204,7 @@ export const ShellTool = Tool.define( |
1055 | 1204 | // Classify every command before deciding the always-grant, so a dynamic `cd` |
1056 | 1205 | // anywhere in the invocation (including through a wrapper or `eval`) suppresses |
1057 | 1206 | // it for all of them, not just commands that happen to parse after it. |
1058 | | - const resolved: { name?: string; info: Effective }[] = [] |
| 1207 | + const resolved: { name?: string; info: Effective; conservative?: boolean }[] = [] |
1059 | 1208 | for (const entry of entries) resolved.push(yield* classify(entry.command)) |
1060 | 1209 |
|
1061 | 1210 | for (let index = 0; index < entries.length; index++) { |
@@ -1074,7 +1223,10 @@ export const ShellTool = Tool.define( |
1074 | 1223 | dynamic: |
1075 | 1224 | entry.command.some((item) => dynamic(item.text, ps)) || |
1076 | 1225 | result.info.wrapped === true || |
1077 | | - result.info.unresolved === true, |
| 1226 | + result.info.unresolved === true || |
| 1227 | + result.conservative === true || |
| 1228 | + (result.info.extraPaths?.length ?? 0) > 0 || |
| 1229 | + /[\\/]/.test(entry.tokens[0] ?? ""), |
1078 | 1230 | }) |
1079 | 1231 | } |
1080 | 1232 |
|
|