AI 開發

AI Agent 為什麼必須跑在 Sandbox 裡?2026 NVIDIA OpenShell 執行時邊界與 MCP tools/call 出站控制詳解

閱讀約 15 分鐘

模型只會填 JSON。真正危險的是誰執行、誰聯網、誰讀盤。2026 年的 NVIDIA OpenShell 把 Agent 放進 openshell-sandbox:建立時鎖 landlock 與檔案策略,出站走帶 TOFU 的二進位制白名單代理,在 L7 對 protocol: mcp 與 legacy json-rpc 分別 enforce。本篇講執行時 sandbox,不重寫會話許可權,也不展開 Sentry。

閱讀前速覽

元件 OpenShell 事實(文件 / PR) JSON 裡看什麼
openshell-sandbox 監管 Agent 程序生命週期;狀態可查詢 sandbox_id、state、supervisor
L7 出站代理 2026 策略在應用層匹配 MCP methods / tools/call 與 json-rpc method connections[].protocol、enforcement
TOFU 二進位制 只有登記過的 binary 才能發起出站連線 proxy.binary_allowlist、matched_rule
Landlock / FS filesystem_policy 與 landlock 在沙箱建立時生效,不能當網路規則熱載入 landlock.applied_at_create
批處理 json-rpc 一批裡一條 deny,整批 deny(與 MCP 行為一致) batch 第二跳 legacy method 被拒

事實截至 2026-11-06:OpenShell 策略文件、openshell-sandbox CLI、2026 年 L7 MCP/json-rpc enforcement 相關 PR 與發行說明。訂單 A-1042、sb_agent_demo_7f3a 為虛構。status JSON 是便於 Diff 的示意,不是未公開 wire dump。

Agent 需要 sandbox,因為 JSON 不能代替核心與出站門

Coding Agent 的 loop 永遠是:模型產出 tool_use,宿主或 runtime 去執行。沒有 sandbox 時,一次幻覺的 tool 名或引數可能變成任意程序、任意路徑、任意 URL。OpenShell 把邊界放在模型外面三層:

  • 程序與檔案:openshell-sandbox 啟動受管程序;Landlock 與 filesystem_policy 在建立時釘死可讀寫的樹。
  • 出站 HTTP:代理按 endpoint 的 protocol 解析 JSON-RPC——MCP 的 tools/call 與 legacy 的 reports.search 走不同規則表。
  • 宿主仍要驗參:代理預設不匹配 arguments;A-1042 形狀是 Schema 的事,不是 sandbox 替你做。

工具名 allow/deny 與 Sentry 硬體層見 OpenShell 工具名與 Sentry,本篇只講 runtime。會話裡 Shell/Git 的 allow/ask/deny 是 harness 名單,見 Coding Agent 的許可權模型。MCP 線型見 Coding Agent 如何呼叫 MCP。環與 state 怎麼落見 Agent Harness 與 JSON State。

openshell-sandbox:監管程序,不是聊天許可權

CLI openshell-sandbox(supervisor 名在狀態 JSON 裡)負責把 Agent worker 放進隔離環境: cgroup / 名稱空間、Landlock、以及掛到同一 sandbox 的 egress 代理。模型側的 tool_use 只在 harness 裡被批准;真正發起 tools/call 的是沙箱裡的 Python 或 runner 二進位制。因此 TOFU(trust-on-first-use)登記的是哪個二進位制可以連哪個 Tool server,而不是模型 token 裡寫了什麼。網路策略 network_policies 可以熱載入;檔案與 Landlock 不行——改盤許可權要重建 sandbox。2026 文件強調 L7 enforcement:對 MCP 端點看 method 與 params.name;對 legacy json-rpc 端點看頂層 method(例如 reports.search)。私網、迴環、鏈路本地與雲後設資料地址預設不在出站 allow 裡。

從 tool_use 到出站:四段 JSON

1. 模型 tool_use(harness 裡)。 名字與 input 仍要過 strict / Schema;sandbox 還沒參與:

{
  "type": "tool_use",
  "id": "toolu_order_01",
  "name": "lookup_order",
  "input": {
    "order_id": "A-1042"
  }
}

2. 虛構 sandbox status。 看重連上的 Tool server、protocol、最近一次 egress 決定(便於和策略 Diff):

{
  "sandbox_id": "sb_agent_demo_7f3a",
  "state": "running",
  "supervisor": "openshell-sandbox",
  "created_at": "2026-11-06T08:12:04Z",
  "landlock": { "applied_at_create": true, "filesystem_policy": "orders_ro_v1" },
  "proxy": {
    "mode": "tofu",
    "binary_allowlist": ["/usr/bin/python3", "/opt/agent/runner"],
    "connections": [
      {
        "tool_server": "orders-mcp",
        "host": "orders.example.com",
        "port": 443,
        "path": "/mcp",
        "protocol": "mcp",
        "enforcement": "enforce",
        "status": "connected"
      },
      {
        "tool_server": "reports-legacy",
        "host": "reports.internal.example.com",
        "port": 8443,
        "path": "/rpc",
        "protocol": "json-rpc",
        "enforcement": "enforce",
        "status": "connected"
      }
    ]
  },
  "last_egress_decision": {
    "allowed": true,
    "method": "tools/call",
    "tool": "lookup_order",
    "matched_rule": "orders-mcp.tools/call.lookup_order"
  }
}

3. 沙箱程序發出的 MCP tools/call。 代理在 L7 讀這份 body,不是讀模型的 prose:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "lookup_order",
    "arguments": { "order_id": "A-1042" }
  }
}

4. 同一批裡的 legacy json-rpc。 第二跳 reports.search 若未在 rules 裡,整批被拒:

[
  {
    "jsonrpc": "2.0",
    "id": "batch-1",
    "method": "tools/call",
    "params": {
      "name": "lookup_order",
      "arguments": { "order_id": "A-1042" }
    }
  },
  {
    "jsonrpc": "2.0",
    "id": "batch-2",
    "method": "reports.search",
    "params": {
      "query": "refund A-1042",
      "limit": 10
    }
  }
]

Sandbox、harness、工具名策略、Schema 各管一段

名字都叫「安全」。物件不同。混在一處就會漏出站或誤攔會話工具:

邊界 看什麼 JSON / 行為 典型失敗
Harness allow/ask/deny 會話裡 Bash、Read、MCP 客戶端是否彈出 以為攔了 Shell 就等於攔了 orders MCP 出站
OpenShell runtime sandbox 沙箱內 binary、Landlock、代理看到的出站 body 在 IDE 裡點了 Allow,但 runner 仍能直連未登記 host
OpenShell 工具名策略 MCP tools/call 的 params.name 工具名 allow/deny 見 OpenShell 與 Sentry 專文——本篇不展開 deny_rules
宿主 Schema tool_use.input 與 arguments 工具名放行但 order_id 亂填仍進庫

許可權檔案講會話,見 Coding Agent 的許可權模型。引數門講 Schema,見 AI 為什麼需要 JSON Schema。工具名與 Sentry 對照見 OpenShell 工具名與 Sentry。

五步把 Agent 放進可審計 sandbox

用虛構訂單 A-1042 在 staging 跑通再放寬 network policy:

  1. 分開存三份 JSON。 messages 裡的 tool_use、宿主 state、openshell policy / sandbox status——不要合成一個 blob。
  2. 建立 sandbox 時寫 filesystem + Landlock。 需要新目錄許可權就重建,不要指望熱載入。
  3. 登記 TOFU binary 與 Tool server。 每個 endpoint 寫清 protocol、host、port、path、enforcement: enforce。
  4. MCP 與 legacy 分端點。 不要在同一 host:port 混 protocol: mcp 與 json-rpc method 表。
  5. 批請求按最壞一條驗收。 lookup 與 reports.search 不要綁在同一 batch 試探 deny。

五步都落在可 Diff 的 JSON 上,才分得清是 harness、代理還是 Schema 擋了呼叫。

出站被拒時攤開四份物件

先看 sandbox status 裡的 last_egress_decision,再對 policy 與真實出站 body。

  • tool_use、status、出站 格式化;arguments 用 Schema 校驗 驗 A-1042。
  • 用 JSONPath 提取 抽 $.params.name、$.method、$.connections[*].protocol。
  • policy 熱載入前後 JSON Diff;多看 connections 與 deny_rules,少改 prompt。
  • openshell logs --tail --source sandbox 看重拒的 binary、host、path。
  • 日誌與 status 裡的 token 先 脫敏。JSONSort 在瀏覽器本地跑。

四份物件對齊,才知道是 binary 未登記、protocol 選錯,還是 legacy method 未 allow。

常見誤區

  • 把 allow/ask/deny 當成 sandbox。 那是 harness 會話門。出站 MCP 仍要靠 OpenShell runtime。
  • 只審計 model tool_use,不看沙箱 egress body。 執行路徑可能在 runner 裡改寫或重發 JSON-RPC。
  • 以為 network_policies 熱載入能改 Landlock。 檔案策略建立後釘死,要改盤就重建 sandbox。
  • MCP 與 legacy json-rpc 共用規則表。 2026 L7 分支按 protocol 解析;混端點會 silent mismatch。
  • 忽略 TOFU binary。 換 interpreter 路徑卻不更新 allowlist,出站會在代理層被拒。
  • 用本篇替代 OpenShell 與 Sentry 專文做 Sentry 深讀。 硬體帶外隔離不是 runtime JSON 能替代的。

FAQ

沒有 OpenShell,只靠 harness 許可權夠嗎?

不夠。許可權檔案管 IDE 裡是否執行某類工具;sandbox 管程序能否讀哪些路徑、哪個 binary 能否向哪個 host 發出 JSON-RPC。兩層都要。

sandbox 會校驗 tools/call 裡的 order_id 嗎?

預設不會。L7 MCP 策略匹配方法和工具名。引數形狀要在宿主用 Schema 驗,見 OpenShell 文件「tool-argument matching not supported yet」。

legacy json-rpc 和 MCP 有什麼區別?

endpoint 上 protocol 不同:MCP 走 tools/call + params.name;legacy 走頂層 method 如 reports.search。規則表分開寫。

一批 JSON-RPC 裡一條被拒會怎樣?

與 MCP 行為一致:一條 deny,整批 deny。不要把 lookup 與未授權的 search 綁在同一 batch。

這和 Docker 跑 Agent 是一回事嗎?

容器是包裝。OpenShell 額外綁 Landlock、按 binary 的 egress L7 策略、以及 openshell-sandbox 生命週期與 status。容器 alone 不會自動解析 MCP tools/call。

結論

AI Agent 必須跑在 sandbox 裡,因為 tool JSON 只描述意圖,不描述核心、磁碟與出站。OpenShell 用 openshell-sandbox、Landlock、TOFU binary 與 L7 MCP/json-rpc 代理把執行關進可 Diff 的邊界。

先對齊 tool_use、sandbox status、出站 tools/call 與 policy,再談放寬規則。會話許可權與工具名策略、Schema 各讀專文,不要混成一張 allow 列表。

延伸閱讀

更新記錄: 首次發布