@wonderwhy-er/desktop-commander
Version:
MCP server for terminal operations and file editing
468 lines (467 loc) • 23.1 kB
JavaScript
import path from 'path';
import fs from 'fs';
import { configManager } from './config-manager.js';
import { capture } from "./utils/capture.js";
import { getShellType, isShellAvailable, programName, resolveShellPath } from './utils/shell.js';
import { sameShellTexts, shellRuns } from './utils/inline-shells.js';
import { shellScriptFiles } from './utils/shell-script-files.js';
import { readPosixCommands } from './utils/posix-commands.js';
import { readPowerShellCommands } from './utils/powershell-commands.js';
import { readCmdCommands } from './utils/cmd-commands.js';
import { withStartedPrograms } from './utils/command-wrappers.js';
import { IncompleteArgumentsError, requireCompleteArguments } from './utils/command-reading.js';
class CommandManager {
getBaseCommand(command) {
return command.split(' ')[0].toLowerCase().trim();
}
/**
* The command name of each command in a command line (chained, piped, in $(), ...).
* asTyped: each first word as typed instead of its base name, for telemetry,
* which then replaces a path whole (a base name alone can't be told from one).
*/
extractCommands(commandString, asTyped = false) {
try {
// Trim any leading/trailing whitespace
commandString = commandString.trim();
// Define command separators - these are the operators that can chain commands
const separators = [';', '&&', '||', '|', '&'];
// This will store our extracted commands
const commands = [];
// Split by common separators while preserving quotes
let inQuote = false;
let quoteChar = '';
let currentCmd = '';
let escaped = false;
for (let i = 0; i < commandString.length; i++) {
const char = commandString[i];
// Handle escape characters
if (char === '\\' && !escaped) {
escaped = true;
currentCmd += char;
continue;
}
// If this character is escaped, just add it
if (escaped) {
escaped = false;
currentCmd += char;
continue;
}
// Handle quotes (both single and double)
if ((char === '"' || char === "'") && !inQuote) {
inQuote = true;
quoteChar = char;
currentCmd += char;
continue;
}
else if (char === quoteChar && inQuote) {
inQuote = false;
quoteChar = '';
currentCmd += char;
continue;
}
// Handle $() command substitution even inside quotes (fixes blocklist bypass)
if (char === '$' && i + 1 < commandString.length && commandString[i + 1] === '(') {
const startIndex = i;
let openParens = 1;
let j = i + 2; // skip past $(
while (j < commandString.length && openParens > 0) {
if (commandString[j] === '(')
openParens++;
if (commandString[j] === ')')
openParens--;
j++;
}
if (j <= commandString.length && openParens === 0) {
const subContent = commandString.substring(i + 2, j - 1);
const subCommands = this.extractCommands(subContent, asTyped);
commands.push(...subCommands);
i = j - 1;
if (!inQuote) {
continue;
}
else {
currentCmd += commandString.substring(startIndex, j);
continue;
}
}
}
// Handle backtick command substitution even inside quotes
if (char === '`') {
const startIndex = i;
let j = i + 1;
while (j < commandString.length && commandString[j] !== '`') {
j++;
}
if (j < commandString.length) {
const subContent = commandString.substring(i + 1, j);
const subCommands = this.extractCommands(subContent, asTyped);
commands.push(...subCommands);
i = j;
if (!inQuote) {
continue;
}
else {
currentCmd += commandString.substring(startIndex, j + 1);
continue;
}
}
}
// If we're inside quotes, just add the character
if (inQuote) {
currentCmd += char;
continue;
}
// Handle subshells - if we see an opening parenthesis, we need to find its matching closing parenthesis
if (char === '(') {
// Find the matching closing parenthesis
let openParens = 1;
let j = i + 1;
while (j < commandString.length && openParens > 0) {
if (commandString[j] === '(')
openParens++;
if (commandString[j] === ')')
openParens--;
j++;
}
// Skip to after the closing parenthesis only if properly balanced
if (j <= commandString.length && openParens === 0) {
const subshellContent = commandString.substring(i + 1, j - 1);
// Recursively extract commands from the subshell
const subCommands = this.extractCommands(subshellContent, asTyped);
commands.push(...subCommands);
// Move position past the subshell
i = j - 1;
continue;
}
}
// Check for separators
let isSeparator = false;
for (const separator of separators) {
if (commandString.startsWith(separator, i)) {
// We found a separator - extract the command before it
if (currentCmd.trim()) {
const baseCommand = this.extractBaseCommand(currentCmd.trim(), asTyped);
if (baseCommand)
commands.push(baseCommand);
}
// Move past the separator
i += separator.length - 1;
currentCmd = '';
isSeparator = true;
break;
}
}
if (!isSeparator) {
currentCmd += char;
}
}
// Don't forget to add the last command
if (currentCmd.trim()) {
const baseCommand = this.extractBaseCommand(currentCmd.trim(), asTyped);
if (baseCommand)
commands.push(baseCommand);
}
// Remove duplicates and return
return [...new Set(commands)];
}
catch (error) {
// If anything goes wrong, log the error but return the basic command to not break execution
capture('server_request_error', {
error: 'Error extracting commands'
});
const baseCmd = this.extractBaseCommand(commandString, asTyped);
return baseCmd ? [baseCmd] : [];
}
}
// This extracts the actual command name from a command string (asTyped: see extractCommands)
extractBaseCommand(commandStr, asTyped = false) {
try {
// Remove environment variables (patterns like KEY=value)
const withoutEnvVars = commandStr.replace(/\w+=\S+\s*/g, '').trim();
// If nothing remains after removing env vars, return null
if (!withoutEnvVars)
return null;
// Get the first token (the command)
const tokens = withoutEnvVars.split(/\s+/);
let firstToken = null;
// Find the first valid token (skip variables)
for (let i = 0; i < tokens.length; i++) {
const token = tokens[i];
// Skip dollar-prefixed tokens (variables) but not $() command substitutions
if (token.startsWith('$') && !token.startsWith('$(')) {
continue;
}
// Check if it starts with special characters like ( that might indicate it's not a regular command
if (token[0] === '(') {
continue;
}
firstToken = token;
break;
}
// No valid command token found
if (!firstToken) {
return null;
}
// handle $() command substitution - extract the inner command
if (firstToken.startsWith('$(') && firstToken.endsWith(')')) {
const inner = firstToken.slice(2, -1).trim();
if (inner) {
const innerTokens = inner.split(/\s+/);
return (asTyped ? innerTokens[0] : path.basename(innerTokens[0])).toLowerCase();
}
return null;
}
// strip path prefix so /usr/bin/sudo gets caught as "sudo"
const baseName = asTyped ? firstToken : path.basename(firstToken);
return baseName.toLowerCase();
}
catch (error) {
capture('Error extracting base command');
return null;
}
}
/**
* Reads a command with the grammar of the shell that will run it: POSIX
* shells (bash, zsh, sh, …), PowerShell, or cmd. fish is read with the
* bash reader, and where that can't parse fish's own syntax (for … end,
* echo (date)), as main reads it: by the names extractCommands() finds.
*/
async readCommands(command, shell, options = {}) {
switch (getShellType(shell)) {
case 'powershell':
return readPowerShellCommands(command, shell);
case 'cmd':
return readCmdCommands(command, options);
}
// Resolve the actual caller selection, not the adapter's anchored launch alias.
// Only filesystem identity is read here; unknown endpoints keep the legacy reading.
let selectedEndpoint;
const selected = resolveShellPath(shell);
if (selected !== null) {
try {
selectedEndpoint = fs.realpathSync(selected);
}
catch { /* Missing identity stays unsupported. */ }
}
const reading = await readPosixCommands(command, selectedEndpoint);
// fish's name, as getShellSpawnArgs() finds it
if (reading.parseError === undefined || !path.basename(shell).toLowerCase().includes('fish'))
return reading;
const { parseError, ...read } = reading;
const names = this.extractCommands(command);
return {
...read,
commands: (names.length > 0 ? names : [this.getBaseCommand(command)])
.map((name, start) => ({
name, words: [name], written: [name], wordTexts: [name], start, nameAtRunTime: false
})),
};
}
/**
* The commands of a reading that run a program: the calls to the script's
* own functions are left out. A script can define a function with a
* program's name, such as an error helper named halt, and the shell then
* runs the function, not the program. A call is the function's when it
* comes after the definition, its name as written has no folder
* (/sbin/halt is the program), and the reader lists the function, so not
* one the text may remove. PowerShell finds a function whatever the case
* of its name, POSIX shells only by its exact name. The function's body is
* read as commands of its own, so a blocked program inside it is still
* found.
*/
withoutOwnFunctionCalls(reading, shellType) {
const sameName = (a, b) => shellType === 'powershell' ? a.toLowerCase() === b.toLowerCase() : a === b;
return reading.commands.filter((cmd) => {
const written = cmd.words[0];
if (written === null || written.includes('/') || written.includes('\\'))
return true;
return !reading.functions.some((fn) => sameName(fn.name, written) && cmd.start >= fn.end);
});
}
/**
* What hides a program from the check among the commands that run
* (CommandCheck's `unseen`), in the order the commands start:
* - a program chosen only when the command runs: a name that is a variable
* or another command's output (& $tool), or eval and Invoke-Expression,
* which run text the command builds (eval "$CMD");
* - a script file given to a shell (bash deploy.sh, or forwarded by runuser): the commands in it
* aren't in the text. A file named only when the command runs ("$SCRIPT")
* counts as a program chosen at run time, named by the shell.
*/
findUnseen(commands, shellType) {
const unseen = [];
for (const cmd of commands) {
if (cmd.nameAtRunTime) {
unseen.push({ what: cmd.name, inFile: false });
continue;
}
const files = this.scriptFiles(cmd, shellType);
unseen.push(...files);
const run = shellRuns(cmd, shellType);
// A text-program shell can identify a script that the ordinary file reader does not handle.
if (files.length === 0 && run.shell !== undefined && typeof run.file === 'string') {
unseen.push({ what: run.file, inFile: true });
}
const unknown = sameShellTexts(cmd, shellType).file === null || run.file === null;
if (unknown && !unseen.some((item) => item.what === cmd.name && !item.inFile)) {
unseen.push({ what: cmd.name, inFile: false });
}
}
return unseen;
}
/**
* What hides the program when a command gives a shell a script or startup file to run
* (bash deploy.sh, pwsh -File deploy.ps1, source deploy.sh), or when
* PowerShell runs one named as the command (./deploy.ps1): the file, or the
* shell when the file is named only when the command runs ("$SCRIPT").
* A shell's command text or standard input adds no file. An explicit Bash
* startup file can still run beside command text in interactive mode.
*/
scriptFiles(cmd, shellType) {
const program = programName(cmd.name, shellType);
const args = cmd.words.slice(1);
const hidden = (file) => {
return file === null ? { what: cmd.name, inFile: false } : { what: file, inFile: true };
};
if (shellType === 'posix' && (program === 'source' || program === '.')) {
requireCompleteArguments(cmd);
return args[0] === undefined ? [] : [hidden(args[0])];
}
// PowerShell runs a .ps1 named as the command (./deploy.ps1, & ./deploy.ps1) as a script file
if (shellType === 'powershell' && /\.ps1$/i.test(cmd.name)) {
requireCompleteArguments(cmd);
return [hidden(cmd.name)];
}
// PowerShell text and file modes use the same host-option readings.
if (getShellType(cmd.name) === 'powershell') {
const file = shellRuns(cmd, shellType).file;
return file === undefined ? [] : [hidden(file)];
}
return shellScriptFiles(program, args, {
written: cmd.written.slice(1),
...(cmd.argumentError === undefined ? {} : { argumentError: cmd.argumentError }),
}).map(hidden);
}
/**
* The check's answer for `command` run by `shell`:
* - allowed: no program it runs is blocked.
* - blocked: a program it runs is in blockedCommands, both compared in the
* form programName() gives; the answer names the entry as written (a call
* to the script's own function is not the program: see withoutOwnFunctionCalls).
* - unseen: allowed, but a program is chosen only when the command runs or
* is in a script file (see findUnseen), so start_process can say the
* blocklist couldn't check it.
* - failed: the command or the settings can't be read. In PowerShell a parse
* error or a missing PowerShell is allowed instead: nothing runs, and
* PowerShell or start_process answers.
*/
checkCommand(command, shell, options = {}) {
return this.checkText(command, shell, new Map(), options);
}
/**
* checkCommand() for a command, or for a command text in it: each text is
* checked once per command in each shell, however many readings give it.
* In pwsh $a pwsh $a … each text gives every later one again, so without
* this the work doubled with each pair.
*/
checkText(command, shell, checked, options) {
const optionsForShell = {
cmdDelayedExpansion: getShellType(shell) === 'cmd' ? options.cmdDelayedExpansion ?? 'unknown' : 'unknown',
};
const key = `${shell}\0${optionsForShell.cmdDelayedExpansion}\0${command}`;
let check = checked.get(key);
if (check === undefined) {
check = this.checkOnce(command, shell, checked, optionsForShell);
checked.set(key, check);
}
return check;
}
async checkOnce(command, shell, checked, options) {
try {
const config = await configManager.getConfig();
const blockedCommands = config.blockedCommands || [];
const shellType = getShellType(shell);
// PowerShell runs nothing when it isn't there or can't parse the command: it, or start_process, answers
if (shellType === 'powershell' && !isShellAvailable(shell))
return { verdict: 'allowed' };
const { cmdDelayedExpansion } = options;
const reading = await this.readCommands(command, shell, options);
// A parse error is the command's fault, not the server's, so it isn't logged or sent
if (reading.parseError !== undefined) {
return shellType === 'powershell'
? { verdict: 'allowed' }
: { verdict: 'failed', reason: reading.parseError };
}
const running = withStartedPrograms(this.withoutOwnFunctionCalls(reading, shellType), shell);
// set_config_value checks only that the list is an array
const entries = blockedCommands.filter((entry) => typeof entry === 'string');
for (const found of running) {
// A name known only at run time ($tool) names no program the list could match
if (found.nameAtRunTime)
continue;
const program = programName(found.name, shellType);
const entry = entries.find((candidate) => programName(candidate, shellType) === program);
if (entry !== undefined)
return { verdict: 'blocked', name: entry };
}
const unseen = [];
let unreadable;
for (const found of running) {
unseen.push(...this.findUnseen([found], shellType));
const run = shellRuns(found, shellType);
const texts = [
...sameShellTexts(found, shellType).texts.map((text) => ({
text,
runs: shell,
cmdDelayedExpansion,
})),
// su -c's text runs in sh, runas's command line as cmd reads it
...run.texts.map((text) => ({
text,
runs: run.shell ?? found.name,
cmdDelayedExpansion: run.cmdDelayedExpansion ?? 'unknown',
})),
];
for (const { text, runs, cmdDelayedExpansion: innerMode } of texts) {
// The shell that runs the command looks a PowerShell's name up itself, in its working folder
// too: one the check can't find is read by the PowerShell running the command, when that is one
const reader = getShellType(runs) !== 'powershell' ? runs
: [runs, shell].find((candidate) => getShellType(candidate) === 'powershell' && isShellAvailable(candidate));
const inner = reader === undefined
? { verdict: 'failed', reason: `${runs} isn't installed here, so the command given to it can't be read` }
: await this.checkText(text, reader, checked, { cmdDelayedExpansion: innerMode });
if (inner.verdict === 'blocked')
return inner;
if (inner.verdict === 'failed')
unreadable ?? (unreadable = inner);
if (inner.verdict === 'unseen')
unseen.push(...inner.unseen);
}
}
if (unreadable)
return unreadable;
return unseen.length > 0 ? { verdict: 'unseen', unseen } : { verdict: 'allowed' };
}
catch (error) {
if (error instanceof IncompleteArgumentsError)
return { verdict: 'failed', reason: error.message };
const reason = error instanceof Error ? error.message : String(error);
console.error('Error validating command:', error);
capture('server_validate_command_error', {
error: reason
});
// Fail closed: deny the command if validation encounters an error.
// This prevents a config read failure from bypassing all command filtering.
return { verdict: 'failed', reason };
}
}
/** Whether a check's answer refuses the command */
isRefused(check) {
return check.verdict === 'blocked' || check.verdict === 'failed';
}
/** Whether the command may run in `shell`: the check doesn't refuse it */
async validateCommand(command, shell) {
return !this.isRefused(await this.checkCommand(command, shell));
}
}
export const commandManager = new CommandManager();