Docs

21.2-Secure-Coding

21.2 Secure Coding Practices

Overview

Secure coding is about writing code that is resistant to attacks by design. This section covers techniques for input validation, output encoding, and secure handling of sensitive data.

Input Validation Principles

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     Input Validation Pyramid                                β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                             β”‚
β”‚                            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                β”‚
β”‚                            β”‚   Business    β”‚ ← Business rules validation    β”‚
β”‚                            β”‚    Logic      β”‚                                β”‚
β”‚                            β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜                                β”‚
β”‚                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                          β”‚
β”‚                       β”‚      Semantic           β”‚ ← Value range, format     β”‚
β”‚                       β”‚      Validation         β”‚                           β”‚
β”‚                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                          β”‚
β”‚              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                 β”‚
β”‚              β”‚           Syntactic Validation            β”‚ ← Type, length  β”‚
β”‚              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                 β”‚
β”‚    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”           β”‚
β”‚    β”‚                    Sanitization Layer                      β”‚ ← Clean  β”‚
β”‚    β”‚              (trim, normalize, encode)                     β”‚   input  β”‚
β”‚    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β”‚
β”‚                                                                             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Validation Strategies

Whitelist vs Blacklist

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Whitelist vs Blacklist Validation                        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Whitelist (Recommended)         β”‚ Blacklist (Avoid)                        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ βœ“ Explicitly allow known good   β”‚ βœ— Try to block known bad                β”‚
β”‚ βœ“ Secure by default             β”‚ βœ— Insecure by default                   β”‚
β”‚ βœ“ Easy to maintain              β”‚ βœ— Constantly needs updating              β”‚
β”‚ βœ“ Handles unknown attacks       β”‚ βœ— New attacks bypass filters             β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Example:                        β”‚ Example:                                 β”‚
β”‚ const allowed = ['jpg', 'png']; β”‚ const blocked = ['exe', 'js'];           β”‚
β”‚ if (allowed.includes(ext))      β”‚ if (!blocked.includes(ext))              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Input Validation Patterns

// ❌ Blacklist approach - can be bypassed
function validateBlacklist(input) {
  const dangerous = ['<script>', 'javascript:', 'onerror'];
  return !dangerous.some((d) => input.includes(d));
}

// βœ“ Whitelist approach - only allow known good
function validateWhitelist(input, pattern) {
  return pattern.test(input);
}

// βœ“ Schema validation - define exactly what's allowed
const userSchema = {
  username: {
    type: 'string',
    pattern: /^[a-zA-Z0-9_]{3,20}$/,
    required: true,
  },
  email: {
    type: 'string',
    pattern: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
    required: true,
  },
  age: {
    type: 'number',
    min: 13,
    max: 120,
  },
};

Output Encoding

Context-Specific Encoding

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                       Context-Specific Encoding                             β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Context          β”‚ Encoding Required                                        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ HTML Body        β”‚ HTML Entity encoding (&lt; &gt; &amp;)                  β”‚
β”‚ HTML Attribute   β”‚ HTML Attribute encoding + quote escape                   β”‚
β”‚ JavaScript       β”‚ JavaScript Unicode encoding (\uXXXX)                     β”‚
β”‚ CSS              β”‚ CSS escape sequences (\XX)                               β”‚
β”‚ URL              β”‚ URL encoding (%XX)                                       β”‚
β”‚ JSON             β”‚ Proper JSON encoding + context encoding                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
// Different encoding for different contexts
const encoders = {
  // HTML body context
  html: (str) =>
    str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;'),

  // HTML attribute context
  htmlAttr: (str) =>
    str
      .replace(/&/g, '&amp;')
      .replace(/"/g, '&quot;')
      .replace(/'/g, '&#x27;')
      .replace(/</g, '&lt;')
      .replace(/>/g, '&gt;'),

  // JavaScript string context
  js: (str) =>
    str
      .replace(/\\/g, '\\\\')
      .replace(/'/g, "\\'")
      .replace(/"/g, '\\"')
      .replace(/\n/g, '\\n')
      .replace(/\r/g, '\\r'),

  // URL component
  url: (str) => encodeURIComponent(str),
};

Secure Data Handling

Sensitive Data Flow

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        Sensitive Data Lifecycle                             β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                             β”‚
β”‚    Collection         Processing         Storage          Disposal          β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”        β”‚
β”‚   β”‚ HTTPS   │───────►│ Memory  │──────►│Encrypted│─────►│ Secure  β”‚        β”‚
β”‚   β”‚ Only    β”‚        β”‚ Only    β”‚       β”‚ Storage β”‚      β”‚ Delete  β”‚        β”‚
β”‚   β”‚         β”‚        β”‚ No Logs β”‚       β”‚         β”‚      β”‚         β”‚        β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β”‚
β”‚       β”‚                  β”‚                 β”‚                β”‚               β”‚
β”‚       β–Ό                  β–Ό                 β–Ό                β–Ό               β”‚
β”‚   Validate           Process          Hash/Encrypt     Zero Memory          β”‚
β”‚   Sanitize           In Memory        Key Management   Delete Files         β”‚
β”‚   Minimize           No Temp Files    Access Control   Clear Backups        β”‚
β”‚                                                                             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Password Handling

Secure Password Requirements

// Password security checklist
const passwordRequirements = {
  minLength: 12, // Minimum 12 characters
  maxLength: 128, // Reasonable maximum
  requireUppercase: true, // At least one uppercase
  requireLowercase: true, // At least one lowercase
  requireNumbers: true, // At least one number
  requireSpecial: true, // At least one special char
  preventCommon: true, // Check against common passwords
  preventUsername: true, // Can't contain username
  preventReuse: 5, // Can't reuse last 5 passwords
};

// Never do this
const bad = {
  storeInPlainText: true, // ❌
  sendInEmail: true, // ❌
  logPasswords: true, // ❌
  useReversibleEncryption: true, // ❌
  useMD5orSHA1: true, // ❌
};

// Always do this
const good = {
  useModernHash: 'bcrypt/argon2', // βœ“
  useSalt: true, // βœ“ (automatic with bcrypt)
  useHTTPS: true, // βœ“
  rateLimit: true, // βœ“
  implementLockout: true, // βœ“
};

Error Handling Security

Secure Error Messages

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      Error Message Security                                 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                             β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚   β”‚                        Development                                    β”‚ β”‚
β”‚   β”‚  Detailed errors                Stack traces                          β”‚ β”‚
β”‚   β”‚  Variable contents              File paths                            β”‚ β”‚
β”‚   β”‚  SQL queries                    Internal architecture                 β”‚ β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚                                   β”‚                                         β”‚
β”‚                                   β–Ό                                         β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚   β”‚                        Production                                     β”‚ β”‚
β”‚   β”‚  Generic messages only          "An error occurred"                   β”‚ β”‚
β”‚   β”‚  Log details server-side        Use error IDs                         β”‚ β”‚
β”‚   β”‚  Never expose internals         Sanitize before display               β”‚ β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚                                                                             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
// ❌ Dangerous - reveals internal information
catch (error) {
    return res.status(500).json({
        error: error.message,
        stack: error.stack,
        query: sql.query
    });
}

// βœ“ Secure - log internally, return generic message
catch (error) {
    const errorId = generateErrorId();
    logger.error({ errorId, error, userId });

    return res.status(500).json({
        error: 'An unexpected error occurred',
        errorId: errorId
    });
}

Secure Configuration

Environment-Based Configuration

// Configuration security patterns
const config = {
  // βœ“ Use environment variables
  dbPassword: process.env.DB_PASSWORD,

  // βœ“ Different settings per environment
  debug: process.env.NODE_ENV !== 'production',

  // βœ“ Secure defaults
  sessionTimeout: parseInt(process.env.SESSION_TIMEOUT) || 3600,

  // βœ“ Validate configuration
  validate() {
    if (!this.dbPassword) {
      throw new Error('DB_PASSWORD must be set');
    }
  },
};

// Never hardcode secrets
const NEVER = {
  apiKey: 'sk_live_1234567890', // ❌
  dbPassword: 'password123', // ❌
  jwtSecret: 'mysecret', // ❌
};

Secure Communication

API Security

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                          API Security Layers                                β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                             β”‚
β”‚  Layer 1: Transport                                                        β”‚
β”‚  β”œβ”€β”€ HTTPS/TLS only                                                        β”‚
β”‚  β”œβ”€β”€ Strong cipher suites                                                  β”‚
β”‚  └── Certificate validation                                                β”‚
β”‚                                                                             β”‚
β”‚  Layer 2: Authentication                                                   β”‚
β”‚  β”œβ”€β”€ API keys or tokens                                                    β”‚
β”‚  β”œβ”€β”€ JWT with proper validation                                            β”‚
β”‚  └── OAuth 2.0 for third-party                                            β”‚
β”‚                                                                             β”‚
β”‚  Layer 3: Authorization                                                    β”‚
β”‚  β”œβ”€β”€ Role-based access control                                             β”‚
β”‚  β”œβ”€β”€ Resource-level permissions                                            β”‚
β”‚  └── Principle of least privilege                                          β”‚
β”‚                                                                             β”‚
β”‚  Layer 4: Input Validation                                                 β”‚
β”‚  β”œβ”€β”€ Schema validation                                                     β”‚
β”‚  β”œβ”€β”€ Type checking                                                         β”‚
β”‚  └── Size limits                                                           β”‚
β”‚                                                                             β”‚
β”‚  Layer 5: Rate Limiting                                                    β”‚
β”‚  β”œβ”€β”€ Per-user limits                                                       β”‚
β”‚  β”œβ”€β”€ Per-endpoint limits                                                   β”‚
β”‚  └── Burst protection                                                      β”‚
β”‚                                                                             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Dependency Security

Managing Third-Party Dependencies

// Security practices for dependencies
const dependencyPractices = {
  // Audit regularly
  audit: 'npm audit',

  // Use lockfiles
  lockfile: 'package-lock.json',

  // Pin versions or use ranges carefully
  versioning: {
    pinned: '1.2.3', // Exact version
    patch: '~1.2.3', // Patch updates only
    minor: '^1.2.3', // Minor updates only
  },

  // Review before installing
  review: [
    'Check npm/GitHub for maintenance',
    'Review source code for red flags',
    'Check download counts and community',
    'Verify package authenticity',
  ],
};

Security Checklist

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      Secure Coding Checklist                                β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                             β”‚
β”‚ Input Handling:                                                            β”‚
β”‚ β–‘ Validate all input on server side                                       β”‚
β”‚ β–‘ Use whitelist validation                                                 β”‚
β”‚ β–‘ Sanitize before processing                                               β”‚
β”‚ β–‘ Set maximum input lengths                                                β”‚
β”‚                                                                             β”‚
β”‚ Output Handling:                                                           β”‚
β”‚ β–‘ Encode output based on context                                           β”‚
β”‚ β–‘ Use templating engines with auto-escaping                                β”‚
β”‚ β–‘ Set proper Content-Type headers                                          β”‚
β”‚                                                                             β”‚
β”‚ Authentication:                                                            β”‚
β”‚ β–‘ Use strong password hashing (bcrypt/argon2)                              β”‚
β”‚ β–‘ Implement account lockout                                                β”‚
β”‚ β–‘ Secure password reset flows                                              β”‚
β”‚ β–‘ Use MFA where appropriate                                                β”‚
β”‚                                                                             β”‚
β”‚ Session Management:                                                        β”‚
β”‚ β–‘ Use secure, HttpOnly cookies                                             β”‚
β”‚ β–‘ Regenerate session ID on login                                           β”‚
β”‚ β–‘ Implement session timeout                                                β”‚
β”‚                                                                             β”‚
β”‚ Error Handling:                                                            β”‚
β”‚ β–‘ Log errors server-side                                                   β”‚
β”‚ β–‘ Return generic error messages                                            β”‚
β”‚ β–‘ Don't expose stack traces                                                β”‚
β”‚                                                                             β”‚
β”‚ Data Protection:                                                           β”‚
β”‚ β–‘ Encrypt sensitive data at rest                                           β”‚
β”‚ β–‘ Use HTTPS for all traffic                                                β”‚
β”‚ β–‘ Minimize data collection                                                 β”‚
β”‚ β–‘ Implement proper access controls                                         β”‚
β”‚                                                                             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key Takeaways

  1. β€’Validate all input - Never trust user input
  2. β€’Encode all output - Context-aware encoding is essential
  3. β€’Use strong cryptography - Modern algorithms, proper key management
  4. β€’Secure by default - Fail closed, not open
  5. β€’Minimize attack surface - Less code, fewer endpoints
  6. β€’Defense in depth - Multiple security layers
  7. β€’Keep secrets secret - Proper configuration management

Files in This Section

  • β€’README.md - This documentation
  • β€’examples.js - Secure coding pattern examples
  • β€’exercises.js - Security implementation exercises
.2 Secure Coding - JavaScript Tutorial | DeepML