summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'plugins')
-rw-r--r--plugins/security-guidance/hooks/ensure_agent_sdk.py4
-rwxr-xr-xplugins/security-guidance/hooks/security_reminder_hook.py4
-rwxr-xr-xplugins/security-guidance/hooks/sg-python.sh7
-rw-r--r--plugins/security-guidance/tests/test_launcher_unreadable_script.py34
4 files changed, 47 insertions, 2 deletions
diff --git a/plugins/security-guidance/hooks/ensure_agent_sdk.py b/plugins/security-guidance/hooks/ensure_agent_sdk.py
index f5d016db..c37b90bb 100644
--- a/plugins/security-guidance/hooks/ensure_agent_sdk.py
+++ b/plugins/security-guidance/hooks/ensure_agent_sdk.py
@@ -845,6 +845,10 @@ if __name__ == "__main__":
pv = _plugin_version_int()
if pv:
metrics["pv"] = pv
+ # See sg-python.sh pass 4: started by real path, the PATH names could
+ # not read the plugin files.
+ if os.environ.get("SG_LAUNCHER_FALLBACK") == "1":
+ metrics["launcher_fallback"] = 1
response: dict[str, object] = {"metrics": metrics}
# One-time user-visible notice when the agentic reviewer is dead on
# arrival. Uses hookSpecificOutput.additionalContext (SessionStart's
diff --git a/plugins/security-guidance/hooks/security_reminder_hook.py b/plugins/security-guidance/hooks/security_reminder_hook.py
index e8df7a1c..c5806146 100755
--- a/plugins/security-guidance/hooks/security_reminder_hook.py
+++ b/plugins/security-guidance/hooks/security_reminder_hook.py
@@ -270,6 +270,10 @@ def emit_metrics(
head = {}
if _PV and "pv" not in metrics:
head["pv"] = _PV
+ # sg-python.sh could only start this interpreter by its real path: the
+ # names on PATH were launcher aliases that cannot read the plugin files.
+ if os.environ.get("SG_LAUNCHER_FALLBACK") == "1":
+ head["launcher_fallback"] = 1
head.update(_usage_metrics())
if head:
metrics = {**head, **metrics}
diff --git a/plugins/security-guidance/hooks/sg-python.sh b/plugins/security-guidance/hooks/sg-python.sh
index 4dee3106..7160baa2 100755
--- a/plugins/security-guidance/hooks/sg-python.sh
+++ b/plugins/security-guidance/hooks/sg-python.sh
@@ -33,6 +33,10 @@ set -e
# changing it from inside the interpreter has no effect.
export PYTHONUTF8=1
+# Set to 1 below only when the interpreter had to be found by its real path
+# (pass 4); the hook reports it as the `launcher_fallback` metric.
+unset SG_LAUNCHER_FALLBACK
+
# Git Bash / MSYS on Windows hands script paths to this shim in POSIX form
# (`/c/Users/...`). When we exec a Windows `python.exe` (which we do on
# Windows since `python3` is the Microsoft Store stub), python interprets the
@@ -173,6 +177,7 @@ for cmd in "python3" "python" "py -3"; do
fi
sg_candidates+=("$c")
done
+export SG_LAUNCHER_FALLBACK=1
sg_fallback=""
sg_tried="|"
for cmd in "${sg_candidates[@]}"; do
@@ -198,6 +203,8 @@ 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
+ # 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
diff --git a/plugins/security-guidance/tests/test_launcher_unreadable_script.py b/plugins/security-guidance/tests/test_launcher_unreadable_script.py
index e3780404..d963eaa0 100644
--- a/plugins/security-guidance/tests/test_launcher_unreadable_script.py
+++ b/plugins/security-guidance/tests/test_launcher_unreadable_script.py
@@ -16,7 +16,7 @@ import sys
import pytest
-from conftest import HOOKS_DIR
+from conftest import HOOKS_DIR, metrics_of, run_hook, stop_payload
pytestmark = pytest.mark.skipif(sys.platform == "win32", reason="POSIX shell wrappers")
@@ -25,7 +25,11 @@ BASH = shutil.which("bash") or "/bin/bash"
REAL = os.path.realpath(sys.executable)
# Echoes its stdin and exits 2, like a hook that has findings to report.
-HOOK = "import sys\nsys.stderr.write('ran:' + sys.stdin.read())\nsys.exit(2)\n"
+HOOK = (
+ "import os, sys\n"
+ "print('fallback=' + os.environ.get('SG_LAUNCHER_FALLBACK', ''))\n"
+ "sys.stderr.write('ran:' + sys.stdin.read())\nsys.exit(2)\n"
+)
ALIAS = """#!{bash}
# A packaged alias: `-c` works, files under HIDE are invisible.
@@ -78,6 +82,18 @@ def test_alias_is_replaced_by_the_interpreter_it_reports(plugin, tmp_path):
# The hook ran, read its payload, and its own exit code came through.
assert r.stderr == 'ran:{"k": 1}'
assert r.returncode == 2
+ assert r.stdout.strip() == "fallback=1"
+
+
+def test_regular_interpreter_is_not_marked_as_fallback(plugin, tmp_path):
+ (tmp_path / "real").mkdir()
+ os.symlink(REAL, tmp_path / "real" / "python3")
+ r = subprocess.run(
+ [BASH, str(SHIM), str(plugin / "hook.py")], input="{}", capture_output=True, text=True, timeout=60,
+ env={"PATH": str(tmp_path / "real"), "HOME": "/", "SG_LAUNCHER_FALLBACK": "1"},
+ )
+ assert r.returncode == 2
+ assert r.stdout.strip() == "fallback="
def test_regular_install_later_on_path_is_used(plugin, tmp_path):
@@ -106,6 +122,7 @@ def test_no_interpreter_can_see_the_script_is_not_reported_as_findings(plugin, t
assert "can't open file" not in r.stderr
assert "cannot read the hook script" in r.stderr
assert str(plugin / "hook.py") in r.stderr
+ assert metrics_of(r.stdout) == {"launcher_unreadable": 1}
def test_missing_script_is_not_reported_as_findings(plugin, tmp_path):
@@ -160,3 +177,16 @@ def test_real_hook_runs_through_an_alias_only_path(tmp_path):
)
assert r.returncode == 0, r.stderr
assert "hook_event=UserPromptSubmit" in (tmp_path / "debug.log").read_text()
+
+
+@pytest.mark.parametrize("flag", ["1", None])
+def test_hook_reports_launcher_fallback(workspace, hook_env, flag):
+ env = dict(hook_env)
+ env.pop("SG_LAUNCHER_FALLBACK", None)
+ if flag:
+ env["SG_LAUNCHER_FALLBACK"] = flag
+ rc, out, _ = run_hook(stop_payload(workspace), env)
+ assert rc == 0
+ m = metrics_of(out)
+ assert m is not None
+ assert m.get("launcher_fallback") == (1 if flag else None)