This document outlines the comprehensive security features implemented in the Sports Buddy App.
- JWT Token Verification: All protected routes verify Supabase JWT tokens
- User Context: Authenticated user information is available in
req.user - Token Extraction: Bearer tokens extracted from Authorization headers
- Session Management: Automatic token refresh handled by Supabase client
- Requires valid JWT token for access
- Used on all protected endpoints
- Adds user information to request object
- Ensures users can only access/modify their own resources
- Used for profile updates and user-specific operations
- Ensures only match creators can modify their matches
- Database-level verification for additional security
- Allows both authenticated and anonymous access
- Used for public content that benefits from user context
- Limit: 1000 requests per 15 minutes per IP
- Purpose: Prevents general API abuse
- Headers: Returns rate limit info in standard headers
- Limit: 10 requests per 15 minutes per IP
- Purpose: Prevents brute force attacks on auth endpoints
- Skip Success: Only failed attempts count toward limit
- Limit: 20 matches per hour per IP
- Purpose: Prevents match spam and resource exhaustion
- Limit: 30 messages per minute per IP
- Purpose: Prevents message spam and chat abuse
- Limit: 60 searches per minute per IP
- Purpose: Protects resource-intensive search operations
- Delay After: 50 requests without delay
- Progressive Delay: 500ms per request after threshold
- Maximum Delay: 10 seconds
- Window: 15 minutes
- Emergency Rate Limit: 100 requests per minute
- Attack Detection: Logs suspicious activity
- Automatic Blocking: Temporary IP blocking for excessive requests
- Content Security Policy: Prevents XSS attacks
- HSTS: Enforces HTTPS connections
- X-Frame-Options: Prevents clickjacking
- X-Content-Type-Options: Prevents MIME type sniffing
- Origin Whitelist: Only allowed origins can access API
- Credentials: Supports authenticated requests
- Methods: Limited to necessary HTTP methods
- Headers: Restricted to required headers
- Size Limits: 10MB maximum request body size
- JSON Limits: Prevents JSON bomb attacks
- Trust Proxy: Proper IP detection behind reverse proxies
const bruteForceProtection = createBruteForceProtection();
// Usage example
const result = bruteForceProtection(userEmail, 5, 15 * 60 * 1000);
if (result.blocked) {
// Handle blocked user
}const adminIPs = process.env.ADMIN_IP_WHITELIST?.split(',') || [];
router.use('/admin', ipWhitelist(adminIPs));- Activated during suspected attacks
- 10 requests per minute limit
- Logs all emergency activations
- Error Logging: All security events logged with context
- IP Tracking: Suspicious IP addresses tracked
- Attack Detection: Automatic logging of potential attacks
- Health Monitoring: Security status in health checks
- Safe Error Messages: No internal information leaked
- Stack Traces: Only in development environment
- Consistent Responses: Standardized error format
- Security Headers: Added to all error responses
# Required security configuration
SUPABASE_URL=your_supabase_url
SUPABASE_ANON_KEY=your_anon_key
SUPABASE_SERVICE_ROLE_KEY=your_service_key
# CORS and origins
ALLOWED_ORIGINS=https://yourdomain.com,https://app.yourdomain.com
# Admin security
ADMIN_IP_WHITELIST=123.456.789.0,987.654.321.0
# Environment
NODE_ENV=production- Environment Variables: Set all required environment variables
- HTTPS Only: Force HTTPS in production
- Rate Limits: Adjust rate limits based on expected traffic
- Monitoring: Set up logging and monitoring
- IP Whitelisting: Configure admin IP restrictions
- Row Level Security: Enabled on all tables
- Policies: Granular access control policies
- Service Role: Used only for admin operations
- Anon Key: Used for public operations only
# Test general rate limit
for i in {1..1010}; do curl -s http://localhost:3000/health; done
# Test auth rate limit
for i in {1..15}; do curl -s -X POST http://localhost:3000/api/auth/signin; done# Test protected endpoint without token
curl -X GET http://localhost:3000/api/profiles/123
# Test with invalid token
curl -X GET -H "Authorization: Bearer invalid" http://localhost:3000/api/profiles/123- Memory Usage: In-memory store for rate limits
- CPU Impact: Minimal per-request processing
- Network: Additional headers in responses
- Redis Integration: For distributed rate limiting (future)
- Database Connection Pooling: Efficient database usage
- Caching: JWT verification caching (implemented by Supabase)
- Environment variables configured
- HTTPS enforced
- Rate limits appropriate for traffic
- CORS origins restricted to production domains
- Admin IP whitelist configured
- Monitoring and logging enabled
- Error messages sanitized for production
- All sensitive endpoints protected with authentication
- Resource ownership verified
- Input validation implemented
- SQL injection prevention (via Supabase)
- XSS prevention (via Helmet)
- Rate limiting on all public endpoints
This security implementation provides enterprise-level protection while maintaining good performance and developer experience.