Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(model-hub): verify an Anthropic key in the header the engine sends it in (#2751)

Call White4f7230dfa028
diff --git a/tests/e2e/test_model_hub_routing.py b/tests/e2e/test_model_hub_routing.py
index cb3f5c8e1..36c800062 100644
--- a/tests/e2e/test_model_hub_routing.py
+++ b/tests/e2e/test_model_hub_routing.py
@@ -822,8 +822,8 @@ def test_d2_healthy_hop_is_served_by_the_real_engine_probe(
if item["path"] == "/v1/messages"
]
assert captured
- # The engine forwards Anthropic credentials as Bearer; x-api-key is
- # used only by Avibe's direct discovery probe path.
+ # The engine forwards a custom-origin Anthropic key as Bearer, the
+ # header Avibe's observation verified it with (#2667).
assert all(
item["headers"].get("authorization")
== f"Bearer {SYNTHETIC_API_KEY}"
diff --git a/tests/e2e/test_model_hub_service_acceptance.py b/tests/e2e/test_model_hub_service_acceptance.py
index 555041973..70c1540ec 100644
--- a/tests/e2e/test_model_hub_service_acceptance.py
+++ b/tests/e2e/test_model_hub_service_acceptance.py
@@ -377,9 +377,11 @@ def test_fresh_install_serves_vibey_and_claude_through_the_hub_and_reaps_an_orph
service = _Service(home, _free_port())
service.seed_config()
started: list[dict[str, Any]] = []
+ # The Anthropic relay takes the key only as Bearer, which is how the engine
+ # sends a custom relay's key, so the Source must also verify with it (#2667).
with _upstream(protocol="openai_chat", models=[_CHAT_MODEL], required_api_key=_CHAT_KEY) as chat, _upstream(
protocol="anthropic", models=[_CLAUDE_MODEL], required_api_key=_ANTHROPIC_KEY,
- required_auth_scheme="protocol",
+ required_auth_scheme="bearer",
) as anthropic:
try:
service.vibe("start", "--no-open-browser", timeout=300)
@@ -405,10 +407,6 @@ def test_fresh_install_serves_vibey_and_claude_through_the_hub_and_reaps_an_orph
"kind": "api_key", "vendor": "custom", "display_name": "Acceptance Anthropic",
"base_url": anthropic.url, "key": _ANTHROPIC_KEY, "protocol": "anthropic",
}, timeout=120)["source"]
- # Avibe checks an Anthropic key with x-api-key; the engine sends a
- # custom relay's key as a bearer token, so the turn must carry it so.
- anthropic.configure(required_auth_scheme="bearer")
-
# A Vibey turn gets the upstream's reply through the hub.
_route(service, "vibey", _VIBEY_MODEL, chat_source["id"], _CHAT_MODEL)
vibey = _turn(service, "vibey")
diff --git a/tests/e2e/test_model_hub_sources.py b/tests/e2e/test_model_hub_sources.py
index 1622214f2..09cc4ed05 100644
--- a/tests/e2e/test_model_hub_sources.py
+++ b/tests/e2e/test_model_hub_sources.py
@@ -42,8 +42,8 @@ def _require_key(upstream: MockLLMUpstream, key: str) -> None:
A model-free probe never proves a key (#1920): the declared interface's
model listing does, only where it refuses a request without the key
- (#1936). The engine sends a custom-origin Anthropic key as Bearer, and
- observation sends the protocol's header, so the mock takes either.
+ (#1936). The engine and observation both send a custom-origin Anthropic
+ key as Bearer (#2667); the mock takes either header, as most relays do.
"""
upstream.configure(required_api_key=key, required_auth_scheme="either")
diff --git a/tests/scenarios/INDEX.yaml b/tests/scenarios/INDEX.yaml
index 90bab9037..37dd71a7b 100644
--- a/tests/scenarios/INDEX.yaml
+++ b/tests/scenarios/INDEX.yaml
@@ -100,6 +100,7 @@ capabilities:
- tests/test_model_hub_request_body_budget.py
- tests/test_model_hub_declared_tools.py
- tests/test_model_hub_l3.py
+ - tests/test_model_hub_migration_bearer.py
- tests/test_model_hub_hosted_executor.py
- tests/test_model_hub_hosted_evidence.py
- tests/test_model_hub_hosted_images.py
diff --git a/tests/scenarios/auth_setup/test_auth_setup_scenarios.py b/tests/scenarios/auth_setup/test_auth_setup_scenarios.py
index 7fd19020b..852947cd0 100644
--- a/tests/scenarios/auth_setup/test_auth_setup_scenarios.py
+++ b/tests/scenarios/auth_setup/test_auth_setup_scenarios.py
@@ -3830,10 +3830,9 @@ def test_api_key_setup_does_not_schedule_a_model(
body = await request.json() if request.method == "POST" else None
requests.append((request.method, request.path, body))
supplied_key = (
- request.headers.get("x-api-key")
- if protocol == "anthropic"
- else request.headers.get("x-goog-api-key")
+ request.headers.get("x-goog-api-key")
if protocol == "google"
+ # At this custom origin the engine sends an Anthropic key as Bearer too.
else request.headers.get("Authorization", "").removeprefix("Bearer ")
) or ""
observed_credentials.add(supplied_key)
diff --git a/tests/scenarios/model_hub/catalog.yaml b/tests/scenarios/model_hub/catalog.yaml
index 16b3e9da3..1e88dd1be 100644
--- a/tests/scenarios/model_hub/catalog.yaml
+++ b/tests/scenarios/model_hub/catalog.yaml
@@ -43,6 +43,7 @@ capability:
- tests/test_model_hub_request_body_budget.py
- tests/test_model_hub_declared_tools.py
- tests/test_model_hub_l3.py
+ - tests/test_model_hub_migration_bearer.py
- tests/test_model_hub_hosted_executor.py
- tests/test_model_hub_hosted_evidence.py
- tests/test_model_hub_hosted_images.py
@@ -1019,6 +1020,16 @@ scenarios:
kind: observation
layer: scenario
test: tests/scenarios/model_hub/test_model_hub_configured_route_scenarios.py::test_mh_protocol_003_manual_selection_requires_matching_response_proof
+ - id: MH-PROTOCOL-005
+ name: >-
+ An Anthropic API key is verified in the header the engine sends it in, x-api-key only at the official origin
+ and Bearer at every other, so a Source verified at add is one the engine's calls authenticate to
+ status: covered
+ kind: contract
+ layer: contract
+ test: tests/test_model_hub_migration_bearer.py::test_a_custom_origin_key_is_verified_as_the_engine_will_send_it
+ related_tests:
+ - tests/test_model_hub_migration_bearer.py::test_an_anthropic_key_is_observed_in_the_header_the_pinned_engine_sends
- id: MH-PROTOCOL-004
name: Codex Model Hub launches use standard Responses provider capabilities
status: partial
diff --git a/tests/test_model_hub_l3.py b/tests/test_model_hub_l3.py
index ee0ec23c9..edecd9cf5 100644
--- a/tests/test_model_hub_l3.py
+++ b/tests/test_model_hub_l3.py
@@ -7910,7 +7910,8 @@ def test_declared_custom_anthropic_relay_verifies_from_its_models_listing(
probe_bodies: list[dict] = []
def supplied(request: web.Request) -> str:
- return request.headers.get("x-api-key", "")
+ # The engine sends a custom relay's key as Bearer, so observation does too.
+ return request.headers.get("Authorization", "").removeprefix("Bearer ")
async def relay_messages(request: web.Request) -> web.Response:
requests.append((request.method, request.path, supplied(request)))
diff --git a/tests/test_model_hub_migration_bearer.py b/tests/test_model_hub_migration_bearer.py
index c22e6b147..e207465f9 100644
--- a/tests/test_model_hub_migration_bearer.py
+++ b/tests/test_model_hub_migration_bearer.py
@@ -16,8 +16,7 @@ from core.handlers.model_hub.adapter import SourceBinding
from core.handlers.model_hub.service import ModelHubError
from tests.test_model_hub_api import _service
from vibe.model_hub_runtime.adapter import CLIProxyEngineAdapter
-from vibe.model_hub_runtime.api_key_vendors import validate_api_key_auth_scheme
-from vibe.model_hub_runtime.client import probe_models
+from vibe.model_hub_runtime.api_key_vendors import anthropic_api_key_header, validate_api_key_auth_scheme
from vibe.model_hub_runtime.config import write_engine_config
from vibe.model_hub_runtime.state import EngineStateError, EngineStateStore, RuntimeSecrets
@@ -64,10 +63,9 @@ async def _relay(*, public_models=False, protocol_success=False, scheme="bearer"
def supplied(request):
requests.append((request.method, request.path, dict(request.headers)))
assert request.headers.get("anthropic-version") == "2023-06-01"
+ # The relay reads the key from one header only; a key in the other is no key.
if scheme == "bearer":
- assert "x-api-key" not in request.headers
return request.headers.get("Authorization", "").removeprefix("Bearer ")
- assert "Authorization" not in request.headers
return request.headers.get("x-api-key", "")
async def messages(request):
@@ -288,12 +286,47 @@ def test_public_payloads_cannot_select_private_scheme(tmp_path):
asyncio.run(scenario())
-def test_legacy_callers_keep_original_header_and_optional_keyword_absence(tmp_path):
[email protected]("url,key,header", [
+ ("https://api.anthropic.com", KEY, "x-api-key"),
+ ("https://API.ANTHROPIC.COM:443/v1", KEY, "x-api-key"),
+ # CPA takes any key containing sk-ant-oat as OAuth, which it sends as Bearer.
+ ("https://api.anthropic.com", "fixture-sk-ant-oat-inside", "bearer"),
+ ("https://api.anthropic.com:0443", KEY, "bearer"),
+ ("https://api.anthropic.com:8443", KEY, "bearer"),
+ ("http://api.anthropic.com", KEY, "bearer"),
+ (CUSTOM, KEY, "bearer"),
+])
+def test_an_anthropic_key_is_observed_in_the_header_the_pinned_engine_sends(url, key, header):
+ assert anthropic_api_key_header(url, key) == (
+ {"x-api-key": key} if header == "x-api-key" else {"Authorization": f"Bearer {key}"}
+ )
+
+
[email protected]("accepts", ["bearer", "x-api-key"])
+def test_a_custom_origin_key_is_verified_as_the_engine_will_send_it(tmp_path, accepts):
+ """Scenario: MH-PROTOCOL-005 -- the engine sends a custom-origin key as Bearer, so a verdict at add predicts its calls (#2667)."""
+ async def scenario():
+ service, _, _ = _service_with_custody(tmp_path)
+ async with _relay(scheme=accepts) as (origin, requests, _):
+ pending = service._require_proven_source_payload({"vendor": "anthropic", "base_url": origin, "key": KEY})
+ if accepts == "bearer":
+ assert (await pending).authenticated is True
+ else:
+ # The relay refuses the header every engine call would carry.
+ with pytest.raises(ModelHubError) as refused:
+ await pending
+ assert refused.value.detail == "modelHub.errors.authentication_failed"
+ assert requests[0][2]["Authorization"] == f"Bearer {KEY}"
+ # With the bearer relay this includes the listing that witnesses the key.
+ assert len(requests) == (3 if accepts == "bearer" else 1)
+ for _, _, headers in requests:
+ assert "x-api-key" not in {name.lower() for name in headers}
+
+ asyncio.run(scenario())
+
+
+def test_a_caller_without_a_scheme_passes_no_scheme_keyword(tmp_path):
async def scenario():
- async with _relay(scheme="legacy") as (origin, requests, _):
- result = await probe_models(vendor="anthropic", protocol="anthropic", base_url=origin, secret=KEY)
- assert result[0].id == "claude-sonnet-4-6"
- assert requests[0][2]["x-api-key"] == KEY
service, _, adapter = _service(tmp_path)
# The existing fake deliberately has no auth_scheme keyword.
result = await service._require_proven_source_payload({"vendor": "anthropic", "key": KEY})
@@ -307,7 +340,7 @@ def test_legacy_callers_keep_original_header_and_optional_keyword_absence(tmp_pa
def test_service_replace_preserves_private_scheme_through_actual_discovery(tmp_path, scheme):
async def scenario():
service, config, runtime = _service_with_custody(tmp_path)
- async with _relay(scheme=scheme or "legacy") as (origin, requests, accepted):
+ async with _relay() as (origin, requests, accepted):
ref = await runtime.provision_credential("anthropic", "anthropic", KEY, origin, auth_scheme=scheme)
source = ModelHubSourceConfig.from_payload({
"id": "src_bearerfixture", "kind": "api_key", "vendor": "anthropic",
@@ -332,9 +365,8 @@ def test_service_replace_preserves_private_scheme_through_actual_discovery(tmp_p
assert await runtime.credential_auth_scheme(replacement) == scheme
assert runtime.state_store.credential_metadata_if_present(ref) is None
assert "auth_scheme" not in answer["source"]
- assert requests[0][2].get("Authorization" if scheme else "x-api-key") == (
- "Bearer fixture-replacement" if scheme else "fixture-replacement"
- )
+ # At a custom origin the engine sends Bearer with or without the scheme.
+ assert requests[0][2]["Authorization"] == "Bearer fixture-replacement"
asyncio.run(scenario())
diff --git a/tests/test_model_hub_replacement_transport.py b/tests/test_model_hub_replacement_transport.py
index c65c9c6e0..13a85c3d9 100644
--- a/tests/test_model_hub_replacement_transport.py
+++ b/tests/test_model_hub_replacement_transport.py
@@ -123,7 +123,7 @@ def test_new_bearer_namespace_is_immutable_and_reserved_before_secret(tmp_path):
])
def test_replacement_recovers_without_old_secret_and_retires_namespace(tmp_path, scheme, damage):
async def scenario():
- async with _relay(scheme=scheme or "legacy") as (origin, requests, accepted):
+ async with _relay() as (origin, requests, accepted):
service, config, runtime, source, ref = await _source(tmp_path, origin, scheme)
_damage(runtime.state_store, ref, damage)
accepted.add("fixture-replacement")
@@ -133,9 +133,8 @@ def test_replacement_recovers_without_old_secret_and_retires_namespace(tmp_path,
assert await runtime.credential_auth_scheme(new_ref) == scheme
assert config.config.sources[0].credential_ref == new_ref
assert "auth_scheme" not in result["source"]
- assert requests and requests[0][2].get("Authorization" if scheme else "x-api-key") == (
- "Bearer fixture-replacement" if scheme else "fixture-replacement"
- )
+ # At a custom origin the engine sends Bearer with or without the scheme.
+ assert requests and requests[0][2]["Authorization"] == "Bearer fixture-replacement"
assert not service.revocations.list()
assert all(not path.exists() for path in runtime.state_store._credential_namespace_paths(ref))
@@ -258,7 +257,7 @@ def test_real_consumers_cannot_downgrade_tagged_metadata(tmp_path, consumer):
@pytest.mark.parametrize("scheme", [None, "bearer"])
def test_recovered_scheme_does_not_bypass_new_key_authentication(tmp_path, scheme):
async def scenario():
- async with _relay(scheme=scheme or "legacy") as (origin, requests, _):
+ async with _relay() as (origin, requests, _):
service, config, runtime, source, ref = await _source(tmp_path, origin, scheme)
_damage(runtime.state_store, ref, "bad_json")
with pytest.raises(ModelHubError):
diff --git a/vibe/model_hub_runtime/adapter.py b/vibe/model_hub_runtime/adapter.py
index f3a59c951..241a67eae 100644
--- a/vibe/model_hub_runtime/adapter.py
+++ b/vibe/model_hub_runtime/adapter.py
@@ -58,7 +58,11 @@ from vibe.model_hub_runtime.client import (
probe_models,
upstream_api_url,
)
-from vibe.model_hub_runtime.api_key_vendors import pinned_api_key_protocol, validate_api_key_auth_scheme
+from vibe.model_hub_runtime.api_key_vendors import (
+ anthropic_api_key_header,
+ pinned_api_key_protocol,
+ validate_api_key_auth_scheme,
+)
from vibe.model_hub_runtime.installer import (
INSTALL_ALREADY_RUNNING_REASON,
INSTALL_PLATFORM_UNSUPPORTED_REASON,
@@ -759,18 +763,15 @@ async def _probe_protocol_response(
raise EngineClientError("unsupported source protocol")
try:
url = upstream_api_url(root, taxonomy.request_path)
+ credential = anthropic_api_key_header(root, secret) if protocol == "anthropic" else {
+ "Authorization": f"Bearer {secret}",
+ }
except (TypeError, ValueError):
raise EngineClientError("source base URL is invalid")
assert url is not None
- headers = {
- "Authorization": f"Bearer {secret}",
- "Accept": "application/json",
- }
+ headers = {**credential, "Accept": "application/json"}
if protocol == "anthropic":
headers["anthropic-version"] = "2023-06-01"
- if auth_scheme != "bearer":
- headers.pop("Authorization")
- headers["x-api-key"] = secret
client_timeout = aiohttp.ClientTimeout(total=timeout)
async def observe() -> _ProtocolEvidence:
diff --git a/vibe/model_hub_runtime/api_key_vendors.py b/vibe/model_hub_runtime/api_key_vendors.py
index aaaec3b2d..f1caf327d 100644
--- a/vibe/model_hub_runtime/api_key_vendors.py
+++ b/vibe/model_hub_runtime/api_key_vendors.py
@@ -54,6 +54,19 @@ def _validated_cpa_anthropic_origin(base_url: str | None) -> bool:
)
+def anthropic_api_key_header(base_url: str, secret: str) -> dict[str, str]:
+ """The header the pinned engine sends a static Anthropic key in.
+
+ Observation authenticates exactly as the engine will, so a verdict at add
+ predicts every later call. CPA (``applyClaudeHeadersWithNativeProfile``)
+ sends ``x-api-key`` only to its official origin, and Bearer to every other
+ origin and for any key its ``sk-ant-oat`` heuristic takes as OAuth.
+ """
+ if _validated_cpa_anthropic_origin(base_url) and "sk-ant-oat" not in secret:
+ return {"x-api-key": secret}
+ return {"Authorization": f"Bearer {secret}"}
+
+
def validate_api_key_auth_scheme(
vendor: str,
protocol: str | None,
diff --git a/vibe/model_hub_runtime/client.py b/vibe/model_hub_runtime/client.py
index 8859ed2f5..32d39ed88 100644
--- a/vibe/model_hub_runtime/client.py
+++ b/vibe/model_hub_runtime/client.py
@@ -50,6 +50,7 @@ from core.handlers.model_hub.stream_wire import (
observe_buffered_protocol_response,
)
from vibe.model_hub_runtime.api_key_vendors import (
+ anthropic_api_key_header,
google_api_base_url,
official_api_key_base_urls,
validate_api_key_auth_scheme,
@@ -952,19 +953,17 @@ async def probe_models(
return await _probe_google_models(root, secret=secret, timeout=timeout)
try:
url = upstream_api_url(root, "/v1/models")
+ credential = {} if secret is None else (
+ anthropic_api_key_header(root, secret) if protocol == "anthropic" else {"Authorization": f"Bearer {secret}"}
+ )
except (TypeError, ValueError):
raise EngineClientError("source base URL is invalid")
assert url is not None
- headers = {"Accept": "application/json"}
+ headers = {"Accept": "application/json", **credential}
if protocol == "anthropic":
# The version header is part of the interface, not the credential, so it
# stays on the uncredentialed request: it must differ in exactly one way.
headers["anthropic-version"] = "2023-06-01"
- if secret is not None:
- if protocol == "anthropic" and auth_scheme != "bearer":
- headers["x-api-key"] = secret
- else:
- headers["Authorization"] = f"Bearer {secret}"
deadline = time.monotonic() + timeout
client_timeout = aiohttp.ClientTimeout(total=timeout)
try: