安全模型
Moltbot 的安全策略和权限控制
概述
Moltbot 连接到真实的消息渠道,因此必须将入站 DM 视为不受信任的输入。安全模型提供多层保护,包括 DM 配对、允许列表、工具权限、沙箱隔离等。
安全层次
- DM 策略: 控制对未认证 DM 的访问
- 允许列表: 白名单机制
- 设备配对: 防止设备仿冒
- 工具权限: 细粒度的工具访问控制
- 沙箱隔离: 非主会话在 Docker 中运行
- 执行批准: 敏感命令需要用户批准
DM 策略
策略类型
类型: DmPolicy = "open" | "pairing" | "closed"
export type DmPolicy = "open" | "pairing" | "closed";
策略说明
| 策略 | 行为 | 安全等级 | 推荐场景 |
|---|---|---|---|
open | 所有 DM 都接受 | 🔴 低 | 仅在允许列表中配置 "*" 时使用 |
pairing | 未知发送者需要配对码 | 🟡 中 | 推荐用于生产环境 |
closed | 拒绝所有 DM | 🟢 高 | 完全禁用 DM |
配置示例
{
channels: {
whatsapp: {
dmPolicy: "pairing"
},
telegram: {
dmPolicy: "pairing",
allowFrom: ["+1234567890"] // 允许列表覆盖
},
discord: {
dmPolicy: "closed" // 完全禁用
}
}
}
配对流程
允许列表匹配
Allowlist 匹配类型
文件: src/channels/allowlist-match.ts
export type AllowlistMatchSource =
| "wildcard"
| "id"
| "name"
| "tag"
| "username"
| "prefixed-id"
| "prefixed-user"
| "prefixed-name"
| "prefixed-tag";
匹配逻辑
export function matchAllowlist(params: {
cfg: MoltbotConfig;
channel: string;
accountId?: string | null;
targetId: string;
targetName?: string | null;
targetTag?: string | null;
targetUsername?: string | null;
}): AllowlistMatch
匹配优先级
- 通配符:
*匹配所有 - 裸 ID:
1234567890匹配特定用户 - 前缀 ID:
telegram:123456789匹配特定渠道用户 - 名称:
username匹配显示名称 - 前缀名称:
telegram:username匹配特定渠道名称 - 标签:
@tag匹配标签 - 前缀标签:
telegram:@tag匹配特定渠道标签 - 用户名:
@username匹配用户名
配置示例
{
channels: {
telegram: {
// 多种匹配方式
allowFrom: [
"*", // 所有用户
"123456789", // 特定用户 ID
"telegram:987654321", // Telegram 用户
"alice", // 显示名称
"telegram:@family", // Telegram 家族标签
"alice@telegram.com" // 邮箱 (某些渠道)
]
}
}
}
设备配对
配对机制
Gateway 支持设备签名和配对,防止设备仿冒攻击。
配对流程
配对管理
WebSocket 方法:
| 方法 | 说明 |
|---|---|
device.pair.list | 列出待批准的设备 |
device.pair.approve | 批准设备配对 |
device.pair.reject | 拒绝设备配对 |
device.token.rotate | 旋转设备令牌 |
device.token.revoke | 撤销设备令牌 |
工具权限
工具策略
文件: src/agents/tool-policy.ts
export type ToolPolicy = {
allow?: string[]; // 允许的 glob 模式
deny?: string[]; // 拒绝的 glob 模式
};
策略层次
- Profile 策略:
tools.profile - Provider Profile 策略:
tools.byProvider[{provider}].profile - Agent 策略:
agents[{id}].tools.allow - Agent Provider 策略:
agents[{id}].tools.byProvider[{provider}].allow - Group 策略: channel/group 特定
- Sandbox 策略:
tools.sandbox.tools.allow - Subagent 策略: subagent 专用
配置示例
{
agents: {
defaults: {
// 全局工具策略
tools: {
profile: {
allow: ["*"],
deny: ["system.run", "system.notify"]
}
}
},
list: {
work: {
// Agent 特定策略
tools: {
allow: ["bash", "read", "write", "edit", "browser"],
deny: ["process", "sessions_*"]
}
}
}
}
}
Glob 模式
// 匹配所有 bash 工具
"bash*"
// 匹配 sessions 开头的工具
"sessions_*"
// 匹配 system.run 和 system.notify
"system.{run,notify}"
// 匹配除了 session 开头的所有工具
"!sessions_*"
沙箱隔离
Sandbox 模式
文件: src/agents/sandbox/types.ts
export type SandboxMode = "off" | "all" | "non-main";
模式说明
| 模式 | 行为 | 适用场景 |
|---|---|---|
off | 不启用沙箱 | 开发环境,完全信任 |
all | 所有会话都启用沙箱 | 最大安全隔离 |
non-main | 仅非主会话启用沙箱 | 推荐:主会话有完全访问,其他会话受限 |
配置示例
{
agents: {
defaults: {
sandbox: {
mode: "non-main",
workspaceAccess: "ro", // 只读访问宿主工作区
pruneMinutes: 60 // 60 分钟后清理
},
tools: {
sandbox: {
tools: {
allow: ["bash", "process", "read", "write", "edit", "sessions_list", "sessions_history", "sessions_send"],
deny: ["browser", "canvas", "nodes", "cron", "discord", "gateway"]
}
}
}
}
}
}
工作空间访问
export type SandboxWorkspaceAccess = "none" | "ro" | "rw";
| 模式 | 挂载点 | 说明 |
|---|---|---|
none | /workspace | 完全隔离,无宿主访问 |
ro | /agent:ro + /workspace | 只读宿主工作区 |
rw | /agent:rw + /workspace | 读写宿主工作区 |
执行批准
批准机制
敏感命令(如 system.run)需要用户批准后执行。
批准策略
{
agents: {
defaults: {
tools: {
exec: {
approvalMode: "always", // "always" | "per-session" | "once"
notify: true,
timeoutMs: 300000 // 5 分钟超时
}
}
}
}
}
群组消息安全
Mention Gating
默认情况下,群组消息需要 @mention bot 才处理。
{
channels: {
discord: {
groups: {
"*": {
requireMention: true,
mentionPattern: "@bot"
}
}
}
}
}
群组工具策略
群组中的工具访问可以单独配置。
{
agents: {
defaults: {
tools: {
groups: {
"*": {
allow: ["read", "write", "edit"],
deny: ["bash", "process", "browser", "canvas"]
}
}
}
}
}
}
安全检查命令
Doctor 命令
moltbot doctor
Doctor 会检查:
- ❌ 风险的 DM 策略(
dmPolicy: "open"而 allowList 包含"*") - ❌ 未配置的通道
- ❌ 配置问题
- ✅ 正常的安全配置
最佳实践
1. 使用 pairing 模式
{
channels: {
whatsapp: {
dmPolicy: "pairing" // ✅ 推荐
}
}
}
2. 细粒度的工具控制
{
agents: {
defaults: {
sandbox: {
mode: "non-main" // ✅ 主会话全访问,其他受限
},
tools: {
sandbox: {
tools: {
deny: ["browser", "canvas", "nodes"] // ✅ 危险工具默认拒绝
}
}
}
}
}
}
3. 使用执行批准
{
agents: {
defaults: {
tools: {
exec: {
approvalMode: "always" // ✅ 敏感命令需批准
}
}
}
}
}
4. 运行 Doctor
moltbot doctor # ✅ 定期检查配置
代码路径引用
| 功能 | 文件路径 |
|---|---|
| Allowlist 匹配 | src/channels/allowlist-match.ts |
| DM 策略 | src/channels/channel-config.ts |
| 设备配对 | src/gateway/server/device-pairing.ts |
| 工具策略 | src/agents/tool-policy.ts |
| Sandbox | src/agents/sandbox/ |
| 执行批准 | src/infra/exec-approval-forwarder.ts |
| Doctor | src/commands/doctor.ts |