Backend Security Best Practices
Practical backend security notes: attacks, vulnerable vs. secure Node.js code, packages and checklists.
Start readingAuthentication
Authorization
Authorization runs after login and decides what the user may access. Broken Access Control is #1 in the OWASP Top 10.
Input Validation
All input is untrusted: body, query, params, headers, cookies, files, webhooks, even data from your own frontend.
Injection
Injection happens when user input is treated as code instead of data.
XSS and CSP
XSS runs an attacker's JavaScript in another user's browser, where it can steal sessions, act as that user, or change the page.
CSRF
The attacker's site makes the victim's browser send a request to your app. The browser adds the cookies automatically, so the request is authenticated.
Sessions and JWT
After login, the server needs to remember the user. There are two main ways.
Rate Limiting
Rate limiting stops brute force, credential stuffing, scraping, spam and cost-based DoS (expensive endpoints, SMS/email sending).
Secrets Management
Secrets are DB passwords, API keys, JWT secrets and private keys. Bots scan public GitHub for leaked keys within minutes.
Security Headers and CORS
The server sends these to tell the browser to turn on its protections.
Error Handling and Logging
Two goals: 1. Don't leak internals to users (stack traces, SQL errors, file paths, versions) 2. Do log enough to detect and investigate attacks, without…
File Uploads
File uploads are one of the riskiest features: remote code execution, stored XSS (SVG/HTML), path traversal, disk-filling DoS and malware.
SSRF
Your server fetches a URL the user gave it (webhooks, "import from URL", link previews, PDF generators). The attacker gives an internal URL instead:
Data Protection and Encryption
Hashing = one way (passwords). Encryption = two way (data you need to read back).
Dependency Security
Most of your app is other people's code. One bad package can compromise everything (event-stream, ua-parser-js, the xz backdoor).
Business Logic
Scanners can't find these bugs, because the code "works". The flaw is in the logic.
Infrastructure and Docker
Secure code on an insecure server is still insecure.
Security Testing
Build security into every stage instead of checking it once at the end ("shift left").
Interview Questions
Interviewers rarely stop at the first answer. They push back with a counter question to see if you really understand the trade-off. Each question below has a…