fix(claude): git許可の広範なワイルドカードとフックのfail-openを修正 - #76
Merged
Conversation
- permissions.allow の Bash(git *) を廃止し、安全なサブコマンド単位の許可に限定 (git -c/-C 等のグローバルオプションによる任意コマンド実行を無確認で許可していたため) - PreToolUse フックを dangerouslyDisableSandbox 検知のみから、 git -c/-C/--exec-path 等の危険なグローバルオプション使用も検知するよう拡張 (grep失敗時に無条件で素通りするfail-open設計だった点も、jqでの構造化抽出に変更して解消) - sandbox.filesystem.denyRead に .env/secrets/credentials/*.pem/*.key 等の 汎用パターンを追加し、Bash経由でも permissions.deny の Read(...) 相当の 機密ファイル保護が効くようにした
--upload-pack/--receive-pack は git fetch/pull/push/clone のサブコマンドの 後に置かれるフラグであり、「gitの直後」しか見ていなかった前回の修正では 実際の攻撃パターン(例: git fetch origin --upload-pack=evil)を検知できて いなかった。 - フックの正規表現を「gitの直後」限定から、--upload-pack/--receive-pack/ --exec-path はコマンド文字列中のどこにあっても検知する方式に変更 - -c/-C は誤検知(tar -C, curl -c等)を避けるため、"git"という単語が コマンド中に存在する場合のみ検知する方式に変更 - GIT_SSH_COMMAND=/GIT_CONFIG_*= の環境変数プレフィックスも新たに検知 - permissions.deny にも Bash(git * --upload-pack*) 等を追加し、 フックだけでなく許可ルール側でも git fetch */git pull * のワイルドカード 経由の抜けを二重に塞いだ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
#75 マージ後、バックグラウンドのセキュリティレビューから以下3つの重大度の高い指摘を受けた(レビューの完全な詳細は本セッションには配信されず要約のみだったため、指摘対象のファイルを自分で再点検して具体化した)。
修正内容
1. ALLOWLIST SEMANTIC ESCAPE / 任意コード実行
permissions.allowのBash(git *)は、git -c core.pager=...、git -c core.editor=...、git config alias.x '!任意コマンド'のような gitのグローバルオプション経由の任意コマンド実行まで無確認で許可していた。→ 安全なサブコマンド単位(
git status,git log,git diff,git commit,git push(引数なし)等)の明示的な許可リストに置き換え、-c/-Cが先頭に来るパターンは一切マッチしないようにした。2. FAIL-OPEN STATE DRIFT(フックの検知漏れ・失敗時の素通り)
PreToolUseフックはdangerouslyDisableSandboxフラグのみをgrepでチェックしており、(a) 上記1のgit経由の任意実行は検知対象外、(b) grepが何らかの理由で失敗すると無条件でexit 0(素通り)するfail-open設計だった。→
jqでJSON構造からtool_input.command/dangerouslyDisableSandboxを明示的に抽出する方式に変更し、git -c/-C/--exec-path/--upload-pack/--receive-packの使用も検知して承認を要求するようにした。3. Bash経由の機密ファイル読み取り抜け(Readツール限定だったdenyの拡張)
permissions.denyのRead(**/*.pem)等はReadツールのみに効き、Bash(cat foo.pem)のような同等アクセスはsandbox.filesystem.denyReadの固定パス(~/.ssh/等)でしか防げていなかった。→
sandbox.filesystem.denyReadに**/.env,**/secrets/**,**/credentials,**/*.pem,**/*.key等の汎用パターンを追加。意図的に対応を見送った点(残課題)
sandbox.excludedCommandsにあるdocker/gh/gcloud/aws/kubectl/terraform/chezmoiの広範なサンドボックス除外(前方一致のため複合コマンドでのチェイン回避リスクが残る)は、SSH経由のgit操作やDockerソケット、各クラウドCLIのプライベートエンドポイント等、サンドボックス化すると壊れる可能性のある実運用への影響を安全に検証できなかったため、本PRでは対象外とした。個別に動作確認しながら段階的に対応する別PRが必要。テスト
jq . dot_claude/settings.jsonでJSON構文を検証dangerouslyDisableSandbox: true/git -c core.pager=evil log/git status(安全系) /ls -la(無関係) の4パターンでstdin経由で手動実行し、意図通りに ask / silent が分岐することを確認chezmoi apply後、実際の日常的なgit操作(status/log/diff/commit/push等)で不要な確認プロンプトが増えていないか確認