MCPcopy
hub / github.com/coder/mux / validateScript

Function validateScript

src/node/services/tools/bash.ts:498–528  ·  view source on GitHub ↗

* Validates bash script input for common issues * Returns error result if validation fails, null if valid

(script: string, config: ToolConfiguration)

Source from the content-addressed store, hash-verified

496 * Returns error result if validation fails, null if valid
497 */
498function 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`.

Callers 1

createBashToolFunction · 0.85

Calls 2

execMethod · 0.65
normalizePathMethod · 0.65

Tested by

no test coverage detected