diff options
| author | Mohamed Hegazy <8000722+mhegazy@users.noreply.github.com> | 2026-11-06 11:40:32 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-06 11:40:32 -0800 |
| commit | a3301039b319e562bb0db6b7245d85f9fd35d44b (patch) | |
| tree | f7c32d1817ed0f32cda8ed5ed9d6eb6616df4a5d /plugins/security-guidance/hooks | |
| parent | a1ecfbc7f138a0ef455088f7f28cda2d22a0f853 (diff) | |
| parent | de917f463b5dbdd924d6299e96d1b9e489339488 (diff) | |
Merge pull request #6920 from anthropics/mhegazy/sg-interp-reparse-telemetry
security-guidance: count Windows sessions whose Python is behind a link (measurement only)
Diffstat (limited to 'plugins/security-guidance/hooks')
| -rw-r--r-- | plugins/security-guidance/hooks/_base.py | 102 | ||||
| -rw-r--r-- | plugins/security-guidance/hooks/ensure_agent_sdk.py | 6 |
2 files changed, 107 insertions, 1 deletions
diff --git a/plugins/security-guidance/hooks/_base.py b/plugins/security-guidance/hooks/_base.py index d518b88e..523049c1 100644 --- a/plugins/security-guidance/hooks/_base.py +++ b/plugins/security-guidance/hooks/_base.py @@ -10,6 +10,7 @@ import json import os import re import stat +import sys import threading from datetime import datetime @@ -415,6 +416,107 @@ def launcher_direct_metric(): return {"launcher_direct": int(how)} if how in ("1", "2") else {} +# Reparse tags that redirect every open below them: symbolic links and +# junctions. App execution aliases (the entries under WindowsApps) carry +# another tag and are not counted. +_SYMLINK_TAG, _MOUNT_POINT_TAG = 0xA000000C, 0xA0000003 + + +def _is_link(path, st, readlink): + """True iff `path` (with lstat result `st`) is a symbolic link or a + junction, None when that cannot be told (Python older than 3.8 has no + reparse tag). A volume mounted at a folder has the junction tag too; its + target is the bare volume name.""" + if not getattr(st, "st_file_attributes", 0) & 0x400: # FILE_ATTRIBUTE_REPARSE_POINT + return False + tag = getattr(st, "st_reparse_tag", None) + if tag is None: + return None + if tag == _MOUNT_POINT_TAG: + try: + target = readlink(path).lstrip("\\?").rstrip("\\").lower() + except (OSError, ValueError, NotImplementedError): + return True + return not (target.startswith("volume{") and target.endswith("}")) + return tag == _SYMLINK_TAG + + +def interp_reparse_metric(executable=None, base_prefix=None, environ=None, platform=None, + lstat=None, readlink=None): + """`interp_reparse`, on Windows: whether the interpreter running this + hook is reached through a symbolic link or junction. Bits: + + 1 the path of the executable goes through one + 2 the path of the standard library (sys.base_prefix) goes through one + 4 such a path, as spelled, is under the user's AppData folders (a + uv-managed Python, for one) + 8 one of the two paths could not be checked + + 0 is sent too, so a Windows row without the key is a row that lost it, + not a "no". Measurement only, on the SessionStart row. It sizes how many + sessions start Python through such a link: inside the packaged Claude + Desktop app, Windows leaks kernel memory on every file opened that way, + and each hook starts an interpreter. See anthropics/claude-code#96870. + + No process is started: one lstat per folder on the two paths, up to the + first link. Where the link points is not followed, and an 8.3 short name + on one side reads as "not under AppData" (bit 4 missing, 1 and 2 right). + The plugin's own folders are not looked at. + + The arguments stand in for the interpreter and the system in tests. + """ + if (platform or sys.platform) != "win32": + return {} + executable = sys.executable if executable is None else executable + base_prefix = getattr(sys, "base_prefix", "") if base_prefix is None else base_prefix + environ = os.environ if environ is None else environ + lstat = lstat or os.lstat + readlink = readlink or os.readlink + + def spelled(p): + # For comparing only: lower case, no \\?\ prefix. + p = os.path.normcase(p) + return p[4:] if p.startswith("\\\\?\\") and not p.startswith("\\\\?\\unc\\") else p + + value = 0 + roots = [] + for name in ("APPDATA", "LOCALAPPDATA"): + try: + root = environ.get(name) + if root: + roots.append(spelled(os.path.abspath(root)).rstrip("\\/")) + except Exception: + pass + checked = {} + for bit, start in ((1, executable), (2, base_prefix)): + try: + if not start: + continue + p = start = os.path.abspath(start) + linked = False + while not linked: + parent = os.path.dirname(p) + if parent == p: + break + if p not in checked: + try: + checked[p] = _is_link(p, lstat(p), readlink) + except FileNotFoundError: + checked[p] = False + if checked[p] is None: + value |= 8 + linked = bool(checked[p]) + p = parent + if linked: + value |= bit + here = spelled(start) + if any(here == r or here.startswith(r + os.sep) for r in roots): + value |= 4 + except Exception: + value |= 8 + return {"interp_reparse": value} + + def _usage_metrics(): """Snapshot the accumulator as metric keys. Returns {} when no API calls AND no HTTP errors were made so skip-path emits don't burn key budget. diff --git a/plugins/security-guidance/hooks/ensure_agent_sdk.py b/plugins/security-guidance/hooks/ensure_agent_sdk.py index 80c69c9c..17e61e12 100644 --- a/plugins/security-guidance/hooks/ensure_agent_sdk.py +++ b/plugins/security-guidance/hooks/ensure_agent_sdk.py @@ -28,7 +28,8 @@ from pathlib import Path # here to match the existing local name (state_dir was already a local var in # main() and _maybe_emit_user_notice). from _base import ( - launcher_direct_metric, launcher_probe_metrics, portable_output, state_dir as _resolve_state_dir, + interp_reparse_metric, launcher_direct_metric, launcher_probe_metrics, portable_output, + state_dir as _resolve_state_dir, write_hook_output, ) from _base import llm_reviews_switched_off, make_state_dir @@ -977,6 +978,9 @@ if __name__ == "__main__": metrics["launcher_fallback"] = 1 metrics.update(launcher_probe_metrics()) metrics.update(launcher_direct_metric()) + # Last, so it is the first key a row over Claude Code's key cap (20, 10 + # in older versions) loses. + metrics.update(interp_reparse_metric()) response: dict[str, object] = {"metrics": metrics} # One-time user-visible notice when the agentic reviewer is dead on # arrival. Uses hookSpecificOutput.additionalContext (SessionStart's |
