Overlong UTF-8 Encoding Attacks
You'll learn how attackers exploit non-standard UTF-8 encodings to sneak malicious input past security filters.
The Core Problem
UTF-8 allows the same character to be encoded in multiple ways—some longer than necessary. For example, the forward slash / should be encoded as 0x2F (one byte), but UTF-8 technically could represent it as 0xC0 0xAF (two bytes) or even longer sequences. These "overlong" encodings are non-standard and prohibited by the UTF-8 specification—but not all validators reject them.
Think of it like writing "cat" as "kat" or "katt"—same sound, different spelling. If a spell-checker only blocks "cat" but accepts the alternatives, you've bypassed the filter.
The Attack
Security filters often scan for dangerous characters like ../ (path traversal), <script> (XSS), or null bytes. If these filters check input before UTF-8 decoding or normalization happens—or if different system layers decode differently—attackers can disguise their payload:
- Normal:
../../../etc/passwd← blocked by filter - Overlong:
.%C0%AF.%C0%AF.%C0%AF← bypasses filter, decoded as../later
The application's security layer sees gibberish and allows it through. Later, when the filesystem or interpreter decodes it into standard UTF-8, the attack payload executes.
Why This Works
Different components may:
- Decode at different stages
- Use different decoding libraries with varying strictness
- Normalize (or fail to normalize) inconsistently
This creates a canonicalization gap—your validator sees one thing, your backend processes another.
Key Takeaway: Always decode and normalize input to its canonical (shortest) UTF-8 form before validation, and reject any overlong or non-standard encodings outright to prevent bypass attacks.