Theo thống kê của OWASP, hơn 95% ứng dụng web có ít nhất một trong 10 lỗ hổng này. Điều đáng buồn hơn: phần lớn đều có thể phòng chống bằng những biện pháp cơ bản.
A01 — Broken Access Control
Lỗ hổng phổ biến nhất: user có thể truy cập resource của người khác.
# Sai — không kiểm tra ownership
def get_post(request, post_id):
post = Post.objects.get(id=post_id)
return JsonResponse(post.to_dict())
# Đúng — verify owner
def get_post(request, post_id):
post = get_object_or_404(Post, id=post_id, author=request.user)
return JsonResponse(post.to_dict())
A02 — Cryptographic Failures
# Sai — lưu password plain text hoặc MD5
user.password = md5(password) # MD5 đã bị crack
# Đúng — Django tự dùng PBKDF2/bcrypt
from django.contrib.auth.hashers import make_password
user.password = make_password(password)
# Sai — dữ liệu nhạy cảm không mã hóa trong DB
user.credit_card = "1234-5678-9012-3456"
# Đúng — mã hóa trước khi lưu
from cryptography.fernet import Fernet
encrypted = fernet.encrypt(credit_card.encode())
A03 — Injection (SQL, Command)
# SQL Injection — sai
query = f"SELECT * FROM posts WHERE title = '{user_input}'"
# Input: '; DROP TABLE posts; --
# Đúng — parameterized query
Post.objects.filter(title=user_input) # Django ORM tự escape
# Command Injection — sai
import os
os.system(f"convert {filename} output.jpg")
# Đúng — dùng list, không dùng string
import subprocess
subprocess.run(["convert", filename, "output.jpg"])
A04 — Insecure Design
Thiết kế thiếu threat modeling từ đầu:
- Không có rate limiting trên login form → brute force
- Password reset link không expire → tài khoản bị chiếm
- Không có multi-factor authentication cho admin
A05 — Security Misconfiguration
# Django checklist cho production
DEBUG = False # Không bao giờ True trên production
ALLOWED_HOSTS = ['yourdomain.com'] # Không dùng ['*']
SECRET_KEY = os.environ['SECRET_KEY'] # Không hardcode
# Tắt thông tin nhạy cảm trong error page
ADMINS = [('Admin', 'admin@example.com')]
# Chạy check
python manage.py check --deploy
A06 — Vulnerable Components
# Kiểm tra dependencies có lỗ hổng
pip install safety
safety check
# GitHub Dependabot — tự động PR khi có CVE mới
A07 — Auth Failures
# Rate limiting login
from django.core.cache import cache
def login_view(request):
ip = request.META['REMOTE_ADDR']
attempts = cache.get(f'login_attempts:{ip}', 0)
if attempts >= 5:
return HttpResponse('Quá nhiều lần thử', status=429)
if not authenticate(request, ...):
cache.set(f'login_attempts:{ip}', attempts + 1, 900) # 15 phút
A03 — XSS (Cross-Site Scripting)
# Django template tự escape — an toàn mặc định
{{ user_input }} ✓ → <script>alert(1)</script>
# Nguy hiểm khi dùng mark_safe hoặc |safe
{{ user_input|safe }} ✗ → KHÔNG dùng với user input!
# JavaScript — escape khi insert vào DOM
// Sai
element.innerHTML = userInput;
// Đúng
element.textContent = userInput;
Bảo mật không phải là tính năng thêm vào sau — nó phải được design từ đầu. Chi phí fix lỗ hổng bảo mật sau khi deploy cao gấp 100 lần so với phòng ngừa từ sớm.
Kết Luận
Đọc OWASP Top 10 là điểm khởi đầu tốt, nhưng không đủ. Thực hành: chạy python manage.py check --deploy, dùng safety check cho dependencies, và luôn sanitize/validate mọi input từ user. Security là quá trình liên tục, không phải one-time task.
Chưa có bình luận. Hãy là người đầu tiên!