AI Agent 許可權控制怎麼做?2026 Schema、Sandbox、Tool Policy 與四層 JSON 安全模型
許可權不是一張 allow 列表。2026 年要把 AI Agent 管住進 JSON,通常要疊四層:Schema 卡 tool_use.input 形狀,Tool Permission 決定 allow / ask / deny 與 SDK / 託管子 Agent 模式,Runtime Sandbox(Claude Bash sandbox、OpenShell network_policies 按 binary)卡程序與出站,Agent Identity + 審計 把誰在什麼 sandbox 裡調了什麼落成可 Diff 的事件。本篇是 ai-coding-agent-permissions-json.html 的進階全棧圖,不重寫 nvidia-openshell-agent-sandbox-json.html 的 MCP L7 深讀。
閱讀前速覽
| 層 | 2026 產品事實 | JSON 裡看什麼 |
|---|---|---|
| ① Schema | 宿主在 tool 執行前用 draft 2020-12 等驗 input;Agents API / MCP 都掛 parameters | pattern、additionalProperties、required |
| ② Tool Policy | Claude Code permissions JSON;managed agents 的 always_allow / always_ask / auto;SDK permission_mode | allow、ask、deny、eval_order |
| ③ Sandbox | 會話 Bash sandbox;OpenShell 按 binary 的 egress + 建立時 Landlock(network 可熱載入) | per_binary、egress_default、sandbox_id |
| ④ Audit | 每條 tool 決策寫 agent id、session、sandbox、各層 verdict | layers.schema、layers.permission、outcome |
| Harness | 環裡仍要分檔案存 schema、manifest、policy、audit——不要合成一個 blob | messages 與 state 分開 |
事實截至 2026-11-07:Claude Code 許可權與 Bash sandbox 文件、OpenAI Agents SDK permission_mode、NVIDIA OpenShell network_policies 與 openshell-sandbox。訂單 A-1042、sb_agent_demo_7f3a、evt 樣例為虛構示意。
AI Agent 許可權控制 = Schema + Policy + Sandbox + 審計,四層 JSON 各管一段
入門篇講會話裡 Bash / Read / Git 的 allow / ask / deny——那是在 harness 門口攔意圖。模型仍可能吐出畸形 order_id、換子 Agent 繞過父級規則,或在 runner 程序裡直連未登記 host。2026 常見做法是四層串聯:
- Schema 層:每個 tool 一份 JSON Schema,
additionalProperties: false+ 業務 pattern(如A-1042),失敗就不進 permission 求值。 - Policy 層:manifest 裡按工具名與 pattern 求值 deny → ask → allow;託管 Explore / Plan 等子 Agent 用 always_allow / always_ask / auto;SDK 側另有 acceptEdits 等 mode。
- Sandbox 層:IDE 裡 Bash sandbox 限制工作區;生產 worker 進 OpenShell,
network_policies按哪個 binary 能連哪——不是用聊天許可權代替出站。 - 審計層:把 schema 結果、permission matched rule、sandbox egress 與
agent.identity寫進一條事件,方便合規與事後 Diff。
入門許可權模型見 Coding Agent 的許可權模型。OpenShell 執行時與 MCP 出站細節見 OpenShell runtime sandbox(本篇只引用 per-binary 網路段)。Harness 怎麼分 state 見 Agent Harness 與 JSON State。Schema 動機見 AI 為什麼需要 JSON Schema;Agents API 形狀見 OpenAI Agents API 與 JSON Schema。
四層從外到內:審計包著 sandbox,sandbox 包著 policy,policy 之前是 Schema
Schema 對的是引數物件,與工具名無關:MCP arguments 與 Claude tool_use.input 都應在宿主驗過再下發。Permission 對的是工具與命令模式:Bash(git push *) 永遠 deny 先於 allow;mcp__orders 可以 default allow,但不替代 Schema。Sandbox 對的是程序與網路事實:即使使用者在 IDE 裡點了 Allow,未登記的 /opt/agent/runner 仍不能向新 host 出站;Landlock 在 sandbox 建立時釘死,與 network 熱載入不是一回事。Identity / 審計 對的是可追溯性:CI bot、人工會話、子 Agent 要有 agent.id 與 session_id,否則四層都過了也無法歸因。Claude Code 的 Bash sandbox(workspace-write 等)是會話內的第三層補丁;OpenShell 是伺服器側 runtime——兩套 JSON 都要保留,不要合併成一條 allow 規則。
四層各留一段 JSON:不要塞進同一條 message
1. Tool input Schema(第一層)。 在註冊 tool 或拉 MCP capabilities 時繫結;執行前校驗 lookup_order 的 input:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "lookup_order_input",
"type": "object",
"additionalProperties": false,
"required": ["order_id"],
"properties": {
"order_id": {
"type": "string",
"pattern": "^[A-Z]-[0-9]{4}$",
"description": "Warehouse order id, e.g. A-1042"
},
"include_line_items": {
"type": "boolean",
"default": false
}
}
}
2. Permission manifest(第二層)。 會話或倉庫級;含託管子 Agent 與 SDK mode。注意 eval_order:
{
"version": "2026-09",
"permission_mode": "default",
"eval_order": ["deny", "ask", "allow"],
"managed_agents": {
"Explore": "always_allow",
"Plan": "always_ask",
"BashReview": "auto"
},
"tools": {
"Bash": {
"default": "ask",
"sandbox": "workspace-write",
"rules": [
{ "pattern": "git status", "decision": "allow" },
{ "pattern": "git diff *", "decision": "allow" },
{ "pattern": "git push *", "decision": "deny" }
]
},
"Edit": { "default": "ask" },
"mcp__orders": { "default": "allow" }
},
"sdk": {
"permission_mode": "acceptEdits",
"bypass_permissions": false
}
}
3. OpenShell network_policies 節選(第三層)。 強調 per-binary egress;Landlock 不在這份檔案裡熱改——詳見 OpenShell 專文:
{
"network_policies_version": "2026.11",
"hot_reload": true,
"per_binary": [
{
"binary": "/opt/agent/runner",
"egress_default": "deny",
"allow": [
{ "host": "orders.example.com", "port": 443, "path_prefix": "/mcp" }
]
},
{
"binary": "/usr/bin/python3",
"egress_default": "deny",
"allow": [
{ "host": "pypi.org", "port": 443 }
]
}
],
"landlock_note": "filesystem_policy applies at sandbox create, not via this file"
}
4. 審計事件(第四層)。 一次成功呼叫四層 verdict 齊全,便於與 harness messages 裡的 tool_use 對齊:
{
"event_id": "evt_9c2f8a01",
"ts": "2026-11-07T06:22:11Z",
"agent": {
"id": "agt_codereview_v3",
"session_id": "sess_7f3a",
"identity": "service-account:ci-bot",
"sandbox_id": "sb_agent_demo_7f3a",
"supervisor": "openshell-sandbox"
},
"layers": {
"schema": { "tool": "lookup_order", "valid": true, "validator": "draft-2020-12" },
"permission": { "decision": "allow", "matched": "mcp__orders", "rule": "default" },
"sandbox": { "binary": "/opt/agent/runner", "egress": "allowed" }
},
"tool_use_id": "toolu_order_01",
"input_digest": "sha256:8f3e…",
"outcome": "executed"
}
Schema、Policy、Sandbox、審計——別用一層冒充全棧
團隊常把「只要 Schema 嚴格」或「只要許可權檔案 deny git push」當成全部安全。對照下面四層物件與典型漏點:
| 層 | 管什麼 | 只做這一層時的漏點 |
|---|---|---|
| JSON Schema | input / arguments 形狀與列舉 |
引數合法仍可能 git push 或 runner 直連外網 |
| Tool Permission | 工具名、Bash pattern、託管 Agent、SDK mode | IDE Allow 一次不等於二進位制出站登記 |
| Runtime Sandbox | 程序、Landlock、per-binary egress | sandbox 預設不驗 argument pattern(訂單號仍是 Schema) |
| Audit / Identity | 誰、哪 session、哪 sandbox、各層結果 | 沒有 audit 無法證明合規,也無法 Diff 誤配 |
會話許可權入門見 Coding Agent 的許可權模型。出站與 openshell-sandbox 生命週期見 OpenShell runtime sandbox。引數哲學見 AI 為什麼需要 JSON Schema——四層是疊加,不是互斥替換。
五步把四層寫成可版本化的 JSON
用虛構訂單 A-1042 在 staging 跑通再放寬 policy:
- 每個 tool 一份 Schema 檔案,與 OpenAPI / MCP 定義同源;CI 裡對樣例 input 跑 validate。
- Permission manifest 進倉庫,deny 規則寫在 allow 前;託管子 Agent 顯式寫 always_ask,別依賴預設。
- Sandbox 分會話與 runtime:本地 Claude Bash sandbox 與工作區根;伺服器 OpenShell 建立時寫 FS,network_policies 單獨版本化。
- 審計流寫 append-only(或不可變物件儲存);欄位固定 schema/permission/sandbox 子物件,便於 JSONPath。
- 拒絕對齊四層:先看 schema_valid,再看 permission decision,再看 sandbox egress,最後看 identity 是否匹配預期 bot。
五步都落在可 Diff 的 JSON 上,才能分清是畸形 order_id、deny 規則、未登記 binary,還是 audit 管道丟了事件。
tool 被拒或「明明 allow 卻不執行」時攤開四份 JSON
不要只盯 model 的 tool_use;並行拉 manifest、network_policies 快照與最近 audit。
- Schema、manifest、policy、audit 用 格式化;input 用 Schema 校驗 驗
A-1042。 - 用 JSONPath 提取 抽
$.tools.Bash.rules[*].pattern、$.per_binary[*].binary、$.layers.permission.matched。 - manifest 與 policy 變更前後 JSON Diff;優先看 deny 與 egress_default,而不是改 system prompt。
- Claude:
/permissions或匯出 JSON;OpenShell:openshell logs --tail --source sandbox。 - audit 與 manifest 裡的 token 先 脫敏;JSONSort 在瀏覽器本地跑。
四份物件對齊後,才能判斷是 Schema 擋了畸形 ID、deny 搶了 allow、binary 未登記,還是子 Agent 用了 always_allow 越過父級預期。
常見誤區
- 只上 Schema,不寫 permission manifest。 合法引數仍可執行危險 Bash 或 MCP。
- 把 SDK bypass_permissions 當成生產開關。 僅適合隔離評測環境,不能替代四層 JSON。
- 用聊天 Allow 代替 OpenShell 登記。 harness 不登記 runner binary,出站仍會死在代理層。
- 託管 Agent 預設 always_allow。 Explore 子 Agent 可以擴大工具面;父 manifest 要明確 always_ask。
- 審計只存 tool_result 文字。 沒有 layers.* 就無法證明 Schema 與 sandbox 當時是否透過。
- 用本篇替代 OpenShell MCP L7 專文調出站。 本文只畫全棧與 per-binary 網路;L7 tools/call 匹配讀 OpenShell 文。
FAQ
和 ai-coding-agent-permissions-json 有什麼區別?
那篇是會話許可權入門(Shell / Git / FS)。本篇在它之上疊 Schema、runtime sandbox、託管 Agent、SDK mode 與審計 JSON——是完整棧而不是重複 allow 列表。
Schema 放在模型側還是宿主側?
必須在宿主(或 gateway)驗。模型 strict 輸出能減少錯形,但不能代替服務端的 additionalProperties 與 pattern。
Claude Bash sandbox 和 OpenShell 要用哪個?
都要,物件不同:前者管開發者筆記本上的工作區寫範圍;後者管伺服器上 runner 程序、Landlock 與 per-binary 出站。JSON 分開存。
managed_agents 的 auto 是什麼意思?
由產品定義的自動策略(常介於 always_ask 與 always_allow 之間)。生產建議對 Plan / BashReview 類子 Agent 寫清 always_ask 或顯式 deny 工具集。
審計 JSON 要保留多久?
按合規要求;技術上應保留足夠欄位重做 Diff(至少 tool_use_id、agent.identity、layers 與 outcome)。PII 先脫敏再進長期儲存。
結論
AI Agent 許可權控制做得穩,靠四層 JSON 各司其職:Schema 卡引數、Policy 卡工具與託管邊界、Sandbox 卡程序與出站、審計卡身份與可證明性。
先對齊許可權入門專文裡的 allow/ask/deny,再按本篇分檔案落地;出站與 L7 細節讀 OpenShell 專文,Schema 與 harness 讀各自文章——不要合成一張「萬能 allow」。
延伸閱讀
- Coding Agent 的許可權模型 allow / ask / deny 入門
- OpenShell runtime sandbox openshell-sandbox、Landlock、MCP 出站——本篇只引 per-binary 網段
- Agent Harness 與 JSON State 環、state、tool_use 放哪
- 為什麼 AI 需要 JSON Schema 引數是第二道門
- OpenAI Agents API 與 JSON Schema 託管 Agent 與 parameters 形狀
- JSONSort 本地工具箱 格式化、校驗、Diff、Schema
- 全部文章 更多 JSON 與 AI 開發筆記
更新記錄: 首次發布