summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/hooks
diff options
context:
space:
mode:
authorMohamed Hegazy <mhegazy@anthropic.com>2026-10-08 01:00:45 +0000
committerClaude <noreply@anthropic.com>2026-10-09 16:04:08 +0000
commit860b93cb3898d30683ef9b1b70755d4fb0c2ff57 (patch)
tree73b675981e0f318bbfd9b8a408ad253cda1f514b /plugins/security-guidance/hooks
parentac996c0dde7fb2a9f805cd5277ffc95ecd44a321 (diff)
security-guidance: send ANTHROPIC_CUSTOM_HEADERS on the plugin's own API callsmhegazy/sg-gateway-headers
The plugin's own API calls (the /v1/models lookup and the review itself) never sent ANTHROPIC_CUSTOM_HEADERS, so a gateway that takes its key in a custom header answered 401 and reviews never ran (anthropics/claude-code#99857). The headers are now parsed the way Claude Code parses them (one `Name: Value` per line, split at the first colon) and sent on every call. A custom line replaces a header of the same name. anthropic-beta lines are not forwarded: the review needs none of the user's betas.
Diffstat (limited to 'plugins/security-guidance/hooks')
-rw-r--r--plugins/security-guidance/hooks/llm.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/plugins/security-guidance/hooks/llm.py b/plugins/security-guidance/hooks/llm.py
index 3c1eb5e3..1f06cee7 100644
--- a/plugins/security-guidance/hooks/llm.py
+++ b/plugins/security-guidance/hooks/llm.py
@@ -267,6 +267,38 @@ def _cap_files_for_prompt(files):
_auth_prefer_token = False
+# A valid HTTP header name (an RFC 9110 token).
+_HEADER_NAME = re.compile(r"[!#$%&'*+.^_`|~0-9A-Za-z-]+")
+_custom_header_lines_skipped = set()
+
+
+def _custom_headers() -> List[Tuple[str, str]]:
+ """ANTHROPIC_CUSTOM_HEADERS as (name, value) pairs, parsed the way Claude
+ Code parses it: one `Name: Value` per line, split at the first colon,
+ trimmed, lines with no name dropped. Lines http.client cannot send (an
+ invalid name, a carriage return or non-Latin-1 text in the value) are
+ skipped. Values are never logged: they are often credentials."""
+ pairs = []
+ raw = os.environ.get("ANTHROPIC_CUSTOM_HEADERS", "")
+ for number, line in enumerate(re.split(r"\r?\n", raw), 1):
+ name, sep, value = line.partition(":")
+ name, value = name.strip(), value.strip()
+ if not sep or not name:
+ continue
+ try:
+ value.encode("latin-1")
+ sendable = bool(_HEADER_NAME.fullmatch(name)) and "\r" not in value
+ except UnicodeEncodeError:
+ sendable = False
+ if not sendable:
+ if number not in _custom_header_lines_skipped:
+ _custom_header_lines_skipped.add(number)
+ debug_log(f"ANTHROPIC_CUSTOM_HEADERS: skipped line {number}, not a valid HTTP header")
+ continue
+ pairs.append((name, value))
+ return pairs
+
+
def _build_auth_headers(use_token, betas=("structured-outputs-2025-11-13",)):
betas = list(betas)
headers = {
@@ -278,6 +310,18 @@ def _build_auth_headers(use_token, betas=("structured-outputs-2025-11-13",)):
betas.append("oauth-2025-04-20")
else:
headers["x-api-key"] = ANTHROPIC_API_KEY
+ for name, value in _custom_headers():
+ lowered = name.lower()
+ if lowered == "anthropic-beta":
+ # Not sent: the review needs none of the user's betas, and Claude
+ # Code drops anthropic-beta lines when the organization's
+ # compliance policy disables experimental betas.
+ continue
+ # As in Claude Code, a custom line wins over a header of the same
+ # name that the plugin set, including the credential header.
+ for existing in [k for k in headers if k.lower() == lowered]:
+ del headers[existing]
+ headers[name] = value
if betas:
headers["anthropic-beta"] = ",".join(betas)
return headers