pulseengine-mcp-security
Security middleware and input validation for MCP servers
This crate provides security middleware for MCP servers, including input validation, request sanitization, and protection against common web vulnerabilities.
What This Protects Against
Input Validation:
- JSON injection attacks
- XSS prevention in tool parameters
- SQL injection prevention (when parameters go to databases)
- Path traversal attacks in file operations
- Command injection in system tools
Request Protection:
- Request size limits to prevent DoS
- Rate limiting integration
- CORS policy enforcement
- Security headers (HSTS, CSP, etc.)
- Parameter type validation
Real-World Testing
This security layer is actively used in the Loxone MCP Server where it:
- Validates 30+ tool parameters against injection attacks
- Sanitizes device names and commands for safe execution
- Protects file system operations from path traversal
- Enforces request size limits for HTTP transport
- Integrates with authentication for complete security
Quick Start
[]
= "0.2.0"
= "0.2.0"
= "1.0"
Basic Usage
Input Validation
use ;
use CallToolRequestParam;
// Create validator with rules
let validator = new;
// Validate tool parameters
let request = CallToolRequestParam ;
match validator.validate_tool_request
CORS Configuration
use ;
let cors_config = CorsConfig ;
let middleware = new;
Request Size Limits
use SecurityConfig;
let config = SecurityConfig ;
Current Status
Solid foundation with room for growth. The basic security validations work well and catch common attack vectors, but this area can always be improved.
What works reliably:
- ✅ Basic input sanitization and validation
- ✅ Request size and parameter limits
- ✅ CORS policy enforcement
- ✅ XSS prevention in string parameters
- ✅ Path traversal prevention
- ✅ Integration with mcp-server framework
Areas for improvement:
- 🔧 More sophisticated injection detection
- 📝 Better examples for different attack scenarios
- 🧪 Security testing utilities
- 🔧 More granular validation rules
Validation Features
String Sanitization
use sanitize_string;
// Remove potentially dangerous content
let clean = sanitize_string;
// Result: "Hello World"
// Validate file paths
let safe_path = sanitize_file_path?;
// Error: Path traversal attempt detected
Parameter Validation
use validate_parameters;
let params = json!;
// Validate against schema and security rules
let validated = validate_parameters?;
SQL Injection Prevention
use check_sql_injection;
let user_input = "'; DROP TABLE users; --";
if check_sql_injection
Middleware Integration
With HTTP Transport
use SecurityMiddleware;
use ;
let app = new
.route
.layer
.layer;
With MCP Server
use ServerConfig;
use SecurityConfig;
let security_config = SecurityConfig ;
let server_config = ServerConfig ;
// Security validation happens automatically
Security Rules
Predefined Rule Sets
use ;
// Strict security for public-facing servers
let strict_rules = strict;
// Moderate security for internal tools
let moderate_rules = moderate;
// Minimal security for development
let dev_rules = development;
Custom Validation Rules
use ValidationRules;
let custom_rules = ValidationRules ;
Security Headers
use SecurityHeaders;
let headers = strict
.with_hsts // 1 year
.with_csp
.with_frame_options
.with_content_type_options
.with_referrer_policy;
Real-World Examples
File Access Validation
// From Loxone implementation - validating file paths
use validate_file_path;
Device Command Validation
// Validate device control commands
Contributing
Security is an ongoing concern and improvements are always welcome. Most valuable contributions:
- Security research - Finding new attack vectors or validation gaps
- Performance optimization - Security checks with minimal overhead
- Testing utilities - Tools for security testing and validation
- Real-world examples - Security patterns from actual deployments
If you find a security issue, please follow responsible disclosure practices.
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Repository: https://github.com/avrabe/mcp-loxone
Note: This crate is part of a larger MCP framework that will be published as a separate repository.