|
| 1 | +# Security Audit Command for Code Search MCP |
| 2 | + |
| 3 | +You are performing a comprehensive security audit of the Code Search MCP server - a Node.js application that provides code search capabilities via the Model Context Protocol. This is a security-sensitive tool that accepts path inputs and spawns external processes. |
| 4 | + |
| 5 | +## Project Context |
| 6 | + |
| 7 | +This project: |
| 8 | +- Is a Node.js MCP server accepting workspace paths from external clients |
| 9 | +- Spawns external processes (ctags, ripgrep) via `child_process.execFile` |
| 10 | +- Writes temporary files to user-controlled directories |
| 11 | +- Implements an allow-list-based workspace access control system |
| 12 | +- Caches symbol indices to disk in `~/.code-search-mcp-cache/` |
| 13 | + |
| 14 | +## Phase 1: Parallel Exploration |
| 15 | + |
| 16 | +Launch multiple Explore agents in parallel to review different focused security areas: |
| 17 | + |
| 18 | +### Agent 1: Path Traversal & Workspace Access Control |
| 19 | +Focus: `src/utils/workspace-path.ts`, `src/mcp/server.ts` |
| 20 | +- Check for path traversal bypasses in `validateAllowedPath()` |
| 21 | +- Look for symlink-based escape vulnerabilities |
| 22 | +- Verify Windows vs Unix path separator handling |
| 23 | +- Check if `..` or path separator edge cases are handled |
| 24 | +- Look for cases where workspace validation is bypassed |
| 25 | + |
| 26 | +### Agent 2: Command Injection & Process Spawning |
| 27 | +Focus: `src/symbol-search/ctags-integration.ts`, `src/symbol-search/text-search-service.ts`, `src/ast-search/` |
| 28 | +- Verify all `execFile` calls use safe argument passing |
| 29 | +- Check if workspace paths passed as `cwd` are properly validated |
| 30 | +- Look for shell command injection via pattern/regex inputs |
| 31 | +- Verify ripgrep glob patterns can't escape the workspace |
| 32 | + |
| 33 | +### Agent 3: Temporary File & Symlink Attacks |
| 34 | +Focus: `src/symbol-search/ctags-integration.ts`, `src/cache/cache-manager.ts` |
| 35 | +- Check `.code-search-tags` file creation for symlink vulnerabilities |
| 36 | +- Verify cache directory creation is safe from TOCTOU races |
| 37 | +- Check if temporary files use secure permissions |
| 38 | +- Look for arbitrary file write vulnerabilities |
| 39 | + |
| 40 | +### Agent 4: Input Validation & Injection |
| 41 | +Focus: `src/mcp/server.ts` - all tool handlers |
| 42 | +- Verify all MCP tool inputs are validated before use |
| 43 | +- Check regex/pattern injection points (ripgrep, AST search) |
| 44 | +- Verify glob pattern sanitization in file/text search |
| 45 | +- Look for unsafe JSON parsing |
| 46 | + |
| 47 | +### Agent 5: Cache Security & Information Disclosure |
| 48 | +Focus: `src/cache/cache-manager.ts` |
| 49 | +- Check if cached data contains sensitive file contents |
| 50 | +- Verify cache files are not world-readable |
| 51 | +- Check for information disclosure in error messages |
| 52 | +- Look for workspace path leakage in responses |
| 53 | + |
| 54 | +### Agent 6: Dependency Vulnerabilities |
| 55 | +Focus: `package.json`, all `src/dependency-analysis/parsers/*.ts` |
| 56 | +- Check for known vulnerabilities in dependencies |
| 57 | +- Verify dependency manifests are parsed safely |
| 58 | +- Look for malicious package detection capabilities |
| 59 | +- Check if `analyze_dependencies` has network exposure |
| 60 | + |
| 61 | +### Agent 7: Denial of Service & Resource Limits |
| 62 | +Focus: All services |
| 63 | +- Check for missing timeout constraints on operations |
| 64 | +- Verify search result limits are enforced |
| 65 | +- Look for memory exhaustion via large file inputs |
| 66 | +- Check if unbounded loops exist in parsers |
| 67 | + |
| 68 | +### Agent 8: Access Control Bypasses |
| 69 | +Focus: `src/mcp/server.ts`, `src/utils/workspace-path.ts` |
| 70 | +- Verify all file operations go through workspace validation |
| 71 | +- Check for direct file reads bypassing `validateAllowedPath()` |
| 72 | +- Look for cases where `normalizeSearchPathFilters` can be bypassed |
| 73 | +- Verify cache operations can't access arbitrary workspaces |
| 74 | + |
| 75 | +## Phase 2: Collect and Analyze |
| 76 | + |
| 77 | +Wait for all agents to complete. Organize findings by severity: |
| 78 | +- **Critical**: Path traversal, arbitrary file read/write, command execution |
| 79 | +- **High**: Symlink attacks, significant DoS vectors, information disclosure |
| 80 | +- **Medium**: DoS resource exhaustion, minor injection risks |
| 81 | +- **Low**: Best practice violations, minor issues |
| 82 | +- **Info**: Security considerations |
| 83 | + |
| 84 | +For each finding, gather: |
| 85 | +- File path and line number |
| 86 | +- Vulnerability type (e.g., CWE-22, CWE-78, CWE-20) |
| 87 | +- Severity level |
| 88 | +- Brief description with exploit scenario |
| 89 | +- Recommended fix with code snippet |
| 90 | + |
| 91 | +## Phase 3: Present Results |
| 92 | + |
| 93 | +If NO issues are found: |
| 94 | +``` |
| 95 | +╔══════════════════════════════════════════════════════════════╗ |
| 96 | +║ SECURITY AUDIT PASSED ║ |
| 97 | +║ ║ |
| 98 | +║ No critical security issues detected in code-search-mcp. ║ |
| 99 | +╚══════════════════════════════════════════════════════════════╝ |
| 100 | +``` |
| 101 | + |
| 102 | +If issues are found, present an ASCII table: |
| 103 | +``` |
| 104 | +╔══════════════════╤════════════════════════════╤═══════════════════════════╤════════════╗ |
| 105 | +║ Severity │ Issue Type (CWE) │ Location │ Description ║ |
| 106 | +╠══════════════════╪════════════════════════════╪═══════════════════════════╪════════════╣ |
| 107 | +║ CRITICAL │ Path Traversal (CWE-22) │ workspace-path.ts:68 │ Symlink ║ |
| 108 | +║ │ │ │ bypass via ║ |
| 109 | +║ │ │ │ junction ║ |
| 110 | +╠══════════════════╪════════════════════════════╪═══════════════════════════╪════════════╣ |
| 111 | +║ HIGH │ Symlink Attack (CWE-59) │ ctags-integration.ts:19 │ .code- ║ |
| 112 | +║ │ │ │ search-tags ║ |
| 113 | +║ │ │ │ link target ║ |
| 114 | +╚══════════════════╧════════════════════════════╧═══════════════════════════╧════════════╝ |
| 115 | +``` |
| 116 | + |
| 117 | +## Phase 4: Remediation Planning |
| 118 | + |
| 119 | +After presenting findings, gather fix details ahead of time, then use AskUserQuestion to confirm: |
| 120 | + |
| 121 | +1. **Fix scope**: Critical only? Critical+High? All issues? |
| 122 | +2. **Fix approach**: Implement fixes directly, create PR, or review together? |
| 123 | +3. **Testing**: Add security tests? Verify existing tests pass? |
| 124 | + |
| 125 | +Proceed with implementation based on user responses. |
| 126 | + |
| 127 | +## Key Security Considerations for This Project |
| 128 | + |
| 129 | +1. **Path Validation is Critical**: This tool's primary security boundary is `validateAllowedPath()`. Any bypass allows reading arbitrary files. |
| 130 | + |
| 131 | +2. **Process Spinning**: Every `execFile` call with user-controlled `cwd` is a potential vulnerability. |
| 132 | + |
| 133 | +3. **MCP Protocol**: The server accepts input from external MCP clients - assume all input is hostile. |
| 134 | + |
| 135 | +4. **Temporary Files**: Files written to user-controlled directories are symlink attack targets. |
| 136 | + |
| 137 | +5. **Workspace Enumeration**: Error messages should not leak valid workspace paths. |
0 commit comments