Input Validation and Output Encoding Review
You'll learn how to audit code for proper input sanitization, validation, and output encoding to prevent injection attacks.
The Core Idea
When reviewing code for security, you're looking for two critical defenses: input validation (ensuring data entering your system is safe and expected) and output encoding (ensuring data leaving your system doesn't execute as code). These are your primary weapons against injection vulnerabilities like XSS, SQL injection, and command injection.
Think of input validation as a bouncer checking IDs at a club—only properly formatted, expected data gets in. Output encoding is like putting dangerous chemicals in sealed containers before shipping—the data stays data and never becomes executable instructions.
What to Look For
Input Validation:
- Are inputs checked against allowlists (preferred) or denylists?
- Do validation routines handle edge cases (null, empty, unicode)?
- Is validation applied server-side, not just client-side?
- Are length limits enforced appropriately?
Sanitization:
- Is user input stripped of dangerous characters before processing?
- Are sanitization libraries used correctly and consistently?
Output Encoding:
- Is data properly encoded for context (HTML, URL, JavaScript, SQL)?
- Are encoding functions applied at output points, not just input?
- Is the correct encoding method used for each context?
Canonicalization:
- Are paths, URLs, and filenames normalized to prevent bypass attacks?
- Does code handle encoded inputs (
%2e%2e%2fbecoming../)?
Review Strategy
Start where untrusted data enters (API endpoints, form handlers), trace its path through the application, and verify it's validated on entry and encoded on exit. Look for missing validation, weak regex patterns, or output points that skip encoding.
Key Takeaway: Effective security code review requires verifying that every input is validated against expected patterns and every output is encoded appropriately for its destination context—missing either creates exploitable injection vulnerabilities.