OpenClaw多Agent安全隔离配置详解
OpenClaw多Agent安全隔离配置详解
前言
在入门篇中,我们了解了OpenClaw多Agent的基本概念和常见配置场景。本文将深入讲解安全最佳实践、沙盒隔离和会话管理,帮助你在生产环境中安全地部署多Agent架构。
五、安全最佳实践
5.1 基础安全配置(60秒强化)
{
gateway: {
mode: "local",
bind: "loopback", // 仅本地绑定
auth: { mode: "token", token: "replace-with-long-random-token" },
},
session: {
dmScope: "per-channel-peer", // 多用户隔离
},
tools: {
profile: "messaging", // 仅消息工具
deny: [
"group:automation",
"group:runtime",
"group:fs",
"sessions_spawn",
"sessions_send",
],
fs: { workspaceOnly: true },
exec: { security: "deny", ask: "always" },
elevated: { enabled: false },
},
channels: {
whatsapp: {
dmPolicy: "pairing", // 配对模式(默认)
groups: { "*": { requireMention: true } }, // 群组需@提及
},
},
}
5.2 文件权限
# 配置文件权限
chmod 600 ~/.openclaw/openclaw.json # 仅用户可读写
chmod 700 ~/.openclaw # 仅用户可访问
5.3 DM访问控制
多用户场景必须设置:
{
session: {
dmScope: "per-channel-peer", // 每个用户独立会话
},
channels: {
whatsapp: {
dmPolicy: "pairing", // 配对模式(默认)
groups: { "*": { requireMention: true } }, // 群组需@提及
},
},
}
5.4 Agent间通信安全
默认禁用,需显式开启:
tools: {
agentToAgent: {
enabled: false, // 默认关闭
allow: ["home", "work"], // 需要显式允许列表
},
}
六、每个Agent的Sandbox和工具配置(v2026.1.6+)
6.1 基础示例
{
agents: {
list: [
{
id: "personal",
workspace: "~/.openclaw/workspace-personal",
sandbox: {
mode: "off", // 个人Agent不使用沙盒
},
// 无工具限制 - 全部可用
},
{
id: "family",
workspace: "~/.openclaw/workspace-family",
sandbox: {
mode: "all", // 始终沙盒
scope: "agent", // 每个Agent一个容器
},
tools: {
allow: ["read"], // 仅允许读取
deny: [
"exec",
"write",
"edit",
"apply_patch",
],
},
},
],
},
}
6.2 高级配置(Docker初始化)
{
agents: {
list: [
{
id: "sandboxed",
workspace: "~/.openclaw/workspace-sandboxed",
sandbox: {
mode: "all",
scope: "agent",
docker: {
// 容器创建后一次性运行(可选)
setupCommand: "apt-get update && apt-get install -y git curl",
},
},
tools: {
allow: [
"read",
"exec",
"sessions_list",
],
deny: [
"write",
"edit",
"browser",
"canvas",
],
},
},
],
},
}
注意事项:
setupCommand在sandbox.docker下,容器创建时运行一次- 当scope为
"shared"时,忽略per-agent sandbox.docker.*覆盖
6.3 权限隔离的好处
- 安全隔离:限制不可信Agent的工具
- 资源控制:沙盒特定Agent,其他Agent在主机运行
- 灵活策略:每个Agent独立的权限配置
七、会话管理
7.1 DM会话隔离(推荐)
安全警告:如果Agent可以接收多用户DM,强烈建议启用安全DM模式。
{
session: {
dmScope: "per-channel-peer", // 每个渠道+发送者隔离
},
}
为什么需要:
- 默认
dmScope: "main":所有DM共享主会话,可能导致上下文泄露 - 问题示例:Alice聊医疗预约,Bob问"我们在聊什么?",模型可能用Alice的上下文回答Bob
7.2 会话维护
{
session: {
maintenance: {
mode: "enforce", // 强制执行
pruneAfter: "45d", // 45天后修剪
maxEntries: 800, // 最多800个会话
rotateBytes: "20mb", // 达到20MB时轮转
resetArchiveRetention: "14d", // 重置归档保留14天
},
},
}
手动清理:
openclaw sessions cleanup --dry-run # 预览影响
openclaw sessions cleanup --enforce # 强制执行
7.3 磁盘预算
{
session: {
maintenance: {
mode: "enforce",
maxDiskBytes: "1gb",
highWaterBytes: "800mb", // 达到80%时清理
},
},
}
下一篇预告:生产环境部署与故障排查(实战篇)