summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorMohamed Hegazy <mhegazy@anthropic.com>2026-10-15 16:24:59 +0000
committerClaude <noreply@anthropic.com>2026-10-15 16:24:59 +0000
commite7b34dd08a55872f3ae45af477993bda1fda764d (patch)
treee94cb91a7066d8391e5e8b0dc1d811465403adf8 /plugins
parent8f814f49002ff824e8a9ba43cf96112dc7f5948d (diff)
security-guidance: find user-level policy files under CLAUDE_CONFIG_DIR
claude-security-guidance.md and security-patterns files were only looked for in ~/.claude, so they were never loaded on installs that keep their config elsewhere. The directory CLAUDE_CONFIG_DIR names is now read as well; ~/.claude is still read, so files already placed there keep working. Fixes #4826
Diffstat (limited to 'plugins')
-rw-r--r--plugins/security-guidance/README.md2
-rw-r--r--plugins/security-guidance/hooks/extensibility.py27
-rw-r--r--plugins/security-guidance/tests/test_user_config_dir.py98
3 files changed, 124 insertions, 3 deletions
diff --git a/plugins/security-guidance/README.md b/plugins/security-guidance/README.md
index 518af894..d2fcff6f 100644
--- a/plugins/security-guidance/README.md
+++ b/plugins/security-guidance/README.md
@@ -70,7 +70,7 @@ Runs two parallel review calls and unions the findings. Catches a few percentage
Drop a `claude-security-guidance.md` in any of:
-- `~/.claude/claude-security-guidance.md` — user-wide rules
+- `~/.claude/claude-security-guidance.md` — user-wide rules (when `CLAUDE_CONFIG_DIR` is set, the same file in that directory is read too)
- `<project>/.claude/claude-security-guidance.md` — project rules, intended to be committed
- `<project>/.claude/claude-security-guidance.local.md` — local overrides, intended to be `.gitignore`'d
diff --git a/plugins/security-guidance/hooks/extensibility.py b/plugins/security-guidance/hooks/extensibility.py
index a9c7f8fe..cb3e0a8d 100644
--- a/plugins/security-guidance/hooks/extensibility.py
+++ b/plugins/security-guidance/hooks/extensibility.py
@@ -12,11 +12,13 @@ Two extensibility points, both additive only:
Discovery, in precedence order (matching CLAUDE.md / settings.json):
- ``~/.claude/<name>`` (user)
+ - ``$CLAUDE_CONFIG_DIR/<name>`` (user, when set to another directory)
- ``<cwd>/.claude/<name>`` (project, committed)
- ``<cwd>/.claude/<name>.local.<ext>`` (project local, gitignored)
Managed delivery via ``managed-settings.json`` is not yet supported.
-Org admins can still push files to ``~/.claude/`` via MDM/GPO.
+Org admins can still push files to ``~/.claude/`` (or the directory
+``CLAUDE_CONFIG_DIR`` names) via MDM/GPO.
Trust model:
- The ``.md`` is repo-controlled and goes into the USER prompt (not system),
@@ -89,11 +91,32 @@ def user_patterns() -> List[Dict[str, Any]]:
# ── claude-security-guidance.md ───────────────────────────────────────────────────────
+def _user_config_dirs() -> List[str]:
+ """User-level config directories. ``~/.claude`` always, so a file placed
+ there keeps loading; then ``CLAUDE_CONFIG_DIR`` when it names another
+ directory. Empty is treated as not set, as in ``_base.state_dir``."""
+ dirs = [os.path.expanduser(os.path.join("~", ".claude"))]
+ configured = (os.environ.get("CLAUDE_CONFIG_DIR") or "").strip()
+ if configured:
+ configured = os.path.expanduser(configured)
+ if _same_dir(configured, dirs[0]):
+ return dirs
+ dirs.append(configured)
+ return dirs
+
+
+def _same_dir(a: str, b: str) -> bool:
+ try:
+ return os.path.samefile(a, b)
+ except OSError:
+ return os.path.normcase(os.path.abspath(a)) == os.path.normcase(os.path.abspath(b))
+
+
def _config_paths(cwd: Optional[str], basename: str) -> List[Tuple[str, str]]:
"""Existing config file paths, lowest precedence first (so concat reads in
precedence order user → project → project-local). Truncation is done on
the concatenated string, so lowest-precedence content is dropped last."""
- paths = [("User", os.path.expanduser(os.path.join("~", ".claude", basename)))]
+ paths = [("User", os.path.join(d, basename)) for d in _user_config_dirs()]
if cwd:
paths.append(("Project", os.path.join(cwd, ".claude", basename)))
# claude-security-guidance.local.md / security-patterns.local.yaml
diff --git a/plugins/security-guidance/tests/test_user_config_dir.py b/plugins/security-guidance/tests/test_user_config_dir.py
new file mode 100644
index 00000000..7c8a1245
--- /dev/null
+++ b/plugins/security-guidance/tests/test_user_config_dir.py
@@ -0,0 +1,98 @@
+"""User-level claude-security-guidance.md and security-patterns files are
+found in the directory CLAUDE_CONFIG_DIR names, as well as in ~/.claude."""
+import json
+import os
+
+import pytest
+
+import extensibility
+
+MARKER = "MARKER_POLICY_LINE"
+PATTERNS = {"patterns": [{"rule_name": "marker_rule", "substrings": ["x"], "reminder": "r"}]}
+
+
+@pytest.fixture
+def home(tmp_path, monkeypatch):
+ h = tmp_path / "home"
+ h.mkdir()
+ monkeypatch.setenv("HOME", str(h))
+ monkeypatch.setenv("USERPROFILE", str(h))
+ monkeypatch.delenv("CLAUDE_CONFIG_DIR", raising=False)
+ return h
+
+
+def write_user_files(d, marker=MARKER, rule="marker_rule"):
+ d.mkdir(parents=True, exist_ok=True)
+ (d / "claude-security-guidance.md").write_text(f"- {marker}\n")
+ patterns = {"patterns": [dict(PATTERNS["patterns"][0], rule_name=rule)]}
+ (d / "security-patterns.json").write_text(json.dumps(patterns))
+
+
+def load():
+ extensibility.load_for_session(None)
+ return extensibility.guidance_block(), [r["ruleName"] for r in extensibility.user_patterns()]
+
+
+def test_files_in_configured_dir_are_loaded(home, monkeypatch):
+ cfg = home / ".claude-work"
+ write_user_files(cfg)
+ monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(cfg))
+ guidance, rules = load()
+ assert MARKER in guidance
+ assert rules == ["user:marker_rule"]
+
+
+def test_default_dir_still_loaded_when_config_dir_is_set(home, monkeypatch):
+ write_user_files(home / ".claude")
+ monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(home / ".claude-work"))
+ guidance, rules = load()
+ assert MARKER in guidance
+ assert rules == ["user:marker_rule"]
+
+
+def test_both_dirs_loaded_default_first(home, monkeypatch):
+ write_user_files(home / ".claude", marker="FROM_DEFAULT", rule="default_rule")
+ cfg = home / ".claude-work"
+ write_user_files(cfg, marker="FROM_CONFIGURED", rule="configured_rule")
+ monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(cfg))
+ guidance, rules = load()
+ assert guidance.index("FROM_DEFAULT") < guidance.index("FROM_CONFIGURED")
+ assert rules == ["user:default_rule", "user:configured_rule"]
+
+
+@pytest.mark.parametrize("spelling", ["{home}/.claude", "{home}/.claude/", "~/.claude", "{home}/link"])
+def test_config_dir_naming_the_default_dir_is_read_once(home, monkeypatch, spelling):
+ write_user_files(home / ".claude")
+ os.symlink(home / ".claude", home / "link")
+ monkeypatch.setenv("CLAUDE_CONFIG_DIR", spelling.format(home=home))
+ guidance, rules = load()
+ assert guidance.count(MARKER) == 1
+ assert rules == ["user:marker_rule"]
+
+
+@pytest.mark.parametrize("value", ["", " "])
+def test_empty_config_dir_is_not_set(home, monkeypatch, value):
+ write_user_files(home / ".claude")
+ monkeypatch.setenv("CLAUDE_CONFIG_DIR", value)
+ assert extensibility._user_config_dirs() == [str(home / ".claude")]
+ guidance, rules = load()
+ assert guidance.count(MARKER) == 1
+
+
+def test_tilde_in_config_dir_is_expanded(home, monkeypatch):
+ write_user_files(home / ".claude-work")
+ monkeypatch.setenv("CLAUDE_CONFIG_DIR", "~/.claude-work")
+ guidance, _ = load()
+ assert MARKER in guidance
+
+
+def test_project_files_unchanged(home, monkeypatch, tmp_path):
+ cfg = home / ".claude-work"
+ write_user_files(cfg, marker="FROM_USER")
+ monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(cfg))
+ proj = tmp_path / "proj"
+ write_user_files(proj / ".claude", marker="FROM_PROJECT", rule="project_rule")
+ extensibility.load_for_session(str(proj))
+ guidance = extensibility.guidance_block()
+ assert guidance.index("FROM_USER") < guidance.index("FROM_PROJECT")
+ assert [r["ruleName"] for r in extensibility.user_patterns()] == ["user:marker_rule", "user:project_rule"]