|
| 1 | +#!/bin/sh |
| 2 | +set -eu |
| 3 | + |
| 4 | +bunx lint-staged |
| 5 | + |
| 6 | +printf '%s\n' "Running security check..." |
| 7 | + |
| 8 | +staged_diff=$(git diff --cached) |
| 9 | + |
| 10 | +if [ -z "$staged_diff" ]; then |
| 11 | + printf '%s\n' "No staged changes. Skipping security check." |
| 12 | + exit 0 |
| 13 | +fi |
| 14 | + |
| 15 | +temp_file=$(mktemp) |
| 16 | +prompt_file=$(mktemp) |
| 17 | + |
| 18 | +cleanup() { |
| 19 | + rm -f "$temp_file" "$prompt_file" |
| 20 | +} |
| 21 | + |
| 22 | +trap cleanup EXIT HUP INT TERM |
| 23 | + |
| 24 | +printf '%s' "$staged_diff" > "$temp_file" |
| 25 | + |
| 26 | +cat > "$prompt_file" <<'EOF' |
| 27 | +以下のgit diffの内容をセキュリティの観点で分析してください。 |
| 28 | +
|
| 29 | +チェック項目: |
| 30 | +- APIキー、パスワード、トークンなどの機密情報のハードコード |
| 31 | +- 危険な関数やコマンドの使用(eval, exec, system呼び出しなど) |
| 32 | +- SQLインジェクション、XSSなどの脆弱性 |
| 33 | +- 企業の機密情報や非公開の技術情報 |
| 34 | +- セキュリティ設定の不適切な変更 |
| 35 | +- ユーザー固有の記述(例:PC名、ユーザー名など)。 |
| 36 | + - tmpファイルなどgitにコミットされないファイルなら問題ない。 |
| 37 | +
|
| 38 | +問題がある場合は「SECURITY_RISK:」で始まる行で具体的に指摘してください。 |
| 39 | +問題がない場合は「OK」とだけ回答してください。 |
| 40 | +
|
| 41 | +差分内容: |
| 42 | +```diff |
| 43 | +EOF |
| 44 | + |
| 45 | +cat "$temp_file" >> "$prompt_file" |
| 46 | +printf '\n```\n' >> "$prompt_file" |
| 47 | + |
| 48 | +if ! command -v claude >/dev/null 2>&1; then |
| 49 | + printf '%s\n' "claude command not found. Unable to run security check." |
| 50 | + exit 1 |
| 51 | +fi |
| 52 | + |
| 53 | +saved_claudecode=${CLAUDECODE-} |
| 54 | +unset CLAUDECODE |
| 55 | + |
| 56 | +if ! result=$(claude --permission-mode default --settings '{"disableAllHooks": true}' --append-system-prompt-file "$prompt_file" -p "pls review" 2>/dev/null); then |
| 57 | + if [ -n "$saved_claudecode" ]; then |
| 58 | + export CLAUDECODE="$saved_claudecode" |
| 59 | + fi |
| 60 | + printf '%s\n' "Security check failed because claude did not return a valid response." |
| 61 | + exit 1 |
| 62 | +fi |
| 63 | + |
| 64 | +if [ -n "$saved_claudecode" ]; then |
| 65 | + export CLAUDECODE="$saved_claudecode" |
| 66 | +fi |
| 67 | + |
| 68 | +printf '%s\n' "========== RESULT ==========" |
| 69 | +printf '%s\n' "$result" |
| 70 | + |
| 71 | +if printf '%s\n' "$result" | grep -q "SECURITY_RISK:"; then |
| 72 | + printf '%s\n' "Security risk detected. Commit aborted." |
| 73 | + exit 1 |
| 74 | +fi |
| 75 | + |
| 76 | +if printf '%s\n' "$result" | grep -qw "OK"; then |
| 77 | + printf '%s\n' "Security check passed." |
| 78 | + exit 0 |
| 79 | +fi |
| 80 | + |
| 81 | +printf '%s\n' "Security check result was ambiguous. Commit aborted." |
| 82 | +exit 1 |
0 commit comments