【Bug已解决】openclaw: "command injection risk" / Untrusted input execution — OpenClaw 命令注入风险解决方案
1. 问题描述
OpenClaw 在处理不受信任的输入时存在命令注入风险:
# 命令注入风险 $ openclaw --auto-approve "运行用户输入的命令: $(cat user_input.txt)" # user_input.txt 包含: rm -rf / # OpenClaw 可能执行危险命令 # 或路径注入 $ openclaw --auto-approve "分析 $(echo '../../../etc/passwd')" # 路径遍历,访问系统文件 # 或变量注入 $ openclaw "分析 $USER_INPUT" # USER_INPUT 包含恶意命令 # 或 --auto-approve 执行危险命令 $ openclaw --full-auto "清理项目" # OpenClaw 执行了 rm -rf src/这个问题在以下场景中特别常见:
- 处理用户输入
- 使用 $(cat) 或 $(echo)
- --full-auto 模式
- 路径遍历
- 变量注入
- CI/CD 中执行
2. 原因分析
| 原因分类 | 具体表现 | 占比 |
|---|---|---|
| 用户输入 | $(cat) | 约 35% |
| --full-auto | 自动执行 | 约 25% |
| 路径遍历 | ../../../ | 约 20% |
| 变量注入 | $VAR | 约 10% |
| 命令拼接 | ; && | 约 5% |
| CI/CD 执行 | 自动化 | 约 5% |
3. 解决方案
方案一:不使用 --full-auto(最推荐)
# 步骤 1:不使用 --full-auto openclaw "task" # 需要手动确认 # 步骤 2:或使用 --auto-approve 但限制工具 openclaw --auto-approve --allow-tool file_edit "task" # 步骤 3:避免 --full-auto + 不受信任输入 # 错误: openclaw --full-auto "运行 $(cat user_input.txt)" # 正确: openclaw "分析 user_input.txt 的内容" # 步骤 4:验证 openclaw --print "hello"方案二:使用 --print 先分析
# 步骤 1:先使用 --print 分析 openclaw --print "分析 user_input.txt 的内容,不执行任何命令" # 步骤 2:审查输出 # 确认内容安全 # 步骤 3:再执行 openclaw --auto-approve "根据分析结果修改 src/index.js" # 步骤 4:验证 git diff方案三:不使用 $(cat) 或 $(echo)
# 步骤 1:不使用命令替换 # 错误: openclaw "分析 $(cat user_input.txt)" # 正确: openclaw "分析 user_input.txt" # 步骤 2:让 OpenClaw 自己读取文件 openclaw "读取 user_input.txt 并分析" # 步骤 3:避免变量注入 # 错误: openclaw "分析 $USER_INPUT" # 正确: openclaw "分析 user_input.txt" # 步骤 4:验证 openclaw --print "分析 user_input.txt"方案四:限制工具权限
# 步骤 1:限制允许的工具 openclaw --auto-approve --allow-tool file_read --allow-tool file_edit "task" # 步骤 2:不允许命令执行 openclaw --auto-approve --deny-tool command_run "task" # 步骤 3:或配置 cat > .openclaw/config.json << 'EOF' { "allowedTools": ["file_read", "file_edit"], "deniedTools": ["command_run"] } EOF # 步骤 4:验证 openclaw --auto-approve "task"方案五:验证输入内容
# 步骤 1:检查输入内容 cat user_input.txt | grep -E "rm|sudo|chmod|>" # 如果有危险命令,不执行 # 步骤 2:使用白名单 if grep -qE "^[a-zA-Z0-9\s]+$" user_input.txt; then openclaw "分析 user_input.txt" else echo "Input contains dangerous characters" fi # 步骤 3:或使用 Python 验证 python3 -c " with open('user_input.txt') as f: content = f.read() if any(cmd in content for cmd in ['rm ', 'sudo', 'chmod', '>']): print('Dangerous input') else: print('Safe') " # 步骤 4:验证 openclaw --print "分析 user_input.txt"方案六:使用 Git 保护
# 步骤 1:使用前先 commit git add -A git commit -m "Before OpenClaw changes" # 步骤 2:运行 OpenClaw openclaw --auto-approve "task" # 步骤 3:查看修改 git diff git diff --name-only # 步骤 4:如果危险,恢复 git checkout . git clean -fd4. 各方案对比总结
| 方案 | 适用场景 | 推荐指数 | 难度 |
|---|---|---|---|
| 方案一:不用 full-auto | 安全 | ⭐⭐⭐⭐⭐ | 低 |
| 方案二:先 --print | 预防 | ⭐⭐⭐⭐⭐ | 低 |
| 方案三:不用 $(cat) | 输入 | ⭐⭐⭐⭐⭐ | 低 |
| 方案四:限制工具 | 权限 | ⭐⭐⭐⭐⭐ | 低 |
| 方案五:验证输入 | 检查 | ⭐⭐⭐⭐ | 中 |
| 方案六:Git 保护 | 恢复 | ⭐⭐⭐⭐⭐ | 低 |
5. 常见问题 FAQ
5.1 命令注入是什么
攻击者通过输入恶意命令,让程序执行非预期操作。
5.2 --full-auto 有什么风险
自动执行所有操作,包括可能危险的命令。
5.3 如何避免命令注入
不使用$(cat)或$VAR,让 OpenClaw 自己读取文件。
5.4 如何限制工具权限
使用--allow-tool和--deny-tool,或在配置中设置。
5.5 $(cat) 有什么问题
将文件内容作为命令行参数,可能包含恶意命令。
5.6 如何验证输入
使用grep或 Python 检查危险命令(rm、sudo、chmod)。
5.7 路径遍历是什么
使用../../../访问项目目录外的文件。
5.8 如何使用 Git 保护
git commit -m "backup" # 危险时 git checkout . + git clean -fd5.9 --print 安全吗
--print只输出不执行,安全用于分析。
5.10 排查清单速查表
□ 1. 不使用 --full-auto □ 2. --print 先分析 □ 3. 不使用 $(cat) 或 $VAR □ 4. openclaw "读取文件" 让自己读 □ 5. --allow-tool 限制工具 □ 6. --deny-tool command_run □ 7. grep 检查危险输入 □ 8. Python 白名单验证 □ 9. git commit 备份 □ 10. git checkout . + clean 恢复6. 总结
- 根本原因:命令注入风险最常见原因是处理用户输入(35%)和 --full-auto 自动执行(25%)
- 最佳实践:不使用
--full-auto,使用--print先分析 - 避免 $(cat):不使用命令替换,让 OpenClaw 自己读取文件
- 限制工具:使用
--allow-tool和--deny-tool限制权限 - 最佳实践建议:使用 Git 保护,验证输入内容,不使用 --full-auto 处理不受信任输入
故障排查流程图
flowchart TD A[命令注入风险] --> B[不使用 --full-auto] B --> C[openclaw "task" 手动确认] C --> D{需要自动?} D -->|是| E[限制工具权限] D -->|否| F[✅ 安全] E --> G[--allow-tool file_edit] G --> H[--deny-tool command_run] H --> I[不使用 $(cat)] I --> j[openclaw "读取文件" 自己读] j --> K[使用 --print 先分析] K --> L[openclaw --print "分析内容"] L --> M{内容安全?} M -->|否| N[不执行,人工审查] M -->|是| O[git commit 备份] O --> P[openclaw --auto-approve "task"] P --> Q[git diff 验证] Q --> R{修改安全?} R -->|是| F R -->|否| S[git checkout . 恢复] S --> F F --> T[长期: 无 full-auto + 限制工具 + Git] T --> U[✅ 长期方案]