Course contentsShow
← Security
Lesson 1209 of 3,05225. Application Security - Secure CodingFree lesson

Defense in Depth Through Layered Validation

Implement multiple validation layers so failure of one control doesn't compromise security.

Defense in Depth Through Layered Validation

What You'll Learn

You'll learn why relying on a single validation check is dangerous and how multiple validation layers create resilient security.

The Core Idea

Defense in depth means implementing multiple, independent security controls so that if one fails, others still protect your application. In validation, this means checking input at multiple points and in different ways—never trusting that a single check is infallible.

Think of it like airport security: you show ID at check-in, pass through metal detectors, get your bags scanned, and agents watch for suspicious behavior. If one control fails (a fake ID slips through), the other layers still catch threats.

Why Single-Layer Validation Fails

Attackers constantly find bypasses:

  • A client-side JavaScript check? Easily disabled in the browser
  • A single regex validation? Might miss edge cases or suffer from canonicalization attacks
  • One allowlist check? Could be circumvented through encoding tricks

When your entire security posture depends on one validation point, a single mistake or bypass compromises everything.

Implementing Layered Validation

Apply validation at multiple trust boundaries:

  1. Client-side validation – improves user experience but is not security
  2. Input validation layer – checks data type, format, and allowlist rules
  3. Business logic validation – ensures semantic correctness (e.g., withdrawal amount ≤ balance)
  4. Output encoding – prevents injection even if bad data got through
  5. Database constraints – final integrity check at storage layer

Each layer uses different techniques and validates different aspects. A canonicalization attack might bypass your path traversal check, but your file extension allowlist and filesystem permissions still block the exploit.

Practical Example Mindset

Don't just check if a filename is "safe"—also verify the extension, validate against an allowlist, canonicalize the path, restrict filesystem access, and sandbox the operation.

Key Takeaway: Never rely on a single validation control; implement multiple independent validation layers at different points so attackers must defeat several defenses, not just one.