Tool Sandbox

Tool Sandbox 是为高风险工具(代码执行、文件操作、Shell 命令)提供隔离执行环境的机制,防止工具执行影响宿主系统。

#type / concept #status / evergreen #tech / ai #tech / security

[!info] related notes

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",
    },
}

常见坑

  1. 不做沙箱: 代码执行直接在宿主系统
  2. 沙箱配置太松: 网络没隔离、资源没限制
  3. 沙箱启动太慢: 每次都创建新容器

参考资料

创建于 2026/6/30 更新于 2026/7/15