Skip to content

安全模型(以官方 Security 为准)

先认清边界

官方明确声明:OpenClaw 默认是"个人助手/单信任边界"模型, 不是"多对抗租户共享网关"的隔离边界。

参考:

必做安全检查

bash
# 基础安全审计
openclaw security audit

# 深度安全审计
openclaw security audit --deep

# 自动修复
openclaw security audit --fix

安全检查清单

markdown
# OpenClaw 安全检查清单

## 网关安全

- [ ] 网关绑定地址为 loopback(127.0.0.1)
- [ ] 已启用认证(token 或 password)
- [ ] 未暴露到公网(除非有额外保护)
- [ ] TLS 已正确配置(如需远程访问)

## 工具权限

- [ ] 使用最小权限 profile
- [ ] 高风险工具需要审批
- [ ] 禁用了不需要的工具
- [ ] 文件系统访问受限

## 密钥管理

- [ ] 敏感信息存储在 secrets 中
- [ ] 配置文件中无明文密钥
- [ ] 密钥有轮换计划
- [ ] 密钥访问有审计日志

## 远程访问

- [ ] 使用 VPN 或 Tailscale
- [ ] SSH 隧道配置正确
- [ ] 远程访问有额外认证层
- [ ] 访问日志已启用

## 审计与监控

- [ ] 操作日志已启用
- [ ] 异常告警已配置
- [ ] 日志保留期限合理
- [ ] 日志存储安全

最小安全基线

1. 网关入口

  • 默认使用 loopback 绑定。
  • 开启 gateway.auth(token/password)。
  • 避免未鉴权直接暴露公网入口。

网关配置示例

yaml
# ~/.openclaw/config.yaml
gateway:
  # 绑定地址(默认 loopback)
  host: 127.0.0.1
  port: 3000

  # 认证配置
  auth:
    enabled: true
    type: token # token | password
    token: ${OPENCLAW_GATEWAY_TOKEN} # 从环境变量读取

  # TLS 配置(远程访问时必需)
  tls:
    enabled: false # 本地开发可禁用
    cert: /path/to/cert.pem
    key: /path/to/key.pem

  # 速率限制
  rate_limit:
    enabled: true
    requests_per_minute: 60
    burst: 10

  # 日志配置
  logging:
    enabled: true
    level: info
    format: json
    output: /var/log/openclaw/gateway.log

2. 工具权限

  • 从最小 profile 起步。
  • 高风险工具(exec/browser/文件写)必须审批。
  • 使用 tools.deny 明确禁用不需要能力。

工具权限配置

yaml
# ~/.openclaw/config.yaml
tools:
  # 使用预定义 profile
  profile: safe # safe | standard | full

  # 安全 profile 定义
  profiles:
    safe:
      allow:
        - read:*
        - search:*
        - web:fetch
      deny:
        - exec:*
        - write:*
        - browser:*

    standard:
      allow:
        - read:*
        - search:*
        - web:*
        - write:temp
      deny:
        - exec:elevated
      require_approval:
        - write:sensitive

    full:
      allow:
        - '*'
      require_approval:
        - exec:elevated
        - write:sensitive

  # 自定义规则
  rules:
    # 允许读取特定目录
    - pattern: 'read:/home/user/docs/**'
      allow: true

    # 禁止读取敏感目录
    - pattern: 'read:**/.ssh/**'
      allow: false

    # 高风险操作需要审批
    - pattern: 'exec:elevated'
      approval:
        enabled: true
        timeout: 300 # 秒
        mode: ask # off | on-miss | always

3. 远程访问

  • 优先 Tailscale/VPN。
  • 次选 SSH 隧道。
  • 即便走隧道仍需鉴权。

远程访问方案

方案 1:Tailscale(推荐)

bash
# 安装 Tailscale
curl -fsSL https://tailscale.com/install.sh | sh

# 启动并连接
tailscale up

# 获取 Tailscale IP
tailscale ip

# 配置 OpenClaw 使用 Tailscale IP
openclaw config set gateway.host 100.x.y.z

方案 2:SSH 隧道

bash
# 创建 SSH 隧道
ssh -L 3000:localhost:3000 user@remote-host -N -f

# 本地访问
openclaw --api-url http://localhost:3000

# 持久化隧道(使用 autossh)
autossh -M 0 -f -N \
  -L 3000:localhost:3000 \
  -o ServerAliveInterval=30 \
  -o ServerAliveCountMax=3 \
  user@remote-host

方案 3:VPN

yaml
# 使用 VPN 时的配置
gateway:
  host: 10.8.0.2 # VPN 内网 IP
  auth:
    enabled: true
    type: token
  tls:
    enabled: true # VPN 内也建议启用 TLS

4. Secrets 管理

  • 密钥放入 secrets 体系,不把敏感值硬编码进配置。
  • 变更后执行 reload/校验流程。

Secrets 配置

yaml
# ~/.openclaw/secrets.yaml(不提交到版本控制)
secrets:
  # API 密钥
  openai_api_key: sk-xxx
  anthropic_api_key: sk-ant-xxx

  # 渠道令牌
  discord_bot_token: xxx
  telegram_bot_token: xxx

  # 数据库连接
  database_url: postgresql://user:pass@host/db

  # 加密密钥
  encryption_key: xxx

# 在配置中引用 secrets
# ~/.openclaw/config.yaml
providers:
  openai:
    api_key: ${secrets.openai_api_key}

channels:
  discord:
    bot_token: ${secrets.discord_bot_token}

Secrets 轮换

bash
# 生成新密钥
openclaw secrets rotate openai_api_key

# 从环境变量导入
openclaw secrets import --env

# 导出到安全存储
openclaw secrets export --vault

# 验证密钥有效性
openclaw secrets validate

组织层策略(开发 + 产品)

  • 开发者负责技术控制(鉴权、策略、日志、告警)。
  • 产品经理负责业务风险分级与审批门槛。
  • 双方共同维护"高风险动作清单"。

高风险动作清单

yaml
# high-risk-actions.yaml
high_risk_actions:
  - action: exec:elevated
    risk_level: critical
    description: 执行提权命令
    approval_required: true
    approvers: [admin, security]
    audit_level: verbose

  - action: write:sensitive
    risk_level: high
    description: 写入敏感文件
    approval_required: true
    approvers: [admin]
    audit_level: verbose

  - action: browser:auth
    risk_level: high
    description: 浏览器认证操作
    approval_required: true
    approvers: [admin]
    audit_level: standard

  - action: network:external
    risk_level: medium
    description: 访问外部网络
    approval_required: false
    audit_level: standard

  - action: file:delete
    risk_level: medium
    description: 删除文件
    approval_required: false
    audit_level: verbose

角色权限矩阵

yaml
# role-permissions.yaml
roles:
  admin:
    description: 管理员
    permissions:
      - '*'
    approval_required_for: []

  developer:
    description: 开发者
    permissions:
      - read:*
      - write:temp
      - exec:standard
      - web:*
    approval_required_for:
      - exec:elevated
      - write:sensitive

  user:
    description: 普通用户
    permissions:
      - read:*
      - web:fetch
    approval_required_for:
      - write:*
      - exec:*

  readonly:
    description: 只读用户
    permissions:
      - read:*
    approval_required_for:
      - '*'

常见误区

  • sessionKey 误认为权限令牌。
  • 让互不信任用户共享同一高权限 agent。
  • 只做接入,不做审计与回放。

误区详解

误区 1:sessionKey = 权限令牌

yaml
# ❌ 错误理解
# sessionKey 只是会话标识,不代表权限

# ✅ 正确做法
# 权限应该通过 tools.profile 和 tools.allow/deny 控制
tools:
  profile: safe
  rules:
    - pattern: 'exec:*'
      allow: false

误区 2:共享高权限 Agent

txt
# ❌ 危险:多个用户共享高权限 agent
agents:
  shared-admin:
    tools:
      profile: full  # 所有用户都有完全权限

# ✅ 正确做法:按角色拆分
agents:
  admin-agent:
    tools:
      profile: full
    allowed_users: [admin]

  dev-agent:
    tools:
      profile: standard
    allowed_users: [developers]

  user-agent:
    tools:
      profile: safe
    allowed_users: [users]

误区 3:只做接入不做审计

txt
# ❌ 缺失:没有审计配置
gateway:
  auth:
    enabled: true

# ✅ 正确做法:启用审计
gateway:
  auth:
    enabled: true

audit:
  enabled: true
  log_all_operations: true
  log_sensitive_data: false  # 不记录敏感数据
  retention_days: 90

alerts:
  - name: high_risk_operation
    condition: "action in ['exec:elevated', 'write:sensitive']"
    notify: [admin, security]

  - name: unusual_activity
    condition: "operation_count > 100 in 1h"
    notify: [admin]