セキュリティ脆弱性を検出し修正を提案。 認証実装、API作成、フォーム処理、デプロイ前に使用。 OWASP Top 10、XSS、SQLインジェクション、CSRF対策の確認。 「セキュリティ」「脆弱性」「監査」で発動。
チェック項目:
検出コマンド:
# 認証チェックの欠如を検出
grep -rn "app\.\(get\|post\|put\|delete\)" --include="*.ts" | grep -v "auth\|middleware\|protect"
チェック項目:
検出コマンド:
# ハードコードされた機密情報
grep -rn "password\s*=\s*['\"]" --include="*.ts" --include="*.tsx"
grep -rn "api_key\s*=\s*['\"]" --include="*.ts" --include="*.tsx"
grep -rn "secret\s*=\s*['\"]" --include="*.ts" --include="*.tsx"
grep -rn "NEXT_PUBLIC_.*SECRET" --include="*.ts" --include="*.tsx"
チェック項目:
検出コマンド:
# SQLインジェクション候補
grep -rn "query.*\`.*\${" --include="*.ts"
grep -rn "exec.*\`.*\${" --include="*.ts"
# コマンドインジェクション候補
grep -rn "child_process\|exec\|spawn" --include="*.ts"
チェック項目:
検出コマンド:
# 危険なHTML挿入
grep -rn "innerHTML\s*=" --include="*.tsx"
grep -rn "dangerouslySetInnerHTML" --include="*.tsx"
grep -rn "__html" --include="*.tsx"
// ✅ 推奨パターン
import { z } from 'zod';
const UserInputSchema = z.object({
email: z.string().email().max(255),
password: z.string().min(8).max(100),
name: z.string().min(1).max(100).regex(/^[a-zA-Z\s]+$/),
});
// 使用時
const validated = UserInputSchema.parse(req.body);
// ❌ 危険
const query = `SELECT * FROM users WHERE id = ${userId}`;
// ✅ 安全(パラメータ化クエリ)
const user = await prisma.user.findUnique({ where: { id: userId } });
// ✅ 安全(prepared statement)
const [rows] = await connection.execute(
'SELECT * FROM users WHERE id = ?',
[userId]
);
// ❌ 危険
<div dangerouslySetInnerHTML={{ __html: userInput }} />
// ✅ 安全(サニタイズ)
import DOMPurify from 'dompurify';
<div dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(userInput) }} />
// ✅ より安全(テキストとして扱う)
<div>{userInput}</div>
// ✅ セキュアなセッション設定
const sessionOptions = {
httpOnly: true, // JavaScriptからのアクセス禁止
secure: true, // HTTPS必須
sameSite: 'strict', // CSRF対策
maxAge: 60 * 60 * 24 // 24時間で期限切れ
};
// ✅ bcryptを使用
import bcrypt from 'bcrypt';
const SALT_ROUNDS = 12;
// ハッシュ化
const hashedPassword = await bcrypt.hash(password, SALT_ROUNDS);
// 検証
const isValid = await bcrypt.compare(password, hashedPassword);
## 🔒 セキュリティ監査結果
### 総合判定: ✅ PASS / ⚠️ WARNING / ❌ FAIL
| 重要度 | ファイル | 行 | 脆弱性タイプ | 説明 | 修正案 |
|--------|---------|-----|-------------|------|--------|
| 🔴 Critical | auth.ts | 42 | A02 | パスワード平文保存 | bcryptでハッシュ化 |
| 🟡 High | api.ts | 15 | A03 | SQLインジェクション | Prismaクエリに変更 |
| 🟠 Medium | form.tsx | 28 | A07 | XSS可能性 | DOMPurifyでサニタイズ |
### 推奨アクション
1. Critical項目を即座に修正
2. High項目を今日中に対応
3. Medium以下は次回リリースまでに対応