summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/hooks/_base.py
diff options
context:
space:
mode:
authorMohamed Hegazy <mhegazy@anthropic.com>2026-11-04 19:36:17 +0000
committerClaude <noreply@anthropic.com>2026-11-04 20:06:22 +0000
commit6fe57dc38d8744937b0e01ee2bb79add5f523903 (patch)
treed73b3508e5dd49c9cdd5c3e0d14b6d35de643a8b /plugins/security-guidance/hooks/_base.py
parentfa4eb54397ee2faccb6940512a19c017cb1337f3 (diff)
security-guidance: keep the state folder to its usermhegazy/sg-state-dir-owner-only
Create ~/.claude/security with mode 0700 from every place that makes it, and narrow the default folder on the next run, so a umask such as 002 does not leave the reviewer's venv group-writable. The in-place state write replaces a link instead of following it, and creates a 0600 file. Co-authored-by: mhegazy <mhegazy@anthropic.com>
Diffstat (limited to 'plugins/security-guidance/hooks/_base.py')
-rw-r--r--plugins/security-guidance/hooks/_base.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/plugins/security-guidance/hooks/_base.py b/plugins/security-guidance/hooks/_base.py
index 28b189a4..bdddab3d 100644
--- a/plugins/security-guidance/hooks/_base.py
+++ b/plugins/security-guidance/hooks/_base.py
@@ -9,6 +9,7 @@ import glob
import json
import os
import re
+import stat
import threading
from datetime import datetime
@@ -42,6 +43,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),