首页 资讯 下载 教程 Skills 社群

OpenClaw Sandbox 沙箱配置:安全执行 Agent 代码和命令

为什么需要 Sandbox

AI Agent 的风险

AI Agent 具备执行代码和命令的能力后:

  • 误操作可能删除重要文件
  • 恶意 Prompt 注入可能导致危险命令
  • Bug 可能导致无限循环或资源耗尽
  • 跨项目污染(A 项目的 Agent 意外修改 B 项目)

Sandbox 的价值

在沙箱中执行:

  • 隔离文件系统:只能访问指定目录
  • 网络限制:只能访问白名单域名
  • 资源限制:CPU、内存、执行时间
  • 可回滚:沙箱可随时重置

Sandbox 方案对比

方案 隔离度 性能 复杂度
Docker 良好
VM 最高 一般
限权进程 最好
WebAssembly

方式 1:Docker Sandbox

基础配置

# config.yaml
sandbox:
  type: "docker"
  defaultImage: "openclaw/sandbox:latest"
  mounts:
    - source: "./workspace"
      target: "/workspace"
      readonly: false
  limits:
    cpu: "1"
    memory: "2g"
    timeout: "5m"

使用沙箱执行

Agent 调用 exec 工具时自动在沙箱内运行:

agents:
  coder:
    provider: "anthropic"
    model: "claude-opus-4-7"
    tools:
      - exec:
          sandbox: true

自定义 Docker 镜像

# Dockerfile.sandbox
FROM python:3.11-slim

# 安装常用工具
RUN apt-get update && apt-get install -y \
    git curl wget vim \
    && rm -rf /var/lib/apt/lists/*

# 创建工作目录
WORKDIR /workspace

# 安装 Python 常用包
RUN pip install numpy pandas requests

CMD ["/bin/bash"]

构建和使用:

docker build -f Dockerfile.sandbox -t my-sandbox .
sandbox:
  defaultImage: "my-sandbox:latest"

方式 2:VM Sandbox

适用场景

需要最高隔离度时:

  • 金融交易相关
  • 涉及敏感数据
  • 未知来源代码

基础配置

sandbox:
  type: "vm"
  provider: "firecracker"  # 或 qemu
  template: "ubuntu-22.04"
  snapshot:
    enabled: true  # 每次重置到快照

Firecracker MicroVM

Firecracker 是 AWS 的轻量 VM:

  • 启动 < 125ms
  • 内存 < 5MB 开销
  • 完全隔离
sandbox:
  type: "vm"
  provider: "firecracker"
  kernel: "/opt/sandbox/vmlinux"
  rootfs: "/opt/sandbox/rootfs.ext4"
  cpuCount: 1
  memory: "512M"

方式 3:限权进程

使用 firejail

# 安装 firejail(Linux)
apt install firejail
sandbox:
  type: "firejail"
  profile: "default"
  restrictions:
    - "net none"      # 无网络
    - "private-tmp"   # 私有 tmp
    - "noroot"         # 非 root

使用 bubblewrap

sandbox:
  type: "bubblewrap"
  profile:
    bindDirs:
      - source: "./workspace"
        target: "/workspace"
        readonly: false
    unshare:
      - "net"
      - "pid"
      - "ipc"

文件系统隔离

只读挂载

sandbox:
  mounts:
    - source: "./project"
      target: "/workspace/project"
      readonly: true    # Agent 只能读不能改
    - source: "./tmp"
      target: "/tmp"
      readonly: false   # 临时文件可写

写入重定向

sandbox:
  writeRedirect:
    enabled: true
    targetDir: "./sandbox-writes"
    # Agent 写的所有内容都在这里,不影响主目录

网络隔离

禁用网络

sandbox:
  network:
    mode: "none"  # 完全无网络

白名单

sandbox:
  network:
    mode: "whitelist"
    allowedHosts:
      - "api.anthropic.com"
      - "api.openai.com"
      - "registry.npmjs.org"
    blockedHosts:
      - "*.internal"

代理模式

sandbox:
  network:
    mode: "proxy"
    proxy: "http://sandbox-proxy:8080"
    # 所有网络请求经过代理审计

资源限制

CPU 和内存

sandbox:
  limits:
    cpu: "2"          # 2 核
    memory: "4g"       # 4GB 内存
    memorySwap: "4g"   # 不允许使用交换空间
    pids: 100          # 最多 100 个进程

执行超时

sandbox:
  limits:
    timeout: "10m"     # 10 分钟执行超时
    totalTimeout: "1h" # 整体 1 小时限制

磁盘 I/O

sandbox:
  limits:
    diskIO:
      read: "100m/s"
      write: "50m/s"

会话级别 Sandbox

每个会话独立 sandbox:

sandbox:
  perSession: true
  sessionTimeout: "1h"
  autoCleanup: true

会话结束后自动清理沙箱,避免状态污染。

敏感操作保护

操作审批

sandbox:
  approval:
    required:
      - "rm -rf"
      - "sudo"
      - "chmod 777"
      - "git push"
    approvalChannel: "feishu:ops"

这些操作即使在沙箱内也需要人工审批。

禁用危险操作

sandbox:
  forbidden:
    - "rm -rf /"
    - "mkfs"
    - "dd if=/dev/zero"
    - "fork bomb patterns"

实际应用场景

1. 代码审查沙箱

agents:
  reviewer:
    sandbox:
      type: "docker"
      mounts:
        - source: "./pr-to-review"
          target: "/workspace"
          readonly: true
      network:
        mode: "none"  # 审查不需要网络

2. 开发环境沙箱

agents:
  developer:
    sandbox:
      type: "docker"
      image: "my-dev-env:latest"
      mounts:
        - source: "./project"
          target: "/workspace"
          readonly: false
      network:
        mode: "whitelist"
        allowedHosts: ["registry.npmjs.org", "github.com"]

3. 生产运维沙箱

agents:
  ops:
    sandbox:
      type: "vm"
      network:
        mode: "proxy"
      approval:
        # 所有生产操作都要审批
        required: ["*"]

沙箱逃逸检测

行为监控

sandbox:
  monitoring:
    detectEscape: true
    suspiciousPatterns:
      - "/proc/*/root"
      - "nsenter"
      - "docker.sock"

检测到可疑行为立即报警。

审计日志

sandbox:
  audit:
    enabled: true
    logAllCommands: true
    logFile: "/var/log/openclaw-sandbox.log"

性能优化

缓存镜像

sandbox:
  cache:
    imageReuse: true  # 复用镜像
    layerCache: true  # 层缓存

预热

sandbox:
  preWarm:
    count: 3  # 预启动 3 个沙箱实例

减少冷启动延迟。

故障排查

沙箱无法启动

openclaw doctor sandbox

常见原因:

  • Docker 未运行
  • 镜像下载失败
  • 权限不足

Agent 抱怨权限

可能需要:

  • 增加挂载目录
  • 放宽网络白名单
  • 调整资源限制

性能下降

  • 升级沙箱方案(VM → Docker → 进程)
  • 减少挂载目录
  • 复用沙箱实例

最佳实践

  1. 按 Agent 分级:危险 Agent 用更强沙箱
  2. 定期更新镜像:保持安全补丁
  3. 监控日志:定期检查异常行为
  4. 审计审批:关键操作必须审批
  5. 测试逃逸:定期做红队测试
  6. 文档清晰:沙箱规则明确告知使用者

注意事项

  • Docker 沙箱不是完全隔离,适合非恶意但可能出错的场景
  • VM 沙箱隔离最强但性能损耗大,按需使用
  • 进程限权方案适合快速场景但隔离度较低
  • 沙箱内 Agent 性能会有一定下降
  • 配合 Microsoft Agent Governance Toolkit 获得完整保障
  • 生产环境强烈建议使用沙箱
  • 开发环境可以根据信任度放宽