summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/hooks/_base.py
diff options
context:
space:
mode:
authorClaude <noreply@anthropic.com>2026-11-05 22:33:05 +0000
committerClaude <noreply@anthropic.com>2026-11-05 22:33:05 +0000
commit64e2f0cbf3f9c076a2709428e92fdb77e4da6f04 (patch)
tree4fcf784090d5f510f152e8f528642efcee751e62 /plugins/security-guidance/hooks/_base.py
parent2dd1569edd65f7f41cb04790dec1780cbc1e5511 (diff)
parentd189be9fc7a63d53d4e2af59accdd70e933e41e9 (diff)
Merge main into mhegazy/sg-index-copy-all-gitlinksmhegazy/sg-index-copy-all-gitlinks
Co-authored-by: mhegazy <mhegazy@anthropic.com>
Diffstat (limited to 'plugins/security-guidance/hooks/_base.py')
-rw-r--r--plugins/security-guidance/hooks/_base.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/plugins/security-guidance/hooks/_base.py b/plugins/security-guidance/hooks/_base.py
index 28b189a4..d518b88e 100644
--- a/plugins/security-guidance/hooks/_base.py
+++ b/plugins/security-guidance/hooks/_base.py
@@ -9,9 +9,16 @@ import glob
import json
import os
import re
+import stat
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.
@@ -42,6 +49,26 @@ def state_dir():
return os.path.abspath(os.path.expanduser(d))
+def make_state_dir(d=None):
+ """Create the state dir (or `d`) with access for its user alone, and
+ narrow the default one to that if it is already there, so a umask such as
+ 002 does not leave the reviewer's venv, logs and session state open to the
+ rest of a shared group. A folder named by SECURITY_WARNINGS_STATE_DIR
+ that already exists is the user's to set. Raises OSError like os.makedirs."""
+ if d is None:
+ d = state_dir()
+ os.makedirs(d, mode=0o700, exist_ok=True)
+ if (os.name != "nt" and not os.environ.get("SECURITY_WARNINGS_STATE_DIR", "").strip()
+ and os.path.abspath(d) == state_dir()):
+ try:
+ st = os.lstat(d)
+ if stat.S_ISDIR(st.st_mode) and st.st_uid == os.getuid() and st.st_mode & 0o077:
+ os.chmod(d, 0o700)
+ except OSError:
+ pass
+ return d
+
+
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),
@@ -378,6 +405,16 @@ def launcher_probe_metrics():
return {"launcher_probe_ms": min(total, cap), "launcher_probe_max_ms": min(slowest, cap)}
+def launcher_direct_metric():
+ """`launcher_direct` when sg-python.sh ran this hook with the interpreter
+ behind a packaged Windows alias instead of the alias: 1 from the path it
+ remembered (one process for the hook), 2 when it had just worked that
+ path out. Read once, like the probe timings.
+ """
+ how = os.environ.pop("SG_LAUNCHER_DIRECT", "")
+ return {"launcher_direct": int(how)} if how in ("1", "2") else {}
+
+
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.