diff options
| author | Mohamed Hegazy <mhegazy@anthropic.com> | 2026-11-07 02:01:32 +0000 |
|---|---|---|
| committer | Claude <noreply@anthropic.com> | 2026-11-07 02:01:32 +0000 |
| commit | 68bbff79681c4054d1844e009ef4320405f8f035 (patch) | |
| tree | 306720ae5aa5b0bc89898a946b5e145a13dd7566 /plugins | |
| parent | 3cb7a94734bd01ae7c977c87548fd4d235dcef19 (diff) | |
security-guidance: read blank and off variables as not set for the reviewer's tokenmhegazy/sg-reviewer-oauth-token
Co-authored-by: mhegazy <mhegazy@anthropic.com>
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/security-guidance/hooks/llm.py | 18 | ||||
| -rw-r--r-- | plugins/security-guidance/tests/test_reviewer_oauth_token.py | 7 |
2 files changed, 18 insertions, 7 deletions
diff --git a/plugins/security-guidance/hooks/llm.py b/plugins/security-guidance/hooks/llm.py index 2625c3d2..6a5eebf2 100644 --- a/plugins/security-guidance/hooks/llm.py +++ b/plugins/security-guidance/hooks/llm.py @@ -1972,10 +1972,16 @@ def _api_retry_status(msg: Any) -> Optional[int]: _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 _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]: @@ -2027,9 +2033,7 @@ 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) and not any( - os.environ.get(name) for name in _OAUTH_VARIABLE_UNSAFE_ENV - ): + 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 diff --git a/plugins/security-guidance/tests/test_reviewer_oauth_token.py b/plugins/security-guidance/tests/test_reviewer_oauth_token.py index 5014a37f..878608db 100644 --- a/plugins/security-guidance/tests/test_reviewer_oauth_token.py +++ b/plugins/security-guidance/tests/test_reviewer_oauth_token.py @@ -36,6 +36,13 @@ class TestReviewerOauthToken: assert env["ANTHROPIC_AUTH_TOKEN"] == OAUTH_TOKEN assert "CLAUDE_CODE_OAUTH_TOKEN" not in env + @pytest.mark.parametrize("name, value", [ + ("ANTHROPIC_BASE_URL", ""), ("ANTHROPIC_BASE_URL", " "), + ("CLAUDE_CODE_SIMPLE", "0"), ("CLAUDE_CODE_REMOTE", "0"), ("CLAUDE_CODE_REMOTE", "false")]) + def test_blank_or_off_counts_as_not_set(self, monkeypatch, name, value): + env = _spawn_env(monkeypatch, ANTHROPIC_AUTH_TOKEN=OAUTH_TOKEN, **{name: value}) + assert env["CLAUDE_CODE_OAUTH_TOKEN"] == OAUTH_TOKEN + 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"] == "" |
