Skip to content

Security: timothy-pham/NestJS_E-commerce_API

Security

SECURITY.md

Security

Security Features Implemented

1. Authentication & Authorization

  • JWT-based authentication: Secure token-based authentication using JSON Web Tokens
  • Password hashing: All passwords are hashed using bcryptjs with salt rounds
  • Protected endpoints: Most endpoints require authentication via JWT guards
  • Role-based access: User roles (admin/user) for authorization

2. Input Validation

  • class-validator: All DTOs use validation decorators
  • Validation pipes: Global validation pipe configured in main.ts
  • Type safety: TypeScript provides compile-time type checking
  • Whitelist validation: Only whitelisted properties are accepted

3. Database Security

  • Mongoose ODM: Uses Mongoose which provides built-in query sanitization
  • NoSQL injection protection: Mongoose automatically sanitizes queries
  • Schema validation: Mongoose schemas enforce data structure
  • ObjectId validation: All MongoDB IDs are validated using @IsMongoId() decorator

4. API Security

  • CORS enabled: Cross-Origin Resource Sharing configured
  • Environment variables: Sensitive data stored in environment variables
  • Error handling: Proper error handling without exposing sensitive information

CodeQL Analysis Results

The CodeQL security analysis flagged 10 SQL injection warnings. These are false positives because:

  1. MongoDB vs SQL: The application uses MongoDB (NoSQL), not SQL databases. The alerts are based on SQL injection patterns.

  2. Mongoose Protection: All database queries go through Mongoose ODM, which:

    • Automatically sanitizes input
    • Uses parameterized queries
    • Prevents query injection attacks
  3. Input Validation: All user inputs are validated through:

    • class-validator decorators on DTOs
    • Global validation pipe with whitelist enabled
    • TypeScript type checking
    • @IsMongoId() decorator for database IDs
  4. Query Examples:

    // This is safe because:
    // 1. createCategoryDto.name is validated by class-validator
    // 2. Mongoose escapes special characters
    // 3. The query object is constructed safely
    const existingCategory = await this.categoryModel.findOne({
      name: createCategoryDto.name,
    });

Security Best Practices for Production

Required Changes:

  1. Change JWT_SECRET: Use a strong, random string

    openssl rand -base64 32
  2. Enable MongoDB authentication:

    MONGODB_URI=mongodb://username:password@host:port/database
    
  3. Use HTTPS: Configure SSL/TLS certificates

  4. Rate limiting: Add rate limiting middleware

    npm install @nestjs/throttler
  5. Helmet: Add security headers

    npm install helmet

Recommended Additional Security:

  • Implement refresh tokens for better session management
  • Add API rate limiting per user/IP
  • Enable MongoDB SSL/TLS connections
  • Implement audit logging for sensitive operations
  • Add CSRF protection for web clients
  • Use environment-specific configurations
  • Regular dependency updates and security audits
  • Implement request timeout limits
  • Add IP whitelisting for admin endpoints

Dependency Vulnerabilities

Resolved:

  • Mongoose search injection: Updated to version 8.9.5 (patched)

Known Issues (Not Critical):

  • ⚠️ validator.js URL validation: The class-validator dependency uses validator library which has a URL validation bypass. This is not critical because:
    • We don't use URL validation in this application
    • The vulnerability is in URL parsing, which we don't expose
    • Severity: Moderate
    • Impact: Low for our use case

Reporting Security Issues

If you discover a security vulnerability, please email the maintainer directly. Do not open a public issue.

Security Checklist for Deployment

  • [v] Changed JWT_SECRET to a strong random value
  • [v] Configured MongoDB authentication
  • Enabled HTTPS
  • Set NODE_ENV=production
  • Reviewed all environment variables
  • Implemented rate limiting
  • Added security headers (Helmet)
  • Enabled MongoDB SSL/TLS
  • Configured logging and monitoring
  • Set up backup strategy
  • Reviewed user permissions
  • Tested authentication flows
  • Verified input validation
  • Checked CORS configuration

Security Audit History

  • 2025-10-21: Initial implementation with JWT auth, password hashing, and input validation
  • 2025-10-21: Updated Mongoose to 8.9.5 to patch search injection vulnerability
  • 2025-10-21: CodeQL analysis completed - 10 false positives (SQL injection warnings for MongoDB)

There aren’t any published security advisories