未设置 API 密钥时会执行 PATH 中解析到的 claude 程序
原文依据:4 处回退分支使用 shutil.which("claude") 查找可执行文件并直接运行。虽然参数列表固定且未使用 shell,但它仍信任当前 PATH 对程序身份的解析。
如果 PATH 被不可信安装项、项目环境或被攻陷的软件抢先放入同名程序,该程序会以运行 Skill 的用户权限执行,并收到压缩提示中的文件内容。
支持,但风险有条件。未设置 ANTHROPIC_API_KEY 时,技能通过当前 PATH 解析名为 claude 的程序并执行它;若 PATH 被其他软件或低信任目录污染,错误的同名程序可获得完整提示内容及该进程已有权限。固定参数列表且未启用 shell,降低了文件内容造成命令注入的风险,但不验证可执行文件身份。用户可限制 PATH、核对 claude 的实际位置,或要求作者固定并验证可信程序路径。
""" api_key = os.environ.get("ANTHROPIC_API_KEY") if api_key: try: import anthropic client = anthropic.Anthropic(api_key=api_key, timeout=CLAUDE_CALL_TIMEOUT_SECONDS) msg = client.messages.create( model=os.environ.get("CAVEMAN_MODEL", "claude-sonnet-4-5"), max_tokens=8192, messages=[{"role": "user", "content": prompt}], ) # Tool-heavy models can put a tool_use or thinking block first; take # the first text block instead of trusting content[0]. text = next((block.text for block in msg.content if getattr(block, "type", None) == "text"), "") return strip_llm_wrapper(text.strip()) except ImportError: pass # anthropic not installed, fall back to CLI # Fallback: use claude CLI (handles desktop auth).查看另外 3 个位置
pass # anthropic not installed, fall back to CLI # Fallback: use claude CLI (handles desktop auth). # Resolve binary via shutil.which so Windows .cmd/.bat shims (e.g. # %APPDATA%\npm\claude.CMD) work without shell=True. On POSIX, # shutil.which returns the same absolute path as the implicit lookup, # so this is a no-op there. Falls back to bare "claude" if not found # on PATH so subprocess raises a clear FileNotFoundError. claude_bin = shutil.which("claude") or "claude" try: result = subprocess.run( [ claude_bin, "--print", "--setting-sources", "", "--strict-mcp-config", ], input=prompt, text=True, Prefers the Anthropic SDK when ANTHROPIC_API_KEY is set; otherwise falls back to the ``claude --print`` CLI (which handles desktop auth). ], input=prompt, text=True, capture_output=True, check=True, encoding="utf-8", errors="replace", timeout=CLAUDE_CALL_TIMEOUT_SECONDS, )