1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Claude Code を CI で最小権限で動かす:クラウドセッションの permission mode 制限を自分のパイプラインに移植する

1
Posted at

Claude Code のクラウドセッションでは、リポジトリにチェックインされた設定を使って bypassPermissions のセッションを始めることはできません。権限を広げるかどうかを、リポジトリの中身に決めさせない設計です。本稿では、この規則を自分で運用する GitHub Actions の CI に取り入れます。権限を与えるのはパイプラインの呼び出し側の 1 か所だけにし、リポジトリ側の設定には制限を強めるルールだけを置きます。そのうえで、設定が実際に効いているかを毎回テストします。仕様は 2026-11-01 時点の公式ドキュメント(ページ最終更新 2026-10-30)に基づきます [1]。

クラウドセッションが受け付けない設定

  • ドキュメントには "a repository's checked-in settings can't start a cloud session in bypass-permissions mode" と書かれています [1]。
  • 無視された defaultMode について警告は出ません。"The setting is ignored silently and the session starts in the permission mode" とあり、セッションはドロップダウンに表示されているモードで始まります [1]。
  • ドロップダウンについては "Auto mode appears only when your organization allows it" と "the selected model supports it. Bypass permissions isn't available." の記述があります [1]。
  • クラウドでは "cloud sessions pre-approve file edits regardless of mode" とされています。そのため Accept edits は default モードに相当します [1]。
  • 一方で "Cloud sessions still honor defaultMode: "acceptEdits" from settings." とあり、acceptEdits は有効なまま扱われます [1]。
  • クラウドで dontAsk がどう扱われるかは、本稿の出典の範囲では扱いません。

CI 向けのモード:dontAsk と完全一致の許可リスト

  • 公式の例は claude -p "run the test suite" --permission-mode dontAsk --allowedTools "Bash(npm test)" "Read" です [1]。
  • dontAsk は "for CI pipelines or restricted environments where you pre-define what Claude may do" と位置づけられています。また "the session never waits for input" とされています [1]。
  • 明示的な ask ルールについては "Claude Code denies calls matching your explicit ask rules rather than prompting." とあり、確認を求めずに拒否されます [1]。

モードを省略した場合の挙動はバージョンで変わってきた

  • モード未指定の claude -p と Agent SDK の実行では、"Calls needing approval are denied unless you approve them with an allow rule" とされています。hook、canUseTool コールバック、--permission-prompt-tool による承認も同じ扱いです [1]。
  • "Before v2.1.302, these runs could start in auto mode" という記述もあります(2026-11-01 時点のドキュメント)[1]。
  • このため CI では、モードをコマンドラインに明示し、Claude Code のバージョンも固定します。

auto と bypassPermissions を CI の既定にしない理由

  • auto モードに入ると "broad allow rules that grant arbitrary code execution are dropped" とされ、広い allow ルールは破棄されます [1]。
  • 停止の条件は "if the classifier blocks an action 3 times in a row or 20 times total" です。さらに "The thresholds of 3 blocks in a row and 20 blocks total are not configurable." とあります(2026-11-01 時点)[1]。
  • 分類器が既定でブロックする対象には "a CI workflow or deploy configuration that passes a secret to a destination" が含まれます。"approving Claude's own pull request, or disabling CI checks" も同様です [1]。
  • 分類器の呼び出しについては "classifier calls count toward your token usage." とされています [1]。
  • クラウドのスケジュールタスクについては "Sessions started by a cloud scheduled task ask the server as Anthropic rolls it out." とあります。判定が返らなかったアクションは "Claude Code denies the action rather than run it unreviewed." として拒否されます [1]。
  • bypassPermissions では "Allow rules have no effect in bypassPermissions" です。使い方は "Only use this mode in isolated environments like containers, VMs, or dev containers" と指示されています [1]。
  • 管理者は "Administrators can block this mode by setting permissions.disableBypassPermissionsMode" により、このモードを禁止できます。設定値は公式ドキュメントで確認してください [1]。
  • サンドボックスについても "Sandboxed commands can still delete your working directory" とされています [1]。

実装の構成(筆者の設計)

  • ci/allowed-tools.txt に許可リストを置き、--permission-mode dontAsk --allowedTools で渡します。
  • .claude/settings.json には deny と ask だけを置きます。allow と defaultMode は禁止し、scripts/validate-settings.sh で Claude の起動より前に検査します。
  • 不正な JSON の settings ファイルは、中のルールごと丸ごと読み飛ばされます。これは Claude Code 2.1.316 の起動時エラー表示で取得した文言に基づきます。
  • 許可リストと deny/ask の両方に同じルールを書くことは禁止します。両方に一致したときの優先順位は、本稿の出典では確認していないためです。
  • scripts/permission-test.sh で、許可したコマンドが実行され、deny と ask に一致したコマンドが実行されないことを、ファイルの有無で毎回判定します。
  • 実機で出力を取得したのは Claude Code 2.1.316 の 2 件だけです(不正 JSON の settings エラーと、deny ルールによる拒否メッセージ)。ワークフロー全体を通しで実行した出力は取得していません。

References

[1] https://code.claude.com/docs/en/permission-modes

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?