summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/tests
diff options
context:
space:
mode:
authorMohamed Hegazy <mhegazy@anthropic.com>2026-10-31 17:01:36 +0000
committerClaude <noreply@anthropic.com>2026-10-31 17:01:47 +0000
commit9d796f8636954cece459f45ff7386eaee912de5c (patch)
tree9d5bb389fd47a62b5431d95520fe5769a63501e6 /plugins/security-guidance/tests
parenta7ba5b93db0c7c8d17f3f1d25853adca79598a1a (diff)
security-guidance: stop the commit reviewer on refused credentials, end hung reviews in time
Diffstat (limited to 'plugins/security-guidance/tests')
-rw-r--r--plugins/security-guidance/tests/test_agentic_auth_stop.py148
-rw-r--r--plugins/security-guidance/tests/test_review_outcomes.py20
2 files changed, 168 insertions, 0 deletions
diff --git a/plugins/security-guidance/tests/test_agentic_auth_stop.py b/plugins/security-guidance/tests/test_agentic_auth_stop.py
new file mode 100644
index 00000000..a2108cfc
--- /dev/null
+++ b/plugins/security-guidance/tests/test_agentic_auth_stop.py
@@ -0,0 +1,148 @@
+"""The agentic reviewer stops as soon as its credentials are refused, instead
+of waiting for the inner CLI to give up, and leaves every other run alone."""
+import os
+import subprocess
+import sys
+import types
+
+import pytest
+
+import llm
+from conftest import write_cli
+
+
+class _Assistant:
+ def __init__(self, error=None):
+ self.content, self.error = [], error
+
+
+class _System:
+ subtype = "api_retry"
+
+ def __init__(self, status):
+ self.data = {"error_status": status}
+
+
+@pytest.fixture
+def sdk(monkeypatch, tmp_path):
+ """Replays `script.messages`, then a clean result, and counts what the
+ review read of them."""
+ script = types.SimpleNamespace(messages=[], read=0)
+
+ class ClaudeAgentOptions:
+ def __init__(self, **kwargs):
+ self.kwargs = kwargs
+
+ class ResultMessage:
+ subtype, usage, total_cost_usd = "success", {}, 0.0
+ structured_output = {"findings": []}
+
+ async def query(prompt, options):
+ for m in script.messages:
+ script.read += 1
+ yield m
+ yield ResultMessage()
+
+ mod = types.ModuleType("claude_agent_sdk")
+ mod.ClaudeAgentOptions, mod.ResultMessage, mod.query = ClaudeAgentOptions, ResultMessage, query
+ mod.AssistantMessage = _Assistant
+ monkeypatch.setitem(sys.modules, "claude_agent_sdk", mod)
+ monkeypatch.setenv("SG_AGENTIC_CLI_PATH", "")
+ monkeypatch.setenv("SECURITY_WARNINGS_STATE_DIR", str(tmp_path / "state"))
+ monkeypatch.delenv("CLAUDE_CODE_REMOTE", raising=False)
+ return script
+
+
+def _review(repo):
+ return llm.agentic_review(str(repo), [("a.py", "+x = 1\n")], ["a.py"])[2]
+
+
+class TestStopsOnRefusedCredentials:
+ def test_first_auth_failure_ends_the_review(self, sdk, tmp_path):
+ sdk.messages = [_Assistant(error="authentication_failed") for _ in range(50)]
+ m = _review(tmp_path)
+ assert sdk.read == 1
+ assert m["agentic_fallback"] == "investigate:_ReviewerAuthFailed"
+
+ def test_repeated_401_retries_end_the_review(self, sdk, tmp_path):
+ sdk.messages = [_System(401) for _ in range(10)]
+ m = _review(tmp_path)
+ assert sdk.read == llm._AGENTIC_MAX_401_RETRIES
+ assert m["agentic_fallback"] == "investigate:_ReviewerAuthFailed"
+
+
+class TestLeavesOtherRunsAlone:
+ def test_a_remote_session_rides_out_401_retries(self, sdk, tmp_path, monkeypatch):
+ monkeypatch.setenv("CLAUDE_CODE_REMOTE", "true")
+ sdk.messages = [_System(401) for _ in range(10)]
+ assert "agentic_fallback" not in _review(tmp_path)
+
+ def test_a_login_renewed_after_one_401_goes_on(self, sdk, tmp_path):
+ sdk.messages = [_System(401), _Assistant()]
+ assert "agentic_fallback" not in _review(tmp_path)
+
+ def test_401s_that_are_not_in_a_row_do_not_add_up(self, sdk, tmp_path):
+ sdk.messages = [_System(401), _System(401), _Assistant()] * 3
+ assert "agentic_fallback" not in _review(tmp_path)
+
+ def test_other_retries_do_not_count(self, sdk, tmp_path):
+ sdk.messages = [_System(s) for s in (529, 429, None, 500, 529, 529)]
+ assert "agentic_fallback" not in _review(tmp_path)
+
+ def test_other_assistant_errors_do_not_stop_it(self, sdk, tmp_path):
+ sdk.messages = [_Assistant(error="rate_limit"), _Assistant(error="server_error")]
+ assert "agentic_fallback" not in _review(tmp_path)
+
+ def test_a_message_without_the_field_is_read_as_before(self, sdk, tmp_path):
+ class Old:
+ content = []
+ sys.modules["claude_agent_sdk"].AssistantMessage = Old
+ sdk.messages = [Old()]
+ assert "agentic_fallback" not in _review(tmp_path)
+
+
+_REFUSED_CLI = """
+import json, os, sys, time
+open(os.environ["SG_TEST_PID_FILE"], "w").write(str(os.getpid()))
+def send(m):
+ sys.stdout.write(json.dumps(m) + chr(10)); sys.stdout.flush()
+for line in sys.stdin:
+ m = json.loads(line)
+ if m.get("type") == "control_request":
+ send({"type": "control_response", "response": {"subtype": "success",
+ "request_id": m["request_id"], "response": {}}})
+ elif m.get("type") == "user":
+ for _ in range(500):
+ send({"type": "assistant", "error": "authentication_failed", "message": {
+ "role": "assistant", "model": "m", "content": [{"type": "text", "text": "401"}]}})
+ time.sleep(0.02)
+ time.sleep(60)
+"""
+
+_DRIVER = """
+import json, llm, os, sys, time
+start = time.time()
+metrics = llm.agentic_review(sys.argv[1], [("a.py", "+x = 1")], ["a.py"])[2]
+pid = int(open(os.environ["SG_TEST_PID_FILE"]).read())
+try:
+ os.kill(pid, 0); alive = True
+except OSError:
+ alive = False
+print(json.dumps({"fallback": metrics.get("agentic_fallback"), "alive": alive}), flush=True)
+os._exit(0)
+"""
+
+
+def test_real_sdk_closes_the_cli_quietly(tmp_path):
+ """Stops the real SDK's child, and prints no traceback for the hook's
+ stderr, which reaches the session."""
+ pytest.importorskip("claude_agent_sdk")
+ env = {**os.environ, "SG_TEST_PID_FILE": str(tmp_path / "pid"),
+ "SG_AGENTIC_CLI_PATH": str(write_cli(tmp_path, "claude", _REFUSED_CLI)),
+ "SECURITY_WARNINGS_STATE_DIR": str(tmp_path / "state"),
+ "PYTHONPATH": str(os.path.dirname(llm.__file__))}
+ r = subprocess.run([sys.executable, "-c", _DRIVER, str(tmp_path)], env=env,
+ capture_output=True, text=True, timeout=60)
+ assert "Traceback" not in r.stderr, r.stderr
+ assert '"fallback": "investigate:_ReviewerAuthFailed"' in r.stdout, (r.stdout, r.stderr)
+ assert '"alive": false' in r.stdout, r.stdout
diff --git a/plugins/security-guidance/tests/test_review_outcomes.py b/plugins/security-guidance/tests/test_review_outcomes.py
index 887ff9c0..806e9bdd 100644
--- a/plugins/security-guidance/tests/test_review_outcomes.py
+++ b/plugins/security-guidance/tests/test_review_outcomes.py
@@ -1,5 +1,6 @@
"""A review that got no verdict is never reported as clean."""
import json
+import threading
import time
import pytest
@@ -188,6 +189,25 @@ class TestFailedReviewIsNotClean:
run.calls = calls
return run
+ def test_review_ends_when_neither_side_returns(self, race, monkeypatch):
+ """A request that never answers holds both sides; the deadline holds anyway."""
+ monkeypatch.setattr(hook, "_RACE_MAX_WAIT_S", 0.1)
+ monkeypatch.setattr(hook, "_RACE_END_S", 0.2)
+ release = threading.Event()
+ monkeypatch.setattr(hook, "analyze_code_security", lambda *a, **k: release.wait(5))
+
+ def stuck_agentic(*a, **k):
+ release.wait(5)
+ started = time.monotonic()
+ try:
+ guidance, vulns, m = race(stuck_agentic)
+ finally:
+ release.set()
+ assert time.monotonic() - started < 2
+ assert (guidance, vulns) == (None, [])
+ assert m["review_failed"] is True
+ assert m["api_error"] == hook._RACE_TIMED_OUT
+
def test_failed_fallback_waits_for_agentic(self, race):
"""Direct calls can fail where the agentic reviewer's CLI works (it
may hold a credential the direct calls lack); its verdict must count."""