summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/hooks
diff options
context:
space:
mode:
authorMohamed Hegazy <mhegazy@anthropic.com>2026-11-04 19:48:01 +0000
committerClaude <noreply@anthropic.com>2026-11-04 19:48:01 +0000
commitd39821e607bb403b432242425b959ce1ea53ad06 (patch)
treee503d8fe49a37699d535747b1e5487b01d343690 /plugins/security-guidance/hooks
parentfa4eb54397ee2faccb6940512a19c017cb1337f3 (diff)
security-guidance: on Windows, name the CLI for the Agent SDK by its full path
On Windows the plugin named no CLI for the Agent SDK unless SG_AGENTIC_CLI_PATH was set, so the SDK chose one. With none named, the plugin now chooses there itself and passes a full path: the CLI bundled with the SDK, else the claude.exe in the native installer's folder, else a claude.exe on PATH. When there is none, it passes the bundled path and the start fails. Folders that are not given as full paths are skipped, and so are the working directory, the project root and folders inside them. The macOS and Linux install paths are no longer probed on Windows. The hooks also set NoDefaultCurrentDirectoryInExePath on Windows when it is absent. Co-authored-by: mhegazy <mhegazy@anthropic.com>
Diffstat (limited to 'plugins/security-guidance/hooks')
-rw-r--r--plugins/security-guidance/hooks/_base.py6
-rw-r--r--plugins/security-guidance/hooks/llm.py78
2 files changed, 72 insertions, 12 deletions
diff --git a/plugins/security-guidance/hooks/_base.py b/plugins/security-guidance/hooks/_base.py
index 28b189a4..ccc3dc1d 100644
--- a/plugins/security-guidance/hooks/_base.py
+++ b/plugins/security-guidance/hooks/_base.py
@@ -12,6 +12,12 @@ import re
import threading
from datetime import datetime
+if os.name == "nt":
+ # Unless this variable exists, Windows looks for a program that is started
+ # by name in the working directory before PATH.
+ os.environ.setdefault("NoDefaultCurrentDirectoryInExePath", "1")
+
+
def state_dir():
"""Return the absolute path of the plugin's state directory.
diff --git a/plugins/security-guidance/hooks/llm.py b/plugins/security-guidance/hooks/llm.py
index 468d9e6d..a9a84c07 100644
--- a/plugins/security-guidance/hooks/llm.py
+++ b/plugins/security-guidance/hooks/llm.py
@@ -805,11 +805,12 @@ _gateway_check_unanswered = False
# depend on the model, so the Sonnet attempt is not made after it.
_gateway_review_timed_out = False
+_WINDOWS = os.name == "nt"
+
def _user_cli_path() -> Optional[str]:
- """The user's own `claude`: SG_AGENTIC_CLI_PATH if set ("" selects the
- SDK's bundled CLI), else the first of the absolute paths below that
- exists, else None."""
+ """The user's own `claude`: SG_AGENTIC_CLI_PATH if set ("" names none), else
+ the first of the absolute paths below that exists, else None."""
cli_path = os.environ.get("SG_AGENTIC_CLI_PATH")
if cli_path is not None:
return cli_path
@@ -821,18 +822,69 @@ def _user_cli_path() -> Optional[str]:
# user's (potentially attacker-supplied) repo, and Windows shutil.which
# searches cwd first — a checked-in ./claude.exe would get spawned.
# Absolute-path probes only.
- for p in (
- os.environ.get("CLAUDE_CODE_EXECPATH"),
- os.path.expanduser("~/.local/bin/claude"),
- "/root/.local/bin/claude",
- # Claude Code Remote container install path.
- "/opt/claude-code/bin/claude",
- ):
+ probes = [os.environ.get("CLAUDE_CODE_EXECPATH")]
+ if not _WINDOWS:
+ # Where Claude Code is installed on macOS and Linux.
+ probes += [
+ os.path.expanduser("~/.local/bin/claude"),
+ "/root/.local/bin/claude",
+ # Claude Code Remote container install path.
+ "/opt/claude-code/bin/claude",
+ ]
+ for p in probes:
if p and os.path.isfile(p):
return p
return None
+def _windows_user_cli() -> Optional[str]:
+ """Windows: the full path of the user's claude.exe, from the native
+ installer's folder, else from PATH; None when neither has one. Skips any
+ folder that is not given as a full path, and the working directory and
+ project root, and any folder inside them, unless that root is the home folder."""
+ skipped = [d for d in (os.getcwd(), os.environ.get("CLAUDE_PROJECT_DIR")) if d and os.path.isdir(d)]
+ home = os.path.expanduser("~")
+ roots = [os.path.normcase(os.path.realpath(d)) for d in skipped
+ if os.path.normcase(os.path.realpath(d)) != os.path.normcase(os.path.realpath(home))]
+ home_bin = os.path.join(home, ".local", "bin")
+ for folder in [home_bin] + os.environ.get("PATH", "").split(os.pathsep):
+ folder = folder.strip().strip('"')
+ cli = os.path.join(folder, "claude.exe")
+ try:
+ real = os.path.normcase(os.path.realpath(folder))
+ # A full path is one that abspath leaves as it is.
+ if (os.path.normcase(os.path.abspath(folder)) == os.path.normcase(os.path.normpath(folder))
+ and os.path.isfile(cli)
+ and not any(os.path.samefile(folder, d) for d in skipped)
+ and not any(real.startswith(r.rstrip(os.sep) + os.sep) for r in roots)):
+ return cli
+ except OSError:
+ pass
+ return None
+
+
+def _cli_for_sdk(cli_path: Optional[str]) -> Optional[str]:
+ """What to pass the Agent SDK as `cli_path`: a given one as it is. With none
+ given, the SDK chooses, except on Windows. There this names the CLI bundled
+ with the SDK, else the user's own, by its full path, and when neither exists
+ the bundled path anyway, so that the start fails."""
+ if cli_path or not _WINDOWS:
+ return cli_path or None
+ # When the SDK module has no file, any full path at which nothing exists will do.
+ sdk_file = getattr(sys.modules.get("claude_agent_sdk"), "__file__", None) or __file__
+ bundled = os.path.join(os.path.dirname(os.path.abspath(sdk_file)), "_bundled", "claude.exe")
+ if os.path.isfile(bundled):
+ return bundled
+ cli_path = _windows_user_cli()
+ if not cli_path:
+ debug_log("no Claude Code for the Agent SDK: none is bundled with it, and there is no claude.exe "
+ "in ~\\.local\\bin or on PATH outside this project (SG_AGENTIC_CLI_PATH can name one)")
+ return bundled
+ debug_log(f"the Agent SDK has no bundled Claude Code; using {cli_path}")
+ _tolerate_unknown_sdk_messages()
+ return cli_path
+
+
def _tolerate_unknown_sdk_messages() -> None:
"""Make the SDK's message parser pass unknown message types through
instead of raising: a CLI newer than the SDK emits types the SDK does not
@@ -980,7 +1032,7 @@ def _call_claude_via_sdk(prompt, output_schema, *, max_tokens=16000, model=None,
if cli_path:
_tolerate_unknown_sdk_messages()
else:
- cli_path = os.environ.get("SG_AGENTIC_CLI_PATH") or None
+ cli_path = _cli_for_sdk(os.environ.get("SG_AGENTIC_CLI_PATH") or None)
default_model = default_review_model()
chosen_model = model or default_model
@@ -2009,7 +2061,7 @@ def _agentic_review(
"the findings list."
)
- # Always prefer the user's installed `claude` over the SDK's bundled CLI.
+ # Prefer the `claude` that _user_cli_path finds over the SDK's bundled CLI.
# The bundled CLI is whatever shipped with the pip-installed SDK version
# and can lag the user's CLI by months — protocol skew between them is a
# top cause of agentic_fallback=2 in production (the SDK reads
@@ -2019,6 +2071,8 @@ def _agentic_review(
cli_path = _gateway_cli or _user_cli_path()
if cli_path:
_tolerate_unknown_sdk_messages()
+ else:
+ cli_path = _cli_for_sdk(cli_path)
# Without a callback the inner CLI writes to this hook's stderr, which
# Claude Code shows Claude as the review notice. Log it instead.