Anticlaw / src /tools /shell.ts
Luis Milke
chore: setup proper gitignore and remove debug garbage
5348690
import { exec } from 'child_process';
import util from 'util';
const execAsync = util.promisify(exec);
// Basic protective blocklist (Windows and Linux)
const DANGEROUS_COMMANDS = [
'rm -rf', 'format', 'diskpart', 'del /s', 'del /q',
'rd /s', 'mkfs', 'dd ', '> /dev/sda', 'chmod -R 777 /'
];
export const shellCommandTool = {
type: 'function',
function: {
name: 'execute_shell_command',
description: 'EXECUTE A TERMINAL COMMAND ON THE USERS MACHINE. \n\nCRITICAL WARNING: You have direct access to the users local operating system. You MUST NOT execute commands that could permanently destroy data, format drives, or expose the system to malicious actors. Be extremely careful. The user trusts you.',
parameters: {
type: 'object',
properties: {
command: {
type: 'string',
description: 'The exact shell command to execute in PowerShell/CMD. CRITICAL: This is a JSON string. If you are passing a complex command like "curl" with JSON body arguments, you MUST escape all interior quotes (\\\") and write the entire command on ONE LINE. Do NOT use actual multiline strings or unescaped quotes, or the tool call will fail to parse and crash silently.'
}
},
required: ['command']
}
}
};
export async function executeShellCommand(command: string): Promise<string> {
const lowerCmd = command.toLowerCase();
// Quick blocklist check
for (const blocked of DANGEROUS_COMMANDS) {
if (lowerCmd.includes(blocked)) {
return `❌ SECURITY BLOCK: The command "${command}" contains a forbidden substring ("${blocked}"). Execution denied.`;
}
}
try {
// Enforce a 30 second timeout so the bot doesn't hang forever
const { stdout, stderr } = await execAsync(command, { timeout: 30000 });
let output = '';
if (stdout) output += `[STDOUT]\n${stdout}\n`;
if (stderr) output += `[STDERR]\n${stderr}\n`;
return output.trim() || `Command executed successfully but returned no output.`;
} catch (error: any) {
return `⚠️ Command Failed:\n[ERROR]\n${error.message}\n` +
(error.stdout ? `[STDOUT]\n${error.stdout}\n` : '') +
(error.stderr ? `[STDERR]\n${error.stderr}\n` : '');
}
}