This document details critical security vulnerabilities discovered in the Deep Code Reasoning MCP server and the comprehensive fixes implemented to address them. The analysis was conducted using a collaborative approach between Claude and the Gemini-powered deep reasoning service, demonstrating the very capabilities this tool provides.
Severity: Critical
Location: src/utils/CodeReader.ts:46
Description: The CodeReader
class performs no validation on file paths, allowing attackers to read any file on the host system that the process has access to.
// VULNERABLE CODE
const content = await fs.readFile(filePath, 'utf-8');
Attack Vector: An attacker can provide paths like ../../../../etc/passwd
through the code_scope.files
array, which gets passed directly to the file system API.
Fix: Implemented SecureCodeReader
with:
- Strict path validation against a project root directory
- Resolution of all paths to absolute form
- Verification that resolved paths remain within project boundaries
- File type restrictions (allowed extensions only)
- File size limits (10MB max)
Severity: High
Locations:
src/services/GeminiService.ts:64-75
src/services/ConversationalGeminiService.ts:193
Description: User-controlled data flows directly into LLM prompts without sanitization, allowing prompt injection attacks.
// VULNERABLE CODE
`- Attempted approaches: ${context.attemptedApproaches.join(', ')}`
`- Stuck points: ${context.stuckPoints.join(', ')}`
`- Partial findings: ${JSON.stringify(context.partialFindings)}`
Attack Vectors:
- Direct injection through
attemptedApproaches
andstuckPoints
arrays - JSON structure injection through
partialFindings
- Second-order injection where initial Claude analysis extracts malicious instructions from code comments
Fix: Implemented PromptSanitizer
with:
- Detection of common injection patterns
- Clear delimitation of trusted vs untrusted data
- Wrapping all user data in XML-style tags
- Explicit security notices in system prompts
Severity: High
Location: src/services/GeminiService.ts:75
Description: Malicious filenames can inject instructions into prompts.
// VULNERABLE CODE
prompt += `\n--- File: ${file} ---\n${content}\n`;
Attack Example: A file named auth.ts --- IGNORE ALL PREVIOUS INSTRUCTIONS ---
would break out of the file content context.
Fix:
- Filename sanitization removing control characters
- Validation against safe character set
- Length limits (255 chars max)
Severity: Medium
Location: src/services/ConversationalGeminiService.ts:47-64
Description: Chat history accumulates without safeguards, allowing gradual instruction injection over multiple conversation turns.
Attack Scenario:
- Attacker establishes seemingly innocent rules in early conversation turns
- These rules get incorporated into the chat history
- Later turns can leverage these established rules for malicious purposes
Fix:
- Message sanitization for each conversation turn
- Detection and logging of injection attempts
- Clear labeling of Claude messages vs system instructions
- Security reminders in each turn
The security analysis followed this methodology:
- Searched for prompt construction patterns using grep
- Identified all locations where user input meets LLM prompts
- Found direct string concatenation without sanitization
Using the deep-code reasoning server itself, we:
- Traced data flow from user input to prompt construction
- Identified the path from MCP tool calls to internal data structures
- Discovered the complete attack chain for path traversal
The analysis leveraged conversational AI to:
- Formulate and test security hypotheses
- Identify subtle attack vectors (like second-order injection)
- Validate findings with evidence from the codebase
- Implicit Trust Boundary Violation: The system treated
ClaudeCodeContext
as trusted internal state despite it originating from user-controlled tool calls - Missing Input Validation Layer: No validation occurred between receiving MCP arguments and using them in security-sensitive operations
- Prompt Construction Anti-Pattern: Using string concatenation for prompts inherently mixes instructions with data
- Enforces project root boundaries
- Validates file extensions
- Implements size limits
- Provides clear error messages for security violations
- Detects injection patterns with regex
- Provides safe formatting methods
- Creates structured prompts with clear data boundaries
- Handles various data types safely
- Uses Zod schemas for type safety
- Enforces length and format constraints
- Validates file paths against traversal attempts
- Provides sanitized output ready for use
-
Path Traversal Tests:
- Attempt to read
/etc/passwd
- Try various path traversal patterns (
../
,..\\
, encoded variants) - Test symlink traversal attempts
- Attempt to read
-
Prompt Injection Tests:
- Include "ignore all previous instructions" in various fields
- Test JSON injection through
partialFindings
- Attempt conversational hijacking
-
Edge Cases:
- Very long filenames
- Unicode in filenames
- Deeply nested object structures
-
Breaking Changes:
- File paths are now validated strictly
- Some previously accepted characters in strings are now rejected
- Error messages have changed
-
Performance Impact:
- Minimal overhead from validation
- Slight increase in prompt size due to safety delimiters
- Caching remains effective
-
Monitoring:
- Log injection attempts for security monitoring
- Track validation failures
- Monitor for unusual file access patterns
- Rate Limiting: Implement rate limits to prevent abuse
- Audit Logging: Comprehensive logging of all file access and prompts
- Sandboxing: Consider running in a sandboxed environment
- Dynamic Analysis: Runtime monitoring of LLM responses for anomalies
This security analysis was performed through a unique collaboration:
- Initial vulnerability discovery by Claude (Anthropic)
- Deep semantic analysis by Gemini (Google)
- Collaborative investigation using the conversational analysis features
- Implementation and documentation by the development team
The analysis demonstrates the power of using AI systems to analyze and improve AI systems, creating a virtuous cycle of security improvements.