Cybersecurity Best Practices for Modern Web Applications: The Defense-in-Depth Approach

On this page

Table of contents

    Modern web security cannot rely on a single defensive layer. With automated bots continuously scanning public endpoints for misconfigurations, SQL injection vectors, and broken access control, adopting a Defense-in-Depth methodology is non-negotiable for enterprise engineering teams.

    1. Authentication & Password Storage Standards

    Passwords must never be stored in plain text or using outdated hashing algorithms like MD5 or SHA-1. Use adaptive hashing algorithms such as Argon2id or bcrypt with an appropriate work factor (cost factor 10 to 12) to resist offline GPU brute-force attacks:

    import bcrypt from 'bcrypt';
    import jwt from 'jsonwebtoken';
    
    const SALT_ROUNDS = 12;
    
    export async function hashPassword(plainPassword: string): Promise<string> {
      return await bcrypt.hash(plainPassword, SALT_ROUNDS);
    }
    
    export async function verifyPassword(plainPassword: string, hash: string): Promise<boolean> {
      return await bcrypt.compare(plainPassword, hash);
    }
    
    export function generateAccessToken(userId: string): string {
      return jwt.sign({ sub: userId }, process.env.JWT_SECRET!, {
        expiresIn: '15m', // Short-lived access token
        algorithm: 'HS256'
      });
    }

    2. Neutralizing SQL Injection with Parameterized Queries

    SQL Injection (SQLi) remains one of the most critical vulnerabilities on the web. It occurs when untrusted user input is directly concatenated into database queries. Always enforce parameterized prepared statements through modern Object-Relational Mapping (ORM) tools or parameterized database drivers.

    3. Preventing Cross-Site Scripting (XSS) with Content Security Policy

    While sanitizing incoming HTML using libraries like DOMPurify is crucial, deploying a rigorous Content Security Policy (CSP) provides a second line of defense that instructs the browser to reject unauthorized scripts, inline eval statements, and cross-site exfiltration channels.

    Leave a Reply