Trust No One
All input (URL params, headers, body, cookies) is untrusted. Validate it as early as possible.
Allowlist vs Blocklist
- Allowlist (Positive Validation): "Only allow 'a-z' and '0-9'". (Secure)
- Blocklist (Negative Validation): "Block 'script' tag". (Insecure - easy to bypass).
// Allowlist example
if (!/^[a-zA-Z0-9]+$/.test(username)) {
throw new Error("Invalid username");
}