summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohamed Hegazy <mhegazy@anthropic.com>2026-11-05 00:52:37 +0000
committerClaude <noreply@anthropic.com>2026-11-05 00:52:37 +0000
commitf03463a7c915b426515702187e8e5e591c1e0159 (patch)
tree7ee4f1a8e636945cb2a7cf6ecd8b7b7c7a1524b6
parent2b4e52d22c949b8406481983d0f79d22a2906b42 (diff)
security-guidance: keep the native install in sessions started above the home foldermhegazy/sg-windows-cli-path
Co-authored-by: mhegazy <mhegazy@anthropic.com>
-rw-r--r--plugins/security-guidance/hooks/llm.py10
-rw-r--r--plugins/security-guidance/tests/test_windows_cli_lookup.py6
2 files changed, 12 insertions, 4 deletions
diff --git a/plugins/security-guidance/hooks/llm.py b/plugins/security-guidance/hooks/llm.py
index a9a84c07..fc8cb882 100644
--- a/plugins/security-guidance/hooks/llm.py
+++ b/plugins/security-guidance/hooks/llm.py
@@ -841,11 +841,13 @@ 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."""
+ project root, and any folder inside them (unless that root holds the home
+ folder, and with it the user's own tools)."""
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))]
+ real_home = os.path.normcase(os.path.realpath(home)).rstrip(os.sep) + os.sep
+ roots = [r.rstrip(os.sep) + os.sep for r in (os.path.normcase(os.path.realpath(d)) for d in skipped)]
+ roots = [r for r in roots if not real_home.startswith(r)]
home_bin = os.path.join(home, ".local", "bin")
for folder in [home_bin] + os.environ.get("PATH", "").split(os.pathsep):
folder = folder.strip().strip('"')
@@ -856,7 +858,7 @@ def _windows_user_cli() -> Optional[str]:
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)):
+ and not any(real.startswith(r) for r in roots)):
return cli
except OSError:
pass
diff --git a/plugins/security-guidance/tests/test_windows_cli_lookup.py b/plugins/security-guidance/tests/test_windows_cli_lookup.py
index 2d2a3541..3afea8e5 100644
--- a/plugins/security-guidance/tests/test_windows_cli_lookup.py
+++ b/plugins/security-guidance/tests/test_windows_cli_lookup.py
@@ -128,6 +128,12 @@ class TestUsersOwnCli:
set_path(monkeypatch, tmp_path / "repo-tools")
assert llm._windows_user_cli() == exe
+ def test_session_above_the_home_folder_still_finds_the_native_install(self, win, monkeypatch):
+ native = put(win.home / ".local" / "bin")
+ monkeypatch.chdir(win.home.parent)
+ monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(win.home.parent))
+ assert llm._windows_user_cli() == native
+
def test_tools_under_the_home_folder_are_found(self, win, monkeypatch):
exe = put(win.home / "AppData" / "tool")
monkeypatch.chdir(win.home)