Skip to content

Security: Gummiees/spotting-cats-backend

SECURITY.md

Security Implementation Guide

Overview

This document outlines the security measures implemented in the backend project to protect against common vulnerabilities including SQL injection, NoSQL injection, XSS, CSRF, and other attacks.

Security Features Implemented

1. Input Validation & Sanitization

  • Express Validator: All user inputs are validated and sanitized using express-validator
  • MongoDB ObjectId Validation: Proper validation of MongoDB ObjectIds to prevent injection
  • Input Length Limits: String inputs are limited to prevent buffer overflow attacks
  • Type Validation: Strict type checking for all input parameters

2. Rate Limiting & DDoS Protection

  • Express Rate Limit: Global rate limiting (100 requests per 15 minutes, only active in production)
  • Express Slow Down: Progressive delays for excessive requests (only active in production)
  • IP-based Limiting: Rate limits are applied per IP address
  • Environment-based: Rate limiting is disabled in development/staging for easier testing

3. Security Headers (Helmet)

  • Content Security Policy (CSP): Prevents XSS attacks
  • HTTP Strict Transport Security (HSTS): Enforces HTTPS
  • X-Content-Type-Options: Prevents MIME type sniffing
  • X-Frame-Options: Prevents clickjacking
  • X-XSS-Protection: Additional XSS protection
  • Referrer Policy: Controls referrer information

4. CORS Configuration

  • Origin Validation: Strict origin checking in production
  • Method Restrictions: Only allowed HTTP methods
  • Header Restrictions: Only necessary headers allowed
  • Environment-based: Different policies for dev/staging/production

5. Request Size Limits

  • Body Size Limits: 10MB maximum request body size
  • URL Length Limits: Prevents large URL attacks
  • Content-Length Validation: Server-side size validation

6. Error Handling & Information Leakage Prevention

  • Sanitized Error Messages: No internal details exposed in production
  • Stack Trace Protection: Stack traces only in development
  • Generic Error Responses: Standardized error responses
  • Security Event Logging: All security events are logged

7. Database Security

  • NoSQL Injection Prevention: Input sanitization and validation
  • Query Projection: Only necessary fields returned
  • Parameterized Queries: MongoDB driver handles parameterization
  • Error Handling: Database errors don't expose internal structure

8. Security Monitoring

  • Suspicious Request Detection: Pattern-based detection of attack attempts
  • Security Event Logging: Comprehensive logging of security events
  • IP Tracking: All requests logged with IP addresses
  • User Agent Logging: Request tracking for security analysis

Environment Configuration

Required Environment Variables

# Server Configuration
PORT=3000
NODE_ENV=production

# CORS Configuration (comma-separated list)
CORS_ORIGINS=https://yourdomain.com,https://www.yourdomain.com

# Database Configuration
MONGO_URL=mongodb://localhost:27017/your-database

# Security Configuration
TRUST_PROXY=true  # Set to true when behind reverse proxy
RATE_LIMIT_MAX=100
REQUEST_SIZE_LIMIT=10mb

Production Security Checklist

  • Set NODE_ENV=production
  • Configure CORS_ORIGINS with only your domains
  • Set TRUST_PROXY=true if behind reverse proxy
  • Use HTTPS in production
  • Configure proper MongoDB authentication
  • Set up monitoring and alerting
  • Regular security updates
  • Database backups
  • Log monitoring

API Security

Validation Middleware

All endpoints use validation middleware:

  • createCatValidation: Validates cat creation requests
  • updateCatValidation: Validates cat update requests
  • getCatByIdValidation: Validates ID parameters
  • deleteCatValidation: Validates deletion requests

Input Sanitization

  • String inputs are trimmed and length-limited
  • Numbers are validated and converted safely
  • ObjectIds are validated before database queries
  • Special characters are filtered where appropriate

Monitoring & Logging

Security Events Logged

  • Suspicious request patterns
  • Rate limit violations
  • Validation failures
  • Authentication attempts
  • 404 errors (potential scanning)

Log Format

{
  "timestamp": "2024-01-01T00:00:00.000Z",
  "type": "suspicious_request",
  "ip": "192.168.1.1",
  "userAgent": "Mozilla/5.0...",
  "method": "POST",
  "url": "/api/cats",
  "details": {
    "body": "sanitized_body",
    "query": "sanitized_query"
  },
  "severity": "high"
}

Additional Security Recommendations

1. Authentication & Authorization

  • Implement JWT or session-based authentication
  • Use bcrypt for password hashing
  • Implement role-based access control (RBAC)
  • Add API key authentication for external services

2. HTTPS & SSL/TLS

  • Use HTTPS in production
  • Configure proper SSL/TLS settings
  • Use secure cookies
  • Implement certificate pinning

3. Database Security

  • Use MongoDB authentication
  • Implement connection pooling
  • Regular database backups
  • Monitor database access logs

4. Monitoring & Alerting

  • Set up security monitoring tools
  • Configure alerts for suspicious activities
  • Monitor rate limit violations
  • Track failed authentication attempts

5. Regular Maintenance

  • Keep dependencies updated
  • Regular security audits
  • Penetration testing
  • Security patch management

Testing Security

Manual Testing

# Test rate limiting
for i in {1..150}; do curl http://localhost:3000/api/cats; done

# Test input validation
curl -X POST http://localhost:3000/api/cats \
  -H "Content-Type: application/json" \
  -d '{"name": "<script>alert(1)</script>", "age": "invalid"}'

# Test CORS
curl -H "Origin: http://malicious.com" \
  -H "Access-Control-Request-Method: POST" \
  -X OPTIONS http://localhost:3000/api/cats

Automated Testing

  • Use tools like OWASP ZAP for security testing
  • Implement security-focused unit tests
  • Regular vulnerability scanning
  • Dependency vulnerability checks

Incident Response

Security Incident Response Plan

  1. Detection: Monitor logs for suspicious activities
  2. Analysis: Investigate and categorize the incident
  3. Containment: Block malicious IPs, disable affected endpoints
  4. Eradication: Remove threats and patch vulnerabilities
  5. Recovery: Restore normal operations
  6. Lessons Learned: Document and improve security measures

Contact Information

Compliance

GDPR Compliance

  • Data minimization
  • User consent management
  • Right to be forgotten
  • Data portability
  • Privacy by design

SOC 2 Compliance

  • Access controls
  • Change management
  • Risk assessment
  • Security monitoring
  • Incident response

Resources

Security Tools

Security Standards

  • OWASP ASVS (Application Security Verification Standard)
  • NIST Cybersecurity Framework
  • ISO 27001 Information Security Management
  • PCI DSS (if handling payment data)

There aren’t any published security advisories