Double Encoding and Nested Encoding Attacks
What you'll learn: How attackers bypass input validation by encoding malicious payloads multiple times, causing security checks to miss threats that later get decoded and executed.
The Core Problem
Imagine a security guard checking IDs at a door. If someone wears a disguise over another disguise, and the guard only removes the first layer before checking, they might miss the real identity underneath.
Double encoding works the same way. An attacker encodes malicious input twice (or more). When your application decodes it only once during validation, the payload still looks harmless. But if the application later decodes it again before using it, the attack payload emerges intact.
How It Works
Consider this XSS payload:
<script>alert('XSS')</script>
URL-encoded once:
%3Cscript%3Ealert('XSS')%3C%2Fscript%3E
URL-encoded twice (double encoded):
%253Cscript%253Ealert('XSS')%253C%252Fscript%253E
Notice %25 is the encoding of % itself. If your validator decodes once, it sees %3Cscript... — which might still get blocked. But if it doesn't validate after that first decode, and the application decodes again later, the dangerous <script> tag appears.
Real-World Scenario
A web application validates path inputs to prevent directory traversal. An attacker submits:
..%252f..%252f..%252fetc%252fpasswd
First decode produces: ..%2f..%2f..%2fetc%2fpasswd (looks safe to a naive validator)
Second decode produces: ../../../etc/passwd (directory traversal!)
The Validation Gap
The vulnerability exists when:
- Validation happens after one decoding pass
- The application decodes input again before use
- No re-validation occurs after the final decode
Key Takeaway: Always validate input in its final, fully-decoded form before use, or validate at every decoding stage to prevent double/nested encoding attacks from bypassing security checks.