OWASP Top 10 là baseline kiến thức security cho mọi web dev — list 10 vulnerability nguy hiểm nhất web do OWASP duy trì. Bài này điểm qua 10 lỗ hổng với ví dụ thực tế + fix cụ thể, không lý thuyết suông. Áp dụng được cho mọi stack backend Node.js, Python, Go, Java.
Hiểu OWASP Top 10 là step 1 cho security mindset. Step 2 là thread modeling cho từng feature mới — đặt câu hỏi "attacker có thể abuse như thế nào" trước khi viết code. Bài viết này tập trung step 1.
A01: Broken Access Control (#1)
User truy cập resource không thuộc về họ. Lỗi phổ biến nhất.
// ❌ Không check ownership
app.get('/api/orders/:id', requireAuth, async (req, res) => {
const order = await db.orders.findById(req.params.id)
res.json(order) // user A xem được order của user B!
})
// ✓ Verify ownership
app.get('/api/orders/:id', requireAuth, async (req, res) => {
const order = await db.orders.findById(req.params.id)
if (order.user_id !== req.user.id) return res.status(403).end()
res.json(order)
})
A02: Cryptographic Failures
- Password: dùng bcrypt/argon2, KHÔNG MD5/SHA1
- Data sensitive: AES-GCM (authenticated encryption)
- Transit: TLS 1.3, HSTS header
- Random:
crypto.randomBytes, khôngMath.random
A03: Injection
SQL, NoSQL, command, LDAP. Luôn parameterized:
// ❌ String concat → SQL injection
db.query(`SELECT * FROM users WHERE email = '${email}'`)
// ✓ Parameterized
db.query('SELECT * FROM users WHERE email = $1', [email])
A04: Insecure Design
Thiết kế thiếu threat modeling. Ví dụ: forgot password mà không có rate limit → enum email tồn tại. Cần threat model trước khi code.
A05: Security Misconfiguration
- Default credential admin/admin
- Stack trace expose production
- Cloud bucket public không cần
- Header missing: HSTS, CSP, X-Frame-Options
A06: Vulnerable Components
npm audit --production
npm install -g snyk && snyk test
# CI: dependabot, renovate auto-PR cho update
A07: Authentication Failures
- Brute-force protect: rate limit + lockout
- Password requirement: ≥ 12 char (NIST), không max length thấp
- Session: TTL ngắn, regenerate sau login, invalidate on logout
- MFA cho account quan trọng
A08: Software/Data Integrity
- CI/CD: verify signature artifact
- Auto-update không có verify → supply chain attack
- Deserialize untrusted data → RCE (Java, Python pickle)
A09: Security Logging Failures
Đăng nhập fail, access denied, payment refund — phải log với context: user, IP, timestamp. Ship vào SIEM (xem Log aggregation).
A10: SSRF
Server fetch URL user supplied → có thể truy cập internal service:
// ❌ Trust user URL
app.post('/preview', async (req, res) => {
const html = await fetch(req.body.url).then(r => r.text())
res.send(html) // user gửi http://169.254.169.254/ → AWS metadata leak
})
// ✓ Whitelist domain + block private IP
const ALLOWED = ['github.com', 'medium.com']
const url = new URL(req.body.url)
if (!ALLOWED.includes(url.hostname)) return res.status(400).end()
if (isPrivateIP(await dns.resolve(url.hostname))) return res.status(400).end()
Checklist baseline
- ✅ Auth check mọi endpoint
- ✅ Parameterized query mọi SQL
- ✅ HTTPS only, HSTS, CSP
- ✅ npm audit + dependabot
- ✅ Rate limit /login, /reset-password
- ✅ Log security event vào SIEM
- ✅ Annual pen test bởi 3rd party
Kết luận
OWASP Top 10 là sàn — không phải trần — của security. Build security mindset từ thiết kế (threat model), code (review, lint), runtime (monitor, alert). Đọc API security checklist cho deep-dive backend.