【Bug已解决】openclaw command injection risk / Untrusted input execution — OpenClaw 命令注入风险解决方案

发布时间:2026/7/12 1:38:23
【Bug已解决】openclaw command injection risk / Untrusted input execution — OpenClaw 命令注入风险解决方案 【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. 常见问题 FAQ5.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[✅ 长期方案]