UNPKG

@wonderwhy-er/desktop-commander

Version:

MCP server for terminal operations and file editing

238 lines (237 loc) • 11.9 kB
import { parse, forEachChild } from 'unbash'; import { braceWords, quotedChars, unquotedChars, MAX_BRACE_WORDS } from './brace-expansion.js'; import { posixBraceSemantics } from './posix-shell-capabilities.js'; /** * The command check's reader for POSIX shells (bash, zsh, sh, dash, ksh, * fish), with unbash, a bash parser. It finds every command the shell would * run, also on later lines and inside $( ), backticks, <( ), { } groups, * subshells, functions and if, while, for and case bodies. Words inside * quotes and heredoc bodies are text. A command name with a {a,b} brace * expansion is the first word it expands to, so {sudo,ls} is sudo, and so is * {$X,sudo} when $X is empty. zsh and * the other shells are read as bash, which covers the commands they have in * common. * * unbash doesn't stop at a syntax error: it returns what it could read and * lists the errors, each on the script it found it in ($( ) and backticks * are scripts of their own). Any error anywhere is a parse error here, so the * check never decides on a command it read only in part. */ /** Quoted and literal text is known before execution; globs depend on the matching files */ const STATIC_PARTS = new Set(['Literal', 'SingleQuoted', 'AnsiCQuoted']); /** Word parts that bash drops when they are empty and unquoted ($X, ${X}, $(cmd)) */ const VANISHING_PARTS = new Set(['SimpleExpansion', 'ParameterExpansion', 'CommandExpansion']); /** The commands that remove a function: unset -f, and zsh's unfunction, unhash -f and disable -f */ const FUNCTION_REMOVERS = new Set(['unset', 'unfunction', 'unhash', 'disable']); /** * Stand-ins for a piece known only at run time in a brace word's characters, * so that braceWords keeps the words next to it ({$X,sudo} makes $X and sudo): * one for a piece in VANISHING_PARTS, one for any other ("$X"). They start * with a NUL, which no command text holds: Node starts no command that has one. */ const DROPPED_IF_EMPTY = '\0$'; const KEPT_IF_EMPTY = '\0"'; /** * A word that brace expansion made and bash may drop: only pieces it drops * when they are empty, and letters, digits or _ after one, which bash may read * into a $X's name ({$X,su}do makes $Xdo) */ const MAY_VANISH = /^(?:\0\$\w*)+$/; /** * The commands that run, in this shell, code the reader doesn't read: eval and * zsh's emulate -c (their words), trap and mapfile or readarray -C (later), . * and source (a file), and alias (its value, where the name is used: an alias * comes before a function) */ const CODE_RUNNERS = new Set(['eval', 'emulate', 'trap', 'mapfile', 'readarray', '.', 'source', 'alias']); /** The words that run the next word as a command in this shell: builtin, command, and zsh's noglob and nocorrect */ const COMMAND_PREFIXES = new Set(['builtin', 'command', 'noglob', 'nocorrect']); /** * A word's value as bash reads it (quotes and escapes removed, $'…' decoded), * or null when part of it is known only at run time: an expansion, a command's * output, or a {a,b} brace expansion, which makes several words (the reader * expands a command name's braces with braceWords). */ function wordValue(word) { return (word.parts ?? []).every((part) => partValue(part) !== null) ? word.value : null; } /** One part's value as bash reads it, or null when it is known only at run time */ function partValue(part) { if (part.type === 'DoubleQuoted' || part.type === 'LocaleString') { if (!part.parts.every((inner) => inner.type === 'Literal')) return null; return part.parts.map((inner) => (inner.type === 'Literal' ? inner.value : '')).join(''); } return STATIC_PARTS.has(part.type) && 'value' in part ? part.value : null; } /** * A word's characters for brace expansion. Only the braces unbash reads as an * expansion are syntax. The rest of the word is text, and so are the quoted * pieces inside the braces: {"a,b",c} gives a,b and c. */ function braceChars(word) { const chars = []; for (const part of word.parts ?? []) { if (part.type !== 'BraceExpansion') { chars.push(...partChars(part)); continue; } // The braces' text as written, except the quoted or expanded pieces inside, which are parts of their own let at = part.pos; for (const inner of part.parts ?? []) { chars.push(...unquotedChars(part.text.slice(at - part.pos, inner.pos - part.pos))); chars.push(...(inner.type === 'Literal' ? unquotedChars(inner.text) : partChars(inner))); at = inner.end; } chars.push(...unquotedChars(part.text.slice(at - part.pos))); } return chars; } /** A part's characters, all text: its value, or a stand-in when it is known only at run time */ function partChars(part) { const value = partValue(part); if (value !== null) return quotedChars(value); return quotedChars(VANISHING_PARTS.has(part.type) ? DROPPED_IF_EMPTY : KEPT_IF_EMPTY); } /** * The words bash makes from a word, each with its value (null when it is known * only at run time) and whether bash may drop it, or null past braceWords' limits. * A brace expansion makes several and bash drops the empty ones: {,sudo} ls * runs sudo, {,} sudo runs sudo, and {$X,sudo} id runs sudo when $X is empty. */ function wordsMade(word, options) { if (!word.parts?.some((part) => part.type === 'BraceExpansion')) { return [{ value: wordValue(word), mayVanish: mayVanish(word) }]; } const made = braceWords(braceChars(word), options); return made && made.map((text) => ({ value: text.includes('\0') ? null : text, mayVanish: MAY_VANISH.test(text) })); } const BRACE_LIMIT_ERROR = `brace expansion makes more than ${MAX_BRACE_WORDS} words, or takes too long to read`; /** Keep inspected words aligned; only a tail after a retained command name may defer a bounded failure. */ function commandWords(words, options) { const expanded = []; for (const source of words) { const made = wordsMade(source, options); if (made === null) { if (!expanded.some((word) => !word.mayVanish)) return null; return { words: expanded, argumentError: BRACE_LIMIT_ERROR }; } expanded.push(...made.map((word) => ({ ...word, source }))); } return { words: expanded }; } /** A retained runtime word still ends name selection even when its source was already listed. */ function commandNames(expanded) { const names = []; const runtimeSources = new Set(); for (let index = 0; index < expanded.length; index++) { const name = expanded[index]; if (name.value !== null || !runtimeSources.has(name.source)) { names.push({ index, name }); if (name.value === null) runtimeSources.add(name.source); } if (!name.mayVanish) break; } return names; } /** Whether bash may drop the word entirely, so that the next word is the command's name */ function mayVanish(word) { const parts = word.parts ?? []; return parts.length > 0 && parts.every((part) => VANISHING_PARTS.has(part.type)); } /** * Whether a command may remove the function `name`: one of FUNCTION_REMOVERS * anywhere among the command's words (so also after builtin, command or * eval), followed by the name or by a word known only at run time */ function mayRemoveFunction(command, name) { const at = command.words.findIndex((word) => word !== null && FUNCTION_REMOVERS.has(word)); return at >= 0 && command.words.slice(at + 1).some((word) => word === null || word === name); } /** * Whether a command may run code the reader doesn't read, which may remove any * function: one of CODE_RUNNERS, also after COMMAND_PREFIXES (command -p eval), * or a name known only at run time ($cmd may be unset or eval) */ function mayRunUnreadCode(command) { const { words } = command; let at = 0; while (COMMAND_PREFIXES.has(words[at] ?? '')) { at++; while (words[at]?.startsWith('-')) at++; } const name = words[at]; return name === null || (name !== undefined && CODE_RUNNERS.has(name)); } /** * The caller supplies its actual selected native endpoint; no endpoint or unsupported capability * retains the legacy reading exactly. Capability capture happens on import, not after caller changes. */ export async function readPosixCommands(command, selectedShell) { const semantics = selectedShell === undefined ? 'legacy' : await posixBraceSemantics(selectedShell) ?? 'legacy'; const options = { semantics }; const script = parse(command); let parseError; const commands = []; // Positions inside backticks count from the backticks' own text, so a command // there is given the start of the backticks around it const visit = (node, backticksStart) => { if (node.type === 'Script') parseError ?? (parseError = node.errors?.[0]?.message); if (node.type === 'Command' && node.name) { const words = [node.name, ...node.args.filter((arg) => arg.type === 'Word')]; const expanded = commandWords(words, options); if (expanded === null) { parseError ?? (parseError = BRACE_LIMIT_ERROR); } else { const candidates = commandNames(expanded.words).map(({ index, name }) => { const remaining = expanded.words.slice(index); return { name: name.value ?? name.source.text, words: remaining.map((word) => word.value), written: remaining.map((word) => word.source.text), wordTexts: remaining.map((word) => word.value ?? word.source.value), start: backticksStart ?? (name.source === words[0] ? node.pos : name.source.pos), nameAtRunTime: name.value === null, ...(expanded.argumentError === undefined ? {} : { argumentError: expanded.argumentError }), }; }); const unsafePrefix = candidates.some((candidate) => candidate.nameAtRunTime || COMMAND_PREFIXES.has(candidate.words[0] ?? '') || mayRunUnreadCode(candidate)); if (expanded.argumentError !== undefined && unsafePrefix) { parseError ?? (parseError = expanded.argumentError); } else { commands.push(...candidates); } } } const inBackticks = node.type === 'CommandExpansion' && node.text.startsWith('`'); const childBackticksStart = inBackticks ? backticksStart ?? node.pos : backticksStart; forEachChild(node, (child) => visit(child, childBackticksStart)); }; visit(script, undefined); if (parseError !== undefined) return { commands: [], functions: [], parseError }; // Only the script's own statements (see CommandReading.functions), and none the // text may remove: wherever the removal is written, it may run before a call, // from a function's body. Code the reader doesn't read may remove any of them. const functions = []; const statements = commands.some((command) => command.argumentError !== undefined || mayRunUnreadCode(command)) ? [] : script.commands; for (const statement of statements) { if (statement.command.type !== 'Function' || statement.background) continue; const name = statement.command.name.value; if (!commands.some((command) => mayRemoveFunction(command, name))) { functions.push({ name, end: statement.command.end }); } } return { commands: commands.sort((a, b) => a.start - b.start), functions }; }