Overview
Notes and examples for securing backend applications (Node.js / Express). Each folder covers one topic: the attack, ❌ vulnerable code, ✅ secure code, the packages to use, and a checklist.
Core idea: defense in depth. Use many layers, so if one fails another still protects you.
Topics
| # | Topic | OWASP Top 10 (2021) |
|---|---|---|
| 01 | Authentication | A07 Identification & Auth Failures |
| 02 | Authorization | A01 Broken Access Control |
| 03 | Input Validation | A03 Injection |
| 04 | Injection (SQL / NoSQL / Command) | A03 Injection |
| 05 | XSS and CSP | A03 Injection |
| 06 | CSRF | A01 Broken Access Control |
| 07 | Sessions and JWT | A07 Identification & Auth Failures |
| 08 | Rate Limiting | A04 Insecure Design |
| 09 | Secrets Management | A05 Security Misconfiguration |
| 10 | Security Headers and CORS | A05 Security Misconfiguration |
| 11 | Error Handling and Logging | A09 Logging & Monitoring Failures |
| 12 | File Uploads | A04 Insecure Design |
| 13 | SSRF | A10 SSRF |
| 14 | Data Protection and Encryption | A02 Cryptographic Failures |
| 15 | Dependency Security | A06 Vulnerable Components, A08 Integrity Failures |
| 16 | Business Logic | A04 Insecure Design |
| 17 | Infrastructure and Docker | A05 Security Misconfiguration |
| 18 | Security Testing and SDLC | All |
| 19 | Interview Questions (with counter questions) | All |
Packages at a glance (Node.js)
| Package | Purpose | Topic |
|---|---|---|
argon2 / bcrypt | Password hashing | 01 |
passport | Auth strategies (local, OAuth) | 01 |
otplib, qrcode | MFA / TOTP | 01 |
@casl/ability, accesscontrol | Permissions / RBAC | 02 |
zod, joi, express-validator | Input validation | 03 |
hpp | HTTP parameter pollution | 03 |
express-mongo-sanitize | NoSQL injection | 04 |
prisma, knex, pg | Parameterized queries | 04 |
isomorphic-dompurify, sanitize-html, escape-html | XSS | 05 |
csrf-csrf, csrf-sync | CSRF tokens (csurf is deprecated) | 06 |
express-session, connect-redis | Server sessions | 07 |
jsonwebtoken, jose | JWT | 07 |
express-rate-limit, rate-limiter-flexible, rate-limit-redis | Rate limiting | 08 |
dotenv, envalid | Env config + validation | 09 |
helmet | Security headers | 10 |
cors | CORS | 10 |
pino, winston, @sentry/node | Logging and error tracking | 11 |
multer, file-type, sharp | Safe file uploads | 12 |
ssrf-req-filter, ipaddr.js | SSRF protection | 13 |
crypto (built in), libsodium-wrappers | Encryption, hashing, random tokens | 14 |
redlock, decimal.js | Locks, money math | 16 |
eslint-plugin-security, supertest, jest | Linting, security tests | 18 |
Tools (not npm packages)
npm audit, Dependabot, Snyk, gitleaks, Semgrep, SonarQube, OWASP ZAP, Burp Suite, Trivy, hadolint, Checkov, Cloudflare/AWS WAF, AWS Secrets Manager / HashiCorp Vault.
Minimal secure Express setup
const express = require('express');
const helmet = require('helmet');
const cors = require('cors');
const rateLimit = require('express-rate-limit');
const app = express();
app.set('trust proxy', 1);
app.use(helmet());
app.use(cors({ origin: ['https://app.example.com'], credentials: true }));
app.use(express.json({ limit: '100kb' }));
app.use(rateLimit({ windowMs: 15 * 60 * 1000, limit: 300 }));
// routes: validate input (zod) → authenticate → authorize → parameterized DB queries
app.use((err, req, res, next) => {
console.error(err);
res.status(err.status || 500).json({ error: err.status < 500 ? err.message : 'Something went wrong' });
});