summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorMohamed Hegazy <mhegazy@anthropic.com>2026-10-27 20:57:30 +0000
committerClaude <noreply@anthropic.com>2026-10-27 20:57:30 +0000
commit3183cba038c1a73c43b41f53586282b6307ff339 (patch)
tree23cd3dea293cf6b9b1330c3f2c2ee9df379dd038 /plugins
parentf8c5e7a1dcb71b10cb74586224b5401f3de5db28 (diff)
security-guidance: hand the reviewer a claude.ai token as its OAuth token
As ANTHROPIC_AUTH_TOKEN the inner CLI still renews a stored login it does not run on, which can sign the user out. As CLAUDE_CODE_OAUTH_TOKEN it leaves the stored login alone. Cache writes stay at five minutes.
Diffstat (limited to 'plugins')
-rw-r--r--plugins/security-guidance/README.md2
-rw-r--r--plugins/security-guidance/hooks/llm.py18
-rw-r--r--plugins/security-guidance/tests/test_reviewer_oauth_token.py38
3 files changed, 55 insertions, 3 deletions
diff --git a/plugins/security-guidance/README.md b/plugins/security-guidance/README.md
index caef1866..8678ce88 100644
--- a/plugins/security-guidance/README.md
+++ b/plugins/security-guidance/README.md
@@ -54,7 +54,7 @@ The pattern warnings need no credentials. The LLM reviews (the diff review on St
- **A cloud provider (Bedrock, Vertex, Foundry)** — if Claude Code is configured for one, reviews run through Claude Code with that provider config.
- **`ANTHROPIC_API_KEY` or `ANTHROPIC_AUTH_TOKEN` in the environment** — used as is, against `ANTHROPIC_BASE_URL` if you set one.
-- **Signed in to Claude Code (`/login`)** — Claude Code passes your sign-in to the plugin's hooks, and reviews use that account.
+- **Signed in to Claude Code (`/login`)** — Claude Code passes your sign-in to the plugin's hooks, and reviews use that account. The commit/push reviewer's Claude Code runs on that token alone and does not renew the login you have stored.
- **Signed in to a Claude apps gateway (`/login`)** — Claude Code passes no sign-in to plugins on a gateway, so reviews run through your installed Claude Code, which uses its own stored sign-in. The plugin never reads it. The plugin looks for Claude Code at `~/.local/bin/claude`, where the native installer puts it on macOS and Linux; on Windows, or for any other install, set `SG_AGENTIC_CLI_PATH` to its full path.
Claude Code does not pass your sign-in to plugins in a session started with `CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC`, `DISABLE_TELEMETRY`, or `DO_NOT_TRACK` set. If no credentials reach the plugin, for that or any other reason, and the plugin cannot review through a Claude apps gateway sign-in, the LLM reviews are skipped and only the pattern warnings run. The plugin tells you so when Claude edits a file in a git repo, at most once a week. Set `ENABLE_CODE_SECURITY_REVIEW=0` if that is what you want; it also hides the notice.
diff --git a/plugins/security-guidance/hooks/llm.py b/plugins/security-guidance/hooks/llm.py
index 468d9e6d..1d778a99 100644
--- a/plugins/security-guidance/hooks/llm.py
+++ b/plugins/security-guidance/hooks/llm.py
@@ -1838,6 +1838,9 @@ def _run_inner_cli(start: Callable[[], Any], stderr_lines: List[str]) -> Any:
return start()
+_OAUTH_TOKEN_PREFIX = "sk-ant-oat"
+
+
def _agentic_spawn_env() -> Dict[str, str]:
"""opts.env for the SDK-spawned inner `claude` CLI.
@@ -1853,7 +1856,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;
@@ -1885,7 +1889,17 @@ 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):
+ # 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["ANTHROPIC_AUTH_TOKEN"] = ""
+ env["FORCE_PROMPT_CACHING_5M"] = "1"
+ return env
+ env["ANTHROPIC_AUTH_TOKEN"] = token
return env
diff --git a/plugins/security-guidance/tests/test_reviewer_oauth_token.py b/plugins/security-guidance/tests/test_reviewer_oauth_token.py
new file mode 100644
index 00000000..f9015a3b
--- /dev/null
+++ b/plugins/security-guidance/tests/test_reviewer_oauth_token.py
@@ -0,0 +1,38 @@
+"""The inner CLI runs on a handed claude.ai token, not a stored login."""
+import llm
+
+OAUTH_TOKEN = "sk-ant-oat01-token"
+
+
+def _spawn_env(monkeypatch, **credentials):
+ monkeypatch.delenv("ANTHROPIC_API_KEY", raising=False)
+ monkeypatch.delenv("ANTHROPIC_AUTH_TOKEN", raising=False)
+ for name, value in credentials.items():
+ monkeypatch.setenv(name, value)
+ return llm._agentic_spawn_env()
+
+
+class TestReviewerOauthToken:
+ def test_claude_ai_token_goes_to_the_oauth_variable(self, monkeypatch):
+ env = _spawn_env(monkeypatch, ANTHROPIC_AUTH_TOKEN=OAUTH_TOKEN)
+ assert env["CLAUDE_CODE_OAUTH_TOKEN"] == OAUTH_TOKEN
+ assert env["ANTHROPIC_AUTH_TOKEN"] == ""
+
+ def test_cache_writes_stay_at_five_minutes(self, monkeypatch):
+ env = _spawn_env(monkeypatch, ANTHROPIC_AUTH_TOKEN=OAUTH_TOKEN)
+ assert env["FORCE_PROMPT_CACHING_5M"] == "1"
+
+ def test_other_bearer_stays_where_it_was(self, monkeypatch):
+ env = _spawn_env(monkeypatch, ANTHROPIC_AUTH_TOKEN="gateway-bearer")
+ assert env["ANTHROPIC_AUTH_TOKEN"] == "gateway-bearer"
+ assert "CLAUDE_CODE_OAUTH_TOKEN" not in env
+
+ def test_api_key_wins_over_a_claude_ai_token(self, monkeypatch):
+ env = _spawn_env(monkeypatch, ANTHROPIC_API_KEY="key", ANTHROPIC_AUTH_TOKEN=OAUTH_TOKEN)
+ assert env["ANTHROPIC_AUTH_TOKEN"] == ""
+ assert "CLAUDE_CODE_OAUTH_TOKEN" not in env
+
+ def test_no_credential_adds_nothing(self, monkeypatch):
+ env = _spawn_env(monkeypatch)
+ assert env["ANTHROPIC_AUTH_TOKEN"] == ""
+ assert "CLAUDE_CODE_OAUTH_TOKEN" not in env