Tool Sandbox
Tool Sandbox 是为高风险工具(代码执行、文件操作、Shell 命令)提供隔离执行环境的机制,防止工具执行影响宿主系统。
#type / concept
#status / evergreen
#tech / ai
#tech / security
[!info] related notes
- 所属 MOC: Tool Calling Engineering MOC
- 相关: [[sandbox|Sandbox]], Tool Execution
Tool Sandbox
一句话定义
Tool Sandbox 是为高风险工具提供隔离执行环境的机制。执行用户提供的代码、读写文件、运行 Shell 命令时,这些操作应该在沙箱中执行。
核心原理
需要沙箱的工具
| 工具 | 风险 | 沙箱需求 |
|---|---|---|
| execute_code | 代码注入 | 必须 |
| read_file | 路径遍历 | 推荐 |
| write_file | 文件覆盖 | 必须 |
| run_shell | 命令注入 | 必须 |
| call_api | 数据泄露 | 可选 |
沙箱配置
sandbox_config = {
"execute_code": {
"type": "docker",
"image": "python:3.11-slim",
"mem_limit": "256m",
"cpu_quota": 50000,
"network_disabled": True,
"read_only": True,
"timeout": 30,
},
"read_file": {
"type": "chroot",
"allowed_paths": ["/data/documents/"],
"max_file_size": "10MB",
},
}
常见坑
- 不做沙箱: 代码执行直接在宿主系统
- 沙箱配置太松: 网络没隔离、资源没限制
- 沙箱启动太慢: 每次都创建新容器