# ๐งช Testing Documentation
## Overview
This project includes comprehensive security tests for the MCP HTTP Server to ensure it's hardened against common attacks and follows security best practices.
## Test Coverage
### โ
**Security Features Tested**
#### ๐ **Authentication & Authorization**
- API key authentication (optional/required modes)
- Multiple API key support
- Header variations (`X-API-Key`, `API-Key`, `Authorization: Bearer`)
- Invalid key rejection
- Missing authentication handling
#### ๐ก๏ธ **Request Validation**
- JSON-RPC 2.0 format validation
- Required field checking (`jsonrpc`, `id`, `method`)
- Dangerous method blocking (`eval`, `exec`, `system`, etc.)
- Malformed JSON rejection
- Empty request body handling
#### ๐ง **Rate Limiting**
- MCP endpoint rate limiting (15min windows)
- General endpoint rate limiting (1min windows)
- Health check bypass
- Configurable limits via environment variables
#### ๐ **CORS Protection**
- Default allow-all behavior
- Configurable origin restrictions
- Preflight request handling
- Cross-origin header management
#### ๐ **Security Headers**
- Helmet.js security headers
- Content type protection
- XSS protection
- Frame options
#### ๐ก **Transport Support**
- Server-Sent Events (SSE) handling
- Content type detection
- Streaming response management
## Running Tests
### **HTTP Server Tests**
```bash
# Run all HTTP security tests
npm run test:http
# Run tests in watch mode
npm run test:http:watch
# Run tests with coverage report
npm run test:http:coverage
```
### **Package Tests (Supabase MCP)**
```bash
# Run all workspace tests
npm test
# Run tests with coverage
npm run test:coverage
```
## Test Results
**Current Status: โ
All 27 tests passing**
```
โ Health Endpoint (1)
โ Status Endpoint (1)
โ Authentication (4)
โ Request Validation (3)
โ Security Headers (1)
โ SSE Support (1)
โ CORS Configuration (2)
โ Rate Limiting Edge Cases (2)
โ Multiple API Keys (2)
โ Header Variations (2)
โ Payload Security (3)
โ Content Type Handling (2)
โ Error Response Format (3)
```
## Test Categories
### **Core Security Tests**
| Category | Tests | Description |
|----------|-------|-------------|
| Authentication | 4 | API key validation and auth flows |
| Request Validation | 3 | JSON-RPC format and dangerous method blocking |
| Security Headers | 1 | Helmet.js security header verification |
### **Edge Case Tests**
| Category | Tests | Description |
|----------|-------|-------------|
| Rate Limiting | 2 | Time windows and bypass logic |
| Multiple API Keys | 2 | Multi-key support and whitespace handling |
| Header Variations | 2 | Different auth header formats |
| Payload Security | 3 | Malformed requests and method validation |
| Content Type | 2 | JSON handling and content type requirements |
| Error Handling | 3 | Structured error response validation |
### **Integration Tests**
| Category | Tests | Description |
|----------|-------|-------------|
| Health Endpoint | 1 | Unauthenticated health checks |
| Status Endpoint | 1 | MCP capability reporting |
| SSE Support | 1 | Event stream transport |
| CORS | 2 | Cross-origin request handling |
## Environment Variables Tested
```bash
# Authentication
MCP_API_KEYS="key1,key2,key3"
# Rate Limiting
MCP_RATE_LIMIT_REQUESTS="100" # Per 15 minutes
MCP_RATE_LIMIT_GENERAL="60" # Per minute
# CORS
MCP_ALLOWED_ORIGINS="https://allowed.com,https://trusted.app"
```
## Security Validation
### **๐ What's Tested**
โ
**Authentication bypass prevention**
โ
**Injection attack protection** (`eval`, `exec`, etc.)
โ
**Rate limiting effectiveness**
โ
**CORS policy enforcement**
โ
**Input validation robustness**
โ
**Error message security** (no info leakage)
โ
**Header security** (XSS, frame protection)
### **๐จ Attack Scenarios Covered**
- **Unauthenticated access attempts**
- **Brute force API key attacks**
- **Code injection via method names**
- **DDoS via rapid requests**
- **Cross-origin attacks**
- **Malformed payload exploits**
## Adding New Tests
### **Test Structure**
```javascript
describe('New Security Feature', () => {
test('should prevent specific attack', async () => {
const app = createTestApp();
await request(app)
.post('/mcp')
.send(maliciousPayload)
.expect(403); // or appropriate error code
});
});
```
### **Test Categories to Add**
- **WebSocket security** (if implemented)
- **File upload validation** (if added)
- **SQL injection prevention** (database operations)
- **Memory exhaustion protection**
- **Timeout handling**
## Security Recommendations
Based on test results, the server includes:
โ
**Production-ready security hardening**
โ
**Configurable authentication** (warn if disabled)
โ
**Rate limiting** (prevent abuse)
โ
**Input validation** (block dangerous operations)
โ
**Security headers** (XSS/CSRF protection)
โ
**Error handling** (no information leakage)
## Continuous Security
```bash
# Run tests before deployment
npm run test:http
# Check for security vulnerabilities
npm audit
# Format and lint
npm run format:check
```
## Test Dependencies
- **Vitest**: Modern test framework
- **Supertest**: HTTP testing library
- **Express**: Test app framework
- **Helmet**: Security headers
- **Rate Limiter**: DDoS protection
---
**๐ Security Status: HARDENED**
**๐งช Test Coverage: 27/27 PASSING**
**๐ Production Ready: YES**