summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/hooks/_base.py
diff options
context:
space:
mode:
authorClaude <noreply@anthropic.com>2026-10-15 22:30:36 +0000
committerClaude <noreply@anthropic.com>2026-10-15 22:30:36 +0000
commitba27dfbea7ee01b2cbaf4fbfd6ed23c074b3ca88 (patch)
treef4dc2ef8188ed691fdc968d299be198548403642 /plugins/security-guidance/hooks/_base.py
parent7b5d1742310abe7d90f10aa574adfa0613226ec6 (diff)
parent7b2259ba542c313099b1f00be8b670f7aa0c63bb (diff)
Merge remote-tracking branch 'origin/main' into mhegazy/sg-no-credentials-notice
# Conflicts: # plugins/security-guidance/hooks/security_reminder_hook.py
Diffstat (limited to 'plugins/security-guidance/hooks/_base.py')
-rw-r--r--plugins/security-guidance/hooks/_base.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/plugins/security-guidance/hooks/_base.py b/plugins/security-guidance/hooks/_base.py
index 44b205f0..f05a0094 100644
--- a/plugins/security-guidance/hooks/_base.py
+++ b/plugins/security-guidance/hooks/_base.py
@@ -94,6 +94,47 @@ PROVENANCE_BANNER = (
)
+# Output that only Claude Code reads: the `metrics` and `rewakeSummary` fields,
+# and the separate `{"async": ...}` line an async hook prints first. Other hosts
+# that run this plugin's hooks can validate hook output against a closed schema
+# and reject the whole output over a field or a second line they do not know,
+# which shows up as a hook error.
+_CLAUDE_CODE_OUTPUT_FIELDS = ("metrics", "rewakeSummary")
+
+
+def _detect_portable_output():
+ forced = os.environ.get("SECURITY_GUIDANCE_PORTABLE_OUTPUT", "").strip().lower()
+ if forced in ("1", "true", "yes", "on"):
+ return True
+ if forced in ("0", "false", "no", "off"):
+ return False
+ if os.environ.get("CLAUDECODE") or os.environ.get("CLAUDE_CODE_ENTRYPOINT"):
+ return False
+ return bool(os.environ.get("PLUGIN_ROOT"))
+
+
+# Decided once, from the environment the host started the hook with: code the
+# hook runs in-process later can set these variables itself.
+_PORTABLE_OUTPUT = _detect_portable_output()
+
+
+def portable_output():
+ """True when stdout should carry only the standard hook output fields.
+
+ SECURITY_GUIDANCE_PORTABLE_OUTPUT=1 or =0 forces it on or off. Otherwise it
+ is on only when the host passes PLUGIN_ROOT and does not identify itself as
+ Claude Code, so Claude Code and direct invocations are unchanged."""
+ return _PORTABLE_OUTPUT
+
+
+def write_hook_output(out):
+ """Print one hook output line. In portable mode the Claude Code only
+ fields are left out; what remains (possibly `{}`) is still valid output."""
+ if portable_output():
+ out = {k: v for k, v in out.items() if k not in _CLAUDE_CODE_OUTPUT_FIELDS}
+ print(json.dumps(out), flush=True)
+
+
def _read_plugin_version_int():
"""Encode plugin.json version "M.m.p" as M*10000 + m*100 + p so it fits the
bool|number metrics constraint. Returns 0 if unreadable."""