mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(proxy): keep each provider's extra headers to that provider's own origins
All configured targets formed one trusted set, so a chat or responses request with x-headroom-base-url set to Anthropic's API received the OpenAI extra headers, and a messages request pointed at OpenAI's API or the OpenAI gateway received the Anthropic extra headers. merge_extra_headers and is_trusted_upstream take a provider. The OpenAI handlers pass "openai" (OpenAI target and default host), the Anthropic handler passes "anthropic" (Anthropic target, oMLX target and default host); HEADROOM_UPSTREAM_ALLOWED_HOSTS entries count for both. Assay-Kind: bugfix Assay-Ask: One provider's operator extra headers are sent to the other provider's API because all configured targets share one trusted set. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
9d4d552e08
commit
37ff98d0e2
@@ -204,9 +204,11 @@ reads this header for routing and then strips it before forwarding upstream.
|
||||
secrets. Because `x-headroom-base-url` is chosen by the *client*, those headers
|
||||
are only attached when the destination is one the operator designated:
|
||||
|
||||
- the origin (scheme, host and port) of a configured provider target
|
||||
(`ANTHROPIC_TARGET_API_URL`, `OPENAI_TARGET_API_URL`, and the
|
||||
Gemini/Vertex/Cloud Code equivalents), or
|
||||
- the origin (scheme, host and port) of that provider's own target: for
|
||||
`OPENAI_TARGET_API_HEADERS`, `OPENAI_TARGET_API_URL` or `https://api.openai.com`;
|
||||
for `ANTHROPIC_TARGET_API_HEADERS`, `ANTHROPIC_TARGET_API_URL`, the oMLX target
|
||||
or `https://api.anthropic.com` (the OpenAI headers never go to Anthropic's API,
|
||||
and the reverse), or
|
||||
- an entry in `HEADROOM_UPSTREAM_ALLOWED_HOSTS` (comma-separated). A bare host
|
||||
means `https` on port 443; write `host:port` for https on another port, or a
|
||||
full URL such as `http://gateway.internal:4000` for plain http.
|
||||
|
||||
@@ -69,10 +69,10 @@ normalized origin. DNS resolution is bounded by `HEADROOM_UPSTREAM_RESOLVE_TIMEO
|
||||
|
||||
**What may ride along with it.** The operator's own `*_extra_headers` secrets — the two
|
||||
credential-shaped maps in [the settings store](#the-settings-store) — travel only to a
|
||||
host the operator designated. Designated means one of the resolved provider API targets
|
||||
(`ANTHROPIC_TARGET_API_URL`, `OPENAI_TARGET_API_URL`, and the Gemini/Vertex/Bedrock/Cloud
|
||||
Code equivalents), the default `https://api.anthropic.com` / `https://api.openai.com`,
|
||||
or an entry in `HEADROOM_UPSTREAM_ALLOWED_HOSTS` (a bare host means https on port 443;
|
||||
host the operator designated for that provider. Designated means that provider's own
|
||||
API target (`OPENAI_TARGET_API_URL` or the default `https://api.openai.com` for the OpenAI
|
||||
headers; `ANTHROPIC_TARGET_API_URL`, the oMLX target or the default
|
||||
`https://api.anthropic.com` for the Anthropic headers), or an entry in `HEADROOM_UPSTREAM_ALLOWED_HOSTS` (a bare host means https on port 443;
|
||||
`host:port` and full URLs name exactly that origin). Matching is exact equality on the
|
||||
*parsed* origin (scheme, hostname, port), never the URL string, so neither
|
||||
`https://api.anthropic.com@evil.example` nor `https://api.anthropic.com.evil.example`
|
||||
|
||||
@@ -192,6 +192,7 @@ def _build_outbound_anthropic_headers(
|
||||
_select_extra_headers_for_destination(extra_headers, routed_to_omlx=routed_to_omlx),
|
||||
upstream_url=upstream_url,
|
||||
config=config,
|
||||
provider="anthropic",
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -3681,6 +3681,7 @@ class OpenAIHandlerMixin:
|
||||
else self._openai_extra_headers_for_upstream(upstream_base_url),
|
||||
upstream_url=(None if self.anthropic_backend is not None else custom_upstream_base_url),
|
||||
config=self.config,
|
||||
provider="openai",
|
||||
)
|
||||
log_outbound_headers(
|
||||
forwarder="openai_chat_completions",
|
||||
@@ -6381,6 +6382,7 @@ class OpenAIHandlerMixin:
|
||||
self._openai_extra_headers_for_upstream(openai_upstream_base_url),
|
||||
upstream_url=custom_upstream_base_url,
|
||||
config=self.config,
|
||||
provider="openai",
|
||||
)
|
||||
# Mirror the WS handler: never forward Codex's client-only lite header
|
||||
# upstream. OpenAI rejects newer Codex models when it leaks, and the HTTP
|
||||
|
||||
@@ -2183,6 +2183,7 @@ def merge_extra_headers(
|
||||
*,
|
||||
upstream_url: str | None,
|
||||
config: Any = None,
|
||||
provider: str | None = None,
|
||||
) -> dict[str, str]:
|
||||
"""Merge configured extra headers into ``headers``, overriding same-named keys.
|
||||
|
||||
@@ -2201,14 +2202,16 @@ def merge_extra_headers(
|
||||
Pass ``None`` when the caller is going to its configured target with no
|
||||
per-request override. Anything else is checked against
|
||||
``upstream_trust.is_trusted_upstream``; an undesignated host still gets its
|
||||
request proxied, just without these headers.
|
||||
request proxied, just without these headers. ``provider`` (``"openai"`` or
|
||||
``"anthropic"``) limits the designated origins to that provider's own, so
|
||||
one provider's extras never go to the other's API.
|
||||
"""
|
||||
if not extra:
|
||||
return headers
|
||||
if upstream_url is not None:
|
||||
from headroom.proxy.upstream_trust import is_trusted_upstream, warn_untrusted_once
|
||||
|
||||
if not is_trusted_upstream(upstream_url, config):
|
||||
if not is_trusted_upstream(upstream_url, config, provider):
|
||||
warn_untrusted_once(upstream_url)
|
||||
return headers
|
||||
# HTTP header names are case-insensitive: drop any existing key that
|
||||
|
||||
@@ -39,6 +39,7 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
from collections.abc import Iterable
|
||||
from typing import Any
|
||||
from urllib.parse import urlparse
|
||||
|
||||
@@ -59,6 +60,17 @@ _API_URL_ATTRS = (
|
||||
"omlx_target_api_url",
|
||||
)
|
||||
|
||||
#: Each provider's extra headers are scoped to that provider's own targets: the
|
||||
#: OpenAI extras are not for Anthropic's API, and the reverse.
|
||||
_PROVIDER_API_URL_ATTRS = {
|
||||
"anthropic": ("anthropic_api_url", "omlx_target_api_url"),
|
||||
"openai": ("openai_api_url",),
|
||||
}
|
||||
_PROVIDER_DEFAULT_HOSTS = {
|
||||
"anthropic": "api.anthropic.com",
|
||||
"openai": "api.openai.com",
|
||||
}
|
||||
|
||||
# Hosts that are always operator-designated: they are what the provider targets
|
||||
# resolve to when nothing is overridden, so omitting them would refuse the
|
||||
# headers on a completely default install.
|
||||
@@ -125,12 +137,24 @@ def _origin(value: str | None) -> Origin | None:
|
||||
return scheme, host.lower(), port if port is not None else _DEFAULT_PORTS.get(scheme, 0)
|
||||
|
||||
|
||||
def trusted_upstream_origins(config: Any = None) -> frozenset[Origin]:
|
||||
"""Origins (scheme, host, port) permitted to receive operator-configured secret headers."""
|
||||
def trusted_upstream_origins(config: Any = None, provider: str | None = None) -> frozenset[Origin]:
|
||||
"""Origins (scheme, host, port) permitted to receive operator-configured secret headers.
|
||||
|
||||
origins: set[Origin] = {("https", host, 443) for host in _DEFAULT_HOSTS}
|
||||
candidates = [getattr(config, attr, None) for attr in _API_URL_ATTRS]
|
||||
candidates.append(resolve_omlx_target_api_url(environ=os.environ))
|
||||
With ``provider`` (``"openai"`` or ``"anthropic"``) only that provider's own
|
||||
targets count, plus ``HEADROOM_UPSTREAM_ALLOWED_HOSTS``; without it, every
|
||||
configured target does.
|
||||
"""
|
||||
|
||||
if provider is None:
|
||||
hosts: Iterable[str] = _DEFAULT_HOSTS
|
||||
attrs: Iterable[str] = _API_URL_ATTRS
|
||||
else:
|
||||
hosts = (_PROVIDER_DEFAULT_HOSTS[provider],)
|
||||
attrs = _PROVIDER_API_URL_ATTRS[provider]
|
||||
origins: set[Origin] = {("https", host, 443) for host in hosts}
|
||||
candidates = [getattr(config, attr, None) for attr in attrs]
|
||||
if provider in (None, "anthropic"):
|
||||
candidates.append(resolve_omlx_target_api_url(environ=os.environ))
|
||||
candidates.extend(os.environ.get(ALLOWED_HOSTS_ENV, "").split(","))
|
||||
for candidate in candidates:
|
||||
origin = _origin(candidate) if isinstance(candidate, str) else None
|
||||
@@ -139,7 +163,7 @@ def trusted_upstream_origins(config: Any = None) -> frozenset[Origin]:
|
||||
return frozenset(origins)
|
||||
|
||||
|
||||
def is_trusted_upstream(url: str | None, config: Any = None) -> bool:
|
||||
def is_trusted_upstream(url: str | None, config: Any = None, provider: str | None = None) -> bool:
|
||||
"""True when ``url`` is on an origin the operator designated.
|
||||
|
||||
``None``/empty means "no per-request override" — the handler is going to the
|
||||
@@ -153,7 +177,7 @@ def is_trusted_upstream(url: str | None, config: Any = None) -> bool:
|
||||
if not origin:
|
||||
# Unparseable destination: refuse rather than guess.
|
||||
return False
|
||||
return origin in trusted_upstream_origins(config)
|
||||
return origin in trusted_upstream_origins(config, provider)
|
||||
|
||||
|
||||
def warn_untrusted_once(url: str | None, *, request_id: str | None = None) -> None:
|
||||
|
||||
@@ -75,3 +75,50 @@ def test_allowed_hosts_entries_designate_one_origin(
|
||||
)
|
||||
def test_malformed_destinations_are_not_trusted(url: str) -> None:
|
||||
assert not is_trusted_upstream(url)
|
||||
|
||||
|
||||
def test_each_provider_extras_stay_with_that_provider() -> None:
|
||||
"""OpenAI extras never go to Anthropic's API, and Anthropic extras never go to OpenAI's."""
|
||||
openai_extra = {"x-operator-oai": "oai-secret"}
|
||||
anthropic_extra = {"x-operator-ant": "ant-secret"}
|
||||
cfg = SimpleNamespace(openai_api_url="https://gateway.example/v1")
|
||||
|
||||
def sent(extra: dict[str, str], url: str, provider: str) -> dict[str, str]:
|
||||
return merge_extra_headers({}, extra, upstream_url=url, config=cfg, provider=provider)
|
||||
|
||||
assert sent(openai_extra, "https://api.openai.com/v1", "openai") == openai_extra
|
||||
assert sent(openai_extra, "https://gateway.example/v1", "openai") == openai_extra
|
||||
assert sent(openai_extra, "https://api.anthropic.com/v1", "openai") == {}
|
||||
assert sent(anthropic_extra, "https://api.anthropic.com/v1", "anthropic") == anthropic_extra
|
||||
assert sent(anthropic_extra, "https://api.openai.com/v1", "anthropic") == {}
|
||||
assert sent(anthropic_extra, "https://gateway.example/v1", "anthropic") == {}
|
||||
|
||||
|
||||
def test_an_allowed_host_is_designated_for_both_providers(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setenv(ALLOWED_HOSTS_ENV, "shared-gateway.example")
|
||||
for provider in ("openai", "anthropic"):
|
||||
assert is_trusted_upstream("https://shared-gateway.example/v1", None, provider)
|
||||
|
||||
|
||||
def test_the_handlers_scope_extras_to_their_own_provider(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""Wiring check: chat sends only OpenAI extras, messages only Anthropic extras."""
|
||||
from headroom.proxy.handlers.anthropic import _build_outbound_anthropic_headers
|
||||
from tests.test_grok_shared_proxy_call_sites import _OPENAI_CLIENT_HEADERS, _run_chat
|
||||
|
||||
monkeypatch.setenv("HEADROOM_ALLOWED_BASE_URLS", "api.anthropic.com")
|
||||
outbound = _run_chat(
|
||||
{**_OPENAI_CLIENT_HEADERS, "x-headroom-base-url": "https://api.anthropic.com"},
|
||||
model="gpt-4o-mini",
|
||||
extras={"X-Operator-Sentinel": "must-not-leak"},
|
||||
)
|
||||
assert outbound.url.startswith("https://api.anthropic.com")
|
||||
assert "x-operator-sentinel" not in outbound.headers
|
||||
|
||||
headers = _build_outbound_anthropic_headers(
|
||||
{},
|
||||
{"x-operator-ant": "ant-secret"},
|
||||
routed_to_omlx=False,
|
||||
upstream_url="https://api.openai.com",
|
||||
config=SimpleNamespace(),
|
||||
)
|
||||
assert "x-operator-ant" not in headers
|
||||
|
||||
Reference in New Issue
Block a user