在前面的文章中,我们已经学习了 OpenClaw 的安装配置、基础使用、工作流编排、多实例并行以及迁移到 Hermes 的完整流程。但有一个至关重要的话题我们还没有深入探讨:**安全与权限**。

OpenClaw 安全与权限 —— 命令审批与隔离、安全策略、权限配置

简介

在前面的文章中,我们已经学习了 OpenClaw 的安装配置、基础使用、工作流编排、多实例并行以及迁移到 Hermes 的完整流程。但有一个至关重要的话题我们还没有深入探讨:安全与权限

当 AI Agent 能够执行 Shell 命令、读写文件、访问网络时,安全问题就不再是「可选项」,而是「必选项」。一个配置不当的 AI Agent 可能会:

  • 🚨 误删重要文件
  • 🚨 泄露敏感数据
  • 🚨 执行恶意命令
  • 🚨 耗尽系统资源
  • 🚨 触发合规违规

本篇将全面深入 OpenClaw 的安全体系,涵盖从基础的命令审批到高级的权限管理,从个人开发者的安全实践到企业级的安全策略。

安全无小事,让我们开始吧。

目录

一、安全威胁模型

1.1 AI Agent 安全威胁

text
威胁模型分类:

┌──────────────────────────────────────────────────────┐
│                  安全威胁模型                          │
│                                                      │
│  1. 提示词注入 (Prompt Injection)                     │
│     ├── 直接注入:用户输入包含恶意指令                  │
│     ├── 间接注入:外部数据包含恶意指令                  │
│     └── 上下文注入:历史对话包含恶意内容                │
│                                                      │
│  2. 命令执行风险 (Command Execution)                   │
│     ├── 越权命令:执行未授权的系统命令                  │
│     ├── 危险操作:rm -rf、chmod 777 等               │
│     └── 资源耗尽:fork bomb、死循环                   │
│                                                      │
│  3. 数据泄露 (Data Leakage)                           │
│     ├── 文件泄露:读取敏感文件                         │
│     ├── 环境变量泄露:API Key、密码                   │
│     └── 网络泄露:将数据发送到外部                     │
│                                                      │
│  4. 权限提升 (Privilege Escalation)                    │
│     ├── 沙箱逃逸:突破隔离限制                         │
│     ├── 水平越权:访问其他用户数据                     │
│     └── 垂直越权:获取管理员权限                       │
└──────────────────────────────────────────────────────┘

1.2 安全层级

text
安全层级(从外到内):

┌─────────────────────────────────────┐
│  Layer 5: 审计与监控                  │ ← 事后追溯
│  ┌─────────────────────────────────┐│
│  │  Layer 4: 权限控制 (RBAC)        ││ ← 谁能做什么
│  │  ┌─────────────────────────────┐││
│  │  │  Layer 3: 安全策略           │││ ← 能做什么
│  │  │  ┌─────────────────────────┐│││
│  │  │  │  Layer 2: 命令审批       ││││ ← 批准执行
│  │  │  │  ┌─────────────────────┐││││
│  │  │  │  │  Layer 1: 沙箱隔离  │││││ ← 隔离环境
│  │  │  │  └─────────────────────┘││││
│  │  │  └─────────────────────────┘│││
│  │  └─────────────────────────────┘││
│  └─────────────────────────────────┘│
└─────────────────────────────────────┘

二、命令审批与隔离

2.1 Shell 命令白名单

最基本的安全措施是限制 AI 可以执行的命令:

yaml
# ~/.config/openclaw/shell-policy.yaml
shell:
  enabled: true
  mode: whitelist  # 白名单模式(推荐)

  # 允许的命令
  whitelist:
    # 文件操作
    - ls
    - cat
    - head
    - tail
    - wc
    - find
    - grep
    - diff

    # 开发工具
    - git
    - python
    - node
    - npm
    - pip
    - make
    - cargo

    # 系统信息
    - uname
    - date
    - whoami
    - pwd

    # 网络工具(受限)
    - curl
    - wget

  # 禁止的命令(即使不在白名单中也不会执行)
  blacklist:
    - rm
    - chmod
    - chown
    - chgrp
    - mkfs
    - fdisk
    - mount
    - umount
    - shutdown
    - reboot
    - kill
    - killall
    - sudo
    - su
    - curl.*-o.*\|.*sh   # 禁止管道到 shell
    - wget.*-O-.*\|.*sh

  # 带参数的命令限制
  restricted_commands:
    - command: curl
      max_args: 3
      forbidden_flags: ["-o", "-O", "|"]
      allowed_domains:
        - api.github.com
        - api.openai.com

    - command: find
      max_depth: 3
      forbidden_paths:
        - /etc/shadow
        - /etc/passwd
        - ~/.ssh
        - ~/.aws

2.2 命令审批流程

对于敏感操作,可以配置审批流程:

yaml
# ~/.config/openclaw/approval-policy.yaml
approval:
  # 自动审批的命令
  auto_approve:
    - ls
    - cat
    - head
    - tail
    - wc
    - pwd
    - date

  # 需要审批的命令
  require_approval:
    - pattern: "rm .*"
      reason: "删除文件操作"
      approvers: ["admin"]

    - pattern: "chmod .*"
      reason: "修改权限操作"
      approvers: ["admin"]

    - pattern: "git push .*"
      reason: "代码推送操作"
      approvers: ["admin", "lead"]

    - pattern: "deploy .*"
      reason: "部署操作"
      approvers: ["admin", "lead", "manager"]

  # 审批超时设置
  timeout: 300s  # 5 分钟超时,超时后自动拒绝

  # 审批通知
  notification:
    method: slack
    channel: "#approvals"
    include_context: true

2.3 交互式审批

bash
# 当 AI 尝试执行需要审批的命令时:

$ openclaw "清理 build 目录下的所有文件"

⚠️  需要审批的命令:rm -rf ./build/*
    原因:删除文件操作
    执行实例:worker-1
    请求时间:2024-01-15 14:30:00

📋 审批详情:
    命令:rm -rf ./build/*
    影响范围:./build/ 目录下的所有文件
    预估影响:删除约 250 个文件,释放 45MB 空间

[1] 批准执行
[2] 拒绝执行
[3] 修改命令后执行
[4] 查看完整上下文

选择 [1-4]: _

2.4 沙箱隔离

yaml
# ~/.config/openclaw/sandbox-config.yaml
sandbox:
  enabled: true

  # 沙箱类型
  type: chroot  # chroot | docker | firejail | nsjail

  # 根目录
  root: /tmp/openclaw-sandbox

  # 挂载点
  mounts:
    - source: /workspace
      target: /workspace
      readonly: false

    - source: /tmp/openclaw-cache
      target: /tmp/cache
      readonly: false

    - source: /etc/ssl/certs
      target: /etc/ssl/certs
      readonly: true

  # 网络限制
  network:
    enabled: true
    type: isolated
    allowed_ips:
      - 127.0.0.1
    allowed_domains:
      - api.openai.com
      - api.anthropic.com
      - github.com
      - pypi.org
      - npmjs.org
    blocked_ranges:
      - 10.0.0.0/8
      - 172.16.0.0/12
      - 192.168.0.0/16

  # 资源限制
  resources:
    max_memory: 512M
    max_cpu: "0.5"
    max_processes: 10
    max_file_descriptors: 64
    max_disk_write: 100M
    timeout: 300s

  # 清理策略
  cleanup:
    on_exit: true
    on_timeout: true
    preserve_logs: true

三、安全策略体系

3.1 输入验证策略

yaml
# ~/.config/openclaw/input-security.yaml
input_security:
  # 输入长度限制
  max_length: 10000

  # 输入内容过滤
  filters:
    # 移除潜在的注入指令
    strip_injection_patterns:
      - "忽略之前的指令"
      - "ignore previous instructions"
      - "system prompt"
      - "you are now"

    # 检测恶意模式
    detect_patterns:
      - type: sql_injection
        enabled: true

      - type: path_traversal
        enabled: true
        patterns:
          - "\.\./"
          - "%2e%2e/"

      - type: command_injection
        enabled: true
        patterns:
          - ";.*rm"
          - "\\|.*sh"
          - "`.*`"
          - "\\$\\("

      - type: xss
        enabled: true

  # 输入编码处理
  encoding:
    normalize_unicode: true
    strip_control_chars: true
    allowed_charsets:
      - utf-8

3.2 输出过滤策略

yaml
# ~/.config/openclaw/output-security.yaml
output_security:
  # 输出内容过滤
  filters:
    # 敏感信息脱敏
    redact_patterns:
      - type: api_key
        pattern: "sk-[a-zA-Z0-9]{20,}"
        replacement: "[API_KEY_REDACTED]"

      - type: password
        pattern: "password\\s*[:=]\\s*\\S+"
        replacement: "password: [REDACTED]"

      - type: email
        pattern: "\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\\b"
        replacement: "[EMAIL_REDACTED]"

      - type: phone
        pattern: "\\b\\d{3}[-.]?\\d{3}[-.]?\\d{4}\\b"
        replacement: "[PHONE_REDACTED]"

      - type: ip_address
        pattern: "\\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b"
        replacement: "[IP_REDACTED]"

      - type: credit_card
        pattern: "\\b\\d{4}[- ]?\\d{4}[- ]?\\d{4}[- ]?\\d{4}\\b"
        replacement: "[CC_REDACTED]"

  # 输出长度限制
  max_length: 50000

  # 输出编码
  encoding:
    escape_html: false
    normalize_unicode: true

3.3 提示词安全策略

yaml
# ~/.config/openclaw/prompt-security.yaml
prompt_security:
  # 系统提示词保护
  system_prompt:
    locked: true  # 防止被覆盖

    # 安全指令(追加到系统提示词)
    safety_instructions: |
      你是一个安全的 AI 助手。你必须遵守以下安全规则:

      1. 不要尝试执行破坏性命令(如 rm -rf)
      2. 不要读取敏感文件(如 /etc/shadow)
      3. 不要泄露 API 密钥或密码
      4. 不要尝试绕过安全限制
      5. 如果用户要求你执行不安全的操作,请拒绝并解释原因
      6. 不要访问内网地址或私有网络资源

  # 防止提示词注入
  injection_protection:
    enabled: true
    strategy: multi-layer

    # 第一层:模式匹配
    layer1_pattern_matching:
      enabled: true
      sensitivity: high

    # 第二层:语义分析
    layer2_semantic_analysis:
      enabled: true
      model: safety-classifier-v1

    # 第三层:行为监控
    layer3_behavior_monitoring:
      enabled: true
      alert_on_suspicious: true

四、权限配置与管理

4.1 基础权限模型

yaml
# ~/.config/openclaw/permissions.yaml
permissions:
  # 默认权限级别
  default_level: restricted

  # 权限级别定义
  levels:
    restricted:
      description: "受限模式 - 只读 + 安全命令"
      shell:
        enabled: true
        mode: whitelist
      file_access:
        read: ["./workspace/**"]
        write: ["./workspace/output/**"]
        execute: false
      network:
        outbound: ["api.openai.com", "api.anthropic.com"]

    developer:
      description: "开发者模式 - 完整开发工具链"
      shell:
        enabled: true
        mode: whitelist
        whitelist: ["ls", "cat", "grep", "find", "git", "python", "node", "npm", "pip"]
      file_access:
        read: ["./**"]
        write: ["./**"]
        execute: true
      network:
        outbound: ["*"]
        blocked: ["10.*", "172.16.*", "192.168.*"]

    admin:
      description: "管理员模式 - 完整权限"
      shell:
        enabled: true
        mode: blacklist
        blacklist: ["rm -rf /", "mkfs", "fdisk"]
      file_access:
        read: ["/**"]
        write: ["/**"]
        execute: true
      network:
        outbound: ["*"]

4.2 RBAC 角色管理

yaml
# ~/.config/openclaw/rbac.yaml
rbac:
  roles:
    viewer:
      description: "只读查看者"
      permissions:
        - "read:tasks"
        - "read:logs"
        - "read:status"
      deny:
        - "write:*"
        - "execute:*"
        - "delete:*"

    developer:
      description: "开发者"
      permissions:
        - "read:*"
        - "write:tasks"
        - "execute:tasks"
        - "write:configs"
        - "read:logs"
      deny:
        - "delete:tasks"
        - "write:rbac"
        - "execute:admin"

    lead:
      description: "技术负责人"
      permissions:
        - "read:*"
        - "write:*"
        - "execute:*"
        - "delete:tasks"
        - "approve:commands"
      deny:
        - "write:rbac"
        - "write:security"

    admin:
      description: "系统管理员"
      permissions:
        - "*:*"

  # 用户角色分配
  users:
    alice:
      roles: [developer]
      active: true

    bob:
      roles: [lead]
      active: true

    charlie:
      roles: [viewer]
      active: true

    dave:
      roles: [admin]
      active: true

4.3 细粒度权限控制

yaml
# ~/.config/openclaw/fine-grained-permissions.yaml
permissions:
  rules:
    # 基于路径的权限
    - name: "workspace-access"
      resource: "file"
      path: "/workspace/**"
      actions: ["read", "write"]
      conditions:
        time_range: "09:00-18:00"
        max_write_size: "10M"

    # 基于命令的权限
    - name: "git-operations"
      resource: "command"
      command: "git"
      actions: ["execute"]
      conditions:
        max_duration: "60s"
        allowed_subcommands: ["status", "log", "diff", "add", "commit", "push", "pull"]
        forbidden_flags: ["--force", "-f"]

    # 基于 API 的权限
    - name: "api-usage"
      resource: "api"
      endpoint: "/api/tasks"
      actions: ["read", "create"]
      conditions:
        rate_limit: "100/minute"
        max_payload: "1M"

4.4 动态权限调整

bash
# 临时提升权限(带超时)
openclaw permissions elevate \
  --user alice \
  --role lead \
  --duration 30m \
  --reason "紧急修复生产问题"

# 查看当前有效权限
openclaw permissions check --user alice

# 输出示例:
# User: alice
# Roles: developer
# Elevated: lead (expires in 15m 30s)
# Reason: 紧急修复生产问题
# Effective permissions:
#   ✅ read:*
#   ✅ write:*
#   ✅ execute:*
#   ✅ delete:tasks
#   ✅ approve:commands
#   ❌ write:rbac
#   ❌ write:security

# 撤销临时权限
openclaw permissions revoke --user alice --role lead

五、输入输出安全

5.1 提示词注入防御

python
# 自定义安全中间件示例
# ~/.config/openclaw/middlewares/safety.py

from openclaw.middleware import Middleware
import re

class PromptInjectionDefense(Middleware):
    """提示词注入防御中间件"""

    INJECTION_PATTERNS = [
        r"ignore\s+previous\s+instructions",
        r"forget\s+all\s+previous",
        r"system\s*[::]\s*",
        r"you\s+are\s+now\s+",
        r"disable\s+safety",
        r"bypass\s+security",
    ]

    def before_process(self, context):
        user_input = context.input.lower()

        for pattern in self.INJECTION_PATTERNS:
            if re.search(pattern, user_input):
                context.logger.warning(
                    f"Potential prompt injection detected: {pattern}"
                )
                context.input = self.sanitize_input(context.input)
                context.flagged = True

        return context

    def sanitize_input(self, text):
        """清理输入文本"""
        # 移除控制字符
        text = re.sub(r'[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]', '', text)
        # 规范化空白字符
        text = re.sub(r'\s+', ' ', text).strip()
        return text

5.2 输出安全过滤

python
# 输出安全过滤中间件
class OutputSecurityFilter(Middleware):
    """输出安全过滤中间件"""

    SENSITIVE_PATTERNS = {
        "api_key": re.compile(r"sk-[a-zA-Z0-9]{20,}"),
        "password": re.compile(r"password\s*[:=]\s*\S+", re.IGNORECASE),
        "private_key": re.compile(r"-----BEGIN\s+(RSA\s+)?PRIVATE\s+KEY-----"),
        "aws_key": re.compile(r"AKIA[0-9A-Z]{16}"),
    }

    def after_process(self, context):
        output = context.output

        for name, pattern in self.SENSITIVE_PATTERNS.items():
            matches = pattern.findall(output)
            if matches:
                context.logger.warning(f"Sensitive data detected in output: {name}")
                output = pattern.sub(f"[{name.upper()}_REDACTED]", output)

        context.output = output
        return context

六、网络与数据安全

6.1 网络隔离配置

yaml
# ~/.config/openclaw/network-security.yaml
network_security:
  # DNS 过滤
  dns:
    allowed_domains:
      - "*.openai.com"
      - "*.anthropic.com"
      - "*.github.com"
      - "*.pypi.org"
    blocked_domains:
      - "*.darkweb.*"
      - "*.malware.*"
    dns_over_https: true
    dns_server: "8.8.8.8"

  # 网络代理
  proxy:
    enabled: true
    url: http://security-proxy.internal:3128
    bypass:
      - "localhost"
      - "127.0.0.1"
      - "*.internal.corp"

  # TLS 设置
  tls:
    min_version: "1.2"
    verify_certificates: true
    allowed_cas:
      - /etc/ssl/certs/ca-certificates.crt

  # 出站限制
  outbound:
    allowed_ports: [443, 80]
    allowed_protocols: ["https", "http"]
    max_connections_per_minute: 60
    max_data_transfer: "100M"

6.2 数据脱敏

yaml
# ~/.config/openclaw/data-security.yaml
data_security:
  # 敏感数据分类
  classification:
    public:
      description: "公开数据"
      handling: "normal"

    internal:
      description: "内部数据"
      handling: "encrypt_at_rest"

    confidential:
      description: "机密数据"
      handling: "encrypt_everywhere"
      ai_processing: false  # 禁止 AI 处理机密数据

    restricted:
      description: "受限数据"
      handling: "no_storage"
      ai_processing: false

  # 自动分类规则
  auto_classification:
    rules:
      - pattern: "*.py"
        classification: internal

      - pattern: "*.pem"
        classification: restricted

      - pattern: "*.env"
        classification: confidential

      - pattern: "*/secrets/*"
        classification: restricted

七、审计与合规

7.1 审计日志配置

yaml
# ~/.config/openclaw/audit-config.yaml
audit:
  enabled: true

  # 审计事件类型
  events:
    command_execution: true
    file_access: true
    network_request: true
    permission_change: true
    config_change: true
    approval_action: true
    error_event: true

  # 日志输出
  outputs:
    - type: file
      path: /var/log/openclaw/audit.log
      format: json
      rotation:
        max_size: 100M
        max_files: 30
        compress: true

    - type: syslog
      server: syslog.internal:514
      protocol: tcp
      tls: true

    - type: siem
      endpoint: https://siem.internal/api/events
      api_key: ${SIEM_API_KEY}

  # 日志格式
  format:
    include:
      - timestamp
      - event_type
      - user
      - instance_id
      - action
      - resource
      - result
      - source_ip
      - user_agent

7.2 审计日志示例

json
{
  "timestamp": "2024-01-15T14:30:00.123Z",
  "event_type": "command_execution",
  "user": "alice",
  "instance_id": "worker-1",
  "action": "execute",
  "resource": "shell",
  "command": "grep -r 'TODO' src/",
  "result": "success",
  "duration_ms": 245,
  "source_ip": "192.168.1.100",
  "user_agent": "openclaw-cli/2.7.3",
  "session_id": "sess_abc123",
  "correlation_id": "corr_xyz789"
}

7.3 合规报告

bash
# 生成合规报告
openclaw audit report \
  --period "2024-01-01/2024-01-31" \
  --format html \
  --output audit-report-january.html

# 生成审计报告摘要
openclaw audit summary --period "last-7d"

# 输出示例:
# ┌────────────────────────────────────────────┐
# │            7 日审计摘要                      │
# ├────────────────────────────────────────────┤
# │                                            │
# │ 总事件数:12,456                            │
# │ 命令执行:8,234 (66.1%)                     │
# │ 文件访问:3,012 (24.2%)                     │
# │ 网络请求:1,210 (9.7%)                      │
# │                                            │
# │ 成功:12,398 (99.5%)                        │
# │ 被拒绝:45 (0.4%)                           │
# │ 失败:13 (0.1%)                             │
# │                                            │
# │ 安全事件:                                   │
# │  ⚠️  提示词注入尝试:3 次                    │
# │  ⚠️  越权命令尝试:2 次                      │
# │  ⚠️  敏感数据访问尝试:1 次                  │
# │                                            │
# │ 活跃用户:5                                  │
# │ 活跃实例:3                                  │
# └────────────────────────────────────────────┘

八、实战安全配置

8.1 个人开发者安全配置

yaml
# ~/.config/openclaw/profiles/personal.yaml
profile:
  name: personal
  description: "个人开发者安全配置"

  shell:
    enabled: true
    mode: whitelist
    whitelist:
      - ls - cat - head - tail - wc
      - grep - find - diff
      - git
      - python - node - npm - pip
      - make - cargo

  sandbox:
    enabled: true
    type: chroot
    root: /tmp/openclaw-sandbox

  permissions:
    level: developer

  audit:
    enabled: true
    output: ~/.openclaw/logs/audit.log

8.2 企业安全配置

yaml
# ~/.config/openclaw/profiles/enterprise.yaml
profile:
  name: enterprise
  description: "企业级安全配置"

  shell:
    enabled: true
    mode: whitelist
    approval_required:
      - pattern: "git push"
      - pattern: "deploy"
      - pattern: "rm -rf"

  sandbox:
    enabled: true
    type: docker
    image: openclaw/sandbox:latest
    network: isolated
    resource_limits:
      memory: 1G
      cpu: "1.0"

  network:
    proxy:
      enabled: true
      url: http://corp-proxy:3128

  rbac:
    enabled: true
    provider: ldap
    ldap_url: ldap://ldap.corp:389
    base_dn: "ou=users,dc=corp,dc=com"

  audit:
    enabled: true
    outputs:
      - type: siem
        endpoint: https://siem.corp/api/events

  data_security:
    classification:
      enforce: true
      ai_processing_blocked: ["confidential", "restricted"]

8.3 CI/CD 安全配置

yaml
# ~/.config/openclaw/profiles/cicd.yaml
profile:
  name: cicd
  description: "CI/CD 流水线安全配置"

  shell:
    enabled: true
    mode: whitelist
    whitelist:
      - git
      - make
      - python
      - node
      - npm
      - docker

  sandbox:
    enabled: true
    type: docker
    image: openclaw/ci-sandbox:latest
    cleanup: always

  permissions:
    level: developer
    auto_approve: true  # CI/CD 中自动审批

  audit:
    enabled: true
    format: json
    output: /tmp/openclaw-audit.json

  rate_limit:
    requests_per_hour: 1000
    tokens_per_hour: 500000

九、安全最佳实践

9.1 安全清单

text
✅ 启动前检查清单:

□ 配置了命令白名单/黑名单
□ 启用了沙箱隔离
□ 设置了网络限制
□ 配置了审计日志
□ 定义了权限角色
□ 设置了输入输出过滤
□ 配置了数据分类策略
□ 启用了 TLS
□ 定期轮换 API 密钥
□ 备份了安全配置

9.2 安全原则

  1. 最小权限原则:只授予完成任务所需的最小权限
  2. 纵深防御:多层安全控制,单点失效不影响整体安全
  3. 默认拒绝:白名单模式优于黑名单模式
  4. 审计追踪:所有操作可追溯,日志不可篡改
  5. 定期审查:定期检查安全策略的有效性
  6. 及时更新:保持 OpenClaw 和安全依赖的最新版本

9.3 安全检查脚本

bash
#!/bin/bash
# security-check.sh - OpenClaw 安全检查脚本

echo "🔒 OpenClaw 安全检查..."

PASS=0
FAIL=0
WARN=0

# 检查 1:沙箱是否启用
if openclaw config get sandbox.enabled | grep -q "true"; then
  echo "✅ 沙箱已启用"
  ((PASS++))
else
  echo "❌ 沙箱未启用"
  ((FAIL++))
fi

# 检查 2:是否使用白名单模式
if openclaw config get shell.mode | grep -q "whitelist"; then
  echo "✅ 使用白名单模式"
  ((PASS++))
else
  echo "⚠️  未使用白名单模式"
  ((WARN++))
fi

# 检查 3:审计日志是否启用
if openclaw config get audit.enabled | grep -q "true"; then
  echo "✅ 审计日志已启用"
  ((PASS++))
else
  echo "❌ 审计日志未启用"
  ((FAIL++))
fi

# 检查 4:API 密钥是否环境变量引用
if grep -q '\${.*API_KEY}' ~/.config/openclaw/config.yaml 2>/dev/null; then
  echo "✅ API 密钥使用环境变量"
  ((PASS++))
else
  echo "⚠️  API 密钥可能硬编码"
  ((WARN++))
fi

# 检查 5:TLS 是否启用
if openclaw config get network.tls.verify_certificates | grep -q "true"; then
  echo "✅ TLS 证书验证已启用"
  ((PASS++))
else
  echo "⚠️  TLS 证书验证未启用"
  ((WARN++))
fi

echo ""
echo "📊 检查结果:✅ $PASS 通过 | ⚠️  $WARN 警告 | ❌ $FAIL 失败"

if [ $FAIL -gt 0 ]; then
  echo "⚠️  发现安全问题,请及时修复!"
  exit 1
fi

总结

本篇我们全面掌握了 OpenClaw 的安全与权限体系:

  • 安全威胁模型:提示词注入、命令执行、数据泄露、权限提升
  • 命令审批与隔离:白名单/黑名单、审批流程、沙箱隔离
  • 安全策略体系:输入验证、输出过滤、提示词安全
  • 权限配置:RBAC 角色管理、细粒度权限控制、动态权限调整
  • 输入输出安全:注入防御、敏感数据脱敏
  • 网络与数据安全:网络隔离、数据分类、数据脱敏
  • 审计与合规:审计日志、合规报告、事件追踪
  • 实战配置:个人/企业/CI-CD 三种场景的安全配置模板
  • 最佳实践:安全清单、安全原则、自动化安全检查

关键要点

  1. 安全是分层体系,需要从沙箱隔离到审计追踪的多层防护
  2. 白名单模式优于黑名单模式——默认拒绝一切,只允许明确授权的
  3. 审计日志不仅是事后追溯的工具,更是实时威胁检测的基础
  4. 权限管理遵循最小权限原则,定期审查和调整
  5. 安全配置需要与使用场景匹配——个人、企业、CI/CD 各有侧重
  6. 自动化安全检查脚本可以持续验证安全状态

下篇预告

至此,OpenClaw 系列的核心教程已经全部完成!接下来的系列文章将进入 Hermes Agent 深度指南,涵盖:

  • 🧠 Hermes 模型系列:本地部署、微调、推理优化
  • 🔧 Hermes 工具开发:自定义工具、插件系统、工具市场
  • 🏗️ 企业级部署:高可用架构、多租户、服务网格
  • 📊 可观测性:指标、追踪、日志、告警
  • 🎯 高级工作流:DAG 编排、状态机、事件驱动

感谢你的阅读,我们 Hermes Agent 系列见!🎉