OpenFang 安全最佳实践

14分钟阅读

自主 AI Agent 的安全不容忽视。OpenFang 从设计之初内置 16 层安全防护:4 层沙箱隔离、4 层网络安全、4 层数据安全和 4 层审计合规。本文提供三种预设配置模板和完整安全检查清单。

OpenFang 安全最佳实践:16 层安全防护详解

Slug: openfang-security-best-practices 分类: usage-guides (使用指南) 目标关键词: OpenFang 安全, OpenFang 安全配置, Agent 安全最佳实践 搜索意图: 信息型(用户想安全部署 OpenFang) 目标字数: ~2000 字 语言: zh


为什么 Agent 安全至关重要?

自主 AI Agent 拥有执行命令、访问网络、读写文件的能力——这些能力如果不加限制,就是安全灾难的配方。一个没有安全防护的 Agent 可能被 Prompt Injection 攻击诱导执行危险命令、泄露敏感数据,或被滥用为攻击跳板。

OpenFang 从设计之初就将安全作为核心架构的一部分,提供了 16 层安全防护机制。本文将从浅入深地介绍每一层的配置方法和最佳实践。

安全架构概览

OpenFang 的 16 层安全防护分为四个大类:

类别层级防护目标
沙箱隔离1–4 层限制 Agent 的执行环境
网络安全5–8 层控制 Agent 的网络访问
数据安全9–12 层保护敏感数据不被泄露
审计与合规13–16 层记录和审计 Agent 行为

第一类:沙箱隔离

第 1 层:文件系统沙箱

限制 Agent 可访问的文件系统路径:

toml
[security.sandbox.filesystem]mode = "strict"                  # off / basic / strictallowed_paths = [    "./workspace/",    "/tmp/openfang/",]denied_paths = [    "/etc/",    "~/.ssh/",    "~/.aws/",]read_only_paths = [    "./config/",    "/usr/share/openfang/",]

strict 模式下,Agent 无法访问任何 allowed_paths 未列出的路径。建议始终为 Agent 创建专用的工作目录。

第 2 层:进程沙箱

限制 Agent 可以执行的系统命令:

toml
[security.sandbox.process]allow_shell = false              # 禁止直接 Shell 访问allowed_commands = [    "git",    "curl",    "python3",    "node",]denied_commands = [    "rm",    "sudo",    "chmod",    "wget",]command_timeout = 30             # 命令最长执行时间(秒)

allow_shell = false 是生产环境必须开启的配置。Agent 只能通过 allowed_commands 列表中的命令与系统交互。

第 3 层:内存限制

防止 Agent 因内存泄漏或恶意代码耗尽系统资源:

toml
[security.sandbox.memory]max_memory_mb = 512              # Agent 内存上限max_per_hand_mb = 128            # 每个 Hand 的内存上限oom_policy = "kill_hand"         # kill_hand / restart / notify

第 4 层:CPU 限制

toml
[security.sandbox.cpu]max_cores = 2                    # 最多使用 2 个 CPU 核心priority = "low"                 # low / normal / highcfs_period_us = 100000cfs_quota_us = 50000             # 50% CPU 配额

第二类:网络安全

第 5 层:网络隔离

控制 Agent 的出站网络访问:

toml
[security.network]mode = "allowlist"               # allowlist / blocklist / unrestrictedallowed_domains = [    "api.anthropic.com",    "api.openai.com",    "github.com",]allowed_ports = [443, 80]block_ip_ranges = [    "10.0.0.0/8",                # 阻止访问内网    "172.16.0.0/12",    "192.168.0.0/16",]dns_over_https = true            # 使用加密 DNS

第 6 层:流量加密

toml
[security.network.tls]min_version = "1.2"verify_certificates = truecertificate_pinning = [    { domain = "api.anthropic.com", pin = "sha256/..." },]

第 7 层:速率限制

防止 Agent 被滥用发送大量请求:

toml
[security.network.rate_limit]requests_per_minute = 60tokens_per_minute = 100000burst_multiplier = 2cooldown_after_exceeded = "5m"

第 8 层:代理与出口控制

所有 Agent 流量通过企业代理出口:

toml
[security.network.proxy]http_proxy = "${HTTP_PROXY}"https_proxy = "${HTTPS_PROXY}"no_proxy = ["localhost", "127.0.0.1"]enforce_proxy = true             # 强制所有流量经过代理

第三类:数据安全

第 9 层:输入清理

防御 Prompt Injection 攻击:

toml
[security.data.input_guard]enabled = truemode = "strict"[security.data.input_guard.rules]detect_jailbreak = truedetect_prompt_leak = truedetect_code_injection = truemax_input_length = 8000sanitize_markdown = true

输入清理器会在 Agent 处理用户输入之前扫描潜在的注入攻击,包括越狱提示词、提示词泄露尝试和代码注入。

第 10 层:输出过滤

防止敏感数据通过 Agent 回复泄露:

toml
[security.data.output_guard]enabled = true[security.data.output_guard.patterns]credit_cards = trueapi_keys = true                  # 检测并屏蔽 API key 格式emails = "mask"                  # mask / block / allowphone_numbers = "mask"custom_patterns = [    "\\b(?:sk|pk)_[a-zA-Z0-9]{32,}\\b",    "\\bAKIA[A-Z0-9]{16}\\b",   # AWS Access Key]

第 11 层:敏感数据加密

toml
[security.data.encryption]encrypt_workspace = trueencrypt_logs = truekey_derivation = "argon2id"rotation_days = 30

第 12 层:数据最小化

toml
[security.data.minimization]log_user_inputs = false          # 不记录用户输入log_agent_outputs = false        # 不记录 Agent 输出retention_days = 7               # 日志保留 7 天strip_pii = true                 # 自动移除 PII

第四类:审计与合规

第 13 层:操作审计

toml
[security.audit]enabled = truelog_level = "info"[security.audit.events]command_execution = truefile_access = truenetwork_request = trueconfig_change = truehand_start_stop = true

审计日志为 JSON 格式,包含时间戳、操作类型、操作者、操作详情和结果:

json
{  "timestamp": "2026-07-10T08:30:15.123Z",  "event": "command_execution",  "hand": "researcher",  "command": "git clone https://github.com/...",  "sandbox": "strict",  "result": "allowed",  "duration_ms": 1240}

第 14 层:异常检测

toml
[security.audit.anomaly_detection]enabled = truebaseline_days = 7sensitivity = "medium"           # low / medium / highalert_on = ["unusual_command", "off_hours_activity", "unusual_destination"]

第 15 层:合规报告

toml
[security.compliance]generate_reports = trueschedule = "0 0 1 * *"           # 每月生成frameworks = ["SOC2", "ISO27001"]export_format = "pdf"

第 16 层:安全事件响应

toml
[security.incident_response]auto_contain = true              # 检测到异常自动隔离 Agentcontainment_action = "pause_all_hands"notify_channels = ["slack:#security", "email:[email protected]"]forensic_snapshot = true         # 触发时保存现场快照

快速安全配置模板

根据你的部署场景,选择对应的预设安全配置:

开发环境(低安全要求)

bash
openfang security preset development

生产环境(标准安全要求)

bash
openfang security preset production

高安全环境(金融/医疗/政府)

bash
openfang security preset hardened

三种预设的关键差异:

配置项DevelopmentProductionHardened
文件系统basicstrictstrict
Shell 访问允许禁止禁止
网络模式blocklistallowlistallowlist
输出过滤关闭开启开启
审计日志关闭开启开启
数据加密关闭开启开启
异常检测关闭mediumhigh
自动隔离关闭关闭开启

安全检查清单

部署到生产环境前,使用 openfang security audit 命令逐项验证:

bash
openfang security audit# 输出示例:# ✅ [1/16] Filesystem sandbox: strict# ✅ [2/16] Process sandbox: no shell access# ✅ [3/16] Memory limit: 512 MB# ✅ [4/16] CPU limit: 2 cores# ✅ [5/16] Network: allowlist mode# ⚠️ [6/16] TLS cert pinning: not configured# ✅ [7/16] Rate limiting: enabled# ...# Score: 14/16 — 2 recommendations pending

常见问题

安全配置太严格导致 Agent 无法正常工作怎么办?
使用逐步放松策略。先从 hardened 预设开始,观察 Agent 的行为。对于被阻止的必要操作,使用审计日志定位具体被拦截的项,然后针对性地添加到允许列表。
Prompt Injection 真的能防御吗?
OpenFang 的输入清理器提供了多层防御,但没有任何防御是完美的。最佳实践是结合多层防护:输入清理 + 沙箱限制 + 网络隔离。即使 Injection 成功绕过了输入清理,沙箱和网络限制也能最小化影响。
如何在容器化环境(Docker/K8s)中叠加安全层?
OpenFang 的安全层与应用层安全是互补关系,不是替代关系。Docker 的 seccomp/AppArmor profiles 可以作为第 0 层额外防护,但不应替代 OpenFang 内置的安全机制。
安全事件发生了怎么办?
1. 检查审计日志确定事件范围
2. 如果配置了 auto_contain = true,Agent 已自动隔离
3. 下载取证快照:openfang security forensics download --run-id xxx
4. 分析根因,修补漏洞
5. 从快照恢复或重新初始化 Agent

下一步