diff options
| author | Mohamed Hegazy <mhegazy@anthropic.com> | 2026-10-14 16:56:05 +0000 |
|---|---|---|
| committer | Claude <noreply@anthropic.com> | 2026-10-19 16:29:44 +0000 |
| commit | 49484affa8f5730ec9bcfd6e25c393bc021cdd34 (patch) | |
| tree | d3a6dbed3a15f8ea358d560cab870afef007951e /plugins/security-guidance/hooks/_base.py | |
| parent | ea10b213686c90bb1984b2016937310e05336cf3 (diff) | |
security-guidance: do not install the agent SDK for reviews that are off
The SessionStart bootstrap and its PostToolUse rescue built a venv of a few
hundred MB for the agent SDK whatever the environment said, including under
SECURITY_GUIDANCE_DISABLE=1, and the rescue started the bootstrap again
every five minutes on machines whose SDK lives in that venv (#5331).
- Nothing is installed when the environment turns off every review that
loads the SDK: the kill switch, ENABLE_CODE_SECURITY_REVIEW=0, or
ENABLE_STOP_REVIEW=0 with ENABLE_COMMIT_REVIEW=0. Reported as
sdk_bootstrap 10. A disabled plugin also no longer shows the
"reviewer unavailable" notice on a Python below 3.10.
- The rescue leaves a finished install under the state dir alone instead
of starting the bootstrap for it every five minutes.
- New counts on the bootstrap's metrics row, set only when true and only
when the install exists: sdk_no_creds and sdk_no_git (the session cannot
run the reviews the install serves), sdk_venv_py (the venv was built by
another Python version than the one running the hooks, #5706). They
size follow-up changes; no behaviour depends on them.
With the reviews on, what is built and when is unchanged.
Diffstat (limited to 'plugins/security-guidance/hooks/_base.py')
| -rw-r--r-- | plugins/security-guidance/hooks/_base.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/plugins/security-guidance/hooks/_base.py b/plugins/security-guidance/hooks/_base.py index 1daa6642..6f2944be 100644 --- a/plugins/security-guidance/hooks/_base.py +++ b/plugins/security-guidance/hooks/_base.py @@ -5,6 +5,7 @@ This module exists so that ``patterns``/``session_state``/``gitutil`` can use ``debug_log`` without importing ``security_reminder_hook`` (which would be a circular import). It must stay free of any other intra-plugin imports. """ +import glob import json import os import re @@ -40,6 +41,47 @@ def state_dir(): return os.path.expanduser("~/.claude/security") +def llm_reviews_switched_off(): + """True when the environment turns off every review that calls a model: + the kill switch (SECURITY_GUIDANCE_DISABLE=1 / ENABLE_SECURITY_REMINDER=0), + ENABLE_CODE_SECURITY_REVIEW=0, or ENABLE_STOP_REVIEW=0 together with + ENABLE_COMMIT_REVIEW=0. Nothing loads the agent SDK then, so nothing + should install it. Same reading of each variable as + security_reminder_hook.py.""" + env = os.environ.get + if env("SECURITY_GUIDANCE_DISABLE", "").strip().lower() in ("1", "true", "yes", "on"): + return True + if env("ENABLE_SECURITY_REMINDER", "1") == "0": + return True + if env("ENABLE_CODE_SECURITY_REVIEW", "1") == "0": + return True + return env("ENABLE_STOP_REVIEW", "1") == "0" and env("ENABLE_COMMIT_REVIEW", "1") == "0" + + +def agent_sdk_site_dirs(state_dir): + """Directories under the state dir that ensure_agent_sdk.py installs the + agent SDK into and llm.py adds to sys.path: the venv's site-packages + (POSIX `lib/pythonX.Y/site-packages`, Windows `Lib/site-packages`) and + the flat `pip install --target` fallback.""" + venv_root = os.path.join(state_dir, "agent-sdk-venv") + return ( + glob.glob(os.path.join(venv_root, "lib", "python*", "site-packages")) + + glob.glob(os.path.join(venv_root, "Lib", "site-packages")) + + [os.path.join(state_dir, "agent-sdk-libs")] + ) + + +def agent_sdk_installed(state_dir): + """True iff one of those directories holds a finished install of the SDK: + its package and the metadata pip writes once the package is unpacked. A + file check only, no import.""" + return any( + os.path.isfile(os.path.join(d, "claude_agent_sdk", "__init__.py")) + and glob.glob(os.path.join(d, "claude_agent_sdk-*.dist-info")) + for d in agent_sdk_site_dirs(state_dir) + ) + + # Debug log file. Lives under the plugin state dir (default ~/.claude/security/) # rather than /tmp because /tmp is world-writable on multi-user hosts (TOCTOU / # symlink-attack surface, cross-user log leakage). Overridable per-process via |
