summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/tests
diff options
context:
space:
mode:
authorMohamed Hegazy <8000722+mhegazy@users.noreply.github.com>2026-10-13 13:46:31 -0700
committerGitHub <noreply@github.com>2026-10-13 20:46:31 +0000
commit8d392c8169665866be1d95f2bfeda5aaae7149dd (patch)
treefe9463366f386249da8a8637c7d4d88095207e05 /plugins/security-guidance/tests
parent99951e9e4678548f90531dfc80be2e95e4618ff0 (diff)
security-guidance: deliver commit and push findings on stderr (#6379)
Commit and push findings went only into hookSpecificOutput.additionalContext, which Claude Code's asyncRewake path never reads: it shows Claude stderr, or stdout when stderr is empty. When the agentic reviewer's inner CLI printed a warning to stderr, Claude got that warning instead of the findings (anthropics/claude-code#99839, also anthropics/claude-code#92987). Findings now go to stderr for every event, as the Stop path already did. The inner CLI's stderr and the Agent SDK's own log lines go to the plugin's debug log.
Diffstat (limited to 'plugins/security-guidance/tests')
-rw-r--r--plugins/security-guidance/tests/test_findings_delivery.py87
1 files changed, 87 insertions, 0 deletions
diff --git a/plugins/security-guidance/tests/test_findings_delivery.py b/plugins/security-guidance/tests/test_findings_delivery.py
new file mode 100644
index 00000000..b71fc8d6
--- /dev/null
+++ b/plugins/security-guidance/tests/test_findings_delivery.py
@@ -0,0 +1,87 @@
+"""Findings reach Claude on the channel Claude Code reads for an asyncRewake
+hook (`stderr || stdout`, never additionalContext)."""
+import json
+import sys
+import types
+
+from conftest import (
+ STUB_VULN, VULN_PY, bash_payload, commit_file, metrics_of, run_hook,
+)
+
+import llm
+import security_reminder_hook as hook
+
+
+def _rewake_body(stdout, stderr):
+ """What Claude Code shows Claude for an exit-2 asyncRewake hook: stderr,
+ else stdout."""
+ return stderr or stdout
+
+
+def _commit(repo, env, content=VULN_PY, session_id="s1"):
+ sha, out = commit_file(repo, "app.py", content)
+ rc, so, se = run_hook(bash_payload(repo, "git commit -m change", out,
+ session_id=session_id), env)
+ return sha, rc, so, se
+
+
+class TestAgenticReviewerOn:
+ def test_findings_not_replaced_by_the_inner_cli_warning(self, agentic_env, gateway, workspace):
+ _, repo = workspace
+ gateway.vulns = [STUB_VULN]
+ sha, rc, so, se = _commit(repo, agentic_env)
+ assert rc == 2, (so, se)
+ assert metrics_of(so)["agentic_fallback"] == 2
+ body = _rewake_body(so, se)
+ assert "command_injection" in body
+ assert "connectors are disabled" not in body
+ assert "Fatal error in message reader" not in body # the SDK's own log line
+
+
+class TestFindingsReachClaude:
+ def test_commit_findings_are_the_rewake_body(self, env, gateway, workspace):
+ _, repo = workspace
+ gateway.vulns = [STUB_VULN]
+ sha, rc, so, se = _commit(repo, env)
+ assert rc == 2
+ body = _rewake_body(so, se)
+ assert "Security Review: Potential vulnerabilities detected" in body
+ assert "command_injection" in body
+ out = json.loads(so.strip().splitlines()[-1])
+ assert out["metrics"]["vulns_found"] == 1
+ assert out["rewakeSummary"].startswith("Commit security review found")
+ assert "hookSpecificOutput" not in out # not repeated where the sync path would show it twice
+
+ def test_stop_findings_also_go_in_decision_and_reason(self, capsys):
+ hook.emit_metrics({"vulns_found": 1}, additional_context="FINDINGS", hook_event_name="Stop")
+ cap = capsys.readouterr()
+ assert cap.err == "FINDINGS"
+ out = json.loads(cap.out)
+ assert out["decision"] == "block" and out["reason"] == "FINDINGS"
+
+ def test_agentic_review_captures_the_inner_cli_stderr(self, monkeypatch, tmp_path, capfd):
+ spawned = []
+
+ class ClaudeAgentOptions:
+ def __init__(self, **kwargs):
+ self.kwargs = kwargs
+ spawned.append(kwargs)
+
+ class ResultMessage:
+ subtype, usage, total_cost_usd = "success", {}, 0.0
+ structured_output = {"findings": []}
+
+ async def query(prompt, options):
+ # What the inner CLI prints on stderr; with a callback set the SDK
+ # pipes it to the callback instead of the hook's own stderr.
+ options.kwargs["stderr"]("claude.ai connectors are disabled")
+ yield ResultMessage()
+
+ sdk = types.ModuleType("claude_agent_sdk")
+ sdk.ClaudeAgentOptions, sdk.ResultMessage, sdk.query = ClaudeAgentOptions, ResultMessage, query
+ sdk.AssistantMessage = type("AssistantMessage", (), {})
+ monkeypatch.setitem(sys.modules, "claude_agent_sdk", sdk)
+ monkeypatch.setenv("SG_AGENTIC_CLI_PATH", "")
+ llm.agentic_review(str(tmp_path), [("a.py", "+x = 1\n")], ["a.py"])
+ assert callable(spawned[0].get("stderr"))
+ assert "connectors" not in capfd.readouterr().err