Security Features
API Key Authentication
Two separate API key systems protect different endpoint categories:- WIDGET_API_KEY: Required for all chat endpoints (
/v1/chat,/v1/chat/stream,/chat) - ADMIN_API_KEY: Required for admin endpoints (
/v1/admin/conversations)
Rate Limiting
IP-based rate limiting prevents abuse:- Tracks request count per client IP address
- Configurable window and max requests via environment variables:
RATE_LIMIT_WINDOW_MS: Time window in milliseconds (default: 60000)RATE_LIMIT_MAX_REQUESTS: Maximum requests per window (default: 60)
- Returns
429 Too Many Requestswhen limit exceeded - Uses
X-Forwarded-Forheader in proxied environments
CORS Protection
Strict Cross-Origin Resource Sharing (CORS) configuration:- Production deployments reject wildcard
*origins - Configurable allowed origins via
CORS_ORIGINenvironment variable - Supports comma-separated list of trusted domains
- See CORS Configuration for details
Security Headers
The backend automatically sets protective HTTP headers on all responses:Timing-Safe Comparison
All API key and password comparisons use Node.jscrypto.timingSafeEqual() to prevent timing attacks that could leak secret values through response time analysis.
Request Validation
All incoming requests are validated using Zod schemas:- Chat requests:
sessionIdformat,messagelength limits (1-4000 chars) - Session IDs: Restricted to alphanumeric and safe characters:
[A-Za-z0-9._:-]{1,128} - Admin queries: Type-safe parameter validation
Additional Hardening
X-Powered-Byheader disabled to prevent server fingerprinting- Request body size limited to 2MB
- No-store cache headers on sensitive endpoints
- Widget bundle cache control based on environment
Best Practices
- Rotate secrets regularly: Change
WIDGET_API_KEY,ADMIN_API_KEY, andDASHBOARD_PASSWORDperiodically - Use HTTPS: Always serve backend and dashboard over HTTPS in production
- Restrict CORS: Set
CORS_ORIGINto specific trusted domains only - Keep secrets server-side: Never expose
OPENAI_API_KEYto client-side code - Monitor rate limits: Adjust thresholds based on your traffic patterns
Related Documentation
- API Keys - API key configuration and rotation
- CORS Configuration - Cross-origin access control