diff options
| author | Mohamed Hegazy <mhegazy@anthropic.com> | 2026-11-09 18:55:10 +0000 |
|---|---|---|
| committer | Claude <noreply@anthropic.com> | 2026-11-09 18:55:10 +0000 |
| commit | f3700b581c4f4df3b69cba3e276b23042f1bf200 (patch) | |
| tree | fc9612a3149037fde554c67591e778a348a7d76f /plugins | |
| parent | 0412be76f50ab4f4f8abff3753efa9269fbbca5b (diff) | |
security-guidance: set an environment variable for the launcher's interpreter probesmhegazy/sg-launcher-probe-env
sg-python.sh asks a candidate interpreter for its version and, for a
packaged Windows alias, for its real path, with `python -c`. Both calls
import only os and sys, so they now run with PYTHONSAFEPATH=1, which tells
Python not to add its usual first entry to the import path.
The variable is set for those two calls alone, so the hook starts with the
same environment as before. Python older than 3.11 ignores the variable.
Co-authored-by: mhegazy <mhegazy@anthropic.com>
Diffstat (limited to 'plugins')
| -rwxr-xr-x | plugins/security-guidance/hooks/sg-python.sh | 7 | ||||
| -rw-r--r-- | plugins/security-guidance/tests/test_launcher_probe_env.py | 75 |
2 files changed, 80 insertions, 2 deletions
diff --git a/plugins/security-guidance/hooks/sg-python.sh b/plugins/security-guidance/hooks/sg-python.sh index b1a5d5b9..f2d5bd8a 100755 --- a/plugins/security-guidance/hooks/sg-python.sh +++ b/plugins/security-guidance/hooks/sg-python.sh @@ -105,7 +105,10 @@ probe() { # Only for `$(probe ...)`: the `exec` replaces that subshell instead of # forking a second process from it. [ "${BASH_SUBSHELL:-0}" -gt 0 ] || return 1 - exec "$@" -c 'import os, sys; t = (sys.argv[1:] or [""])[0]; sys.exit(3) if t and not (os.path.isfile(t) and os.access(t, os.R_OK)) else print(f"{sys.version_info[0]}.{sys.version_info[1]}"); sys.platform == "win32" and sys.version_info >= (3, 10) and print(sys.executable)' "$SG_TARGET" 2>/dev/null + # PYTHONSAFEPATH (3.11+, ignored before): the probe needs the standard + # library only, so Python is told not to add its usual first entry to the + # import path. For this call alone; the hook's environment is unchanged. + PYTHONSAFEPATH=1 exec "$@" -c 'import os, sys; t = (sys.argv[1:] or [""])[0]; sys.exit(3) if t and not (os.path.isfile(t) and os.access(t, os.R_OK)) else print(f"{sys.version_info[0]}.{sys.version_info[1]}"); sys.platform == "win32" and sys.version_info >= (3, 10) and print(sys.executable)' "$SG_TARGET" 2>/dev/null } # Use as `ask ... || { note_unreadable "<name>"; continue; }` (see ask below). @@ -409,7 +412,7 @@ done for cmd in "python3" "python" "py -3"; do case "$sg_unreadable" in *"|$cmd|"*) ;; *) continue ;; esac # shellcheck disable=SC2086 - c=$($cmd -c 'import sys; print(sys.executable)' 2>/dev/null) || continue + c=$(PYTHONSAFEPATH=1 $cmd -c 'import sys; print(sys.executable)' 2>/dev/null) || continue c=${c%$'\r'} # native Windows Python prints CRLF [ -n "$c" ] || continue if command -v cygpath >/dev/null 2>&1; then diff --git a/plugins/security-guidance/tests/test_launcher_probe_env.py b/plugins/security-guidance/tests/test_launcher_probe_env.py new file mode 100644 index 00000000..4431115b --- /dev/null +++ b/plugins/security-guidance/tests/test_launcher_probe_env.py @@ -0,0 +1,75 @@ +"""sg-python.sh runs its `python -c` probes with PYTHONSAFEPATH=1. + +The launcher asks a candidate interpreter for its version and, for a packaged +Windows alias, for its real path. Both questions need the standard library +only. The variable is set for those two calls alone, so the hook's environment +is unchanged. + +Interpreters are emulated with shell scripts, so these tests need a POSIX +shell. +""" +import os +import shutil +import subprocess +import sys + +import pytest + +from conftest import HOOKS_DIR + +pytestmark = pytest.mark.skipif(sys.platform == "win32", reason="POSIX shell wrappers") + +SHIM = HOOKS_DIR / "sg-python.sh" +BASH = shutil.which("bash") or "/bin/bash" +REAL = os.path.realpath(sys.executable) + +# Logs the variable at each start, then behaves like the real interpreter. +LOGGING = """#!{bash} +if [ "${{1:-}}" = "-c" ]; then kind=probe; else kind=hook; fi +echo "{name} $kind [${{PYTHONSAFEPATH-unset}}]" >> '{log}' +exec '{real}' "$@" +""" +# A packaged alias: cannot read the hook script, but says where its interpreter is. +ALIAS = """#!{bash} +echo "alias probe [${{PYTHONSAFEPATH-unset}}]" >> '{log}' +case "$2" in + "import sys; print(sys.executable)") echo '{own}' ;; + *) exit 3 ;; +esac +""" + + +@pytest.fixture +def box(tmp_path): + (tmp_path / "bin").mkdir() + (tmp_path / "hook.py").write_text("print('ran')\n") + return tmp_path + + +def _write(path, template, box, **kw): + path.write_text(template.format(bash=BASH, real=REAL, log=box / "log", **kw)) + path.chmod(0o755) + + +def _run(box, **env): + r = subprocess.run([BASH, str(SHIM), str(box / "hook.py")], input="", capture_output=True, + text=True, timeout=60, env={"PATH": str(box / "bin"), "HOME": "/", **env}) + assert r.returncode == 0 and r.stdout.strip() == "ran", r.stderr + return (box / "log").read_text().splitlines() + + +def test_version_probe_has_the_variable_and_the_hook_env_is_unchanged(box): + _write(box / "bin" / "python3", LOGGING, box, name="python3") + assert _run(box) == ["python3 probe [1]", "python3 hook [unset]"] + + +def test_alias_path_probe_has_the_variable(box): + (box / "own").mkdir() + _write(box / "own" / "python", LOGGING, box, name="own") + _write(box / "bin" / "python3", ALIAS, box, own=box / "own" / "python") + assert _run(box) == ["alias probe [1]", "alias probe [1]", "own probe [1]", "own hook [unset]"] + + +def test_a_value_from_outside_is_passed_on_unchanged(box): + _write(box / "bin" / "python3", LOGGING, box, name="python3") + assert _run(box, PYTHONSAFEPATH="0") == ["python3 probe [1]", "python3 hook [0]"] |
