* Validates bash script input for common issues * Returns error result if validation fails, null if valid
(script: string, config: ToolConfiguration)
| 496 | * Returns error result if validation fails, null if valid |
| 497 | */ |
| 498 | function validateScript(script: string, config: ToolConfiguration): BashToolResult | null { |
| 499 | // Check for empty script |
| 500 | if (!script || script.trim().length === 0) { |
| 501 | return { |
| 502 | success: false, |
| 503 | error: "Script parameter is empty. This likely indicates a malformed tool call.", |
| 504 | exitCode: -1, |
| 505 | wall_duration_ms: 0, |
| 506 | }; |
| 507 | } |
| 508 | |
| 509 | // Detect redundant cd to working directory |
| 510 | const cdPattern = /^\s*cd\s+['"]?([^'";&|]+)['"]?\s*[;&|]/; |
| 511 | const match = cdPattern.exec(script); |
| 512 | if (match) { |
| 513 | const targetPath = match[1].trim(); |
| 514 | const normalizedTarget = config.runtime.normalizePath(targetPath, config.cwd); |
| 515 | const normalizedCwd = config.runtime.normalizePath(".", config.cwd); |
| 516 | |
| 517 | if (normalizedTarget === normalizedCwd) { |
| 518 | return { |
| 519 | success: false, |
| 520 | error: `Redundant cd to working directory detected. The tool already runs in ${config.cwd} - no cd needed. Remove the 'cd ${targetPath}' prefix.`, |
| 521 | exitCode: -1, |
| 522 | wall_duration_ms: 0, |
| 523 | }; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | return null; // Valid |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Rewrite cmd.exe-style null-device redirects (e.g. `>nul`, `2>nul`) into `/dev/null`. |
no test coverage detected