OWASP Top 10 Mitigation
🔥 Vibe Prompt
"Secure a Flask API: fix SQLi, XSS, CSRF, broken auth, insecure deserialization."
from flask import Flask, request, session
import bleach, html
app = Flask(__name__)
# 1. SQL Injection - use parameterized queries
@app.route("/user/<int:user_id>")
def get_user(user_id):
cur.execute("SELECT * FROM users WHERE id = %s", (user_id,))
# 2. XSS - sanitize output
@app.route("/comment")
def show_comment():
raw = request.args.get("text", "")
safe = bleach.clean(raw, tags=["b", "i", "em", "strong"], strip=True)
return html.escape(safe)
# 3. CSRF - token validation
@app.route("/transfer", methods=["POST"])
def transfer():
token = request.form.get("csrf_token")
if not token or token != session.get("csrf_token"):
return "CSRF detected!", 403
# Process transfer
# 4. Broken Auth - rate limiting
from flask_limiter import Limiter
limiter = Limiter(app)
@app.route("/login", methods=["POST"])
@limiter.limit("5 per minute")
def login():
pass
# 5. Secure Deserialization
import json
@app.route("/api/data", methods=["POST"])
def api_data():
try:
data = json.loads(request.data) # Safe!
except json.JSONDecodeError:
return "Invalid JSON", 400
OWASP Mitigation Checklist
| Risk | Mitigation | |------|-----------| | SQLi | Parameterized queries / ORM | | XSS | Output encoding / CSP | | CSRF | Anti-CSRF tokens | | Broken Auth | MFA / rate limiting | | Insecure Deserialization | JSON only / validate |
Web Security Course Complete! 🎉
- ✅ HTTP Security Headers
- ✅ CSP
- ✅ CORS
- ✅ CSRF
- ✅ Full OWASP Mitigation
Key Points
- Understand the core concepts thoroughly
- Practice with hands-on code examples
- Apply knowledge to real-world problems
- Review and reinforce through exercises
Further Learning
- Official documentation
- Open source projects on GitHub
- Community forums and discussions
- Related courses and tutorials
從 OWASP Top 10 到真正的安全防護
OWASP Top 10 不是一份 checklist——它不是「做完這 10 項就安全了」。它的真正價值是讓你了解攻擊者最常利用的弱點有哪些。
安全防護的層級
真正的安全需要多層次防護(Defense in Depth):
1️⃣ 程式碼層級
├── 參數化查詢(SQL Injection)
├── 輸出編碼(XSS)
├── CSRF Token / SameSite Cookie
└── 最小權限原則
2️⃣ 架構層級
├── API Gateway(速率限制、請求驗證)
├── WAF(Web Application Firewall)
├── 網路隔離(VPC、安全群組)
└── 資料加密(TLS + KMS)
3️⃣ 維運層級
├── 依賴掃描(Dependabot、Snyk)
├── 安全測試(SAST、DAST)
├── 日誌監控(CloudTrail、GuardDuty)
└── 事故應變(Incident Response Plan)
安全不是一次性的
安全是一個持續的過程,不是一次性的活動:
| 階段 | 時機 | 活動 | |:----:|:----:|:----:| | 設計 | 開發前 | Threat Modeling、安全架構審查 | | 開發 | 撰寫程式碼時 | SAST、Code Review、依賴掃描 | | 測試 | 部署前 | DAST、滲透測試、配置審查 | | 維運 | 上線後 | 漏洞掃描、日誌監控、事件回應 | | 改進 | 持續 | 事後覆盤、更新安全政策 |
課程總結
這五堂課涵蓋了 Web 安全的完整知識體系:
- OWASP Top 10 — 了解攻擊者最常利用的弱點
- SQL Injection — 參數化查詢是唯一解
- XSS + CSP — 輸出編碼 + Content Security Policy
- CSRF + SameSite — SameSite Cookie 是現代標準
- 完整緩解策略 — 從設計到維運的完整安全防護
你現在已經具備了開發安全 Web 應用所需的基礎知識。真正的挑戰不是學會這些技術,而是在每天寫程式時都記得應用它們。