Trang chủ / Bài 15
Chuyên sâu ⏱ 30 phút
15

Security Audit và Code Review Tự động

Dùng Claude Code như một security engineer để phát hiện lỗ hổng và review code tự động

🎯 Mục tiêu bài học

  • Chạy security audit toàn bộ codebase
  • Phát hiện OWASP Top 10 vulnerabilities
  • Tự động hóa code review trước khi merge PR
  • Tạo security checklist cho dự án

Claude Code như Security Engineer

Claude Code hiểu các vulnerability patterns phổ biến và có thể scan toàn bộ codebase để tìm lỗ hổng bảo mật — nhanh hơn và rẻ hơn nhiều so với hire security consultant.

Security Audit cơ bản

> Chay security audit cho toan bo thu muc src/.
  Tim cac van de: SQL injection, XSS, CSRF, insecure deserialization,
  hardcoded secrets, weak cryptography, path traversal.
  Bao cao theo muc do nghiem trong: Critical, High, Medium, Low.

> Tim tat ca noi dung user input ma khong co validation
  hoac sanitization trong project nay.

> Kiem tra cac dependency co known vulnerabilities:
  pip-audit || safety check

OWASP Top 10 Checklist

> Kiem tra project theo OWASP Top 10 2021:
  A01: Broken Access Control - co endpoint nao thieu auth khong?
  A02: Cryptographic Failures - co luu password plain text khong?
  A03: Injection - co SQL/command injection risk khong?
  A05: Security Misconfiguration - debug mode bat trong production?
  A07: Auth Failures - token expiry, brute force protection?
  Bao cao ket qua va priority fix.

Automated PR Review

Tạo command /security-review chạy trước mỗi PR merge:

# .claude/commands/security-review.md
Review cac thay doi trong PR nay ve bao mat:

1. Doc tat ca file da thay doi (git diff main)
2. Tim cac van de bao mat trong code moi
3. Kiem tra khong co credentials, API keys bi commit
4. Verify tat ca user input duoc validate
5. Kiem tra authorization logic
6. Bao cao: PASS neu an toan, FAIL neu co van de Critical/High

Tìm Hardcoded Secrets

> Tim tat ca hardcoded secrets, API keys, passwords trong codebase.
  Kiem tra ca git history (git log -p) khong chi code hien tai.

> /security-review
# Dung truoc moi lan merge PR

Dependency Scanning

> Chay: pip-audit --format=json
  Phan tich ket qua va de xuat update cho cac package co CVE nghiem trong.
  Tao PR update dependencies.

Tạo Security Policy

> Dua tren codebase hien tai, tao file SECURITY.md voi:
  - Cac van de bao mat da biet va cach report
  - Security checklist cho developer
  - Cac bien moi truong can thiet va cach quan ly an toan

⚠️ Quan trọng

Claude Code là công cụ hỗ trợ, không thay thế hoàn toàn security audit chuyên nghiệp. Với ứng dụng production xử lý data nhạy cảm, vẫn cần penetration testing từ chuyên gia.

💡 Tích hợp vào CI/CD

Thêm bước claude -p "$(cat .claude/commands/security-review.md)" vào pipeline để tự động chặn PR có lỗ hổng bảo mật trước khi merge.