diff options
| author | Mohamed Hegazy <8000722+mhegazy@users.noreply.github.com> | 2026-11-05 14:22:10 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-05 14:22:10 -0800 |
| commit | 53a110dc37e6dbcb20426b6b2e2b325e2ca5dc17 (patch) | |
| tree | 0f96a3a17c97b983c564b7d10fcef93ac58258d6 /plugins/security-guidance/hooks/_base.py | |
| parent | 88cc978a56e28d54180fda4e423bfddde1e7f532 (diff) | |
| parent | 6fe57dc38d8744937b0e01ee2bb79add5f523903 (diff) | |
Merge pull request #7001 from anthropics/mhegazy/sg-state-dir-owner-only
security-guidance: keep the state folder to its user
Diffstat (limited to 'plugins/security-guidance/hooks/_base.py')
| -rw-r--r-- | plugins/security-guidance/hooks/_base.py | 21 |
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), |
