security-audit
安全性審計技能
本技能提供 PHP/Laravel 應用程式的安全性檢查指引,聚焦於 OWASP Top 10 與常見漏洞模式。
嚴重漏洞檢查
SQL Injection (SQL 注入)
嚴重程度: 🔴 嚴重
檢測模式:
// ❌ 危險 - 字串拼接
$query = "SELECT * FROM users WHERE id = " . $id;
DB::select("SELECT * FROM users WHERE name = '$name'");
// ✅ 安全 - 參數化查詢
User::where('id', $id)->first();
DB::select("SELECT * FROM users WHERE name = ?", [$name]);
檢查要點:
- 是否有原生 SQL 字串拼接
- 是否使用
DB::raw()處理使用者輸入 whereRaw()是否有參數綁定
XSS (跨站腳本攻擊)
嚴重程度: 🔴 嚴重
檢測模式:
// ❌ 危險 - 直接輸出
{!! $userInput !!}
echo $request->input('name');
// ✅ 安全 - 自動跳脫
{{ $userInput }}
e($request->input('name'));
檢查要點:
- Blade 模板中是否使用
{!! !!}輸出使用者輸入 - 是否有
echo直接輸出未消毒的資料 - JavaScript 中是否嵌入未編碼的資料
硬編碼秘密 (Hardcoded Secrets)
嚴重程度: 🔴 嚴重
檢測模式:
// ❌ 危險 - 硬編碼
$apiKey = "sk-1234567890abcdef";
$password = "admin123";
// ✅ 安全 - 環境變數
$apiKey = config('services.api.key');
$password = env('DB_PASSWORD');
檢查要點:
- 程式碼中是否包含 API 金鑰、密碼、Token
- 設定檔是否有敏感資料(應使用
.env) - Git 歷史中是否曾經提交過秘密
認證與授權漏洞
嚴重程度: 🟡 高
檢查要點:
- 是否驗證當前使用者有權存取資源
- 是否有 IDOR (Insecure Direct Object Reference)
- 是否遵循最小權限原則
- Session 管理是否安全
// ❌ 危險 - IDOR
public function show($id) {
return User::find($id); // 未驗證權限
}
// ✅ 安全 - 權限檢查
public function show($id) {
$user = User::find($id);
$this->authorize('view', $user);
return $user;
}
敏感資料暴露
嚴重程度: 🟡 高
檢查要點:
- API Response 是否包含不必要的敏感欄位
- Log 是否記錄密碼、Token、信用卡號
- 錯誤訊息是否暴露系統細節
// ❌ 危險 - 暴露密碼
Log::info('User login', ['password' => $request->password]);
// ✅ 安全 - 遮蔽敏感資料
Log::info('User login', ['user_id' => $user->id]);
Mass Assignment 風險
檢查要點:
- Model 是否設定
$fillable或$guarded - 是否使用
$request->all()直接寫入資料庫
// ❌ 危險
User::create($request->all());
// ✅ 安全
User::create($request->only(['name', 'email']));
檔案上傳安全
檢查要點:
- 是否驗證檔案類型(MIME type)
- 是否限制檔案大小
- 是否使用隨機檔名儲存
- 上傳目錄是否在 webroot 外
輸出格式
對每個安全檢查提供:
| 類型 | 嚴重程度 | 檔案:行號 | 問題描述 | 修復建議 |
|---|---|---|---|---|
| SQL Injection | 🔴 嚴重 | path:123 | 描述 | 建議 |
More from changgenglu/changgenglu-blog
laravel-expert
Activates when user requests Laravel framework guidance, version migration, Eloquent patterns, middleware design, service container usage, or Laravel best practices. Do NOT use for generic PHP questions unrelated to the framework. Examples: 'How to use Service Container?', 'Translate this to Laravel 12'.
8pdf
Use this skill whenever the user wants to do anything with PDF files. This includes reading or extracting text/tables from PDFs, combining or merging multiple PDFs into one, splitting PDFs apart, rotating pages, adding watermarks, creating new PDFs, filling PDF forms, encrypting/decrypting PDFs, extracting images, and OCR on scanned PDFs to make them searchable. If the user mentions a .pdf file or asks to produce one, use this skill.
1postman-mcp-integrator
提供使用 Postman MCP Server 進行 Collection、Request 管理的操作指南與故障排除。當需要透過代理人自動化維護 Postman 集合時觸發。
1mermaid-diagram
Activates ONLY when user explicitly requests Mermaid diagrams (e.g., 'use Mermaid', 'draw a Mermaid chart', 'create Mermaid sequence diagram'). Ensures GitLab 13.12.15 (Mermaid 8.9.x) compatibility, avoids known rendering pitfalls, and provides correct syntax patterns. Do NOT use for ASCII diagrams (use ascii-diagram-artist instead).
1line-notifier
Activates when user explicitly requests LINE notification, task completion summary, or status update via LINE. Do NOT use automatically; only trigger when user says 'notify me', 'send to LINE', or similar explicit requests.
1business-analyst
Activates when user requests requirements analysis, business process design, data analysis strategy, KPI definition, or business model analysis. Do NOT use for technical implementation details. Examples: 'Analyze user requirements', 'Define KPIs for success'.
1