Claude Codeで秘密情報を安全に管理するための多層防御アプローチガイド。
Implements multi-layer security to protect secrets from Claude Code access.
/plugin marketplace add sk8metalme/ai-agent-setup/plugin install development-toolkit@ai-agent-setupThis skill inherits all available tools. When active, it can use any tool Claude has access to.
Claude Codeで秘密情報を安全に管理するための多層防御アプローチガイド。
Claude Codeは強力な開発支援ツールですが、秘密情報(APIトークン、データベース認証情報、SSH鍵など)の取り扱いには注意が必要です。本スキルでは、**3層の防御層(L1-L3)**を組み合わせた包括的なアプローチで秘密情報を保護します。
| 問題 | 詳細 |
|---|---|
| 自動読み込み | Claude Codeは.env, .env.localなどを自動的にメモリに読み込む可能性がある |
| deny設定のバグ | settings.jsonのdeny設定が完全に機能しないバグ(Issue #6631, #6699) |
| 間接的な漏洩 | system reminderなど間接的な方法で内容が漏洩する可能性がある |
原則: 秘密情報はClaude Codeがアクセスできない場所に置く
# 1. ~/.secrets/ ディレクトリを作成
mkdir -p ~/.secrets
chmod 700 ~/.secrets
# 2. プロジェクト用ディレクトリを作成
mkdir -p ~/.secrets/my-project
# 3. .envファイルを移動(シンボリックリンクは作成しない)
mv .env ~/.secrets/my-project/.env
# 4. テンプレートから作成する場合
cp global/templates/secrets/.env.example ~/.secrets/my-project/.env
vi ~/.secrets/my-project/.env
方法A: sourceコマンド
# プロジェクトディレクトリで実行
set -a
source ~/.secrets/my-project/.env
set +a
npm run dev
方法B: シェル関数(推奨)
~/.bashrc または ~/.zshrc に追加:
load_project_env() {
local project_name="${1:-$(basename $(pwd))}"
local env_file="$HOME/.secrets/${project_name}/.env"
if [[ -f "$env_file" ]]; then
set -a
source "$env_file"
set +a
echo "✅ Loaded: $env_file"
else
echo "⚠️ Not found: $env_file"
fi
}
alias lenv='load_project_env'
使用例:
cd ~/projects/my-project
lenv # ~/.secrets/my-project/.env を読み込み
~/.claude/settings.json のdenied配列に秘密情報パターンを追加。
# テンプレートを確認
cat global/templates/secrets/settings-deny-secrets.json
# 手動で ~/.claude/settings.json の "permissions.denied" に追加
{
"permissions": {
"denied": [
"Read(~/.secrets/*)",
"Read(**/.env)",
"Read(**/.env.*)",
"Read(**/credentials.json)",
"Bash(cat:*/.env*)",
"Bash(cat:*credentials*)"
]
}
}
deny設定のバグを補完するため、protect-secrets.shを設置。
protect-secrets.shを実行install-global.sh 実行時に以下が自動的に設定されます:
~/.claude/hooks/protect-secrets.sh (実行スクリプト)~/.claude/hooks/protect-secrets.conf (設定ファイル)~/.claude/settings.json にHook設定追加~/.claude/hooks/protect-secrets.conf を編集:
# 独自の秘密情報パターンを追加
SECRETS_PATTERNS=".env|\.secrets|credentials|my_custom_pattern"
# デバッグモード有効化
DEBUG=1
# ログを確認
tail -f ~/.claude/hooks/protect-secrets.log
# 手動テスト
CLAUDE_TOOL_INPUT='{"file_path":".env"}' ~/.claude/hooks/protect-secrets.sh
# 期待: exit 1, BLOCKメッセージ表示
$HOME/
├── .secrets/ # 秘密情報(Claude Codeアクセス外)
│ ├── project-a/
│ │ └── .env
│ ├── project-b/
│ │ └── .env
│ └── shared/
│ └── common.env
│
├── .claude/
│ ├── settings.json # deny設定
│ └── hooks/
│ ├── protect-secrets.sh # PreToolUse Hook
│ └── protect-secrets.conf
│
└── projects/ # 開発プロジェクト
├── project-a/
│ ├── .claude/
│ └── src/
└── project-b/
# Homebrew (macOS)
brew install gitleaks
# または
go install github.com/gitleaks/gitleaks/v8@latest
# テンプレートをコピー
cp global/templates/secrets/gitleaks.toml .
# スキャン実行
gitleaks detect --config gitleaks.toml
# pre-commitフックに統合
cat <<'EOF' > .git/hooks/pre-commit
#!/bin/bash
gitleaks protect --staged --config gitleaks.toml
EOF
chmod +x .git/hooks/pre-commit
秘密情報ファイルをGitから除外:
# 秘密情報
.env
.env.*
!.env.example
!.env.template
.secrets/
secrets/
credentials/
*.pem
*.key
*.p12
*.pfx
id_rsa*
id_ed25519*
service-account.json
.netrc
.npmrc
.pypirc
~/.secrets/ ディレクトリを作成(chmod 700).envファイルをプロジェクト外に移動install-global.shを実行(Hookとdenied設定を自動適用)load_project_envを追加.gitignoreに秘密情報パターンを追加mkdir -p ~/.secrets/<project-name>.env.exampleをコピーして~/.secrets/<project-name>/.envを作成A: PreToolUse Hook(L3)が動作しているか確認:
# Hookのログを確認
DEBUG=1
tail -f ~/.claude/hooks/protect-secrets.log
A: 実行権限と所有者を確認:
ls -la ~/.claude/hooks/protect-secrets.sh
# -rwxr-xr-x (実行権限が必要)
# 修正
chmod +x ~/.claude/hooks/protect-secrets.sh
A: 以下の対策を追加:
多層防御の原則:
~/.secrets/に配置この3層を組み合わせることで、Claude Codeで安全に開発できます。
This skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.
This skill should be used when the user asks to "create an agent", "add an agent", "write a subagent", "agent frontmatter", "when to use description", "agent examples", "agent tools", "agent colors", "autonomous agent", or needs guidance on agent structure, system prompts, triggering conditions, or agent development best practices for Claude Code plugins.
This skill should be used when the user asks to "create a hook", "add a PreToolUse/PostToolUse/Stop hook", "validate tool use", "implement prompt-based hooks", "use ${CLAUDE_PLUGIN_ROOT}", "set up event-driven automation", "block dangerous commands", or mentions hook events (PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification). Provides comprehensive guidance for creating and implementing Claude Code plugin hooks with focus on advanced prompt-based hooks API.