summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/hooks
diff options
context:
space:
mode:
authorMohamed Hegazy <8000722+mhegazy@users.noreply.github.com>2026-11-09 14:59:27 -0800
committerGitHub <noreply@github.com>2026-11-09 14:59:27 -0800
commitfa5015cff6782d33feeeb31dd2753eea3be83568 (patch)
tree3df5ef73fdae1a87dec616913896d8b5ee98eb8b /plugins/security-guidance/hooks
parent97c32530afada4841fca83fde0d470812a3dcfab (diff)
parent68bbff79681c4054d1844e009ef4320405f8f035 (diff)
Merge pull request #6921 from anthropics/mhegazy/sg-reviewer-oauth-token
security-guidance: stop the commit reviewer renewing a stored login
Diffstat (limited to 'plugins/security-guidance/hooks')
-rw-r--r--plugins/security-guidance/hooks/llm.py30
1 files changed, 27 insertions, 3 deletions
diff --git a/plugins/security-guidance/hooks/llm.py b/plugins/security-guidance/hooks/llm.py
index ee9ad2be..6a5eebf2 100644
--- a/plugins/security-guidance/hooks/llm.py
+++ b/plugins/security-guidance/hooks/llm.py
@@ -1971,6 +1971,19 @@ def _api_retry_status(msg: Any) -> Optional[int]:
return status if isinstance(status, int) else None
+_OAUTH_TOKEN_PREFIX = "sk-ant-oat"
+
+
+def _oauth_variable_is_safe() -> bool:
+ """Whether a claude.ai token may move out of ANTHROPIC_AUTH_TOKEN. With a
+ base URL set it can be a gateway's bearer, bare mode ignores the OAuth
+ variable, and a cloud session has no stored login to protect and rotates
+ its own token."""
+ bare = os.environ.get("CLAUDE_CODE_SIMPLE", "").strip().lower()
+ return not (os.environ.get("ANTHROPIC_BASE_URL", "").strip()
+ or bare in ("1", "true", "yes", "on") or _is_remote_session())
+
+
def _agentic_spawn_env() -> Dict[str, str]:
"""opts.env for the SDK-spawned inner `claude` CLI.
@@ -1986,7 +1999,8 @@ def _agentic_spawn_env() -> Dict[str, str]:
blanking the inherited OAuth token there strands the grandchild with
zero credentials → ProcessError → agentic silently falls back to
single-shot on every commit. So forward the OAuth token whenever it
- is the only credential.
+ is the only credential, as CLAUDE_CODE_OAUTH_TOKEN when it is a
+ claude.ai token.
"""
env = {
# Policy settings can enable this plugin inside the reviewing CLI too;
@@ -2018,7 +2032,16 @@ def _agentic_spawn_env() -> Dict[str, str]:
# No API key — forward the OAuth token from the parent process so the
# SDK grandchild has credentials. Empty string is fine (the SDK will
# use whatever auth path is left).
- env["ANTHROPIC_AUTH_TOKEN"] = os.environ.get("ANTHROPIC_AUTH_TOKEN") or ""
+ token = os.environ.get("ANTHROPIC_AUTH_TOKEN") or ""
+ if token.startswith(_OAUTH_TOKEN_PREFIX) and _oauth_variable_is_safe():
+ # Given as ANTHROPIC_AUTH_TOKEN, the inner CLI still renews a stored
+ # claude.ai login it does not run on, which can sign the user out.
+ # Given as its OAuth token, it leaves the stored login alone. That
+ # route also turns on 1-hour cache writes, which cost more.
+ env["CLAUDE_CODE_OAUTH_TOKEN"] = token
+ env["FORCE_PROMPT_CACHING_5M"] = "1"
+ token = ""
+ env["ANTHROPIC_AUTH_TOKEN"] = token
return env
@@ -2217,7 +2240,8 @@ def _agentic_review(
# env as {**os.environ, **opts.env}, so the inner claude inherits
# it and prefers it over ANTHROPIC_API_KEY — but some model
# endpoints reject OAuth bearers (401 → exit 1 → silent
- # fallback). Override with empty so API-key auth wins.
+ # fallback). Override with empty so API-key auth wins. With no
+ # API key, a claude.ai token moves to CLAUDE_CODE_OAUTH_TOKEN.
#
# On CCR (entrypoint=remote*) the daemon passes auth on file
# descriptors to the top-level claude process; the SDK-spawned