|
1 | 1 | import { ConfirmationService } from "../utils/confirmation-service"; |
2 | 2 | import { ToolResult } from "../types/index"; |
3 | | -import { exec } from "child_process"; |
| 3 | +import { spawn } from "child_process"; |
4 | 4 | import { promisify } from "util"; |
| 5 | +import * as shellQuote from "shell-quote"; |
5 | 6 |
|
6 | | -const execAsync = promisify(exec); |
| 7 | +const promisifyFn = promisify; // kept in case other parts of this file evolve to use promisify |
7 | 8 |
|
8 | 9 | export class BashTool { |
9 | 10 | private currentDirectory: string = process.cwd(); |
10 | 11 | private confirmationService = ConfirmationService.getInstance(); |
11 | 12 |
|
| 13 | + private async runCommandSafely( |
| 14 | + command: string, |
| 15 | + options: { cwd?: string; timeout?: number; maxBuffer?: number }, |
| 16 | + ): Promise<{ stdout: string; stderr: string }> { |
| 17 | + const parsed = shellQuote.parse(command); |
| 18 | + const argv = parsed |
| 19 | + .filter(token => typeof token === "string") |
| 20 | + .map(token => token as string); |
| 21 | + |
| 22 | + if (argv.length === 0) { |
| 23 | + return { stdout: "", stderr: "" }; |
| 24 | + } |
| 25 | + |
| 26 | + const cmd = argv[0]; |
| 27 | + const args = argv.slice(1); |
| 28 | + |
| 29 | + return await new Promise((resolve, reject) => { |
| 30 | + const child = spawn(cmd, args, { |
| 31 | + cwd: options.cwd, |
| 32 | + shell: false, |
| 33 | + }); |
| 34 | + |
| 35 | + let stdout = ""; |
| 36 | + let stderr = ""; |
| 37 | + const maxBuffer = options.maxBuffer ?? 1024 * 1024; |
| 38 | + const timeout = options.timeout ?? 30000; |
| 39 | + |
| 40 | + let killedForTimeout = false; |
| 41 | + let killedForBuffer = false; |
| 42 | + |
| 43 | + const timer = setTimeout(() => { |
| 44 | + killedForTimeout = true; |
| 45 | + child.kill(); |
| 46 | + }, timeout); |
| 47 | + |
| 48 | + child.stdout?.on("data", (data: Buffer | string) => { |
| 49 | + const chunk = data.toString(); |
| 50 | + if (stdout.length + chunk.length > maxBuffer) { |
| 51 | + killedForBuffer = true; |
| 52 | + child.kill(); |
| 53 | + return; |
| 54 | + } |
| 55 | + stdout += chunk; |
| 56 | + }); |
| 57 | + |
| 58 | + child.stderr?.on("data", (data: Buffer | string) => { |
| 59 | + const chunk = data.toString(); |
| 60 | + if (stderr.length + chunk.length > maxBuffer) { |
| 61 | + killedForBuffer = true; |
| 62 | + child.kill(); |
| 63 | + return; |
| 64 | + } |
| 65 | + stderr += chunk; |
| 66 | + }); |
| 67 | + |
| 68 | + child.on("error", error => { |
| 69 | + clearTimeout(timer); |
| 70 | + reject(error); |
| 71 | + }); |
| 72 | + |
| 73 | + child.on("close", code => { |
| 74 | + clearTimeout(timer); |
| 75 | + if (killedForTimeout) { |
| 76 | + return reject(new Error("Command timed out")); |
| 77 | + } |
| 78 | + if (killedForBuffer) { |
| 79 | + return reject(new Error("Command output exceeded buffer limit")); |
| 80 | + } |
| 81 | + resolve({ stdout, stderr }); |
| 82 | + }); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
12 | 86 | async execute(command: string, timeout: number = 30000): Promise<ToolResult> { |
13 | 87 | try { |
14 | 88 | // Check if user has already accepted bash commands for this session |
@@ -53,7 +127,7 @@ export class BashTool { |
53 | 127 | } |
54 | 128 | } |
55 | 129 |
|
56 | | - const { stdout, stderr } = await execAsync(command, { |
| 130 | + const { stdout, stderr } = await this.runCommandSafely(command, { |
57 | 131 | cwd: this.currentDirectory, |
58 | 132 | timeout, |
59 | 133 | maxBuffer: 1024 * 1024, |
|
0 commit comments