| id | web_security |
| name | web_security |
| description | 驗證、授權與 OWASP 安全檢查清單 |
🔒 安全性技能
常見攻擊防護
SQL Injection
❌ 危險
$query = "SELECT * FROM users WHERE id = " . $_GET['id'];
✅ 安全
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$id]);
XSS (跨站腳本)
❌ 危險
<div><?= $userInput ?></div>
✅ 安全
<div><?= htmlspecialchars($userInput, ENT_QUOTES, 'UTF-8') ?></div>
CSRF (跨站請求偽造)
<form method="POST">
<input type="hidden" name="_token" value="<?= csrf_token() ?>">
</form>
認證安全
密碼處理
$hash = password_hash($password, PASSWORD_BCRYPT, ['cost' => 12]);
if (password_verify($password, $hash)) {
}
Session 安全
session_regenerate_id(true);
session_set_cookie_params([
'lifetime' => 0,
'path' => '/',
'secure' => true, // HTTPS only
'httponly' => true, // 禁止 JS 存取
'samesite' => 'Lax'
]);
JWT 注意事項
- 使用強密鑰
- 設定合理過期時間
- 考慮 Token 撤銷機制
- 儲存在 httpOnly Cookie
輸入驗證
$rules = [
'email' => 'required|email|max:255',
'password' => 'required|min:8',
];
HTTP Headers
# 安全標頭
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
Content-Security-Policy: default-src 'self'
安全檢查清單
相關技能
核心原則:deny-by-default、最小權限、伺服端必驗(不信任 client)、所有秘鑰用 secret manager。