BACK TO TOPICS
Intermediate

Secure Coding Practices

1. Input Validation

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");
}

Module Progress

1Input Validation
2Output Encoding
3Error Handling
4Logging
5Dependency Management
Progress20%