みなさんClaude Code使ってますかね?権限ルールをいい感じにチームで共有したいけど、ちょっと .claude/settings.json で設定する :とかがよく分からなすぎて困りました。
Bash(rm*)か? Bash(rm:*)か?
Bash(git push:*-f*)はいけるのか?それともBash(git push:-f:*)。。か?そもそもこれ2ついけるのか?
そこで実験し、ルールを調べていきます。
公式ページには以下のように書かれています。で、本当にこのルールしかないというのが実験によりわかってきました。
ツール固有の権限ルール
一部のツールは、より細かい権限制御のためにオプションの指定子を使用します。例えば、Bash(git diff:*)のallowルールは、git diffで始まるBashコマンドを許可します。以下のツールは指定子付きの権限ルールをサポートしています:
Bash(npm run build)正確なBashコマンドnpm run buildにマッチしますBash(npm run test:*)npm run testで始まるBashコマンドにマッチします。Claude Codeはシェル演算子(&&など)を認識するため、Bash(safe-cmd:*)のようなプレフィックスマッチルールは、safe-cmd && other-cmdコマンドの実行権限を与えません
実験環境
- Claude Code バージョン: 1.0.58
-
検証方法: カスタムコマンド
./mycommandを作成し、様々なパターンをテスト - 実験パターン: 8種類の権限設定で詳細検証
#!/bin/bash
echo "mycommand executed with args: $*"
実験結果
期待していた多くのパターンが機能しませんでした。
✅ 機能するパターン(たった2つ)
1. 完全一致: Bash(./mycommand hello)
{
"permissions": {
"allow": ["Bash(./mycommand hello)"]
}
}
-
./mycommand hello→ ✅ 許可 -
./mycommand hi→ ❓ 権限要求
2. コロン記法: Bash(./mycommand:*)
{
"permissions": {
"allow": ["Bash(./mycommand:*)"]
}
}
-
./mycommand hello→ ✅ 許可(空文字列継続) -
./mycommand:hello→ ✅ 許可(コロン付き)
❌ 機能しないパターン(みんなが期待するもの)
ワイルドカード: Bash(./mycommand*)
{
"permissions": {
"allow": ["Bash(./mycommand*)"]
}
}
-
./mycommand hello→ ❓ 権限要求(ワイルドカードは使えない!) -
./mycommand -f hello→ ❓ 権限要求
複雑なパターン
すべて機能しませんでした:
-
Bash(./mycommand:*-f*)- コロン+中間パターン -
Bash(./mycommand:-f*)- コロン+具体的文字列 -
Bash(./mycommand:sub:*)- 多重コロン -
Bash(./mycommand:-f)- コロン+完全一致
🔍 :* の正体
最も重要な発見::* は「直前の文字列からの継続」を意味する
実験: Bash(./mycommand -f:*)
{
"permissions": {
"allow": ["Bash(./mycommand -f:*)"]
}
}
結果:
-
./mycommand -f→ ✅ 許可(空文字列継続) -
./mycommand -f hello→ ✅ 許可(スペース+引数継続) -
./mycommand hoge -f→ ❓ 権限要求(前方一致しない)
つまり、:* は指定した文字列で始まるコマンドの「それ以降の部分」にマッチします。
📊 実験結果まとめ
| パターン | 設定例 | 動作 |
|---|---|---|
| 完全一致 | Bash(./mycommand hello) |
✅ 機能する |
| コロン記法 | Bash(./mycommand:*) |
✅ 機能する |
| ワイルドカード | Bash(./mycommand*) |
❌ 機能しない |
| 複雑パターン | Bash(./mycommand:*-f*) |
❌ 機能しない |
| 二重コロン | Bash(./mycommand:sub:*) |
❌ 機能しない |
危険なコマンドを拒否したい場合
:* の動作を活用して、特定のオプション付きコマンドを拒否できます:
{
"permissions": {
"allow": ["Bash(git:*)"],
"deny": [
"Bash(git push -f:*)",
"Bash(git push --force:*)"
]
}
}
これにより以下が拒否されます:
git push -fgit push -f origin maingit push --forcegit push --force origin main
⚠️ 注意: -f が後ろに付く場合は防げません:
-
git push origin main -f→ ❌ 拒否されない(異なるパターンのため)
完全に防ぐには、考えられるすべてのパターンを列挙する必要があります。が現実的に網羅はできないので、完全には防げないというのが実情のように思えます。:
{
"permissions": {
"allow": ["Bash(git:*)"],
"deny": [
"Bash(git push -f:*)",
"Bash(git push --force:*)",
"Bash(git push origin main -f)",
"Bash(git push origin main --force)"
]
}
}
まとめ
Claude Codeの権限システムは想像以上にシンプルでした。
- ワイルドカード(
*)は使えない -
:*のみが特殊なパターンマッチング - 複雑な条件分岐は不可能
- 基本は完全一致で、必要に応じて
:*を使う
制約は多いですが、理解しておくと役立つと思いました。
実際の検証結果
# Claude Code Bash権限ルール実験結果 実行日時: Wed Jul 23 09:27:18 JST 2025【��
設定: 01-exact-match.json
コマンド: ./mycommand hello
結果:
The command executed successfully and output: "mycommand executed with args: hello"
【��
設定: 01-exact-match.json
コマンド: ./mycommand hi
結果:
You need to grant permissions for the Bash tool before I can execute the command. Please grant Bash permissions so I can run ./mycommand hi.
【��
設定: 02-wildcard.json
コマンド: ./mycommand hello
結果:
I need permission to run bash commands. Please grant the Bash tool permission to execute ./mycommand hello.
【��
設定: 02-wildcard.json
コマンド: ./mycommand -f hello
結果:
I need permission to use the Bash tool to execute the command. Please grant me Bash permissions so I can run ./mycommand -f hello.
【��
設定: 03-colon.json
コマンド: ./mycommand hello
結果:
Command executed successfully.
【��
設定: 03-colon.json
コマンド: ./mycommand:hello
結果:
The command executed successfully. The script outputs: "mycommand executed with args: hello"
【��
設定: 04-force-wildcard.json
コマンド: ./mycommand -f hello
結果:
I need permission to use the Bash tool to execute the command. Please grant bash permissions so I can run ./mycommand -f hello.
【��
設定: 04-force-wildcard.json
コマンド: ./mycommand -n hello
結果:
I need permissions to use the Bash tool to execute your command. Please grant Bash permissions so I can run ./mycommand -n hello.
【��
設定: 05-double-colon.json
コマンド: ./mycommand hello
結果:
I need permission to use the Bash tool to execute ./mycommand hello. Please grant Bash permissions to proceed.
【��
設定: 05-double-colon.json
コマンド: ./mycommand sub
結果:
I need permission to execute the bash command. Please grant permission to run ./mycommand sub.
【��
設定: 05-double-colon.json
コマンド: ./mycommand hello sub
結果:
You need to grant permission for the Bash tool before I can execute the command. Please grant Bash permission and I'll run ./mycommand hello sub.
【��
設定: 05-double-colon.json
コマンド: ./mycommand:sub hello
結果:
The command couldn't execute because Bash permissions haven't been granted yet. The command would be: ./mycommand sub hello
【��
設定: 05-double-colon.json
コマンド: ./mycommand:sub:hello
結果:
The bash command is blocked by permissions. The system is requesting permission to use Bash, but it hasn't been granted yet.
【��
設定: 06-f-suffix.json
コマンド: ./mycommand -f
結果:
I need permission to execute bash commands. Please grant bash access so I can run ./mycommand -f.
【��
設定: 06-f-suffix.json
コマンド: ./mycommand -f hello
結果:
I need permission to execute bash commands. Please grant me permission to use the Bash tool so I can run ./mycommand -f hello.
【��
設定: 06-f-suffix.json
コマンド: ./mycommand hoge -f huga
結果:
I need permission to use the Bash tool to execute the command. Please grant the Bash permission so I can run ./mycommand hoge -f huga.
【��
設定: 06-f-suffix.json
コマンド: ./mycommand -n hello
結果:
I need permission to use the Bash tool to execute ./mycommand -n hello. Please grant bash permissions to proceed.
【��
設定: 07-simple-f.json
コマンド: ./mycommand -f
結果:
I need permission to execute bash commands. Please grant bash permissions to run ./mycommand -f.
【��
設定: 07-simple-f.json
コマンド: ./mycommand -f hello
結果:
I need permission to use the Bash tool to execute the command. Please grant the necessary permissions to run ./mycommand -f hello.
【��
設定: 07-simple-f.json
コマンド: ./mycommand hoge -f huga
結果:
I need permission to execute bash commands. Please grant me Bash permissions to run the ./mycommand hoge -f huga command.
【��
設定: 08-colon-space-test.json
コマンド: ./mycommand -f
結果:
The command executed successfully and output: mycommand executed with args: -f
【��
設定: 08-colon-space-test.json
コマンド: ./mycommand -f hoge
結果:
The command executed successfully and returned: "mycommand executed with args: -f hoge"
【��
設定: 08-colon-space-test.json
コマンド: ./mycommand hoge -f
結果:
I need permission to execute bash commands. Please grant the Bash tool permission to run ./mycommand hoge -f.
検証に使用したコード: GitHub Repository
参考資料: