Claude Code によるセキュリティファースト開発
Claude Code ワークフローで安全なコードを生成する方法。security-auditor agent から PreToolUse hooks、自動監査まで。
AI 支援開発において重要な疑問があります:Claude が書くコードの安全性をどう確保するか?
答えは AI 支援を避けることではありません。Claude Code の公式ツール、security-auditor agent、PreToolUse hooks、permission settings を使ってワークフローにセキュリティを組み込むことです。
このガイドでは、公式機能を使用してセキュリティを意識した開発のために Claude Code を設定する方法を説明します。
公式セキュリティツール
security-auditor Agent
Claude Code ドキュメントによると、security-auditor は脆弱性検出に特化したエージェントです:
Task({
subagent_type: "security-auditor",
model: "sonnet", // or "opus" for critical code
prompt: `
Audit the authentication module for security vulnerabilities.
Check for:
- OWASP Top 10 vulnerabilities
- Hardcoded secrets
- SQL injection patterns
- XSS vulnerabilities
- Authentication bypass risks
`
})
security-auditor を使用するタイミング:
| シナリオ | モデル | 理由 |
|---|---|---|
| 認証/決済の変更 | Sonnet/Opus | クリティカルなコード |
| 新しい API エンドポイント | Sonnet | 入力検証 |
| ファイルアップロード処理 | Sonnet | サニタイズ |
| 外部 API 連携 | Sonnet | データ漏洩防止 |
自動セキュリティ適用のための Hooks
公式 hooks ドキュメントによると、hooks は手動介入なしでセキュリティポリシーを自動的に適用します。
PreToolUse Hook - 危険な操作のブロック
.claude/settings.json で設定:
{
"hooks": {
"PreToolUse": [{
"matcher": "Bash",
"hooks": [{
"type": "prompt",
"prompt": "If the command contains destructive operations (rm -rf, DROP TABLE, git push --force), return 'ask' for user confirmation. Otherwise return 'approve'."
}]
}]
}
}
利用可能な hook イベント:
| イベント | タイミング | セキュリティ用途 |
|---|---|---|
PreToolUse | ツール実行前 | 危険なコマンドのブロック |
PostToolUse | ツール実行後 | ファイル変更の監査 |
Stop | Agent が停止を検討時 | セキュリティレビューの確認 |
SessionStart | セッション開始時 | セキュリティコンテキストの読み込み |
Stop Hook - セキュリティレビューの確認
{
"hooks": {
"Stop": [{
"matcher": "*",
"hooks": [{
"type": "prompt",
"prompt": "If code in auth/, payment/, or admin/ was modified, verify security-auditor was run. If not, block and explain."
}]
}]
}
}
Permission Settings
Claude Code ドキュメントによると、settings.json でアクセス制御を行います:
{
"permissions": {
"allow": [
"Read",
"Write",
"Edit",
"Bash(npm run:*)",
"Bash(git:*)"
],
"deny": [
"Read(.env)",
"Read(.env.*)",
"Read(secrets/**)",
"Read(**/*credentials*)",
"Bash(*rm -rf*)",
"Bash(*DROP*)"
]
}
}
Permission パターン構文:
Bash(npm run:*)- すべての npm run コマンドを許可Read(.env)- .env ファイルの読み取りを拒否Read(secrets/**)- secrets/ 内のすべての読み取りを拒否Bash(*DROP*)- DROP を含むコマンドを拒否
セキュリティの課題
Claude がコードを書く際、いくつかのリスクがあります:
- トレーニングデータからコピーされた安全でないパターン
- 入力検証の欠如
- 情報を漏洩する不適切なエラー処理
- ハードコードされた認証情報やシークレット
- SQL インジェクション、XSS、その他の脆弱性
解決策:agents と hooks を使ってセキュリティ要件をコード化し、Claude が自動的にそれらに従うようにすることです。
セキュリティファースト CLAUDE.md の構築
セキュリティセクションテンプレート
# Security Requirements
## Non-Negotiables
These rules cannot be overridden for any reason:
1. **No Secrets in Code**
- Never hardcode credentials, API keys, or tokens
- Use environment variables for all secrets
- Reference secrets through secure config management
2. **Input Validation**
- All user inputs must be validated
- Use allowlist validation over blocklist
- Validate on both client and server
3. **Output Encoding**
- Encode all outputs for the context (HTML, URL, JS)
- Use framework-provided escaping mechanisms
- Never concatenate raw user input into responses
4. **Authentication & Authorization**
- All endpoints must verify authentication
- Check authorization before every action
- Use principle of least privilege
5. **Data Protection**
- Encrypt sensitive data at rest and in transit
- Minimize data collection and retention
- Log access to sensitive data
## Security Review Requirements
These types of changes require explicit security review:
- Authentication/authorization logic
- Payment processing
- Personal data handling
- API endpoint creation
- Database queries
- File uploads
- Email sending
- External API calls
入力検証標準
# Input Validation Standards
## Validation Library
Use Zod for all runtime validation:
\`\`\`typescript
import { z } from 'zod';
// Define schema
const UserSchema = z.object({
email: z.string().email(),
age: z.number().min(0).max(150),
name: z.string().min(1).max(100),
});
// Validate
const result = UserSchema.safeParse(input);
if (!result.success) {
throw new ValidationError(result.error);
}
\`\`\`
## Validation Rules
- String inputs: Define max length
- Numeric inputs: Define valid range
- Email: Use proper email validation
- URLs: Validate protocol and domain
- IDs: Validate format (UUID, numeric, etc.)
- Arrays: Validate length and item types
- Objects: Validate required fields
セキュアコーディングパターン
# Secure Coding Patterns
## Database Queries
ALWAYS use parameterized queries:
\`\`\`typescript
// NEVER do this
const query = \`SELECT * FROM users WHERE email = '\${email}'\`;
// ALWAYS do this
const user = await db.user.findUnique({
where: { email }, // Prisma handles parameterization
});
\`\`\`
## Error Handling
Never expose internal details:
\`\`\`typescript
// NEVER do this
catch (error) {
return res.status(500).json({ error: error.message });
}
// ALWAYS do this
catch (error) {
console.error('Internal error:', error); // Log full error
return res.status(500).json({ error: 'An error occurred' }); // Generic message
}
\`\`\`
## Authentication Checks
Always verify auth at the start:
\`\`\`typescript
export async function handler(req, res) {
// First: Authenticate
const user = await getAuthenticatedUser(req);
if (!user) {
return res.status(401).json({ error: 'Unauthorized' });
}
// Second: Authorize
if (!user.canAccessResource(resourceId)) {
return res.status(403).json({ error: 'Forbidden' });
}
// Then: Proceed with logic
// ...
}
\`\`\`
セキュリティのための Agent 委譲
機密性の高い変更に対してセキュリティレビューを自動的に実行するよう Claude を設定します:
# Agent Delegation Rules
## Automatic Security Review
These changes automatically trigger security-auditor agent:
- Files in: auth/, payment/, admin/
- Functions containing: password, token, secret, key, credential
- Operations: user creation, permission changes, data deletion
- External calls: API integrations, webhooks, email
## Security Agent Configuration
When security-auditor runs, it checks for:
- OWASP Top 10 vulnerabilities
- Hardcoded secrets
- Missing authentication
- SQL injection patterns
- XSS vulnerabilities
- Insecure direct object references
- Missing rate limiting
- Insufficient logging
Task Tool での実装
// Automatic security review for auth changes
Task({
subagent_type: "security-auditor",
model: "sonnet",
prompt: `
Review changes to auth/ directory.
Check for OWASP Top 10 vulnerabilities:
1. Injection
2. Broken authentication
3. Sensitive data exposure
4. XML external entities
5. Broken access control
6. Security misconfiguration
7. Cross-site scripting
8. Insecure deserialization
9. Using components with known vulnerabilities
10. Insufficient logging
Report findings with severity levels.
`
})
セキュリティワークフローパターン
パターン 1:実装前レビュー
セキュリティに関わるコードを書く前に:
Task({
subagent_type: "security-auditor",
model: "sonnet",
prompt: `
Planning to implement password reset functionality.
Review security considerations:
1. What vulnerabilities should I address?
2. What OWASP vulnerabilities are relevant?
3. What's the secure implementation pattern?
4. What tests should I write?
`
})
パターン 2:自動 hooks を使った実装
自動 hooks を使った実装時:
{
"hooks": {
"PostToolUse": [{
"matcher": "Write",
"hooks": [{
"type": "prompt",
"prompt": "If the written file is in auth/, payment/, or admin/ directories, trigger a security scan and report any issues found."
}]
}]
}
}
パターン 3:実装後の監査
実装後:
Task({
subagent_type: "security-auditor",
model: "opus", // Use Opus for critical security review
prompt: `
Comprehensive security audit of password reset feature.
Check for:
- Token generation entropy
- Timing attacks in token comparison
- Rate limiting bypass
- User enumeration
- Session fixation after reset
- Proper invalidation of old sessions
Provide findings categorized by severity:
- Critical
- High
- Medium
- Low
`
})
一般的な脆弱性と予防
SQL インジェクション
脆弱なコード:
const query = `SELECT * FROM users WHERE id = ${userId}`;
安全なコード:
const user = await prisma.user.findUnique({ where: { id: userId } });
Hook による保護:
{
"hooks": {
"PreToolUse": [{
"matcher": "Write",
"hooks": [{
"type": "prompt",
"prompt": "If the code contains string concatenation with SQL keywords (SELECT, INSERT, UPDATE, DELETE), return 'block' with warning about SQL injection. Otherwise 'approve'."
}]
}]
}
}
クロスサイトスクリプティング(XSS)
脆弱なコード:
<div dangerouslySetInnerHTML={{ __html: userContent }} />
安全なコード:
<div>{userContent}</div> // React auto-escapes
CLAUDE.md ルール:
## XSS Prevention
- Never use dangerouslySetInnerHTML without sanitization
- Use DOMPurify for HTML content that must be rendered
- Escape all dynamic content in non-React contexts
認証バイパス
脆弱なコード:
if (user.role === 'admin') {
// Allow action
}
安全なコード:
if (await authService.hasPermission(user.id, 'admin:action')) {
// Allow action
}
機密データの露出
脆弱なコード:
return res.json({ user }); // Includes password hash!
安全なコード:
return res.json({ user: sanitizeUser(user) });
継続的セキュリティのための Hooks
Pre-Commit セキュリティチェック
{
"hooks": {
"Stop": [{
"matcher": "*",
"hooks": [{
"type": "command",
"command": "npm run security-scan",
"onFailure": "block"
}]
}]
}
}
自動シークレット検出
{
"hooks": {
"PreToolUse": [{
"matcher": "Write",
"hooks": [{
"type": "prompt",
"prompt": "Scan the code for potential secrets: API keys, passwords, tokens, private keys. If found, return 'block' with the line numbers. Otherwise 'approve'."
}]
}]
}
}
ファイルアクセス制御
{
"permissions": {
"deny": [
"Read(.env*)",
"Read(**/secrets/**)",
"Read(**/*credential*)",
"Read(**/*.pem)",
"Read(**/*.key)",
"Write(.env*)",
"Edit(.env*)"
]
}
}
test-runner によるセキュリティテスト
自動セキュリティテストスイート
// Launch parallel security tests
Task({
subagent_type: "test-runner",
model: "haiku",
prompt: "Run all security-related tests in tests/security/"
})
Task({
subagent_type: "security-auditor",
model: "sonnet",
prompt: "Verify test coverage for authentication edge cases"
})
セキュリティテスト例
describe('Password Reset Security', () => {
it('rejects invalid tokens', async () => {
const response = await request(app)
.post('/api/auth/reset-password')
.send({ token: 'invalid', password: 'newpassword' });
expect(response.status).toBe(400);
expect(response.body).not.toContain('token');
});
it('prevents timing attacks on token validation', async () => {
// Use constant-time comparison
const times = [];
for (let i = 0; i < 100; i++) {
const start = performance.now();
await validateToken(`token${i}`);
times.push(performance.now() - start);
}
// Variance should be minimal
expect(standardDeviation(times)).toBeLessThan(5);
});
it('rate limits reset requests', async () => {
const email = '[email protected]';
// First 3 requests succeed
for (let i = 0; i < 3; i++) {
const response = await request(app)
.post('/api/auth/forgot-password')
.send({ email });
expect(response.status).toBe(200);
}
// 4th request is rate limited
const response = await request(app)
.post('/api/auth/forgot-password')
.send({ email });
expect(response.status).toBe(429);
});
});
セキュリティ監査チェックリスト
デプロイ前に:
## Pre-Deployment Security Checklist
### Authentication
- [ ] All endpoints require authentication (except public ones)
- [ ] Tokens expire appropriately
- [ ] Password requirements enforced
- [ ] Account lockout after failed attempts
### Authorization
- [ ] Permission checks on all protected actions
- [ ] No privilege escalation paths
- [ ] Admin functions properly protected
### Data Protection
- [ ] Sensitive data encrypted at rest
- [ ] HTTPS enforced
- [ ] No secrets in code or logs
- [ ] PII handling compliant
### Input/Output
- [ ] All inputs validated
- [ ] All outputs properly encoded
- [ ] File uploads validated and sanitized
- [ ] No SQL injection vulnerabilities
### Logging & Monitoring
- [ ] Security events logged
- [ ] Logs don't contain sensitive data
- [ ] Alerting configured for anomalies
エンタープライズセキュリティ機能
Claude Code は4 層の設定階層を通じてエンタープライズセキュリティ要件をサポートします:
Enterprise Layer(Managed Settings)
// /etc/claude-code/settings.json (IT managed)
{
"permissions": {
"deny": [
"Bash(*curl*)",
"Bash(*wget*)",
"Read(/etc/**)",
"Write(/etc/**)"
]
},
"mcpServers": {
"allowlist": ["filesystem", "memory"]
}
}
監査ログ設定
## Audit Requirements
Log these events to secure audit system:
- Authentication attempts (success/failure)
- Authorization decisions
- Data access (especially sensitive data)
- Configuration changes
- API calls to external services
コンプライアンス統合
## Compliance Requirements
- PCI DSS: No card data in logs, encrypt at rest
- GDPR: Data minimization, right to deletion
- HIPAA: PHI access controls and audit trails
- SOC 2: Change management documentation
セキュリティのためのモデル選択
| タスク | 推奨モデル | 理由 |
|---|---|---|
| クイックセキュリティスキャン | Haiku 4.5 | 高速パターンマッチング |
| コードレビュー | Sonnet 4.5 | バランスの取れた分析 |
| セキュリティ監査 | Sonnet 4.5 | 徹底的なレビュー |
| クリティカルセキュリティ | Opus 4.5 | 最高の知性 |
クイックモデル切り替え(v2.0.65+):プロンプト入力中に Option+P(macOS)または Alt+P(Windows/Linux)を押します。
はじめに
今日:
- CLAUDE.md に security-auditor 委譲ルールを追加
- 機密ファイルに対する permission denials を設定
- 危険なコマンドに対する PreToolUse hook を 1 つ追加
今週:
- セキュリティ検証のための Stop hook を実装
- セキュリティテストスイートを追加
- クリティカルモジュールに対して security-auditor を実行
今月:
- pre-commit セキュリティ hooks をデプロイ
- CI/CD にセキュリティスキャンを統合
- チームにセキュリティファースト開発をトレーニング
セキュリティは目的地ではなく旅です。Claude Code の agents と hooks を使えば、セキュリティのベストプラクティスを自動化できます。
参考:Claude Code Documentation、Claude Code GitHub、Hooks Documentation