summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/hooks/sg-python.sh
AgeCommit message (Collapse)Author
13 dayssecurity-guidance: set an environment variable for the launcher's ↵mhegazy/sg-launcher-probe-envMohamed Hegazy
interpreter probes 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>
2026-10-27security-guidance: stop starting the packaged Python alias on every hookmhegazy/sg-launcher-alias-real-pathmhegazy
On Windows with the Python Install Manager, python, python3 and py are packaged aliases under ...\Microsoft\WindowsApps, and each start of one makes Windows run an App Installer update check for the package. The launcher started the chosen alias twice per hook (probe, then the hook), on every Edit, prompt and Stop. See anthropics/claude-code#98929. When the name that answers is such an alias and its interpreter is a regular python*.exe elsewhere, the launcher now checks that interpreter once (same version, reads the hook script when started by itself), runs the hook with it, and keeps its path in the state folder for a day. Later hooks start it directly: one process, none through the alias. The remembered path is used only while PATH and the hook folder are unchanged and the file is still there. A hook that fails on it empties the entry so the next hook searches again. Not used while VIRTUAL_ENV is set. Other systems, regular installs and Microsoft Store Python are unchanged. Hooks report launcher_direct (1 remembered, 2 just checked) as the last metric key on rows they already send.
2026-10-20Merge branch 'main' into mhegazy/sg-launcher-probe-timingmhegazy/sg-launcher-probe-timingClaude
2026-10-19security-guidance: report how long the launcher waits for interpreter probesMohamed Hegazy
sg-python.sh asks each candidate interpreter for its version before running a hook, and waits as long as that takes. Nothing recorded how long. The launcher now times each probe with bash builtins (no extra process) and passes the result to the hook, which reports two keys when the probes of one launch took a second or more together: - launcher_probe_ms: all probes of this launch - launcher_probe_max_ms: the slowest one The keys go last on a row, so they never displace an existing key. Measurement only: the launcher still waits for every probe and picks the same interpreter.
2026-10-19security-guidance: find Python releases newer than the launcher's listmhegazy/sg-launcher-newer-pythonMohamed Hegazy
sg-python.sh prefers the versioned names python3.13 to python3.10, then python3 / python / py -3 when they are 3.10 or newer. A machine whose `python3` is older (stock macOS: 3.9) and whose only newer interpreter is python3.14 ended up on 3.9, which cannot load the agent SDK, so the commit reviewer was off with no sign that an installed Python had been missed (#4907, #5024). - When none of the existing candidates is 3.10 or newer, try python3.14 to python3.19, oldest first, before settling for an older Python. - An interpreter that is picked today is still picked: the newer names are only reached where the launcher used to fall back to a Python below 3.10 or find none. Names not on PATH cost no process.
2026-10-15security-guidance: start fewer processes in the hook launcherMohamed Hegazy
sg-python.sh runs on every hook, and under Git Bash each command substitution is a new Windows process, whether or not the interpreter it probes exists (#6470). On a machine with only `python` it started 13 processes before the hook; it now starts 1. - Skip interpreter names that are not on PATH (a builtin lookup) before probing them. - The probe execs the interpreter in its subshell instead of forking again from it. - Remember the first Python below 3.10 seen in pass 2 instead of probing the same names a second time in pass 3. - Convert drive paths (/c/Users/...) to Windows form in-shell under Git Bash / MSYS2; mount points and Cygwin still go through cygpath. Interpreter order and the choice made are unchanged.
2026-10-15security-guidance: report launcher fallback and give-up as metricsMohamed Hegazy
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.
2026-10-15security-guidance: never run a hook with a Python that cannot read itMohamed Hegazy
On Windows, python/python3/py can be packaged launcher aliases (Microsoft Store Python, Python Install Manager). Started through the alias, the interpreter answers `-c` but has a different view of %APPDATA% and cannot see a plugin staged there. sg-python.sh picked it, CPython failed with "can't open file ... [Errno 2]" and exit code 2, and exit 2 from the asyncRewake Stop hook was delivered to the model as review findings. The model's reply ran the Stop hook again, so the session looped without ever reviewing anything (#6251, #5781). The launcher's probe now also checks that the interpreter can read the hook script. When no name on PATH qualifies, it tries the other python3/python entries on PATH by full path and the real executable each alias reports. If nothing can read the script it says so and exits 1, so a launch failure is never reported as findings.
2026-05-30security-guidance: purge text=True from subprocess.run + bake PYTHONUTF8=1 ↵fix-2099-text-true-pythonutf8Mohamed Hegazy
(#2099) URGENT WINDOWS FIX. Sibling of #2056 / PR #2075 but covering 14 more sites that PR #2075 missed. The bug class: on Windows with cp1252 default encoding (typical en-US locale), `subprocess.run(..., text=True)` decodes child stdout AND stderr via `locale.getpreferredencoding()`. When git emits a UTF-8 byte that's undefined in cp1252 (e.g. `0x81` from ف, present in any path/filename/branch ref/commit message containing Arabic/Hebrew/CJK), Python's internal `_readerthread` raises UnicodeDecodeError. The thread crash is silent in Python 3.13+ (only printed to stderr), but `subprocess.run` returns `stdout=None` and the caller AttributeErrors on `.strip()`. The user sees a misleading "WinError 267" or similar catch-all message instead of the real decode failure. PR #2075 fixed 6 specific helpers in `diffstate.py` / `gitutil.py`. This commit covers the 14 survivors. Plus a defense-in-depth belt: `PYTHONUTF8=1` exported by sg-python.sh. This commit: 1. sg-python.sh: `export PYTHONUTF8=1` (PEP 540). No-op on macOS/Linux (already UTF-8). On Windows, makes Python's `locale.getpreferredencoding()` return UTF-8 instead of cp1252 — so even if a future regression slips in text=True, the decode succeeds. Must be set BEFORE Python starts; changing it from inside the interpreter has no effect. 2. gitutil.py: convert 8 subprocess.run sites from `capture_output=True, text=True` to `capture_output=True` + manual `r.stdout.decode("utf-8", errors="replace")`: - _git_rev_parse_head (stdout = SHA, stderr risk) - _find_git_index (stdout = PATH, primary bug site) - _temp_index git add (returncode only, stderr risk) - _git_toplevel (stdout = PATH, primary bug site) - _git_dir (stdout = PATH, primary bug site) - _git_rev_list_range (stdout = SHAs, stderr risk) - _detect_main_branch (stdout = ref, stderr risk) - merge-base --is-ancestor (returncode only, stderr risk) 3. security_reminder_hook.py: convert 6 subprocess.run sites (rev-parse @{u}/@{u}@{1}/local_ref, merge-base, HEAD lookup, reflog SHA resolution) — same pattern. 4. security_reminder_hook.py: fix the misleading log line in handle_user_prompt_submit. Was: debug_log("Failed to capture git baseline (not a git repo?)") Now includes the cwd in the message so the next reporter doesn't waste an hour grepping for the real WinError, per reporter's secondary finding. Verified locally on macOS Python 3.13: - py_compile clean on all modified files. - bash -n sg-python.sh clean. - sg-python.sh actually propagates PYTHONUTF8=1 to child Python (verified via probe — sys.flags.utf8_mode=1). - Existing 353 tests still pass — 0 regression. - 25 new tests in test_2099_subprocess_text_true.py: * 10 static-shape catchers (one per hooks/*.py file). Any future PR that reintroduces text=True OR encoding= in subprocess.run fails this check at PR time. Single source of truth for the regression class. * 2 sg-python.sh verifiers (literal export + actual propagation to child Python). * 5 macOS end-to-end against a real git repo containing non-cp1252 content (`ف.py` filename): _git_toplevel, _git_dir, _find_git_index, _git_rev_parse_head, _git_rev_list_range all return clean values without AttributeError / UnicodeDecodeError. * 7 round-trip bytes-decode pattern verifiers (parametrized over Arabic ف, Hebrew א, Japanese 案, raw 0x81, multiple cp1252-undefined bytes, real-world git diff headers). * 1 sanity check that cp1252 strict DOES raise on 0x81 (proves the test environment can catch the bug class). - Full suite: 378/378 pass in 5.56s. - End-to-end tmux smoke test driving real claude 2.1.145 CLI: Made a git commit via Bash tool call. All 4 hooks fired through the fixed plugin path: 11:28:16.730 Hook called with args: …/plugin/hooks/security_reminder_hook.py 11:28:16.734 Processing: hook_event=UserPromptSubmit 11:28:16.825 Captured git baseline: 445f7f213256 11:28:19.923 Hook called with args: … 11:28:19.923 Processing: hook_event=PostToolUse, tool=Bash 11:28:19.971 Commit review: detected git commit in command 11:28:20.020 Commit review: 1/1 sha(s) resolved, 1 files 11:28:26.415 Hook called with args: … 11:28:26.416 Processing: hook_event=Stop 11:28:26.550 Stop hook: empty review set Confirms: PYTHONUTF8=1 export doesn't break anything; converted helpers (_git_rev_parse_head, _git_toplevel, _git_dir, _find_git_index) run end-to-end without issue on the happy path. NOT verified end-to-end on Windows with actual non-cp1252 content in path/filename/stderr. The static-shape catcher pins the regression class permanently. Reporter's PYTHONUTF8=1 workaround empirically proves the encoding-mode fix works for the affected scenario; this commit just bakes it in. Closes #2099. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-28security-guidance: enable LLM review on default macOS Python 3.9fix-2071-macos-python-39Mohamed Hegazy
Fixes anthropics/claude-plugins-official#2071 — on macOS where the default `python3` is Apple's Command Line Tools Python 3.9.6, the plugin's agentic commit reviewer silently does not run, even when the user has a newer Python installed. Three compounding factors in the bug: 1. `sg-python.sh` only checks the major version (`3`), so it always picks 3.9 even when 3.10+ is on PATH. 2. `claude_agent_sdk` requires Python >=3.10 — pip install on 3.9 returns "No matching distribution" -> bootstrap returns BUILD_FAILED. 3. Even with a hand-built 3.12 venv, `llm.py` imports the SDK in-process into the hook's interpreter (still 3.9), which raises SyntaxError. The existing venv-probe in `ensure_agent_sdk.py` uses the venv's own Python (3.12) so it reports NOOP_VENV (healthy) while the consumer fails — misleading telemetry on top of silent feature degradation. Per BQ telemetry, 14,073 external macOS users hit sdk_bootstrap=BUILD_FAILED in the past 4 days (the default-macOS cohort), out of ~86K total external installed users. Combined with ~20K other users in similar broken-bootstrap states (Windows pre-#2055, Linux <3.10), about half the installed base has a silently-broken agentic reviewer. This PR implements the reporter's items #1, #3, and #4. Item #2 (running the SDK out-of-process) is deferred as a bigger refactor. Item #1 — hooks/sg-python.sh — prefer >=3.10 binaries via 3-pass probe: Pass 1: python3.13 / 3.12 / 3.11 / 3.10 (>=3.10 by name, highest wins) Pass 2: bare python3 / python / py -3 (accept only if reported >=3.10) Pass 3: bare python3 / python / py -3 (any Python 3, FALLBACK so pattern checks still work on macOS-default 3.9 — no regression vs today; SDK-dependent paths detect the version mismatch inside Python and degrade cleanly via item #4) Item #4 — ensure_agent_sdk.py — health-check honesty: Added HOOK_PY_INCOMPATIBLE=6 outcome with short-circuit at top of main(): if sys.version_info < (3, 10): return HOOK_PY_INCOMPATIBLE, "hook_py", f"py_{...}" Telemetry consequences after rollout: sdk_bootstrap=6 is a new clean bucket; some users currently miscounted in sdk_bootstrap=3 BUILD_FAILED (wasted pip cycles) and sdk_bootstrap=1 NOOP_VENV (falsely-healthy) move to sdk_bootstrap=6. The remaining NOOP_VENV count becomes trustworthy. Item #3 — ensure_agent_sdk.py — one-time user-visible notice: When outcome == HOOK_PY_INCOMPATIBLE and a marker file at `~/.claude/security/.agentic_unavailable_notice_v<pv>` doesn't exist, the SessionStart response includes hookSpecificOutput.additionalContext + systemMessage explaining the situation. Marker file is plugin- version-keyed so a future fix (e.g. shipping out-of-process SDK) can bump pv and re-notify users. BUILD_FAILED is intentionally excluded from the notice — it covers transient causes where a permanent banner would mislead. Verified locally on macOS Python 3.13: - py_compile clean on both files. - Existing 45-test smoke + extensibility suite: 45/45 PASS in 2.50s. - Unit test of simulated 3.9 path: HOOK_PY_INCOMPATIBLE returned with correct phase/kind; notice shown on first call, suppressed on second, reshown on bumped pv; BUILD_FAILED correctly does NOT trigger notice. NOT verified: actual Python 3.9 behavior end-to-end (would need a 3.9 install). Worth a follow-up smoke test in a 3.9 venv before next release. The unit test simulating 3.9 covers the logic but not the runtime invocation through the shim. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-27Convert POSIX script paths to Windows form before exec on Git Bashfix-2043-windows-posix-pathMohamed Hegazy
Fixes #2043. On Git Bash for Windows, Claude Code hands script paths to the shim in POSIX form (`/c/Users/...`). We exec a Windows `python.exe` (the `python3` Microsoft Store stub fails the probe), and Windows Python interprets the leading `/` as the root of the current drive — `/c/...` becomes `C:\c\Users\...` or `D:\c\Users\...` depending on which drive the shell happens to be on, fails with ENOENT, and every Edit/Write/MultiEdit blocks until the session restarts. Convert absolute path args via `cygpath -w` (a Git Bash builtin) before exec. Guarded by `command -v cygpath` so macOS/Linux fall straight through unchanged; `cygpath -w` is idempotent on already-Windows paths so the rare mixed-form case is safe. Only `/*` paths are converted — Windows-form paths reaching the shim are already openable by python.exe. Verified locally: - cygpath absent on macOS → guard skips → POSIX behavior unchanged - end-to-end shim invocation with a POSIX path on macOS exits 0 - stubbed cygpath -w on /c/Users/test/hook.py produces C:\Users\test\hook.py Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-26Update security-guidance pluginsecurity-guidance-updateMohamed Hegazy