summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xplugins/security-guidance/hooks/sg-python.sh61
-rw-r--r--plugins/security-guidance/tests/test_launcher_broken_interpreter.py220
2 files changed, 279 insertions, 2 deletions
diff --git a/plugins/security-guidance/hooks/sg-python.sh b/plugins/security-guidance/hooks/sg-python.sh
index a5aa62a0..f3dcae23 100755
--- a/plugins/security-guidance/hooks/sg-python.sh
+++ b/plugins/security-guidance/hooks/sg-python.sh
@@ -84,6 +84,14 @@ esac
# Names whose interpreter runs but cannot see the hook script ("|"-separated).
sg_unreadable=""
+# The other names that are there but cannot be used, each with why, e.g.
+# "python3 (exit 127)". sg_failed_short is for one short line: python3, python
+# and py -3, a full path by its last part, each once. sg_failed_versioned is
+# the first of the python3.NN names.
+sg_failed=""
+sg_failed_short=""
+sg_failed_versioned=""
+sg_failed_others=""
probe() {
# $1..N: the interpreter command (may be multi-word like `py -3`)
@@ -117,10 +125,41 @@ probe() {
# Use as `ask ... || { note_unreadable "<name>"; continue; }` (see ask below).
note_unreadable() {
- [ $? -eq 3 ] && sg_unreadable="$sg_unreadable|$1|"
+ local rc=$?
+ if [ "$rc" -eq 3 ]; then
+ sg_unreadable="$sg_unreadable|$1|"
+ else
+ note_failed "$1" "exit $rc"
+ fi
return 0
}
+# For the message at the end only; changes nothing about the search.
+note_failed() {
+ local entry="${1##*/} ($2)"
+ sg_failed="${sg_failed:+$sg_failed, }$1 ($2)"
+ case "$1" in
+ */*) ;;
+ python3.*)
+ [ -z "$sg_failed_versioned" ] || sg_failed_others=1
+ sg_failed_versioned="${sg_failed_versioned:-$entry}"
+ return 0 ;;
+ esac
+ case ", $sg_failed_short, " in
+ *", $entry, "*) ;;
+ *) sg_failed_short="${sg_failed_short:+$sg_failed_short, }$entry" ;;
+ esac
+}
+
+# $1 answered with $v and is not started: notes it when that is not a version,
+# as from a version manager's shim that prints its complaint and exits 0.
+note_answer() {
+ case "$v" in
+ [0-9]*.[0-9]*) ;;
+ *) note_failed "$1" "unexpected answer" ;;
+ esac
+}
+
# Where packaged aliases exist (and bash has what telling them apart needs).
sg_windows=""
case "${OSTYPE:-}" in
@@ -381,6 +420,7 @@ for cmd in "python3.13" "python3.12" "python3.11" "python3.10"; do
sg_direct "$cmd"
exec "$cmd" "$@"
fi
+ note_failed "$cmd" "unexpected answer"
done
# Pass 2 — bare interpreters, but only if SDK-compatible. Covers Linux distros
@@ -403,6 +443,7 @@ for cmd in "python3" "python" "py -3"; do
case "$v" in
[0-9]*.[0-9]*) [ -n "$sg_older" ] || sg_older="$cmd" ;;
esac
+ note_answer "$cmd"
done
# Pass 2b — minor versions newer than the pass 1 list, so a machine whose
@@ -420,6 +461,7 @@ for cmd in "python3.14" "python3.15" "python3.16" "python3.17" "python3.18" "pyt
if is_sdk_compatible "$v"; then
exec "$cmd" "$@"
fi
+ note_failed "$cmd" "unexpected answer"
done
# Pass 3 — fallback to any Python 3, even <3.10. Pattern-based checks
@@ -478,6 +520,7 @@ for cmd in "${sg_candidates[@]}"; do
case "$v" in
[0-9]*.[0-9]*) [ -n "$sg_fallback" ] || sg_fallback="$cmd" ;;
esac
+ note_answer "$cmd"
done
if [ -n "$sg_fallback" ]; then
exec "$sg_fallback" "$@"
@@ -490,15 +533,29 @@ if [ -n "$sg_unreadable" ]; then
echo " hook script: $SG_TARGET" >&2
echo " on Windows this happens when python, python3 and py are Microsoft Store or Python Install Manager aliases;" >&2
echo " install Python from https://python.org and put it ahead of ...\\Microsoft\\WindowsApps in PATH" >&2
+ [ -z "$sg_failed" ] || echo " also tried: $sg_failed" >&2
# No Python to report it, so the launcher emits the metric itself.
echo '{"metrics": {"launcher_unreadable": 1}}'
exit 1
fi
-echo "security-guidance: no working Python 3 interpreter found." >&2
+# Claude Code's terminal shows only the first line of a failed hook, so what
+# tells a broken interpreter from a missing one is on it. Not all of it: a
+# version manager's folder has a name per version.
+sg_why=""
+if [ -n "$sg_failed" ]; then
+ sg_why="${sg_failed_short:-$sg_failed_versioned}"
+ if [ -n "$sg_failed_short" ] && [ -n "$sg_failed_versioned" ]; then sg_failed_others=1; fi
+ sg_why=" On PATH but not usable: $sg_why${sg_failed_others:+, and more}."
+fi
+echo "security-guidance: no working Python 3 interpreter found.$sg_why" >&2
echo " tried: python3.13 to python3.10, python3, python, py -3, python3.14 to python3.19" >&2
+[ -z "$sg_failed" ] || echo " failed: $sg_failed" >&2
if [ -n "$sg_skipped" ]; then
echo " not started: $sg_skipped (App Installer's alias, which is not a Python)" >&2
fi
+# --version, because it is safe in any folder: -m and -c import from it.
+[ -z "$sg_failed" ] || echo " running one of the failed ones with --version in the project folder may show why" >&2
+[ -z "$sg_failed" ] || echo " if that does not help:" >&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_broken_interpreter.py b/plugins/security-guidance/tests/test_launcher_broken_interpreter.py
new file mode 100644
index 00000000..787a739b
--- /dev/null
+++ b/plugins/security-guidance/tests/test_launcher_broken_interpreter.py
@@ -0,0 +1,220 @@
+"""sg-python.sh tells a broken interpreter from a missing one.
+
+Both used to end in the same "no working Python 3 interpreter found", so a
+version manager's shim with no version set read as Python not being installed.
+What failed is on the first line, because Claude Code's terminal shows only
+that one for a failed hook. See anthropics/claude-code#86709.
+
+The interpreters are shell scripts, which Git Bash starts as well, so this
+runs on Windows too.
+"""
+import os
+import shutil
+import subprocess
+import sys
+
+import pytest
+
+from conftest import HOOKS_DIR
+
+SHIM = HOOKS_DIR / "sg-python.sh"
+BASH = shutil.which("bash") or "/bin/bash"
+try:
+ BASH_MAJOR = int(subprocess.run([BASH, "-c", "echo ${BASH_VERSINFO[0]}"], capture_output=True,
+ text=True).stdout.strip() or 0)
+except (OSError, ValueError):
+ BASH_MAJOR = 0
+FIRST = "security-guidance: no working Python 3 interpreter found."
+WHY = " On PATH but not usable: "
+
+
+@pytest.fixture
+def box(tmp_path):
+ (tmp_path / "bin").mkdir()
+ (tmp_path / "hook.py").write_text("import sys\nprint('ran', sys.stdin.read())\n")
+ return tmp_path
+
+
+def _add(folder, name, body):
+ folder.mkdir(exist_ok=True)
+ path = folder / name
+ path.write_bytes(("#!/bin/sh\n%s\n" % body).encode())
+ path.chmod(0o755)
+
+
+def _run(box, *folders, ostype=None):
+ env = {"HOME": str(box), "SECURITY_WARNINGS_STATE_DIR": str(box / "state")}
+ for name in ("SYSTEMROOT", "USERPROFILE", "TEMP"):
+ if name in os.environ:
+ env[name] = os.environ[name]
+ if ostype:
+ env["OSTYPE"] = ostype
+ # The launcher's own PATH, set inside: Git Bash adds its folders to the one it is given.
+ script = 'export PATH="$1"; for f in "${@:4}"; do PATH="$PATH:$f"; done; exec "$2" "$3" "$HOOK"'
+ env["HOOK"] = str(box / "hook.py")
+ posix = [_posix(f) for f in (folders or (box / "bin",))]
+ return subprocess.run([BASH, "-c", script, "bash", posix[0], _posix(BASH), _posix(SHIM)] + posix[1:],
+ input="payload", capture_output=True, text=True, timeout=60, env=env, cwd=box)
+
+
+def _posix(path):
+ path = str(path)
+ if sys.platform == "win32" and path[1:2] == ":":
+ path = "/" + path[0].lower() + path[2:].replace("\\", "/")
+ return path
+
+
+def test_the_first_line_names_what_failed(box):
+ _add(box / "bin", "python3", "exit 127")
+ _add(box / "bin", "python", "exit 49")
+ r = _run(box)
+ assert r.returncode == 1
+ lines = r.stderr.splitlines()
+ assert lines[0] == FIRST + WHY + "python3 (exit 127), python (exit 49)."
+ assert " failed: python3 (exit 127), python (exit 49)" in lines
+
+
+def test_the_first_line_has_python3_and_a_later_one_has_them_all(box):
+ for minor in (13, 12, 11, 10):
+ _add(box / "bin", "python3.%d" % minor, "exit 127")
+ _add(box / "bin", "python3", "exit 1")
+ lines = _run(box).stderr.splitlines()
+ assert lines[0] == FIRST + WHY + "python3 (exit 1), and more."
+ assert (" failed: python3.13 (exit 127), python3.12 (exit 127), python3.11 (exit 127), "
+ "python3.10 (exit 127), python3 (exit 1)") in lines
+
+
+@pytest.mark.parametrize("name", ["python3.12", "python3.14"])
+def test_a_versioned_name_is_named_when_it_is_all_there_is(box, name):
+ _add(box / "bin", name, "exit 1")
+ assert _run(box).stderr.splitlines()[0] == FIRST + WHY + name + " (exit 1)."
+
+
+@pytest.mark.parametrize("name", ["python3.12", "python3", "python3.14"])
+@pytest.mark.parametrize("body", ["exit 0", "echo no version is set; exit 0"])
+def test_a_name_that_exits_0_with_no_version_is_named(box, name, body):
+ _add(box / "bin", name, body)
+ r = _run(box)
+ assert r.returncode == 1
+ assert r.stderr.splitlines()[0] == FIRST + WHY + name + " (unexpected answer)."
+ assert "version is set" not in r.stderr + r.stdout
+
+
+def test_the_interpreters_own_words_are_not_repeated(box):
+ _add(box / "bin", "python3", "echo own-words >&2; echo own-words; exit 1")
+ r = _run(box)
+ assert r.returncode == 1
+ assert "own-words" not in r.stderr + r.stdout
+
+
+def test_nothing_is_named_when_nothing_is_there(box):
+ r = _run(box)
+ assert r.returncode == 1
+ assert r.stderr.splitlines()[0] == FIRST
+ assert "failed:" not in r.stderr and "--version" not in r.stderr
+
+
+@pytest.mark.parametrize("name", ["python3.12", "python3.14"])
+@pytest.mark.parametrize("answer", ["3.9", "1.2.3 is not installed"])
+def test_a_versioned_name_with_another_version_is_named(box, name, answer):
+ _add(box / "bin", name, "echo '%s'" % answer)
+ assert _run(box).stderr.splitlines()[0] == FIRST + WHY + name + " (unexpected answer)."
+
+
+def test_an_older_python3_is_still_used_and_nothing_is_said(box):
+ _add(box / "bin", "python3.12", "echo 3.9")
+ _add(box / "bin", "python3", "[ \"$1\" = -c ] && { echo 3.9; exit 0; }; echo older ran")
+ r = _run(box)
+ assert (r.returncode, r.stdout.strip(), r.stderr) == (0, "older ran", "")
+
+
+def test_a_folder_named_like_a_version_is_a_path(box):
+ _add(box / "bin", "python3", "exit 4")
+ _add(box / "python3.12", "python3", "exit 5")
+ first = _run(box, box / "bin", "python3.12").stderr.splitlines()[0]
+ assert first == FIRST + WHY + "python3 (exit 4), python3 (exit 5)."
+
+
+def test_several_versioned_names_alone(box):
+ _add(box / "bin", "python3.12", "exit 1")
+ _add(box / "bin", "python3.14", "exit 2")
+ assert _run(box).stderr.splitlines()[0] == FIRST + WHY + "python3.12 (exit 1), and more."
+
+
+def test_a_wrapper_that_talks_before_it_answers(box):
+ _add(box / "bin", "python3", "echo notice; exec '%s' \"$@\"" % _posix(sys.executable))
+ r = _run(box)
+ assert r.returncode == 1
+ assert r.stderr.splitlines()[0] == FIRST + WHY + "python3 (unexpected answer)."
+
+
+def test_the_py_launcher_is_named_with_its_option(box):
+ _add(box / "bin", "py", "exit 103")
+ lines = _run(box).stderr.splitlines()
+ assert lines[0] == FIRST + WHY + "py -3 (exit 103)."
+ assert " failed: py -3 (exit 103)" in lines
+
+
+@pytest.mark.parametrize("second", ["second", "Doe, John"])
+def test_full_paths_are_kept_off_the_first_line(box, second):
+ _add(box / "bin", "python3", "exit 1")
+ _add(box / second, "python3", "exit 2")
+ lines = _run(box, box / "bin", box / second).stderr.splitlines()
+ assert lines[0] == FIRST + WHY + "python3 (exit 1), python3 (exit 2)."
+ assert any(line.startswith(" failed: python3 (exit 1), ") and line.endswith(second + "/python3 (exit 2)")
+ for line in lines)
+
+
+def test_a_second_one_that_gives_no_version(box):
+ _add(box / "bin", "python3", "exit 1")
+ _add(box / "second", "python3", "exit 0")
+ first = _run(box, box / "bin", box / "second").stderr.splitlines()[0]
+ assert first == FIRST + WHY + "python3 (exit 1), python3 (unexpected answer)."
+
+
+def test_the_same_failure_in_two_folders_is_on_the_first_line_once(box):
+ _add(box / "bin", "python3", "exit 127")
+ _add(box / "second", "python3", "exit 127")
+ assert _run(box, box / "bin", box / "second").stderr.splitlines()[0] == FIRST + WHY + "python3 (exit 127)."
+
+
+def _skipped_alias(box, code):
+ """The launcher as Git Bash runs it: python is App Installer's alias, one more behind it."""
+ apps = box / "AppData" / "Local" / "Microsoft" / "WindowsApps"
+ target = box / "Microsoft.DesktopAppInstaller_8wekyb3d8bbwe"
+ _add(target, "AppInstallerPythonRedirector.exe", "exit 49")
+ apps.mkdir(parents=True)
+ os.symlink(target / "AppInstallerPythonRedirector.exe", apps / "python")
+ _add(box / "Doe, John", "python", "exit %d" % code)
+ return apps
+
+
+@pytest.mark.skipif(sys.platform == "win32", reason="the alias is emulated with a link")
+@pytest.mark.skipif(BASH_MAJOR < 4, reason="the launcher tells aliases apart in bash 4 and later")
+def test_a_broken_one_behind_a_skipped_alias(box):
+ apps = _skipped_alias(box, 127)
+ r = _run(box, apps, box / "Doe, John", ostype="msys")
+ lines = r.stderr.splitlines()
+ assert lines[0] == FIRST + WHY + "python (exit 127)."
+ assert any(line.startswith(" not started: python (") for line in lines)
+
+
+def test_failures_are_listed_next_to_an_interpreter_that_cannot_read_the_hook(box):
+ _add(box / "bin", "python3", "exit 3")
+ _add(box / "bin", "python", "exit 127")
+ r = _run(box)
+ assert r.returncode == 1
+ assert "cannot read the hook script" in r.stderr.splitlines()[0]
+ assert " also tried: python (exit 127)" in r.stderr.splitlines()
+ assert "launcher_unreadable" in r.stdout
+
+
+@pytest.mark.parametrize("body", ["exit 1", "exit 0", "echo 3.9"])
+def test_a_failed_name_is_not_reported_when_another_works(box, body):
+ _add(box / "bin", "python3.13", body)
+ _add(box / "bin", "python3", "exit 1")
+ _add(box / "bin", "python", "exec '%s' \"$@\"" % _posix(sys.executable))
+ r = _run(box)
+ assert r.returncode == 0, r.stderr
+ assert r.stdout.strip() == "ran payload"
+ assert r.stderr == ""