WayrApp Backend & Ecosystem Documentation - v1.0.0
    Preparing search index...

    Module securityMiddleware

    Node Security Hardening Middleware

    This module provides comprehensive security middleware components designed to harden Express.js applications against common web vulnerabilities and attacks. It implements multiple layers of security controls including CORS policy enforcement, rate limiting, input sanitization, security headers, and request size validation. These middleware functions form the security foundation for the WayrApp backend infrastructure and are essential for protecting the distributed node architecture against malicious requests and abuse.

    The security middleware is applied early in the Express middleware stack (src/app.ts) to ensure all incoming requests are properly validated and secured before reaching application logic. Special rate limiting configurations are also applied to authentication endpoints to prevent brute force attacks and credential stuffing attempts.

    Each middleware component can be used independently or as part of the complete security stack, making it suitable for both monolithic deployments and distributed microservice architectures where different nodes may require different security configurations.

    Exequiel Trujillo

    1.0.0

    // Complete security stack setup in main application (src/app.ts)
    import {
    corsOptions,
    defaultRateLimiter,
    helmetOptions,
    sanitizeInput,
    securityHeaders,
    requestSizeLimiter
    } from '@/shared/middleware/security';

    const app = express();

    // Apply security middleware stack
    app.use(helmet(helmetOptions));
    app.use(securityHeaders);
    app.use(cors(corsOptions));
    app.use(defaultRateLimiter);
    app.use(requestSizeLimiter);
    app.use(sanitizeInput);
    // Authentication endpoint protection (src/modules/users/routes/authRoutes.ts)
    import { authRateLimiter } from '@/shared/middleware/security';

    const router = Router();

    // Apply strict rate limiting to auth endpoints
    router.post('/login', authRateLimiter, loginController);
    router.post('/register', authRateLimiter, registerController);
    // Custom rate limiter for specific endpoints
    import { createRateLimiter } from '@/shared/middleware/security';

    // Create custom rate limiter for API endpoints
    const apiRateLimiter = createRateLimiter(
    60 * 1000, // 1 minute window
    50 // 50 requests per minute
    );

    app.use('/api/v1', apiRateLimiter);

    Variables

    corsOptions
    defaultRateLimiter
    authRateLimiter
    SWAGGER_UI_VERSION
    helmetOptions

    Functions

    createRateLimiter
    sanitizeInput
    securityHeaders
    requestSizeLimiter