diff options
Diffstat (limited to 'plugins')
| -rwxr-xr-x | plugins/security-guidance/hooks/sg-python.sh | 21 | ||||
| -rw-r--r-- | plugins/security-guidance/tests/test_launcher_newer_python.py | 55 |
2 files changed, 74 insertions, 2 deletions
diff --git a/plugins/security-guidance/hooks/sg-python.sh b/plugins/security-guidance/hooks/sg-python.sh index f1b9d68c..a02c7d6c 100755 --- a/plugins/security-guidance/hooks/sg-python.sh +++ b/plugins/security-guidance/hooks/sg-python.sh @@ -135,7 +135,8 @@ is_sdk_compatible() { # 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. +# of these wins; the user doesn't have to PATH-prefer it. Releases newer +# than this list are pass 2b. for cmd in "python3.13" "python3.12" "python3.11" "python3.10"; do on_path "$cmd" || continue v=$(probe "$cmd") || { note_unreadable "$cmd"; continue; } @@ -165,6 +166,22 @@ for cmd in "python3" "python" "py -3"; do esac done +# Pass 2b — minor versions newer than the pass 1 list, so a machine whose +# only SDK-compatible Python is a recent release (system `python3` 3.9 next to +# Homebrew's python3.14) is not left on pass 3. Tried only when nothing +# above is compatible: an interpreter that already works is never traded for +# a newer one, which may not have the SDK or wheels for its dependencies yet. +# Oldest first for the same reason. Missing names cost no process (on_path). +# See #4907, #5024. +for cmd in "python3.14" "python3.15" "python3.16" "python3.17" "python3.18" "python3.19"; do + on_path "$cmd" || continue + v=$(probe "$cmd") || { note_unreadable "$cmd"; continue; } + v=${v%$'\r'} + if is_sdk_compatible "$v"; then + 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 @@ -237,7 +254,7 @@ if [ -n "$sg_unreadable" ]; then exit 1 fi echo "security-guidance: no working Python 3 interpreter found." >&2 -echo " tried: python3.13, python3.12, python3.11, python3.10, python3, python, py -3" >&2 +echo " tried: python3.13 to python3.10, python3, python, py -3, python3.14 to python3.19" >&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 diff --git a/plugins/security-guidance/tests/test_launcher_newer_python.py b/plugins/security-guidance/tests/test_launcher_newer_python.py new file mode 100644 index 00000000..fc152b73 --- /dev/null +++ b/plugins/security-guidance/tests/test_launcher_newer_python.py @@ -0,0 +1,55 @@ +"""sg-python.sh finds Python releases newer than its versioned list. + +The versioned names it prefers stop at python3.13. A machine whose `python3` +is below 3.10 (stock macOS: 3.9) and whose only newer interpreter is +python3.14 used to end up on 3.9, which cannot load the agent SDK, so the +commit reviewer was off without a word. See #4907, #5024. + +Interpreters are emulated with shell scripts, so these tests need a POSIX +shell. +""" +import sys + +import pytest + +from test_launcher_process_count import PYTHON, _add, _run, _starts, box # noqa: F401 + +pytestmark = pytest.mark.skipif(sys.platform == "win32", reason="POSIX shell wrappers") + + +def test_newer_release_beats_a_python3_below_3_10(box): + _add(box, "python3", PYTHON, version="3.9") + _add(box, "python3.14", PYTHON, version="3.14") + r = _run(box, "hook.py") + assert "ran:python3.14" in r.stdout, r.stdout + r.stderr + assert _starts(box) == ["python3 probe", "python3.14 probe", "python3.14 hook.py"] + + +def test_newer_release_is_found_when_it_is_the_only_python(box): + _add(box, "python3.15", PYTHON, version="3.15") + r = _run(box, "hook.py") + assert "ran:python3.15" in r.stdout, r.stdout + r.stderr + assert _starts(box) == ["python3.15 probe", "python3.15 hook.py"] + + +@pytest.mark.parametrize("working", ["python3.12", "python3", "python"]) +def test_an_interpreter_that_already_works_is_kept(box, working): + _add(box, working, PYTHON, version="3.12") + _add(box, "python3.14", PYTHON, version="3.14") + r = _run(box, "hook.py") + assert f"ran:{working}" in r.stdout, r.stdout + r.stderr + assert "python3.14 probe" not in _starts(box) + + +def test_oldest_of_the_newer_releases_is_tried_first(box): + _add(box, "python3.16", PYTHON, version="3.16") + _add(box, "python3.14", PYTHON, version="3.14") + r = _run(box, "hook.py") + assert "ran:python3.14" in r.stdout, r.stdout + r.stderr + + +def test_no_python_at_all_still_starts_nothing(box): + r = _run(box, "hook.py") + assert r.returncode == 1 + assert "python3.14" in r.stderr + assert _starts(box) == [] |
