diff options
| author | Mohamed Hegazy <mhegazy@anthropic.com> | 2026-10-27 21:07:52 +0000 |
|---|---|---|
| committer | Claude <noreply@anthropic.com> | 2026-10-27 21:07:52 +0000 |
| commit | 9cecb353d26598596cd993576a1dd00b59bb6a31 (patch) | |
| tree | 17801589524420d597ada35d57867c4848289649 /plugins | |
| parent | 3183cba038c1a73c43b41f53586282b6307ff339 (diff) | |
security-guidance: keep the token where the OAuth variable does not fit
A custom base URL, bare mode and cloud sessions keep ANTHROPIC_AUTH_TOKEN.
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/security-guidance/README.md | 2 | ||||
| -rw-r--r-- | plugins/security-guidance/hooks/llm.py | 14 | ||||
| -rw-r--r-- | plugins/security-guidance/tests/test_reviewer_oauth_token.py | 13 |
3 files changed, 22 insertions, 7 deletions
diff --git a/plugins/security-guidance/README.md b/plugins/security-guidance/README.md index 8678ce88..70fcd4fb 100644 --- a/plugins/security-guidance/README.md +++ b/plugins/security-guidance/README.md @@ -53,7 +53,7 @@ SECURITY_REVIEW_MODEL=claude-opus-5-5 The pattern warnings need no credentials. The LLM reviews (the diff review on Stop and the commit/push reviewer) use the first of these that applies: - **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. +- **`ANTHROPIC_API_KEY` or `ANTHROPIC_AUTH_TOKEN` in the environment** — used as is, against `ANTHROPIC_BASE_URL` if you set one. With no base URL, the commit/push reviewer treats a claude.ai token (`sk-ant-oat`) the way it treats a sign-in, below. - **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. diff --git a/plugins/security-guidance/hooks/llm.py b/plugins/security-guidance/hooks/llm.py index 1d778a99..97b3ed5c 100644 --- a/plugins/security-guidance/hooks/llm.py +++ b/plugins/security-guidance/hooks/llm.py @@ -1839,6 +1839,10 @@ def _run_inner_cli(start: Callable[[], Any], stderr_lines: List[str]) -> Any: _OAUTH_TOKEN_PREFIX = "sk-ant-oat" +# Where a token with that prefix stays in ANTHROPIC_AUTH_TOKEN: a custom base +# URL means it is 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. +_OAUTH_VARIABLE_UNSAFE_ENV = ("ANTHROPIC_BASE_URL", "CLAUDE_CODE_SIMPLE", "CLAUDE_CODE_REMOTE") def _agentic_spawn_env() -> Dict[str, str]: @@ -1890,15 +1894,16 @@ def _agentic_spawn_env() -> Dict[str, str]: # SDK grandchild has credentials. Empty string is fine (the SDK will # use whatever auth path is left). token = os.environ.get("ANTHROPIC_AUTH_TOKEN") or "" - if token.startswith(_OAUTH_TOKEN_PREFIX): + if token.startswith(_OAUTH_TOKEN_PREFIX) and not any( + os.environ.get(name) for name in _OAUTH_VARIABLE_UNSAFE_ENV + ): # 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 + token = "" env["ANTHROPIC_AUTH_TOKEN"] = token return env @@ -2095,7 +2100,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 diff --git a/plugins/security-guidance/tests/test_reviewer_oauth_token.py b/plugins/security-guidance/tests/test_reviewer_oauth_token.py index f9015a3b..5014a37f 100644 --- a/plugins/security-guidance/tests/test_reviewer_oauth_token.py +++ b/plugins/security-guidance/tests/test_reviewer_oauth_token.py @@ -1,12 +1,14 @@ """The inner CLI runs on a handed claude.ai token, not a stored login.""" import llm +import pytest 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 in ("ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN", "CLAUDE_CODE_OAUTH_TOKEN", + "ANTHROPIC_BASE_URL", "CLAUDE_CODE_SIMPLE", "CLAUDE_CODE_REMOTE"): + monkeypatch.delenv(name, raising=False) for name, value in credentials.items(): monkeypatch.setenv(name, value) return llm._agentic_spawn_env() @@ -26,6 +28,13 @@ class TestReviewerOauthToken: env = _spawn_env(monkeypatch, ANTHROPIC_AUTH_TOKEN="gateway-bearer") assert env["ANTHROPIC_AUTH_TOKEN"] == "gateway-bearer" assert "CLAUDE_CODE_OAUTH_TOKEN" not in env + assert "FORCE_PROMPT_CACHING_5M" not in env + + @pytest.mark.parametrize("name", ["ANTHROPIC_BASE_URL", "CLAUDE_CODE_SIMPLE", "CLAUDE_CODE_REMOTE"]) + def test_token_stays_put_where_the_oauth_variable_is_unsafe(self, monkeypatch, name): + env = _spawn_env(monkeypatch, ANTHROPIC_AUTH_TOKEN=OAUTH_TOKEN, **{name: "1"}) + assert env["ANTHROPIC_AUTH_TOKEN"] == OAUTH_TOKEN + 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) |
