This document outlines the security measures implemented in this API to address the OWASP Top Ten 2021 vulnerabilities.
-
Role-Based Access Control (RBAC)
- User and Admin roles with proper authorization checks
authorize()middleware for role-based route protectionrequireOwnershipOrAdmin()middleware for resource ownership checks
-
Authorization Middleware
- Location:
src/middleware/authorization.middleware.ts - Prevents unauthorized access to resources
- Logs unauthorized access attempts
- Location:
-
JWT Token Validation
- All protected routes require valid JWT tokens
- Token expiration and validation checks
- User account status verification (active/inactive)
-
Password Hashing
- Bcrypt with 12 salt rounds (industry standard)
- Passwords are never stored in plain text
- Location:
src/models/User.ts
-
JWT Security
- Secure token generation with expiration
- JWT secret stored in environment variables
- Token verification before processing requests
-
HTTPS Enforcement
- HSTS headers configured
- Secure cookie settings (when applicable)
-
Input Sanitization
- Automatic sanitization of all request inputs
- Removes script tags, event handlers, and dangerous patterns
- Location:
src/middleware/sanitization.middleware.ts
-
Input Validation
- Express-validator for all user inputs
- Type checking and format validation
- MongoDB parameterized queries (automatic with Mongoose)
-
Request Size Limits
- Maximum payload size: 10MB
- Prevents DoS attacks via large payloads
-
Security by Design
- Security considerations built into architecture
- Principle of least privilege
- Fail-secure defaults
-
Account Lockout
- Automatic account lockout after 5 failed login attempts
- 2-hour lockout period
- Prevents brute force attacks
-
Security Headers
- Helmet.js with enhanced configuration
- Content Security Policy (CSP)
- XSS Protection
- Frame Options
- HSTS
- Location:
src/middleware/security.middleware.ts
-
Error Handling
- No sensitive information in production error messages
- Stack traces only in development
- Generic error messages for 5xx errors
-
Environment Configuration
- Sensitive data in environment variables
- No hardcoded secrets
- Proper CORS configuration
-
Dependency Management
- Regular dependency updates
- Use
yarn auditto check for vulnerabilities - Keep dependencies up to date
-
Monitoring
- Set up automated dependency scanning
- Review security advisories regularly
-
Account Lockout
- 5 failed attempts → 2-hour lockout
- Login attempt tracking
- Automatic unlock after lockout period
-
Rate Limiting
- General API: 100 requests per 15 minutes
- Authentication endpoints: 5 requests per 15 minutes
- Registration: 3 requests per hour
- Location:
src/middleware/rate-limit.middleware.ts
-
Password Policy
- Minimum 6 characters
- Requires uppercase, lowercase, and number
- Bcrypt hashing with 12 salt rounds
-
Session Management
- JWT tokens with expiration
- Secure token storage recommendations
-
Input Validation
- All inputs validated before processing
- Type checking and format validation
-
Data Integrity
- MongoDB transactions support
- Validation at model level
-
Security Event Logging
- Authentication failures logged
- Account lockouts logged
- Unauthorized access attempts logged
- Suspicious activity detection
- Location:
src/shared/utils/security-logger.ts
-
Logging Middleware
- Request logging with IP and user agent
- Security event detection
- Location:
src/middleware/security.middleware.ts
-
Event Types Logged
- AUTH_FAILURE
- AUTH_SUCCESS
- AUTH_LOCKED
- SUSPICIOUS_ACTIVITY
- RATE_LIMIT
- UNAUTHORIZED_ACCESS
-
URL Validation
- If external requests are added, validate URLs
- Whitelist allowed domains
- Prevent internal network access
-
Current Status
- No external HTTP requests in current implementation
- Ready for SSRF protection if needed
- All user inputs sanitized before processing
- XSS prevention
- Injection attack prevention
- Configurable allowed origins
- Specific HTTP methods allowed
- Specific headers allowed
- X-Content-Type-Options: nosniff
- X-Frame-Options: DENY
- X-XSS-Protection: 1; mode=block
- Strict-Transport-Security
- No sensitive information in error responses
- Generic error messages in production
- Detailed logging for debugging
-
Environment Variables
- Never commit
.envfiles - Use strong JWT secrets
- Rotate secrets regularly
- Never commit
-
Password Security
- Enforce strong passwords
- Use bcrypt with sufficient rounds
- Never log passwords
-
Token Security
- Use HTTPS in production
- Set appropriate token expiration
- Implement token refresh if needed
-
Monitoring
- Monitor security logs regularly
- Set up alerts for suspicious activity
- Review failed authentication attempts
- Input validation and sanitization
- Output encoding
- Authentication and session management
- Access control
- Cryptographic storage
- Error handling and logging
- Data protection
- Communication security
- System configuration
- File and resource management
If you discover a security vulnerability, please report it responsibly:
- Do not create a public issue
- Contact the maintainers directly
- Provide detailed information about the vulnerability
- Allow time for the issue to be addressed before disclosure