diff options
Diffstat (limited to 'plugins/security-guidance/hooks/sg-python.sh')
| -rwxr-xr-x | plugins/security-guidance/hooks/sg-python.sh | 54 |
1 files changed, 49 insertions, 5 deletions
diff --git a/plugins/security-guidance/hooks/sg-python.sh b/plugins/security-guidance/hooks/sg-python.sh index e6ac7a80..67119105 100755 --- a/plugins/security-guidance/hooks/sg-python.sh +++ b/plugins/security-guidance/hooks/sg-python.sh @@ -47,21 +47,65 @@ fi probe() { # $1..N: the interpreter command (may be multi-word like `py -3`) - # Probe writes the major version to stdout and exits 0 iff it's >=3. - "$@" -c 'import sys; print(sys.version_info[0])' 2>/dev/null + # Writes "<major>.<minor>" to stdout and exits 0 iff at least Python 3. + "$@" -c 'import sys; print(f"{sys.version_info[0]}.{sys.version_info[1]}")' 2>/dev/null } +# True iff arg is a "M.m" version string >= 3.10. claude_agent_sdk requires +# Python >= 3.10; below that, pip install fails ("No matching distribution") +# and the LLM-powered review (Stop / commit / push) silently no-ops while +# pattern checks (PostToolUse regex) keep working. macOS ships 3.9.6 as the +# default `python3` on current versions, so this guard matters in practice. +# See anthropics/claude-plugins-official#2071. +is_sdk_compatible() { + case "$1" in + 3.1[0-9]|3.[2-9][0-9]|[4-9].*|[1-9][0-9].*) return 0 ;; + *) return 1 ;; + esac +} + +# Pass 1 — try minor-versioned binaries in descending order. These are only +# present if the user explicitly installed them (Homebrew / python.org / pyenv), +# so picking one here always upgrades over the system `python3`. Highest +# available wins; the user doesn't have to PATH-prefer it. +for cmd in "python3.13" "python3.12" "python3.11" "python3.10"; do + v=$(probe "$cmd") || continue + if is_sdk_compatible "$v"; then + exec "$cmd" "$@" + fi +done + +# Pass 2 — bare interpreters, but only if SDK-compatible. Covers Linux distros +# that ship 3.10+ as the default `python3`, and Windows where `python` / +# `py -3` resolves to the user's python.org install. for cmd in "python3" "python" "py -3"; do - # Word-split intentionally so `py -3` works # shellcheck disable=SC2086 v=$(probe $cmd) || continue - if [ "$v" = "3" ]; then + if is_sdk_compatible "$v"; then # shellcheck disable=SC2086 exec $cmd "$@" fi done +# Pass 3 — fallback to any Python 3, even <3.10. Pattern-based checks +# (PostToolUse regex on Edit/Write) only need 3.6+ and are useful on their +# own; the SDK-dependent paths will detect the version mismatch and degrade +# inside the Python code. Without this fallback, the entire plugin would +# stop working on default macOS, which is a regression vs today. +for cmd in "python3" "python" "py -3"; do + # shellcheck disable=SC2086 + v=$(probe $cmd) || continue + # Accept anything that successfully reported a "M.m" string. + case "$v" in + [0-9]*.[0-9]*) + # shellcheck disable=SC2086 + exec $cmd "$@" + ;; + esac +done + echo "security-guidance: no working Python 3 interpreter found." >&2 -echo " tried: python3, python, py -3" >&2 +echo " tried: python3.13, python3.12, python3.11, python3.10, python3, python, py -3" >&2 echo " on Windows, install Python from https://python.org (NOT the Microsoft Store)" >&2 +echo " on macOS, install Python 3.10+ via Homebrew (\`brew install python\`)" >&2 exit 1 |
