Course contentsShow
← Security
Lesson 1166 of 3,05224. Application Security - Input ValidationFree lesson

Defense: Canonical Form Validation Strategies

Implementing robust validation by converting all input to canonical form before security checks and using safe APIs.

Defense: Canonical Form Validation Strategies

What you'll learn: How to safely validate input by converting it to a standard, simplified form first—closing the door on encoding tricks and path manipulation attacks.

The Core Problem

Attackers exploit the gap between how input looks and what it actually means. They use double encoding, Unicode tricks, mixed case, null bytes, and path traversal sequences to sneak past validation. If you check the raw input without standardizing it first, you're checking the disguise, not the real data.

The Canonical Form Solution

Canonicalization means converting input to its simplest, standard representation before applying security checks. Think of it like removing all disguises before checking someone's ID at a secure facility.

For example:

  • ../../../etc/passwd and .%2F..%2F..%2Fetc%2Fpasswd both canonicalize to the same dangerous path
  • <script> and %3Cscript%3E both represent the same XSS vector
  • admin and ADMIN might be the same username

Implementation Strategy

  1. Decode all encoding layers (URL encoding, HTML entities, Unicode normalization)
  2. Resolve path shortcuts (collapse .., ., redundant slashes)
  3. Normalize case according to your context (case-insensitive filesystems, usernames)
  4. Remove null bytes and other terminator tricks
  5. THEN validate against your allowlist or security rules

Use Safe APIs

Modern frameworks provide canonicalization functions:

  • File path resolution APIs that automatically collapse traversals
  • URL parsers that decode and normalize
  • Unicode normalization libraries

Don't write your own parser—use battle-tested libraries that handle edge cases.

Key Takeaway:
Always convert input to canonical form before validation—validate the reality, not the disguise. Use trusted canonicalization APIs rather than building your own.