summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/tests
diff options
context:
space:
mode:
authorMohamed Hegazy <8000722+mhegazy@users.noreply.github.com>2026-10-20 13:26:58 -0700
committerGitHub <noreply@github.com>2026-10-20 13:26:58 -0700
commit07b02ef8249ec166557a5bd4f045c57af6a62c3f (patch)
tree2ff40064a277ee8563ab474db6cd3ddb206c0856 /plugins/security-guidance/tests
parentdfffc8c43f48692db02eac092dbe208293fda224 (diff)
parent0b0a62192591f8c01097c51f591bab11ec8627f0 (diff)
Merge pull request #6809 from anthropics/mhegazy/sg-reviewer-managed-mcp
security-guidance: run reviews on machines with a managed MCP config
Diffstat (limited to 'plugins/security-guidance/tests')
-rw-r--r--plugins/security-guidance/tests/test_reviewer_tools.py123
1 files changed, 123 insertions, 0 deletions
diff --git a/plugins/security-guidance/tests/test_reviewer_tools.py b/plugins/security-guidance/tests/test_reviewer_tools.py
index 3e2aa86c..3b9521ff 100644
--- a/plugins/security-guidance/tests/test_reviewer_tools.py
+++ b/plugins/security-guidance/tests/test_reviewer_tools.py
@@ -1,6 +1,8 @@
"""The inner Claude Code the plugin starts for a review gets only the tools
the review needs."""
+import json
import sys
+import threading
import types
import pytest
@@ -9,11 +11,22 @@ import llm
READ_ONLY = {"tools": "Read,Grep,Glob", "strict-mcp-config": None}
NO_TOOLS = {"tools": "", "strict-mcp-config": None}
+# Once an inner CLI has refused --strict-mcp-config.
+READ_ONLY_NO_MCP = {"tools": "Read,Grep,Glob", "disallowedTools": "mcp__*"}
+NO_TOOLS_NO_MCP = {"tools": "", "disallowedTools": "mcp__*"}
+
+
+@pytest.fixture(autouse=True)
+def strict_mcp_flag_sent(monkeypatch):
+ monkeypatch.setattr(llm, "_send_strict_mcp", True)
def test_tool_flags():
assert llm._only_tools_args(("Read", "Grep", "Glob")) == READ_ONLY
assert llm._only_tools_args() == NO_TOOLS
+ llm._send_strict_mcp = False
+ assert llm._only_tools_args(("Read", "Grep", "Glob")) == READ_ONLY_NO_MCP
+ assert llm._only_tools_args() == NO_TOOLS_NO_MCP
def test_flags_reach_the_command_line_of_the_installed_sdk():
@@ -26,6 +39,16 @@ def test_flags_reach_the_command_line_of_the_installed_sdk():
cmd = transport.SubprocessCLITransport(prompt="x", options=opts)._build_command()
assert cmd[cmd.index("--tools") + 1] == "Read,Grep,Glob"
assert cmd.count("--tools") == 1 and "--strict-mcp-config" in cmd
+ assert "--disallowedTools" not in cmd
+
+ llm._send_strict_mcp = False
+ opts = sdk.ClaudeAgentOptions(
+ cli_path="claude", allowed_tools=["Read", "Grep", "Glob"],
+ extra_args=llm._only_tools_args(llm._REVIEWER_TOOLS))
+ cmd = transport.SubprocessCLITransport(prompt="x", options=opts)._build_command()
+ assert cmd[cmd.index("--tools") + 1] == "Read,Grep,Glob"
+ assert cmd[cmd.index("--disallowedTools") + 1] == "mcp__*"
+ assert cmd.count("--disallowedTools") == 1 and "--strict-mcp-config" not in cmd
@pytest.fixture
@@ -82,3 +105,103 @@ def test_single_turn_sdk_review_gets_no_tools(sdk):
assert llm._call_claude_via_sdk("review this", schema) is not None
assert len(sdk) == 1
assert sdk[0]["extra_args"] == NO_TOOLS and sdk[0]["allowed_tools"] == []
+
+
+# What Claude Code prints, before exiting 1, on a machine whose administrator
+# delivers the MCP servers.
+REFUSAL = "You cannot use --strict-mcp-config when an enterprise MCP config is present"
+
+
+@pytest.fixture
+def managed_mcp(sdk, monkeypatch):
+ """The stand-in SDK's inner CLI now refuses --strict-mcp-config. Gives
+ the options of the refused starts and of the ones that ran."""
+ answer = sys.modules["claude_agent_sdk"].query
+ refused = []
+
+ async def query(prompt, options):
+ if "strict-mcp-config" in options.kwargs["extra_args"]:
+ refused.append(options.kwargs)
+ options.kwargs["stderr"](REFUSAL)
+ raise RuntimeError("Command failed with exit code 1")
+ async for msg in answer(prompt, options):
+ yield msg
+
+ monkeypatch.setattr(sys.modules["claude_agent_sdk"], "query", query)
+ return refused, sdk
+
+
+def test_agentic_review_runs_where_the_strict_mcp_flag_is_refused(managed_mcp, tmp_path):
+ _, vulns, metrics = llm.agentic_review(str(tmp_path), [("a.py", "+x = 1\n")], ["a.py"])
+ assert len(vulns) == 1
+ # The refused start's line is not what child_stderr is there to count.
+ assert "agentic_fallback" not in metrics and "child_stderr" not in metrics
+ refused, ran = managed_mcp
+ assert [o["extra_args"] for o in refused] == [READ_ONLY]
+ # investigate, second investigate pass, refute
+ assert [o["extra_args"] for o in ran] == [READ_ONLY_NO_MCP] * 3
+ for opts in ran:
+ assert opts["allowed_tools"] == ["Read", "Grep", "Glob"]
+
+
+def test_single_turn_sdk_review_runs_where_the_strict_mcp_flag_is_refused(managed_mcp):
+ schema = {"type": "object", "properties": {}}
+ assert llm._call_claude_via_sdk("review this", schema) is not None
+ refused, ran = managed_mcp
+ assert [o["extra_args"] for o in refused] == [NO_TOOLS]
+ assert [o["extra_args"] for o in ran] == [NO_TOOLS_NO_MCP]
+ # The refusal is remembered for the rest of the hook process.
+ assert llm._call_claude_via_sdk("review this", schema) is not None
+ assert len(refused) == 1 and ran[-1]["extra_args"] == NO_TOOLS_NO_MCP
+
+
+def test_other_start_failures_are_not_retried(sdk, monkeypatch, tmp_path):
+ async def query(prompt, options):
+ sdk.append(options.kwargs)
+ options.kwargs["stderr"]("Fallback model cannot be the same as the main model")
+ raise RuntimeError("Command failed with exit code 1")
+ yield
+
+ monkeypatch.setattr(sys.modules["claude_agent_sdk"], "query", query)
+ guidance, vulns, metrics = llm.agentic_review(str(tmp_path), [("a.py", "+x = 1\n")], ["a.py"])
+ assert guidance is None and vulns == []
+ assert metrics["agentic_fallback"] == "investigate:RuntimeError"
+ assert llm._call_claude_via_sdk("review this", {"type": "object", "properties": {}}) is None
+ assert [o["extra_args"] for o in sdk] == [READ_ONLY, NO_TOOLS]
+
+
+def test_reviews_started_together_are_each_started_again(managed_mcp):
+ refused, ran = managed_mcp
+ both_refused = threading.Barrier(2, timeout=10)
+ refuse = sys.modules["claude_agent_sdk"].query
+
+ async def query(prompt, options):
+ if "strict-mcp-config" in options.kwargs["extra_args"]:
+ both_refused.wait()
+ async for msg in refuse(prompt, options):
+ yield msg
+
+ sys.modules["claude_agent_sdk"].query = query
+ schema = {"type": "object", "properties": {}}
+ results = []
+ threads = [threading.Thread(
+ target=lambda: results.append(llm._call_claude_via_sdk("review this", schema)))
+ for _ in range(2)]
+ for t in threads:
+ t.start()
+ for t in threads:
+ t.join(20)
+ assert len(results) == 2 and None not in results
+ assert len(refused) == 2 and [o["extra_args"] for o in ran] == [NO_TOOLS_NO_MCP] * 2
+
+
+def test_review_rows_say_when_the_flag_was_left_out(managed_mcp, capsys):
+ import security_reminder_hook as hook
+
+ def row():
+ hook.emit_metrics({"vulns_found": 0, "review_ms": 10})
+ return json.loads(capsys.readouterr().out)["metrics"]
+
+ assert "strict_mcp_off" not in row()
+ assert llm._call_claude_via_sdk("review this", {"type": "object", "properties": {}}) is not None
+ assert row()["strict_mcp_off"] == 1