AI エージェントに鍵を渡しても大丈夫か — 6 つの渡し方
3 分で読む
Claude Code や Codex などの AI エージェントに、API キーを使う作業を頼むことがあります。鍵の渡し方はいくつかあり、渡し方によって鍵に起きることが違います。5 つの道具の公式文書で、渡し方ごとに確かめました。
まず、言葉をそろえる
| 言葉 | 意味 |
|---|---|
| 鍵 | システムが「使ってよい相手か」を確かめるための、秘密の文字列。パスワード、API キー、アクセストークンなどがある。ここでは、これらをまとめて「鍵」と呼ぶ |
| API キー | 鍵の一種。プログラムが別のシステムの API を呼ぶときに、自分が誰かを示すために送る文字列。API キーを持っている人は、持ち主と同じ操作ができる |
| AI エージェント | 指示を受けて、自分でファイルを読み、コマンドを動かして作業を進める AI |
| シェル・コマンド | パソコンに文字で命令する画面(シェル)と、そこに打つ命令(コマンド) |
| .env | API キーやパスワードなど、秘密の設定を書いておくファイル |
| 環境変数 | プログラムを動かすときに外から渡す設定の値。.zshrc などに書いておく |
| MCP | AI に外のサービスや道具をつなぐための決まり |
| OAuth | 固定の鍵の代わりに、期限と範囲を決めて許可を出す仕組み |
鍵の渡し方は 6 つ
鍵の文字列が AI の目に触れる所にあるほど、漏れる道が増えます。いちばん安全なのは、鍵の文字列そのものを AI に渡さない OAuth です。
対話文に貼る
貼った鍵は AI の会社に送られ、 出典を開く確認 2026-10-04 出典を開く確認 2026-10-04 出典を開く確認 2026-10-04Claude Code Docs(Data usage)原文
This data includes all user prompts and model outputs, encrypted in transit via TLS 1.2+.
Claude Code Docs(Data usage)原文
Claude Code clients store session transcripts locally in plaintext under
GitHub Docs(Copilot session data)原文
By default, locally-run sessions created with Copilot CLI or the GitHub Copilot app are synced to your GitHub account.
ファイルに書いて、AI に読ませる
AI が .env を読めば、中身は会話に入り、貼ったのと同じことになります。Gemini CLI は .env を自動で読み込みます。 出典を開く確認 2026-10-04 出典を開く確認 2026-10-04 出典を開く確認 2026-10-04Gemini CLI Docs(Configuration)原文
The CLI automatically loads environment variables from an .env file.
Claude Code Docs(Configure permissions)原文
To block Claude’s file tools from reading a file or directory, add a Read deny rule for its path, such as Read(./.env) or Read(./secrets/**)
Claude Code Docs(Configure permissions)原文
They don’t apply to a command that reads files without naming them
環境変数にしておく
.zshrc などに書いた環境変数は、AI には見えませんが、AI が動かすコマンドは初めからそれを受け継ぎます。読んだ文章に仕込まれた指示で、環境変数がまるごと外に送られた実証もあります。 出典を開く確認 2026-10-04 出典を開く確認 2026-10-04 出典を開く確認 2026-10-04Tracebit(2025-07-28)原文
all their environment variables have been sent to the attacker's server
OpenAI Codex Docs(Advanced configuration)原文
shell_environment_policy controls which environment variables Codex passes to spawned commands.
GitHub Docs(Copilot CLI command reference)原文
Redact an environment variable from shell and MCP server environments (can be used multiple times).
MCP の設定に書く
MCP の設定ファイルに鍵を直接書くと、ファイルごと漏れます。各社とも直接書かず、 出典を開く確認 2026-10-04 出典を開く確認 2026-10-04 出典を開く確認 2026-10-04Gemini CLI Docs(MCP servers)原文
Even when explicitly defined, you should avoid hardcoding secrets.
Astrix Security(State of MCP Server Security 2025・2025-10-15)原文
Over half (53%) rely on static API keys or Personal Access Tokens (PATs), which are long-lived and rarely rotated.
Astrix Security(State of MCP Server Security 2025・2025-10-15)原文
Only 8.5% use OAuth, the modern, preferred method for secure delegation.
OAuth で許可する、クラウドの鍵置き場に登録する
| 渡し方 | 何が起きるか |
|---|---|
| OAuth で許可する | 鍵の文字列を AI に渡さない。5 つの道具とも外部の MCP サーバーに OAuth でつなげる Claude Code MCP サーバーを追加するときの注意 。Copilot のクラウドのエージェントは未対応 GitHub Docs(Configure MCP servers for Copilot)原文do not currently support remote MCP servers that leverage OAuth for authentication and authorization.出典を開く確認 2026-10-04 |
| クラウドの鍵置き場に登録する | Claude Code と Codex OpenAI Codex Docs(Cloud environments)原文Programs receive a placeholder; the proxy substitutes the real value for allowed destinations.出典を開く確認 2026-10-04 |
期限の短い許可は、漏れたときの被害を小さくします。 出典を開く確認 2026-10-04Model Context Protocol 仕様(Authorization・2025-06-18)原文
short-lived access tokens to reduce the impact of leaked tokens
道具ごとの、鍵を守る設定
各行の原文は、調査の詳細に道具ごとに並べています。読ませない設定・環境変数を絞る設定・MCP の鍵の書き方・OAuth の 4 つで見ると、どの道具にも「初めは何もしない」所があります。
渡すなら、この順で
まず対話文に貼らないこと、次に権限を絞ることです。OWASP も、AI の拡張機能には OAuth と最小限の権限を使うよう勧めています。 出典を開く確認 2026-10-04 出典を開く確認 2026-10-04OWASP Top 10 for LLM Applications 2025(LLM06 Excessive Agency)原文
an LLM extension that reads a user’s code repo should require the user to authenticate via OAuth and with the minimum scope required.
Claude Code Docs(Configure permissions)原文
For OS-level enforcement that blocks all processes from accessing a path, enable the sandbox.
- 漏れたときに誰が止めるかは シークレットスキャンとは — 漏れた鍵は誰が止めるか にまとめてあります
- 鍵がどこから漏れるかは API キーはどこから漏れるのか にあります
- MCP サーバーを追加する前の確かめ方は MCP サーバーを追加する前に、何を確かめるか にあります
ここから先は調査の詳細です(約 4 分)。上のカードだけで決められます。調べた 1 件ずつの記録は IT連携マップ に、出典 URL と調査日つきで公開しています。
調査の詳細
2026 年 10 月 4 日に、Claude Code・Codex・Gemini CLI・GitHub Copilot・Cursor の公式文書(データの扱い・権限・設定・環境変数・MCP・クラウドの環境・除外設定)と、事例の報告、GitGuardian・Astrix Security の調査、MCP の仕様、freee・マネーフォワード・サイボウズの公式 MCP の文書を読みました。表の言い方は各文書の文から取り、原文は[原文・出典]に置いています。
除外設定の書き方と、効かないところ
| 道具 | 書き方 | 効かないところ |
|---|---|---|
| Claude Code | 設定ファイル(settings.json)の拒否の規則に Read(./.env) と書く。Claude Code Docs(Configure permissions)原文To block Claude’s file tools from reading a file or directory, add a Read deny rule for its path, such as Read(./.env) or Read(./secrets/**)出典を開く確認 2026-10-04 Claude Code Docs(Configure permissions)原文The dialog lists all permission rules and the settings.json file each rule comes from.出典を開く確認 2026-10-04 Claude Code Docs(Configure permissions)原文Read and Edit deny rules apply to Claude’s built-in file tools, to file commands Claude Code recognizes in Bash, such as cat, head, tail, sed, and tee出典を開く確認 2026-10-04 |
ファイルを自分で開くスクリプトなど Claude Code Docs(Configure permissions)原文or to arbitrary subprocesses that read or write files indirectly, like a Python or Node script that opens files itself.出典を開く確認 2026-10-04 |
| Codex | .env の読み取りを拒む規則を書ける OpenAI Codex Docs(Configuration reference)原文glob subpaths such as "**/*.env" can deny reads with "deny".出典を開く確認 2026-10-04 |
環境変数は初期設定で KEY・SECRET・TOKEN を含むものも渡す OpenAI Codex Docs(Advanced configuration)原文so Codex doesn’t automatically remove variable names containing出典を開く確認 2026-10-04 |
| Gemini CLI | プロジェクトの一番上に .geminiignore を作る。Gemini CLI Docs(Ignoring files)原文Create a file named .geminiignore in the root of your project directory.出典を開く確認 2026-10-04 Gemini CLI Docs(Configuration)原文Whether to ignore generic .env files in the project directory.出典を開く確認 2026-10-04 |
この設定に対応していない道具 Gemini CLI Docs(Ignoring files)原文file will exclude them from tools that support this feature, although they will still be visible to other services出典を開く確認 2026-10-04 |
| GitHub Copilot | リポジトリの設定の Content exclusion にパスを書く。GitHub Docs(Excluding content from GitHub Copilot)原文enter the paths to files from which Copilot should be excluded.出典を開く確認 2026-10-04 GitHub Docs(Excluding content from GitHub Copilot)原文Organizations with a Copilot Business or Copilot Enterprise plan.出典を開く確認 2026-10-04 |
CLI と、編集ソフト(IDE)のエージェントモード GitHub Docs(Excluding content from GitHub Copilot)原文GitHub Copilot CLI and Agent mode in Copilot Chat in IDEs do not support content exclusion.出典を開く確認 2026-10-04 |
| Cursor | プロジェクトの一番上に .cursorignore を作る。.env は初めから除外 Cursor Docs(無視ファイル)原文Cursor は .gitignore 内のファイルと、以下のデフォルトの ignore リストに含まれるファイルを自動的に無視します。出典を開く確認 2026-10-04 |
エージェントの端末と MCP の道具 Cursor Docs(無視ファイル)原文Agent が使用する Terminal および MCP サーバーツールでは、.cursorignore で指定されたコードへのアクセスをブロックできません。出典を開く確認 2026-10-04 |
会話の記録と、送られる先
| 道具 | 分かっていること |
|---|---|
| Claude Code | 手元に、暗号化しない文字のまま保存する。Claude Code Docs(Data usage)原文Claude Code clients store session transcripts locally in plaintext under出典を開く確認 2026-10-04 Claude Code Docs(Environment variables)原文Set to 1 to strip credentials from the environments of the subprocesses Claude Code starts, such as Bash commands, hooks, and stdio MCP servers.出典を開く確認 2026-10-04 |
| Codex | 会話の記録を手元に保存する OpenAI Codex Docs(Advanced configuration)原文By default, Codex saves local session transcripts under出典を開く確認 2026-10-04 |
| Cursor | Privacy Mode を切ると、プロンプトなどを保存し、学習に使うことがある Cursor 学習に使われるか(プライバシー) 。エージェントのコマンドは利用者の端末で動く |
MCP の鍵の書き方
| 何が | 分かっていること |
|---|---|
| Claude Code | 鍵を持つ MCP サーバーは、自分だけの範囲(local)に追加できる Claude Code MCP サーバーを追加するときの注意 |
| Codex | 鍵を環境変数の名前で指定できる OpenAI Codex Docs(MCP)原文Environment variable name for a bearer token to send in Authorization.出典を開く確認 2026-10-04 |
| GitHub Copilot CLI | 設定の中で環境変数を参照できる GitHub Docs(Copilot CLI command reference)原文Supports $VAR, ${VAR}, and ${VAR:-default} expansion.出典を開く確認 2026-10-04 |
| MCP の仕様 | 手元で動かす MCP サーバーは、鍵を環境から読む Model Context Protocol 仕様(Authorization・2025-06-18)原文Implementations using an STDIO transport SHOULD NOT follow this specification, and instead retrieve credentials from the environment.出典を開く確認 2026-10-04 |
| freee | 公式の MCP サーバーがある freee会計 MCP 対応 |
| マネーフォワードの公式 MCP | API キーは、発行した人の権限の範囲でしか使えない マネーフォワード クラウド(MCPサーバーの認証・認可)原文APIキーを発行したユーザー自身がマネーフォワード クラウドの各サービスで付与されている権限でアクセス可能なデータのみにアクセスできます。出典を開く確認 2026-10-04 |
| kintone の公式 MCP | 本番では、API トークンの権限を必要最小限にするようサイボウズ自身が書いている cybozu developer network(kintone MCPサーバーのクイックスタート)原文本番環境で利用する場合は、対象アプリのAPIトークンの権限や「パスワード認証」時のユーザー権限を必要最小限に設定し、AIがアクセスできる範囲を適切に制限してください。出典を開く確認 2026-10-04 |
OAuth とクラウドの鍵置き場
| 道具 | 分かっていること |
|---|---|
| Codex | OAuth に対応したサーバーには codex mcp login でつなぐ |
| Gemini CLI | 外部の MCP サーバーへの OAuth に対応 Gemini CLI Docs(MCP servers)原文supports OAuth 2.0 authentication for remote MCP servers using SSE or HTTP transports.出典を開く確認 2026-10-04 |
| GitHub Copilot CLI | OAuth の手続きをやり直すコマンドがある。クラウドのエージェントでは、登録した鍵はログで伏せられる GitHub Docs(Copilot cloud agent の secrets)原文Secret values are masked in Copilot cloud agent session logs.出典を開く確認 2026-10-04 |
| Cursor | OAuth に対応 Cursor MCP 対応 。クラウドのエージェントの鍵は、会話の記録などで伏せられる |
文章に仕込まれた指示で、鍵が外に送られた事例
AI は、読んだ文章の中の指示に従ってしまうことがあります(プロンプトインジェクション)。
| 対象 | 起きたこと | 種類 |
|---|---|---|
| Claude Code | 確認を飛ばしてファイルを読み、中身をネットワークに送れた GitHub Advisory Database(GHSA-x5gv-jw7f-j6xj・CVE-2025-55284)原文it was possible to bypass the Claude Code confirmation prompts to read a file and then send file contents over the network without user confirmation.出典を開く確認 2026-10-04 |
研究者の実証 |
| Gemini CLI | 環境変数がまるごと攻撃者のサーバーに送られた Tracebit(2025-07-28)原文all their environment variables have been sent to the attacker's server出典を開く確認 2026-10-04 |
研究者の実証 |
| GitHub Copilot Chat | リポジトリ全体から鍵の名前を探させ、外に出せた Legit Security(2025-10-08)原文I also managed to get Copilot to search the victim's entire codebase for the keyword出典を開く確認 2026-10-04 |
研究者の実証 |
| npm(JavaScript の部品の置き場)の部品(s1ngularity) | 入り込んだ悪意のあるコードが、利用者の AI の道具を危険なオプション付きで動かした Wiz(2025-08-27)原文Notably, the campaign weaponized installed AI CLI tools by prompting them with dangerous flags出典を開く確認 2026-10-04 |
実際の攻撃 |
AI に書かせると、鍵は漏れやすいか
| 調べたもの | 結果 |
|---|---|
| 公開されている GitHub のコミット(コードの変更の記録) | Claude Code を使ったコミットで鍵が混ざる割合は 3.2%、全体は 1.5% GitGuardian(The State of Secrets Sprawl 2026・2026-03-17)原文Claude Code-assisted commits showed a 3.2% secret-leak rate, versus a 1.5% baseline across all public GitHub commits.出典を開く確認 2026-10-04 |
| AI エージェントが作った変更の依頼 | 漏れた本物の鍵の 67.6% は、人が入れていた Sakib et al.「Trust but Verify?」(arXiv 2607.12428・2026-07-14)原文human collaborators are responsible for introducing 67.6% of genuine leaked secrets within these agent-assisted workflows出典を開く確認 2026-10-04 |
差はありますが、AI が原因とは言えません。報告した本人たちも、道具の欠陥と単純に読むべきではないと書いています。 出典を開く確認 2026-10-04GitGuardian(The State of Secrets Sprawl 2026・2026-03-17)原文
That gap is meaningful, but it should not be read as a simple tool failure.
言えないこと
- 文書に書かれている設定と動きを並べたもので、5 つの道具を同じ条件で動かして比べたわけではありません
- 事例は、報告した人の実証と報告です。私たちは再現していません
- コミットの割合は、報告した会社の数え方によるもので、AI を使った場合と使わない場合を条件を揃えて比べたものではありません
- 公開されている MCP サーバーの割合は、Astrix Security が GitHub で集めた範囲のものです
- Gemini CLI の文書には、環境変数の中の鍵らしい値を伏せる働きの説明がありますが、同じ文書の設定一覧ではその設定の初期値が無効になっており、どちらが今の動きかは確かめていません
- 会話がどれだけの期間保存されるかは、契約や設定によって違います。ここでは扱っていません