Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(proxy): prefer explicit proxy auth header (#3422)

宇宙的山峰f3965f7c59ce
diff --git a/headroom/proxy/server.py b/headroom/proxy/server.py
index 8ff2e7eca..215c06b63 100644
--- a/headroom/proxy/server.py
+++ b/headroom/proxy/server.py
@@ -2624,11 +2624,18 @@ def read_proxy_token(headers: Mapping[str, str]) -> str | None:
expected to be lowercase (Starlette's ``Headers`` is case-insensitive; the
WebSocket middleware lowercases the raw ASGI pairs itself).
"""
+ raw = headers.get("x-headroom-proxy-token")
+ if raw is not None:
+ return str(raw) or None
+
+ # A provider credential can legitimately occupy Authorization (for example,
+ # an OAuth/subscription client). Prefer the explicit proxy header whenever
+ # it is present so that the upstream credential is not mistaken for the
+ # proxy credential.
auth = str(headers.get("authorization") or "")
if auth.lower().startswith("bearer "):
return auth[7:].strip() or None
- raw = headers.get("x-headroom-proxy-token")
- return str(raw) if raw else None
+ return None
class WebSocketAuthMiddleware:
diff --git a/tests/test_proxy_hardening.py b/tests/test_proxy_hardening.py
index 85ddec4d8..06221cc8d 100644
--- a/tests/test_proxy_hardening.py
+++ b/tests/test_proxy_hardening.py
@@ -65,6 +65,19 @@ class TestInboundAuthToken:
resp = c.get("/stats", headers={"X-Headroom-Proxy-Token": "s3cr3t-token"})
assert resp.status_code != 401
+ def test_token_set_accepts_custom_header_with_upstream_oauth(self):
+ """The upstream OAuth bearer must not override the proxy credential."""
+ app = _make_app(proxy_token="s3cr3t-token")
+ with TestClient(app, base_url="http://testserver", client=NONLOOPBACK) as c:
+ resp = c.get(
+ "/stats",
+ headers={
+ "Authorization": "Bearer oauth-subscription-token",
+ "X-Headroom-Proxy-Token": "s3cr3t-token",
+ },
+ )
+ assert resp.status_code != 401
+
def test_token_set_rejects_wrong_token(self):
app = _make_app(proxy_token="s3cr3t-token")
with TestClient(app, base_url="http://testserver", client=NONLOOPBACK) as c:
@@ -174,6 +187,24 @@ class TestWebSocketAuthMiddleware:
assert downstream.called is True
assert not _closed_with_policy_violation(sent)
+ async def test_accepts_custom_header_with_upstream_oauth(self):
+ """The upstream OAuth bearer must not override the proxy credential."""
+ downstream = _SpyApp()
+ mw = WebSocketAuthMiddleware(downstream, proxy_token="s3cr3t-token")
+
+ sent = await _drive(
+ mw,
+ _ws_scope(
+ headers=[
+ ("authorization", "Bearer oauth-subscription-token"),
+ ("x-headroom-proxy-token", "s3cr3t-token"),
+ ]
+ ),
+ )
+
+ assert downstream.called is True
+ assert not _closed_with_policy_violation(sent)
+
async def test_loopback_is_exempt(self):
"""Same trust boundary the HTTP gate already grants loopback."""
downstream = _SpyApp()