Node.js Security Audit
Structured security audit for Node.js HTTP servers and web applications.
Audit Checklist
Critical (Must Fix Before Deploy)
Hardcoded Secrets
- Search for: API keys, passwords, tokens in source code
- Pattern:
grep -rn "password\|secret\|token\|apikey\|api_key" --include="*.js" --include="*.ts" | grep -v node_modules | grep -v "process.env\|\.env"
- Fix: Move to env vars, fail if missing:
if (!process.env.SECRET) process.exit(1);
XSS in Dynamic Content
- Search for:
innerHTML, template literals injected into DOM, unsanitized user input in responses
- Fix: Use
textContent, or escape: str.replace(/[&<>"']/g, c => ({'&':'&','<':'<','>':'>','"':'"',"'":"'"}[c]))
SQL/NoSQL Injection
- Search for: String concatenation in queries,
eval(), Function() with user input
- Fix: Parameterized queries, input validation
High (Should Fix)
CORS Misconfiguration
- Search for:
Access-Control-Allow-Origin: *
- Fix: Allowlist specific origins:
const origin = ALLOWED.has(req.headers.origin) ? req.headers.origin : ALLOWED.values().next().value
Auth Bypass
- Check: Every route that should require auth actually checks it
- Common miss: Static file routes, agent/webhook endpoints, health checks that expose data
Path Traversal
- Check:
path.normalize() + startsWith(allowedDir) on all file-serving routes
- Extra: Resolve symlinks with
fs.realpathSync() and re-check
Medium (Recommended)
Security Headers
const HEADERS = {
'X-Frame-Options': 'SAMEORIGIN',
'X-Content-Type-Options': 'nosniff',
'Referrer-Policy': 'strict-origin-when-cross-origin',
'Permissions-Policy': 'camera=(), microphone=(), geolocation=()',
};
// Apply to all responses
Rate Limiting
const attempts = new Map(); // ip -> { count, resetAt }
const LIMIT = 5, WINDOW = 60000;
function isLimited(ip) {
const now = Date.now(), e = attempts.get(ip);
if (!e || now > e.resetAt) { attempts.set(ip, {count:1, resetAt:now+WINDOW}); return false; }
return ++e.count > LIMIT;
}
Input Validation
- Body size limits:
if (bodySize > 1048576) { req.destroy(); return; }
- JSON parse in try/catch
- Type checking on expected fields
Low (Consider)
Dependency Audit: npm audit
Error Leakage: Don't send stack traces to clients in production
Cookie Security: HttpOnly; Secure; SameSite=Strict
Report Format
## Security Audit: [filename]
### Critical
1. **[Category]** Description — File:Line — Fix: ...
### High
...
### Medium
...
### Low
...
### Summary
X critical, X high, X medium, X low
1---
2name: nodejs-security-audit
3description: Audit Node.js HTTP servers and web apps for security vulnerabilities. Checks OWASP Top 10, CORS, auth bypass, XSS, path traversal, hardcoded secrets, missing headers, rate limiting, and input validation. Use when reviewing server code before deployment or after changes.
4metadata:
5 source: https://skillmd.com/skills/modbender/nodejs-security-audit
6---
7
8# Node.js Security Audit
9
10Structured security audit for Node.js HTTP servers and web applications.
11
12## Audit Checklist
13
14### Critical (Must Fix Before Deploy)
15
16**Hardcoded Secrets**
17- Search for: API keys, passwords, tokens in source code
18- Pattern: `grep -rn "password\|secret\|token\|apikey\|api_key" --include="*.js" --include="*.ts" | grep -v node_modules | grep -v "process.env\|\.env"`
19- Fix: Move to env vars, fail if missing: `if (!process.env.SECRET) process.exit(1);`
20
21**XSS in Dynamic Content**
22- Search for: `innerHTML`, template literals injected into DOM, unsanitized user input in responses
23- Fix: Use `textContent`, or escape: `str.replace(/[&<>"']/g, c => ({'&':'&','<':'<','>':'>','"':'"',"'":"'"}[c]))`
24
25**SQL/NoSQL Injection**
26- Search for: String concatenation in queries, `eval()`, `Function()` with user input
27- Fix: Parameterized queries, input validation
28
29### High (Should Fix)
30
31**CORS Misconfiguration**
32- Search for: `Access-Control-Allow-Origin: *`
33- Fix: Allowlist specific origins: `const origin = ALLOWED.has(req.headers.origin) ? req.headers.origin : ALLOWED.values().next().value`
34
35**Auth Bypass**
36- Check: Every route that should require auth actually checks it
37- Common miss: Static file routes, agent/webhook endpoints, health checks that expose data
38
39**Path Traversal**
40- Check: `path.normalize()` + `startsWith(allowedDir)` on all file-serving routes
41- Extra: Resolve symlinks with `fs.realpathSync()` and re-check
42
43### Medium (Recommended)
44
45**Security Headers**
46```javascript
47const HEADERS = {
48 'X-Frame-Options': 'SAMEORIGIN',
49 'X-Content-Type-Options': 'nosniff',
50 'Referrer-Policy': 'strict-origin-when-cross-origin',
51 'Permissions-Policy': 'camera=(), microphone=(), geolocation=()',
52};
53// Apply to all responses
54```
55
56**Rate Limiting**
57```javascript
58const attempts = new Map(); // ip -> { count, resetAt }
59const LIMIT = 5, WINDOW = 60000;
60function isLimited(ip) {
61 const now = Date.now(), e = attempts.get(ip);
62 if (!e || now > e.resetAt) { attempts.set(ip, {count:1, resetAt:now+WINDOW}); return false; }
63 return ++e.count > LIMIT;
64}
65```
66
67**Input Validation**
68- Body size limits: `if (bodySize > 1048576) { req.destroy(); return; }`
69- JSON parse in try/catch
70- Type checking on expected fields
71
72### Low (Consider)
73
74**Dependency Audit:** `npm audit`
75**Error Leakage:** Don't send stack traces to clients in production
76**Cookie Security:** `HttpOnly; Secure; SameSite=Strict`
77
78## Report Format
79
80```
81## Security Audit: [filename]
82
83### Critical
841. **[Category]** Description — File:Line — Fix: ...
85
86### High
87...
88
89### Medium
90...
91
92### Low
93...
94
95### Summary
96X critical, X high, X medium, X low
97```