feat(hooks-pre-tool-validate): secret-detection preset 追加 (順位 146) - #201
Conversation
- AWS Access/Secret Key, OpenAI sk-, GitHub PAT/OAuth, Anthropic sk-ant- の 6 種類 hardcoded secret pattern を regex 検出 - Bash command + Edit/Write content の両方をスキャン (handle_write_edit_tool に secret scan 統合、old_string は除外して secret 削除操作を allow) - default_preset_names に含めて Tier 1 security-critical default-on (漏洩の非対称性 = 1 度漏れたら手遅れ、jj-message-required の opt-in とは別判断) - handle_write_edit_tool を 4 helper 関数に分割 (関数長 50 行制限対応) - 既存 BlockedPattern.exception 機構を再利用、Anthropic sk-ant- を OpenAI sk- pattern の exception で除外 (Rust regex negative lookahead 代替、順位 144 で導入) - ~/.claude/rules/common/security.md § Secret Management の機械強制層 cargo test 202/202 pass (新規 secret-detection 18 tests 追加、回帰なし) dogfood: PreToolUse 層で AI 自身の echo AKIA... 投入を実 block 確認 同梱 (queue 登録のみ、本 PR では実装せず): - 順位 199-202: PR #200 post-merge-feedback T2-1/T3-1/T3-2/T3-3 採用 entries prior session 由来の registration、todo10.md / todo-summary.md に entries 追記 bundle 理由: docs/todo-summary.md が同ファイル内で 199-202 行追加 + 146 行削除を 抱え、jj 非対話 split (path 単位) で分離不能 (partial-file split が必要なため)。 無理に分けると surgery コスト > 収益、reviewer から見ても一貫性なし。
📝 WalkthroughWalkthroughPR ChangesSecret Detection Preset Implementation
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/hooks-pre-tool-validate/src/main.rs (1)
1327-1341: ⚡ Quick win
ghu_/ghr_の回帰テストも追加しておくと契約が固まります。Line [517] の正規表現は
gho|ghs|ghu|ghrを対象にしていますが、追加テストはgho_とghs_のみです。ghu_/ghr_も1本ずつ置いておくと、将来の regex 変更での取りこぼしを防げます。追加テスト例
#[test] fn secret_detection_blocks_github_server_token() { assert!(is_blocked_with( "let token = \"ghs_abcdefghijklmnopqrstuvwxyz0123456789\";", SECRET_DETECT )); } + #[test] + fn secret_detection_blocks_github_user_to_server_token() { + assert!(is_blocked_with( + "let token = \"ghu_abcdefghijklmnopqrstuvwxyz0123456789\";", + SECRET_DETECT + )); + } + + #[test] + fn secret_detection_blocks_github_refresh_token() { + assert!(is_blocked_with( + "let token = \"ghr_abcdefghijklmnopqrstuvwxyz0123456789\";", + SECRET_DETECT + )); + }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/hooks-pre-tool-validate/src/main.rs` around lines 1327 - 1341, Add two more unit tests to cover GitHub token prefixes `ghu_` and `ghr_` so the secret-detection regex (which targets `gho|ghs|ghu|ghr` around line 517) is fully exercised; duplicate the pattern used in secret_detection_blocks_github_oauth_token and secret_detection_blocks_github_server_token by creating tests that call is_blocked_with with strings containing "let token = \"ghu_abcdefghijklmnopqrstuvwxyz0123456789\";" and "let token = \"ghr_abcdefghijklmnopqrstuvwxyz0123456789\";" using SECRET_DETECT to ensure those cases are blocked.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/hooks-pre-tool-validate/src/main.rs`:
- Around line 533-537: Update the SECRET_DETECTION_MSG text so it no longer
suggests editing `.env*` as an automated fix (which conflicts with
check_protected_file blocking `.env*` edits); instead explicitly instruct that
remediation is manual and recommend moving secrets to environment variables
(std::env::var or process.env), a secret manager (1Password/Doppler/AWS/GH
Secrets), or a local .env only for dev with .gitignore, and instruct test
fixtures to use clearly invalid short tokens like "AKIATEST" to avoid regex
matches; change the message in main.rs where SECRET_DETECTION_MSG is defined and
ensure wording references check_protected_file to clarify the block.
---
Nitpick comments:
In `@src/hooks-pre-tool-validate/src/main.rs`:
- Around line 1327-1341: Add two more unit tests to cover GitHub token prefixes
`ghu_` and `ghr_` so the secret-detection regex (which targets `gho|ghs|ghu|ghr`
around line 517) is fully exercised; duplicate the pattern used in
secret_detection_blocks_github_oauth_token and
secret_detection_blocks_github_server_token by creating tests that call
is_blocked_with with strings containing "let token =
\"ghu_abcdefghijklmnopqrstuvwxyz0123456789\";" and "let token =
\"ghr_abcdefghijklmnopqrstuvwxyz0123456789\";" using SECRET_DETECT to ensure
those cases are blocked.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: d9f3d7ed-cf52-46d7-bc14-bf1d663b4f5d
📒 Files selected for processing (5)
.claude/hooks-config.tomldocs/todo-summary.mddocs/todo10.mddocs/todo9.mdsrc/hooks-pre-tool-validate/src/main.rs
💤 Files with no reviewable changes (1)
- docs/todo9.md
| **対応方法:** | ||
| - 環境変数に移管: Rust なら `std::env::var("API_KEY")`、Node.js なら `process.env.API_KEY` | ||
| - Secret manager (1Password / Doppler / AWS Secrets Manager / GitHub Actions Secrets 等) を使用 | ||
| - `.env` ファイル + `.gitignore` で local-only 管理 (本番は別途) | ||
| - test fixture でも、regex に match する形式 (16 chars 以上の AKIA... 等) は避け、`AKIATEST` 等の明らかに無効な短い形を使う |
There was a problem hiding this comment.
SECRET_DETECTION_MSG の案内が実行可能な対処とズレています。
Line [536] で .env 管理を案内していますが、同ファイルの check_protected_file により .env* 編集はブロックされます。Claude 経由での対処として誤解を招くため、手動対応である旨を明記した方が安全です。
修正例
- `.env` ファイル + `.gitignore` で local-only 管理 (本番は別途)
+ - `.env` ファイル + `.gitignore` で local-only 管理 (本番は別途)
+ ※ `.env*` は保護対象のため、Claude ではなくユーザー側で編集してください🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/hooks-pre-tool-validate/src/main.rs` around lines 533 - 537, Update the
SECRET_DETECTION_MSG text so it no longer suggests editing `.env*` as an
automated fix (which conflicts with check_protected_file blocking `.env*`
edits); instead explicitly instruct that remediation is manual and recommend
moving secrets to environment variables (std::env::var or process.env), a secret
manager (1Password/Doppler/AWS/GH Secrets), or a local .env only for dev with
.gitignore, and instruct test fixtures to use clearly invalid short tokens like
"AKIATEST" to avoid regex matches; change the message in main.rs where
SECRET_DETECTION_MSG is defined and ensure wording references
check_protected_file to clarify the block.
Summary
secret-detectionpreset をhooks-pre-tool-validateに追加default_preset_names()に含めて Tier 1 default-on (漏洩の非対称性 = 1 度漏れたら手遅れ、jj-message-required の opt-in とは別判断)~/.claude/rules/common/security.md§ Secret Management の機械強制層 (feedback_pipeline_over_rules.md適用)Changes
src/hooks-pre-tool-validate/src/main.rs:preset_secret_detection()+SECRET_DETECTION_MSG定数 + 4 helper 関数 (resolve_edit_file_path,check_protected_file,check_secret_in_content,check_todo_staleness_for_edit) でhandle_write_edit_toolを 50 行制限内に分割 + 18 新規 tests.claude/hooks-config.toml:blocked_patternsにsecret-detection追加 + コメント sectiondocs/todo9.md: 順位 146 entry retiredocs/todo-summary.md: 146 行削除 + 199-202 行追加 (prior session の post-merge-feedback registrations)docs/todo10.md: 199-202 entries の詳細記載 (prior session 由来)既存設計との関係
jj-message-required) と同型実装パターン、BlockedPattern.exception機構を再利用 (Rust regex は negative lookahead 非対応、PR test(hooks): UTF-8 boundary + aggregation cap 拡充 (順位 125 + 57 + 91) #171 で導入された 2 段判定)exceptionで除外 + 専用 pattern で別途検出 (defense in depth)aws_secret_access_key = "...") に限定して FP 軽減Test plan
cargo test -p hooks-pre-tool-validate: 202/202 pass (新規 18 secret-detection tests 含む、回帰なし)pnpm build:hooks-pre-tool-validate→.claude/exe deploy → AI 自身の AWS-Access-Key 形式投入を PreToolUse 層で実 block 確認ls -la) / Edit (regular code) は exit 0 で allow 確認pre-push-review 非ブロッキング指摘 (follow-up 候補)
//非 doc コメント禁止 lint) との衝突あり、別途 function-level doc 追記で対応検討ghu_/ghr_token type の dedicated test 欠落 (gho_/ghs_と同 regex alternation 内のため低リスク、test matrix gap)同梱: 順位 199-202 (queue 登録のみ)
prior session で post-merge-feedback skill が PR #200 T2-1/T3-1/T3-2/T3-3 採用判定後に prepared した todo entries (実装はせず queue 登録のみ)。docs/todo-summary.md が同ファイル内で 199-202 行追加 + 146 行削除を抱え、jj 非対話 split (path 単位) で分離不能だったため bundle (詳細は commit description 参照)。
関連
jj-message-requiredpreset (template)~/.claude/rules/common/security.md§ Secret ManagementSummary by CodeRabbit
リリースノート
新機能
ドキュメント