1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
"""Which credential a review sends to the default API host. Made-up values."""
import os
import sys
import pytest
import llm
from conftest import edit_payload, run_hook, stop_payload, ups_payload, VULN_PY
OAT = "sk-ant-oat01-made-up"
KEY = "sk-ant-api03-made-up"
BEARER = "vck_made-up"
K, T, B = "ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_BASE_URL"
GATEWAY = "https://gateway.example.com"
def _route(env, portable=False):
env = dict(env)
return llm._route_credentials(env, portable), env
@pytest.mark.parametrize("portable", [False, True])
@pytest.mark.parametrize("before, after", [
({K: OAT}, {T: OAT}),
({K: OAT, B: ""}, {T: OAT, B: ""}),
({K: OAT, B: " "}, {T: OAT, B: " "}),
({K: OAT, T: ""}, {T: OAT}),
# The session's own token is kept, the key could not have worked.
({K: OAT, T: OAT + "2"}, {T: OAT + "2"}),
({K: KEY}, {K: KEY}),
({K: KEY, T: OAT}, {K: KEY, T: OAT}),
({K: ""}, {K: ""}),
({T: OAT}, {T: OAT}),
({}, {}),
({K: OAT, B: "https://api.anthropic.com"}, {T: OAT, B: "https://api.anthropic.com"}),
# With a base URL the token was meant for it.
({T: BEARER, B: "https://api.anthropic.com"}, {T: BEARER, B: "https://api.anthropic.com"}),
({K: OAT, B: "https://["}, {K: OAT, B: "https://["}),
# A gateway decides for itself what it takes.
({K: OAT, B: GATEWAY}, {K: OAT, B: GATEWAY}),
({T: BEARER, B: GATEWAY}, {T: BEARER, B: GATEWAY}),
])
def test_claude_ai_token_as_key(before, after, portable):
assert _route(before, portable) == (False, after)
@pytest.mark.parametrize("before, after", [
({T: BEARER}, {}),
({T: BEARER, B: ""}, {B: ""}),
({K: KEY, T: BEARER}, {K: KEY}),
({K: OAT, T: BEARER}, {T: OAT}),
])
def test_other_bearer_without_a_base_url_on_another_host(before, after):
assert _route(before, portable=True) == (True, after)
@pytest.mark.parametrize("env", [{T: BEARER}, {K: KEY, T: BEARER}, {K: OAT, T: BEARER}])
def test_other_bearer_in_claude_code_is_left_alone(env):
assert _route(env) == (False, env)
@pytest.mark.parametrize("env", [{K: OAT}, {T: BEARER}, {K: OAT, T: BEARER}])
def test_cloud_provider_is_left_alone(env, monkeypatch):
monkeypatch.setattr(llm, "_HAS_3P_PROVIDER_AT_LOAD", True)
assert _route(env, portable=True) == (False, env)
def _load(hook_env, **extra):
"""What a fresh import of llm makes of the environment."""
import subprocess
env = {k: v for k, v in hook_env.items() if k not in (K, T, B, "CLAUDECODE", "CLAUDE_CODE_ENTRYPOINT")}
env.update(extra)
code = ("import sys; sys.path.insert(0, %r); import llm; e = llm._agentic_spawn_env(); "
"import os; child = {**os.environ, **e}; "
"print(repr([llm.ANTHROPIC_API_KEY, llm.ANTHROPIC_AUTH_TOKEN, llm.HAS_API_CREDENTIALS, "
"llm.AUTH_TOKEN_HELD_BACK, child.get('ANTHROPIC_API_KEY') or '', "
"child.get('ANTHROPIC_AUTH_TOKEN') or child.get('CLAUDE_CODE_OAUTH_TOKEN') or '']))"
% os.path.dirname(llm.__file__))
r = subprocess.run([sys.executable, "-c", code], env=env, capture_output=True, text=True)
assert r.returncode == 0, r.stderr
import ast
return ast.literal_eval(r.stdout.strip().splitlines()[-1])
def test_the_reviewing_cli_gets_the_token_as_a_token(hook_env):
assert _load(hook_env, **{K: OAT}) == ["", OAT, True, False, "", OAT]
def test_api_key_is_unchanged(hook_env):
assert _load(hook_env, **{K: KEY}) == [KEY, "", True, False, KEY, ""]
def test_held_back_token_reaches_nothing(hook_env):
assert _load(hook_env, SECURITY_GUIDANCE_PORTABLE_OUTPUT="1", **{T: BEARER}) == [
"", "", False, True, "", ""]
def test_same_token_in_claude_code_is_sent_as_before(hook_env):
assert _load(hook_env, **{T: BEARER}) == ["", BEARER, True, False, "", BEARER]
class TestHook:
@pytest.fixture
def env(self, hook_env):
e = {k: v for k, v in hook_env.items() if k not in (K, T, B)}
for name in ("CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC", "DISABLE_TELEMETRY", "DO_NOT_TRACK"):
e.pop(name, None)
e["SECURITY_GUIDANCE_PORTABLE_OUTPUT"] = "1"
return e
@pytest.mark.parametrize("other", [{}, {"DISABLE_TELEMETRY": "1"}])
def test_held_back_token_is_said_once(self, workspace, env, other):
_, repo = workspace
env.update(other)
env[T] = BEARER
(repo / "app.py").write_text(VULN_PY)
payload = edit_payload(repo, repo / "app.py", new_string=VULN_PY)
_, out, _ = run_hook(payload, env)
assert "ANTHROPIC_BASE_URL is not set, so it was not sent anywhere" in out
assert BEARER not in out
_, out, _ = run_hook(payload, env)
assert "not running" not in out
def test_with_its_base_url_the_review_runs(self, workspace, env, stub_api):
_, repo = workspace
env[T] = BEARER
env[B] = f"http://127.0.0.1:{stub_api.server_port}"
run_hook(ups_payload(repo), env)
(repo / "app.py").write_text(VULN_PY)
run_hook(stop_payload(repo), env)
assert stub_api.calls.count("/v1/messages") == 1
|