|
| 1 | +# Rate Limiting and CORS Configuration |
| 2 | + |
| 3 | +## Overview |
| 4 | +This implementation provides comprehensive rate limiting and CORS configuration for the API Gateway to prevent brute-force attacks and ensure proper cross-origin resource sharing. |
| 5 | + |
| 6 | +## Features Implemented |
| 7 | + |
| 8 | +### 1. Rate Limiting |
| 9 | +- **General API**: 50 requests/minute (production), 100 requests/minute (development) |
| 10 | +- **Authentication**: 5 attempts per 15 minutes |
| 11 | +- **Strict Operations**: 10 requests per minute |
| 12 | +- **Progressive Slowdown**: Delays after 20 requests in 15 minutes |
| 13 | + |
| 14 | +### 2. CORS Configuration |
| 15 | +- **Preflight Support**: Handles OPTIONS requests correctly |
| 16 | +- **Origin Validation**: Allows specific frontend domains |
| 17 | +- **Credentials**: Supports cookies and authorization headers |
| 18 | +- **Development Mode**: Allows localhost origins in development |
| 19 | + |
| 20 | +### 3. Security Headers |
| 21 | +- **Helmet**: Comprehensive security headers |
| 22 | +- **CSP**: Content Security Policy |
| 23 | +- **HSTS**: HTTP Strict Transport Security |
| 24 | +- **XSS Protection**: Cross-site scripting protection |
| 25 | + |
| 26 | +## API Endpoints |
| 27 | + |
| 28 | +### Rate Limiting Test |
| 29 | +```bash |
| 30 | +# Test rate limiting (limited to 10 requests/minute) |
| 31 | +GET /api/test-security/rate-limit |
| 32 | +``` |
| 33 | + |
| 34 | +### CORS Test |
| 35 | +```bash |
| 36 | +# Test CORS configuration |
| 37 | +GET /api/test-security/cors |
| 38 | + |
| 39 | +# Test CORS preflight |
| 40 | +OPTIONS /api/test-security/cors |
| 41 | +``` |
| 42 | + |
| 43 | +### Security Headers Test |
| 44 | +```bash |
| 45 | +# Test security headers |
| 46 | +GET /api/test-security/security-headers |
| 47 | +``` |
| 48 | + |
| 49 | +## Configuration |
| 50 | + |
| 51 | +### Environment Variables |
| 52 | +```bash |
| 53 | +NODE_ENV=production # Enables stricter rate limits |
| 54 | +JWT_SECRET=your-secret-key |
| 55 | +``` |
| 56 | + |
| 57 | +### Rate Limits |
| 58 | +- **General**: 50/min (prod), 100/min (dev) |
| 59 | +- **Auth**: 5 attempts per 15 minutes |
| 60 | +- **Strict**: 10 requests per minute |
| 61 | +- **Speed Limit**: 20 requests, then 500ms delay |
| 62 | + |
| 63 | +### CORS Origins |
| 64 | +- `http://localhost:3000` (React) |
| 65 | +- `http://localhost:5173` (Vite) |
| 66 | +- `http://localhost:8080` (Vue) |
| 67 | +- Production domains (to be added) |
| 68 | + |
| 69 | +## Testing |
| 70 | + |
| 71 | +### 1. Rate Limiting Test |
| 72 | +```bash |
| 73 | +# Make multiple requests to test rate limiting |
| 74 | +for i in {1..15}; do |
| 75 | + curl -w "%{http_code}\n" http://localhost:5000/api/test-security/rate-limit |
| 76 | +done |
| 77 | +``` |
| 78 | + |
| 79 | +### 2. CORS Test |
| 80 | +```bash |
| 81 | +# Test from browser console |
| 82 | +fetch('http://localhost:5000/api/test-security/cors', { |
| 83 | + method: 'GET', |
| 84 | + headers: { |
| 85 | + 'Content-Type': 'application/json', |
| 86 | + }, |
| 87 | +}) |
| 88 | +.then(response => response.json()) |
| 89 | +.then(data => console.log(data)); |
| 90 | +``` |
| 91 | +
|
| 92 | +### 3. Preflight Test |
| 93 | +```bash |
| 94 | +# Test CORS preflight |
| 95 | +curl -X OPTIONS \ |
| 96 | + -H "Origin: http://localhost:3000" \ |
| 97 | + -H "Access-Control-Request-Method: POST" \ |
| 98 | + -H "Access-Control-Request-Headers: Content-Type" \ |
| 99 | + http://localhost:5000/api/test-security/cors |
| 100 | +``` |
| 101 | +
|
| 102 | +## Error Responses |
| 103 | +
|
| 104 | +### Rate Limit Exceeded |
| 105 | +```json |
| 106 | +{ |
| 107 | + "success": false, |
| 108 | + "message": "Too many requests from this IP, please try again later.", |
| 109 | + "retryAfter": "1 minute" |
| 110 | +} |
| 111 | +``` |
| 112 | +
|
| 113 | +### CORS Error |
| 114 | +```json |
| 115 | +{ |
| 116 | + "success": false, |
| 117 | + "message": "CORS policy violation: Origin not allowed", |
| 118 | + "origin": "http://disallowed-origin.com" |
| 119 | +} |
| 120 | +``` |
| 121 | +
|
| 122 | +## Security Features |
| 123 | +
|
| 124 | +1. **Brute Force Protection**: Stricter limits on auth endpoints |
| 125 | +2. **Progressive Delays**: Slows down repeated requests |
| 126 | +3. **IP-based Limiting**: Tracks requests per IP address |
| 127 | +4. **Origin Validation**: Only allows trusted domains |
| 128 | +5. **Security Headers**: Comprehensive security headers via Helmet |
| 129 | +
|
| 130 | +## Production Considerations |
| 131 | +
|
| 132 | +1. **Redis Store**: Consider using Redis for distributed rate limiting |
| 133 | +2. **Whitelist**: Add trusted IPs for higher limits |
| 134 | +3. **Monitoring**: Implement rate limit monitoring |
| 135 | +4. **Custom Messages**: Customize error messages per endpoint |
| 136 | +5. **Bypass Options**: Add bypass mechanisms for admin users |
| 137 | +
|
| 138 | +## Files Created |
| 139 | +- `src/middleware/rateLimiter.middleware.js` - Rate limiting configuration |
| 140 | +- `src/middleware/cors.middleware.js` - CORS configuration |
| 141 | +- `src/config/app.config.js` - Environment configuration |
| 142 | +- `src/routes/test-security.routes.js` - Test endpoints |
0 commit comments