Skip to content

Security: vsmm-world/express-typescript-template

Security

SECURITY.md

Security Implementation - OWASP Top Ten 2021

This document outlines the security measures implemented in this API to address the OWASP Top Ten 2021 vulnerabilities.

A01:2021 – Broken Access Control

Implemented Measures:

  1. Role-Based Access Control (RBAC)

    • User and Admin roles with proper authorization checks
    • authorize() middleware for role-based route protection
    • requireOwnershipOrAdmin() middleware for resource ownership checks
  2. Authorization Middleware

    • Location: src/middleware/authorization.middleware.ts
    • Prevents unauthorized access to resources
    • Logs unauthorized access attempts
  3. JWT Token Validation

    • All protected routes require valid JWT tokens
    • Token expiration and validation checks
    • User account status verification (active/inactive)

A02:2021 – Cryptographic Failures

Implemented Measures:

  1. Password Hashing

    • Bcrypt with 12 salt rounds (industry standard)
    • Passwords are never stored in plain text
    • Location: src/models/User.ts
  2. JWT Security

    • Secure token generation with expiration
    • JWT secret stored in environment variables
    • Token verification before processing requests
  3. HTTPS Enforcement

    • HSTS headers configured
    • Secure cookie settings (when applicable)

A03:2021 – Injection

Implemented Measures:

  1. Input Sanitization

    • Automatic sanitization of all request inputs
    • Removes script tags, event handlers, and dangerous patterns
    • Location: src/middleware/sanitization.middleware.ts
  2. Input Validation

    • Express-validator for all user inputs
    • Type checking and format validation
    • MongoDB parameterized queries (automatic with Mongoose)
  3. Request Size Limits

    • Maximum payload size: 10MB
    • Prevents DoS attacks via large payloads

A04:2021 – Insecure Design

Implemented Measures:

  1. Security by Design

    • Security considerations built into architecture
    • Principle of least privilege
    • Fail-secure defaults
  2. Account Lockout

    • Automatic account lockout after 5 failed login attempts
    • 2-hour lockout period
    • Prevents brute force attacks

A05:2021 – Security Misconfiguration

Implemented Measures:

  1. Security Headers

    • Helmet.js with enhanced configuration
    • Content Security Policy (CSP)
    • XSS Protection
    • Frame Options
    • HSTS
    • Location: src/middleware/security.middleware.ts
  2. Error Handling

    • No sensitive information in production error messages
    • Stack traces only in development
    • Generic error messages for 5xx errors
  3. Environment Configuration

    • Sensitive data in environment variables
    • No hardcoded secrets
    • Proper CORS configuration

A06:2021 – Vulnerable and Outdated Components

Recommendations:

  1. Dependency Management

    • Regular dependency updates
    • Use yarn audit to check for vulnerabilities
    • Keep dependencies up to date
  2. Monitoring

    • Set up automated dependency scanning
    • Review security advisories regularly

A07:2021 – Identification and Authentication Failures

Implemented Measures:

  1. Account Lockout

    • 5 failed attempts → 2-hour lockout
    • Login attempt tracking
    • Automatic unlock after lockout period
  2. 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
  3. Password Policy

    • Minimum 6 characters
    • Requires uppercase, lowercase, and number
    • Bcrypt hashing with 12 salt rounds
  4. Session Management

    • JWT tokens with expiration
    • Secure token storage recommendations

A08:2021 – Software and Data Integrity Failures

Implemented Measures:

  1. Input Validation

    • All inputs validated before processing
    • Type checking and format validation
  2. Data Integrity

    • MongoDB transactions support
    • Validation at model level

A09:2021 – Security Logging and Monitoring Failures

Implemented Measures:

  1. Security Event Logging

    • Authentication failures logged
    • Account lockouts logged
    • Unauthorized access attempts logged
    • Suspicious activity detection
    • Location: src/shared/utils/security-logger.ts
  2. Logging Middleware

    • Request logging with IP and user agent
    • Security event detection
    • Location: src/middleware/security.middleware.ts
  3. Event Types Logged

    • AUTH_FAILURE
    • AUTH_SUCCESS
    • AUTH_LOCKED
    • SUSPICIOUS_ACTIVITY
    • RATE_LIMIT
    • UNAUTHORIZED_ACCESS

A10:2021 – Server-Side Request Forgery (SSRF)

Implemented Measures:

  1. URL Validation

    • If external requests are added, validate URLs
    • Whitelist allowed domains
    • Prevent internal network access
  2. Current Status

    • No external HTTP requests in current implementation
    • Ready for SSRF protection if needed

Additional Security Measures

1. Request Sanitization

  • All user inputs sanitized before processing
  • XSS prevention
  • Injection attack prevention

2. CORS Configuration

  • Configurable allowed origins
  • Specific HTTP methods allowed
  • Specific headers allowed

3. Security Headers

  • X-Content-Type-Options: nosniff
  • X-Frame-Options: DENY
  • X-XSS-Protection: 1; mode=block
  • Strict-Transport-Security

4. Error Handling

  • No sensitive information in error responses
  • Generic error messages in production
  • Detailed logging for debugging

Security Best Practices

  1. Environment Variables

    • Never commit .env files
    • Use strong JWT secrets
    • Rotate secrets regularly
  2. Password Security

    • Enforce strong passwords
    • Use bcrypt with sufficient rounds
    • Never log passwords
  3. Token Security

    • Use HTTPS in production
    • Set appropriate token expiration
    • Implement token refresh if needed
  4. Monitoring

    • Monitor security logs regularly
    • Set up alerts for suspicious activity
    • Review failed authentication attempts

Security Checklist

  • 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

Reporting Security Issues

If you discover a security vulnerability, please report it responsibly:

  1. Do not create a public issue
  2. Contact the maintainers directly
  3. Provide detailed information about the vulnerability
  4. Allow time for the issue to be addressed before disclosure

References

There aren’t any published security advisories