Node.js Security Guide
Here’s a complete, beginner-to-advanced guide on Node.js Security, covering common vulnerabilities, best practices, and practical tips to secure your Node.js applications.
🔐 Node.js Security Guide
Security is critical in Node.js apps because they often expose APIs, handle sensitive data, and run on the server. Implementing security best practices helps prevent attacks like XSS, CSRF, SQL Injection, and more.
1️⃣ Use Environment Variables for Secrets
-
Never hardcode secrets (DB passwords, API keys, JWT secrets)
-
Store in
.envfile (development) or CI/CD secrets (production)
-
Add
.envto.gitignore
2️⃣ Use HTTPS
-
Always use HTTPS in production
-
Generate SSL certificate via Let’s Encrypt or commercial CA
-
In Express.js:
3️⃣ Prevent Cross-Site Scripting (XSS)
-
Never trust user input
-
Sanitize input using libraries like DOMPurify, validator
-
Escape output in templates
-
Use HTTP headers with helmet:
4️⃣ Prevent SQL/NoSQL Injection
-
Use parameterized queries or ORM libraries
-
MySQL Example:
-
MongoDB Example:
-
Avoid string concatenation for queries
5️⃣ Protect Against Cross-Site Request Forgery (CSRF)
-
Use CSRF tokens in forms or API requests
-
Express example:
6️⃣ Secure Cookies
-
httpOnly→ prevent JS access -
secure→ only HTTPS -
sameSite→ prevent CSRF
7️⃣ Use JWT Securely
-
Keep secret key in environment variable
-
Set short expiration for tokens
-
Use
httpsto transmit tokens -
Example:
8️⃣ Limit Request Size and Rate
-
Prevent DoS attacks
-
Limit body size:
-
Rate limiting:
9️⃣ Handle Errors Securely
-
Never send stack traces to clients in production
10️⃣ Dependency Security
-
Regularly check dependencies:
-
Avoid outdated or untrusted packages
11️⃣ Enable Content Security Policy (CSP)
-
Protects against XSS and data injection
-
Example with helmet:
12️⃣ Secure Headers with Helmet
Helmet sets common HTTP headers:
Includes:
-
XSS protection
-
HSTS (HTTP Strict Transport Security)
-
Frameguard (Clickjacking protection)
-
Content-Type sniffing prevention
13️⃣ Session Security
-
Use express-session with secure config:
14️⃣ Prevent Directory Traversal
-
Never serve files using user input directly
-
Example safe usage:
15️⃣ Monitor Security in Production
-
Tools:
-
Snyk → Vulnerability scanning
-
Node Security Platform (nsp)
-
PM2 monitoring
-
WAF / Cloudflare
-
16️⃣ Security Best Practices Summary
| Area | Recommendation |
|---|---|
| Secrets | Store in .env or secret managers |
| HTTPS | Always in production |
| Input | Validate and sanitize |
| Database | Use parameterized queries |
| CSRF | Use tokens & secure cookies |
| JWT | Short-lived & HTTPS only |
| Logging | Avoid sensitive info in logs |
| Dependencies | Keep updated & check vulnerabilities |
| Headers | Use Helmet & CSP |
| Rate Limiting | Prevent DoS attacks |
🎯 Bonus: Quick Node.js Security Checklist
-
NODE_ENV=production -
HTTPS + secure cookies
-
Helmet headers + CSP
-
Validate & sanitize all input
-
JWT/Session security
-
Limit body size & request rate
-
No stack traces in production
-
Keep dependencies updated
-
Audit packages (
npm audit) -
Use environment variables for all secrets
