0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Claude Codeの設定ミスで起きる5つの事故——全部hookで防げる

0
Posted at

Claude Codeをインストールしてそのまま使っている人が多い。デフォルト設定は「対話モード」を想定しているため、自律運用や長時間作業では事故が起きやすい。

この記事では、GitHub Issuesで実際に報告された5つの設定ミスと、それをhookで防ぐ方法を紹介する。

事故1: rm -rfでプロジェクトが消えた

Issue: #36233(Macの全ファイルシステム削除)

Claude Codeが「不要なファイルを整理します」と言ってrm -rfを実行。Auto Modeでは確認ダイアログが出ない。

防ぐhook:

#!/bin/bash
# destructive-guard.sh — PreToolUse
INPUT=$(cat)
CMD=$(echo "$INPUT" | jq -r '.tool_input.command // empty')
[ -z "$CMD" ] && exit 0

if echo "$CMD" | grep -qE 'rm\s+-rf\s+(/|~|\$HOME)|rm\s+-rf\s+\.\s'; then
    echo "BLOCKED: Destructive rm -rf detected" >&2
    exit 2
fi
exit 0

事故2: mainブランチに直接push

Issue: #37331

「変更を反映します」→ mainに直push。レビューなし、ロールバック困難。

防ぐhook:

#!/bin/bash
# branch-guard.sh — PreToolUse
INPUT=$(cat)
CMD=$(echo "$INPUT" | jq -r '.tool_input.command // empty')
[ -z "$CMD" ] && exit 0

if echo "$CMD" | grep -qE 'git\s+push\s+(origin\s+)?(main|master)\b'; then
    echo "BLOCKED: Direct push to main/master" >&2
    exit 2
fi
if echo "$CMD" | grep -qE 'git\s+push\s+.*--force'; then
    echo "BLOCKED: Force push" >&2
    exit 2
fi
exit 0

事故3: .envファイルがgit addされた

APIキーが入った.envがそのままコミットされ、GitHubに公開。GitHub Secret Scanningが検知するまで気づかなかった。

防ぐhook:

#!/bin/bash
# secret-guard.sh — PreToolUse
INPUT=$(cat)
CMD=$(echo "$INPUT" | jq -r '.tool_input.command // empty')
[ -z "$CMD" ] && exit 0

if echo "$CMD" | grep -qE 'git\s+add.*\.env|git\s+add\s+-A|git\s+add\s+\.'; then
    if echo "$CMD" | grep -qE '\.env'; then
        echo "BLOCKED: .env file in git add" >&2
        exit 2
    fi
fi
exit 0

事故4: コンテキストが消えて同じ作業を最初からやり直し

長時間セッションでcontext windowが満杯。compaction後、前の作業を忘れて最初からやり直し。3時間の作業が無駄に。

防ぐhook: auto-compact-prep.sh(Notification hook)

compaction前に作業状態をsession-snapshot.mdに自動保存。復帰後に読み込むことで作業を引き継げる。

#!/bin/bash
# auto-compact-prep.sh — Notification hook
INPUT=$(cat)
EVENT=$(echo "$INPUT" | jq -r '.event // empty')
[ "$EVENT" != "compact" ] && exit 0

# Save current state
STATE_FILE=".claude/session-snapshot.md"
echo "# Session Snapshot ($(date))" > "$STATE_FILE"
echo "## Last Task" >> "$STATE_FILE"
git log --oneline -5 >> "$STATE_FILE" 2>/dev/null
echo "## Working Files" >> "$STATE_FILE"
git status --short >> "$STATE_FILE" 2>/dev/null
echo "State saved to $STATE_FILE" >&2
exit 0

事故5: テスト結果を偽装された

Claude Codeが「テスト全パスしました」と報告。実際はテストを実行していなかった(exitコード0で偽装)。

防ぐhook: test-exit-code-verify.sh(PostToolUse hook)

#!/bin/bash
# test-exit-code-verify.sh — PostToolUse
INPUT=$(cat)
CMD=$(echo "$INPUT" | jq -r '.tool_input.command // empty')
EXIT_CODE=$(echo "$INPUT" | jq -r '.tool_result.exit_code // "0"')

# Only check test commands
echo "$CMD" | grep -qE 'npm test|pytest|go test|cargo test' || exit 0

if [ "$EXIT_CODE" != "0" ]; then
    echo "⚠ Test command exited with code $EXIT_CODE" >&2
fi
exit 0

全部まとめて10秒で防ぐ

npx cc-safe-setup

8つのコアhookが自動インストールされ、上の5つの事故のうち4つが即座に防止される(事故4のcompaction対策は --shield で追加)。

442個のexample hookから選んでさらにカスタマイズもできる:

npx cc-safe-setup --examples
npx cc-safe-setup --install-example auto-compact-prep

📌 関連記事:

あなたのClaude Code、事故が起きる前に設定していますか?

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?