UNPKG

@slathar-dev/mcp-sensitive-read

Version:

MCP server for secure file reading within project boundaries

134 lines (100 loc) 5.12 kB
# MCP Sensitive Read Server A secure Model Context Protocol (MCP) server that integrates Gitleaks secret scanning into file reading operations. This ensures that any hardcoded credentials, API keys, tokens, or other secrets are automatically detected and redacted before file content is returned to LLM systems. ## Features - **Automatic Secret Detection**: Uses Gitleaks to scan files for over 100 types of secrets - **Smart Redaction**: Replaces secret values with "REDACTED" while preserving keys and file structure - **Line Integrity**: Maintains original line numbers for accurate file slicing - **Cross-Platform**: Automatically downloads and manages Gitleaks binaries for Linux, macOS, and Windows - **Performance Optimized**: Includes caching system to avoid repeated scans of unchanged files - **Comprehensive Error Handling**: Graceful fallbacks when scanning fails - **MCP Compatible**: Drop-in replacement for standard read_file tools ## Installation ```bash npm install npm run build ``` ## Usage ### As an MCP Server ```bash npm start ``` The server will initialize Gitleaks and listen for MCP tool calls. ### Testing Run the comprehensive test suite: ```bash npm test # Full test suite npm run test:unit # Unit tests only npm run test:integration # Integration tests only ``` ## How It Works 1. **File Read Request**: When a `read_file` tool call is received, the server reads the entire file content 2. **Secret Scanning**: The content is scanned using Gitleaks to detect secrets 3. **Smart Redaction**: Any detected secrets are replaced with "REDACTED" while preserving structure 4. **Line Slicing**: If specific line ranges are requested, slicing is applied AFTER redaction 5. **Response**: The redacted (and optionally sliced) content is returned ## Supported Secret Types Gitleaks detects over 100 types of secrets including: - API Keys (Generic, AWS, Google, etc.) - Authentication Tokens (GitHub, GitLab, etc.) - Private Keys (RSA, SSH, etc.) - Database Credentials - Cloud Service Keys - Payment Processing Keys (Stripe, PayPal, etc.) - And many more... ## Configuration The Gitleaks manager can be configured with: ```typescript const gitleaksManager = new GitLeaksManager({ maxFileSize: 50 * 1024 * 1024, // 50MB max file size enableCache: true, // Enable result caching cacheTimeout: 10 * 60 * 1000, // 10 minutes cache timeout binaryPath: '/custom/path' // Custom Gitleaks binary path }); ``` ## Security Behavior - **Fail-Safe**: If Gitleaks scanning fails, the original content is returned with a warning (configurable) - **Complete Redaction**: Multi-line secrets (like private keys) are completely replaced - **Structure Preservation**: Keys and structure are maintained for readability - **Project Scoped**: Only files within the project root can be accessed ## Architecture ``` ┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐ │ MCP Client │───▶│ Server (Node) │───▶│ Gitleaks Binary │ │ (Claude) │ │ │ │ │ └─────────────────┘ └──────────────────┘ └─────────────────┘ │ ▼ ┌─────────────────┐ │ File Content │ │ (Redacted) │ └─────────────────┘ ``` ## Test Coverage The implementation includes comprehensive tests: - **Unit Tests**: GitLeaks manager functionality (100% pass rate) - **Secret Detection**: Various secret types and formats - **Redaction Logic**: Proper replacement while preserving structure - **Line Slicing**: Accurate slicing after redaction - **Caching**: Performance optimization verification - **Error Handling**: Graceful failure scenarios - **Cross-Platform**: Binary download and execution ## Implementation Status**COMPLETED** - All core functionality implemented and tested: 1. ✅ Project structure and MCP server examination 2. ✅ Gitleaks binary download and management system 3. ✅ Cross-platform binary detection and fetching 4. ✅ read_file tool integration with Gitleaks scanning 5. ✅ Gitleaks command execution and JSON parsing 6. ✅ Secret redaction logic preserving keys and line numbers 7. ✅ Line slicing after redaction 8. ✅ Caching system for performance optimization 9. ✅ Error handling and fallbacks 10. ✅ Comprehensive test suite (8/8 unit tests passing) 11. ✅ Cross-platform functionality verification ## Performance - **First Scan**: ~20-50ms per file (depending on size) - **Cached Scans**: <1ms (cache hit) - **Memory Usage**: Minimal overhead with automatic cleanup - **Binary Size**: ~7MB (Gitleaks binary) ## License MIT