summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/tests
diff options
context:
space:
mode:
authorMohamed Hegazy <mhegazy@anthropic.com>2026-10-12 19:27:29 +0000
committerClaude <noreply@anthropic.com>2026-10-15 22:39:39 +0000
commit5a1d326c862f33d05a9e65f30e50a71a4e71d4a5 (patch)
tree532cf1018de7e6a6880dfdc8c9175f2bfacd9c09 /plugins/security-guidance/tests
parent5ac3c91066c7ec7f36e84348316742f7dd12de6a (diff)
security-guidance: report launcher fallback and give-up as metrics
Two count-only metrics, emitted only when nonzero, so the launcher change can be checked in the field: - launcher_fallback: the hook ran, but only because the launcher started the interpreter by its real path (the names on PATH could not read the plugin files). Added to the hook's and the SessionStart bootstrap's existing metrics. - launcher_unreadable: no interpreter could read the hook script. No Python runs in that case, so the launcher prints the metrics line itself before exiting 1.
Diffstat (limited to 'plugins/security-guidance/tests')
-rw-r--r--plugins/security-guidance/tests/test_launcher_unreadable_script.py34
1 files changed, 32 insertions, 2 deletions
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)