返回

11. OpenClaw 安全設定最佳實務

安全是 OpenClaw 部署的關鍵考量。本文詳細介紹 DM 配對機制、沙箱模式、權限控制、敏感資料處理、網路安全等安全設定,幫助你建構安全可靠的 AI 助手系統。

適用於 OpenClaw v2026.2 | 本文適合所有 OpenClaw 使用者,特別是準備正式環境部署或團隊使用的使用者。

TL;DR: DM 配對防止未授權存取:"dmPolicy": "pairing"。沙箱隔離危險操作:"sandbox": {"mode": "non-main"}。敏感資料過濾:"security": {"sensitiveData": {"patterns": ["sk-*"]}}。遠端存取用 Tailscale 而非公網暴露。執行 openclaw doctor --security 檢查安全狀態。

安全風險概述

主要風險類型

風險類型 說明 潛在後果
未授權存取 陌生人可以使用你的 Agent 資源濫用、隱私洩露
權限濫用 Agent 執行超出預期的操作 資料損壞、系統破壞
資料洩露 敏感資訊被不當處理 隱私洩露、合規問題
網路攻擊 外部攻擊者利用漏洞 系統入侵、資料竊取
Prompt 注入 惡意指令繞過安全限制 未授權操作

安全模型

flowchart TB
    subgraph 外層["網路層"]
        FW[防火牆]
        TLS[HTTPS/TLS]
    end
    
    subgraph 中層["存取控制"]
        DM[DM 配對]
        WL[白名單]
        AUTH[認證]
    end
    
    subgraph 內層["執行控制"]
        SBX[沙箱]
        PERM[權限]
        AUDIT[稽核]
    end
    
    subgraph 核心["Agent"]
        AG[Agent 核心]
    end
    
    外層 --> 中層 --> 內層 --> 核心

DM 配對機制

什麼是 DM 配對?

DM(Direct Message)配對是一種保護機制,要求新使用者先完成配對才能與 Agent 對話。

配對流程

sequenceDiagram
    participant S as 陌生人
    participant B as Bot
    participant G as Gateway
    participant O as 所有者
    
    S->>B: 傳送訊息 "你好"
    B->>G: 訊息到達
    G->>G: 檢查配對狀態
    G-->>B: 未配對,產生配對碼
    B-->>S: "請配對:配對碼 ABC123"
    S->>O: 告訴所有者配對碼
    O->>G: openclaw pairing approve telegram ABC123
    G->>G: 加入白名單
    G-->>S: 配對成功
    S->>B: 傳送訊息 "你好"
    B-->>S: 正常回應

設定 DM 配對

{
  "channels": {
    "telegram": {
      "botToken": "your_token",
      "dmPolicy": "pairing"
    },
    "whatsapp": {
      "dmPolicy": "pairing"
    },
    "discord": {
      "dmPolicy": "pairing"
    }
  }
}

配對模式

模式 說明 安全級別 適用場景
pairing 需要配對碼 ⭐⭐⭐⭐ 個人使用(預設)
open 任何人可用 公開服務
whitelist 僅白名單使用者 ⭐⭐⭐⭐⭐ 高安全需求

配對管理

# 檢視待配對請求
openclaw pairing list

# 輸出範例:
# Channel    Code     User        Created
# telegram   ABC123   @stranger   2026-02-26 10:30:00
# discord    XYZ789   user#1234   2026-02-26 10:35:00

# 批准配對
openclaw pairing approve telegram ABC123

# 拒絕配對
openclaw pairing reject telegram ABC123

# 檢視已配對使用者
openclaw pairing show telegram

白名單設定

{
  "channels": {
    "telegram": {
      "dmPolicy": "open",
      "allowFrom": [
        123456789,
        987654321
      ]
    },
    "whatsapp": {
      "dmPolicy": "open",
      "allowFrom": [
        "+8613800138000",
        "+8613800138001"
      ]
    }
  }
}

取得使用者 ID

# Telegram:向 @userinfobot 傳送訊息
# Discord:在頻道中使用 /id 指令或檢視使用者資料
# WhatsApp:從聯絡人資訊中取得

沙箱模式

什麼是沙箱?

沙箱是一種隔離機制,限制 Agent 的操作範圍,防止意外或惡意的系統修改。

沙箱模式

模式 說明 適用場景
off 無沙箱,完全信任 主工作階段、可信環境
non-main 非主工作階段啟用沙箱 多使用者、團隊共享
always 始終啟用沙箱 不信任環境、公開服務

設定沙箱

{
  "agents": {
    "defaults": {
      "sandbox": {
        "mode": "non-main",
        "allowlist": ["read", "write", "edit", "bash"],
        "denylist": ["browser", "canvas", "nodes"],
        "container": {
          "image": "openclaw/sandbox:latest",
          "timeout": 30000,
          "memory": "512m",
          "cpu": "0.5"
        }
      }
    }
  }
}

工具權限控制

{
  "agents": {
    "defaults": {
      "sandbox": {
        "mode": "non-main",
        "allowlist": [
          "read",
          "write",
          "edit",
          "bash",
          "web_search",
          "sessions_list",
          "sessions_history",
          "sessions_send"
        ],
        "denylist": [
          "browser",
          "canvas",
          "nodes",
          "cron",
          "discord",
          "gateway"
        ]
      }
    }
  }
}

檔案系統權限

{
  "agents": {
    "defaults": {
      "permissions": {
        "filesystem": {
          "read": ["~/workspace", "~/documents"],
          "write": ["~/workspace/output"],
          "deny": ["~/.ssh", "~/.gnupg", "~/.openclaw/credentials"]
        }
      }
    }
  }
}

網路權限

{
  "agents": {
    "defaults": {
      "permissions": {
        "network": {
          "outbound": {
            "allow": [
              "api.anthropic.com",
              "api.openai.com",
              "github.com"
            ],
            "deny": ["*"]
          }
        }
      }
    }
  }
}

Elevated 權限

什麼是 Elevated 權限?

Elevated 權限是臨時提升權限的機制,用於需要更高權限的操作。

設定 Elevated 權限

{
  "agents": {
    "defaults": {
      "elevatedAccess": {
        "enabled": true,
        "allowlist": [123456789, 987654321],
        "requireConfirmation": true,
        "timeout": 300000
      }
    }
  }
}

使用 Elevated 權限

# 在聊天中請求提升權限
/elevated on

# 執行需要提升權限的操作
# ...

# 關閉提升權限
/elevated off

敏感資料處理

敏感資訊類型

類型 範例 處理方式
API Keys sk-ant-xxx, sk-xxx 不記錄、不輸出
密碼 登入密碼、資料庫密碼 不儲存、不傳輸
Token OAuth token、Session token 加密儲存
個人資訊 身分證、銀行卡 不記錄、匿名化

設定敏感資訊過濾

{
  "security": {
    "sensitiveData": {
      "patterns": [
        "sk-[a-zA-Z0-9]{20,}",
        "xox[baprs]-[a-zA-Z0-9-]+",
        "[0-9]{15,19}",
        "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}"
      ],
      "redact": true,
      "log": false
    }
  }
}

憑證儲存

# 憑證儲存位置
~/.openclaw/credentials/

# 加密儲存
# OpenClaw 使用系統鑰匙圈加密儲存敏感憑證

憑證管理

# 檢視已儲存的憑證
openclaw credentials list

# 輸出範例:
# Name           Type        Created
# telegram-bot   api_key     2026-02-20
# gmail-oauth    oauth       2026-02-21

# 刪除憑證
openclaw credentials delete telegram-bot

# 匯出憑證(加密)
openclaw credentials export --output credentials.enc

# 匯入憑證
openclaw credentials import --input credentials.enc

網路安全

繫結設定

{
  "gateway": {
    "bind": "loopback",  // 僅本地存取
    "port": 18789,
    "tls": {
      "enabled": false
    }
  }
}
繫結選項 說明 安全級別
loopback 僅本地存取 ⭐⭐⭐⭐⭐
private 私有網路存取 ⭐⭐⭐⭐
all 所有介面存取

Tailscale 設定

{
  "gateway": {
    "tailscale": {
      "mode": "serve",  // serve 或 funnel
      "resetOnExit": true
    },
    "auth": {
      "mode": "password",
      "allowTailscale": true
    }
  }
}

認證設定

{
  "gateway": {
    "auth": {
      "mode": "password",
      "password": "your-secure-password",
      "sessionTimeout": 3600000
    }
  }
}

稽核與日誌

操作日誌

{
  "logging": {
    "level": "info",
    "audit": {
      "enabled": true,
      "events": [
        "tool.invoke",
        "session.create",
        "session.reset",
        "pairing.approve",
        "pairing.reject",
        "elevated.enable",
        "elevated.disable"
      ],
      "retention": 30
    }
  }
}

檢視稽核日誌

# 檢視稽核日誌
openclaw logs --type audit

# 輸出範例:
# [2026-02-26 10:30:00] INFO  tool.invoke session=main tool=bash command="ls -la"
# [2026-02-26 10:30:05] INFO  pairing.approve channel=telegram code=ABC123 user=123456789
# [2026-02-26 10:31:00] INFO  elevated.enable session=telegram:user:123456789

# 過濾特定事件
openclaw logs --type audit --filter "tool.invoke"

# 按時間範圍
openclaw logs --type audit --from 2026-02-01 --to 2026-02-28

使用情況監控

# 檢視使用情況
openclaw usage --today

# 輸出範例:
# Date: 2026-02-26
# Requests: 128
# Tokens In: 125,000
# Tokens Out: 45,000
# Cost: $2.10
# Top Tools: bash(45), read(32), write(18)

# 異常偵測
openclaw usage --alerts

# 輸出範例:
# ⚠️  High usage detected: 1000+ requests in 1 hour
# ⚠️  Unusual tool pattern: bash commands to sensitive directories

安全檢查清單

基礎安全

  • 啟用 DM 配對或白名單
  • 設定沙箱模式
  • 設定合理的工具權限
  • 啟用操作稽核日誌
  • 定期更換 API Key

進階安全

  • 設定檔案系統權限限制
  • 設定網路出站白名單
  • 啟用敏感資訊過濾
  • 設定 Elevated 權限確認
  • 定期審查配對使用者

企業級安全

  • 使用 Docker 沙箱隔離
  • 設定 Tailscale 私有網路
  • 啟用 HTTPS/TLS
  • 設定使用量告警
  • 定期安全稽核

安全診斷

# 執行安全診斷
openclaw doctor --security

# 輸出範例:
# ✅ DM Pairing: Enabled
# ✅ Sandbox Mode: non-main
# ⚠️  File Permissions: Some directories not restricted
# ✅ Network Outbound: Restricted to allowlist
# ⚠️  Elevated Access: No confirmation required
# ✅ Audit Logging: Enabled
# ✅ Credentials: Encrypted

# 建議:
# - Restrict file access to ~/workspace only
# - Enable elevated access confirmation

小結

安全設定是 OpenClaw 部署的重要環節:

  • DM 配對:防止未授權存取
  • 沙箱模式:限制操作範圍
  • 權限控制:精細化的工具和檔案權限
  • 敏感資料:過濾和保護敏感資訊
  • 網路安全:繫結和認證設定
  • 稽核日誌:追蹤和監控操作

本篇小結

  • 理解了主要安全風險和防護機制
  • 掌握了 DM 配對和白名單設定
  • 學會了沙箱模式和權限控制
  • 了解了敏感資料處理和網路設定
  • 掌握了稽核日誌和安全檢查清單

更新記錄

  • 2026-02-26:初版發布,基於 OpenClaw v2026.2

系列導航