10 Security Rules for Writing Safe Code
Never trust external data. Validate on entry, sanitize on exit. Use parameterized queries.
// ❌ BAD: SQL Injection
db.query(`SELECT * WHERE name = '${query}'`)
// ✅ GOOD: Parameterized
db.query('SELECT * WHERE name = $1', [query])Every component runs with minimum permissions. Database users should have only needed access. Don't use admin credentials for app connections.
No API keys, passwords, or tokens in source code. Use environment variables or secrets managers.
// ❌ BAD
const API_KEY = 'sk_live_123';
// ✅ GOOD
const apiKey = process.env.API_KEY;Use established libraries (Passport, Auth0). Never roll your own auth. Use secure, httpOnly, sameSite cookies.
Use TLS 1.2+ for all connections. Encrypt sensitive data in database with AES-256-GCM.
Check permissions on every request. Deny by default. Test for horizontal and vertical privilege escalation.
SQL, NoSQL, Command, and LDAP injection: never concatenate untrusted data into commands.
Run npm audit in CI. Use Dependabot. Remove unused dependencies. Pin versions in production.
Validate file type (magic bytes), limit size, generate random filenames, store outside webroot. Never execute uploaded files.
Log authentication failures, authorization denials. Never log passwords, tokens, or secrets.