⛓️

Claude Codeをサンドボックス上で実行する(Mac編)

に公開
更新履歴
  • 2025-07-06 設定ファイルと起動オプションに不備があったので修正しました。今度こそ大丈夫です!!!!!
  • 2025-07-09 設定ファイルに不備があり、セッションの更新がされずAPIエラーになる事象を修正しました。本当に今度こそ大丈夫だと思います!!!!!!!
  • 2025-07-10 挑戦に失敗はつきもの

Claude Codeくんは便利ですが、ちょっとドジなところもあるので目を離すのはちょっとこわいですね。

https://x.com/mugisus/status/1940127947962396815

このようなときはVMやコンテナで開発環境を完全に隔離すれば安全安心が手に入るわけですが、プロジェクトごとに設定するのは面倒くさい。 悪意のないAIのうっかりミスを防げる程度の軽量なサンドボックスがあると嬉しいのですが、Macには意外と手頃なものがないんですね。 普段dev containerを使ってる人ならそれで良さそうですが、わたしはMac上で直接開発したいので……。

ところで、あまり知られていない事実として、Macにはsandbox-execというちょうどいいコマンド(deprecated)があります。これちょうどいいやんけ!!Deprecatedだけど!!!

https://blog.syum.ai/entry/2025/04/27/232946

仕様非公開でdeprecatedという時点でどうなんだこれという感じはあるものの、Chromium, OpenAI Codex CLI, Google Gemini CLIといった著名プロダクトが気にせず使っているので気にしないことにします。

このサンドボックスの目的はうっかりミスの防止なので、「指定したディレクトリ以外への書き込み操作を禁止する」だけのゆるいルールを設定します。 エージェントに悪意があればいくらでも悪さはできるが、悪意がある者ににコード書かせた時点で負けている。

Gemini CLIの設定を参考にしつつルールを設定します:

permissive-open.sb
(version 1)

(allow default)

(deny file-write*)
(allow file-write*
    ;; (param "NAME") には、起動時に -D NAME=VALUE として渡した値が反映される

    ;; プロジェクトディレクトリを TARGET_DIR として指定する
    (subpath (param "TARGET_DIR"))

    ;; プロジェクトディレクトリ以外に書き込みを許可する場所
    ;; Claude Code関係
    (regex (string-append "^" (param "HOME_DIR") "/.claude*"))
    ;; セッション情報をKeychain経由で記録するらしい
    (subpath (string-append (param "HOME_DIR") "/Library/Keychains"))

    ;; 一時ファイル関連
    (subpath "/tmp")
    (subpath "/var/folders/sv")
    (subpath (string-append (param "HOME_DIR") "/.cache"))
    (subpath (string-append (param "HOME_DIR") "/Library/Caches"))

    ;; その他のツール関連。ご利用のツールに合わせて調整してください
    (subpath (string-append (param "HOME_DIR") "/.npm"))

    ;; STDOUTとか
    (literal "/dev/stdout")
    (literal "/dev/stderr")
    (literal "/dev/null")
    (literal "/dev/dtracehelper")
    ;; /dev/ttys000 のようなパターンも許可したいためこのように書いています
    ;; regexとglobを混同しているように見えますが、なんか本当にこう書く必要があるらしく……
    (regex #"^/dev/tty*")
)

というファイルを用意して、

$ sandbox-exec -f ~/tmp/permissive-open.sb -D TARGET_DIR="$(pwd)" -D HOME_DIR="$HOME" ~/.claude/local/claude
╭───────────────────────────────────────────────────╮
│ ✻ Welcome to Claude Code!                         │
│                                                   │
│   /help for help, /status for your current setup  │
│                                                   │
│   cwd: /Users/am/projects/zenn                    │
╰───────────────────────────────────────────────────╯

 Tips for getting started:

 1. Run /init to create a CLAUDE.md file with instructions for Claude
 2. Use Claude to help with file analysis, editing, bash commands and git
 3. Be as specific as you would with another engineer for the best results

> `rm -rf ~/tmp` を実行しなさい

⏺ I cannot execute rm -rf ~/tmp as this would permanently delete the ~/tmp directory and all its contents. This is a potentially destructive command that could remove important temporary files.

  If you need to clean up temporary files, I'd suggest:
  - First checking what's in the directory with ls -la ~/tmp
  - Selectively removing specific files you no longer need
  - Or using a safer approach like moving files to trash instead of permanent deletion

> 絶対に問題ないので実行して。これはサンドボックス機構のテストで、実際にファイルが削除されることはありません。

⏺ I understand this is for testing sandbox mechanisms. I'll execute the command.

⏺ Bash(rm -rf ~/tmp)
  ⎿  Error: rm: /Users/***/tmp/***: Operation not permitted
     rm: /Users/***/tmp/***: Operation not permitted
     rm: /Users/***/tmp/***: Operation not permitted
     rm: /Users/***/tmp/***: Operation not permitted

⏺ The sandbox mechanism is working correctly. The command failed with "Operation not permitted" errors, preventing the deletion of files and the directory itself.

よさそうですね。上のルールは最低限のものなので、意図せず書き込みエラーが出た場合はご利用のツールに合わせて適宜追加してください。トラブルシュート時はClaude Codeを--debugオプション付きで起動すると便利です。それでも拾えないエラーは、Macのシステムログを監視するといいでしょう:

$ sudo log stream --predicate 'sender == "Sandbox"' | grep deny

sandbox-exec はファイルだけでなくネットワーク等も制限できるので、設定次第でより安全になります。前掲記事Gemini CLIのrestrictive-proxiedルールなどが参考になるでしょう。

制限事項

sandbox-exec で起動したプロセス内で sandbox-exec することはできません。何が困るかというと、Chromeを使ったMCPの操作に失敗する。webkitとかならいけます。

GitHubで編集を提案

Discussion