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.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(copilot): preserve native inline OAuth identity (#4144)
Mansurshakh Japarovad62166ba305
diff --git a/docs/content/docs/vscode-copilot.mdx b/docs/content/docs/vscode-copilot.mdxindex 2f0c5786a..e4df79ed3 100644--- a/docs/content/docs/vscode-copilot.mdx+++ b/docs/content/docs/vscode-copilot.mdx@@ -117,8 +117,20 @@ snapshot in response metadata (for example, a request for `gpt-5.5` returnedCopilot plan and organization policy.The proxy supports the native Copilot OpenAI-compatible request paths used by-GPT and Claude models. Headroom's upstream auth hook replaces local client auth-with the current Copilot API token on every Copilot-bound request.+GPT and Claude models. Inline completions retain the editor's own `ghu_` OAuth+bearer and native integration metadata on the completions host, including an+operator-configured gateway with a base-path prefix. Headroom does not replace+that editor identity with the proxy operator's Copilot seat or add chat-specific+headers. Chat and Responses retain their existing managed Copilot API-token+policy; a raw OAuth bearer on those routes does not gain the inline exception.++A configured base path is removed only for the same scheme and authority, at a+complete path-segment boundary. Other recognized completions endpoints keep+their native `/v1` paths.++The inline auth branch logs only the credential kind (`ghu_***`), not the+editor's OAuth token. Case variants of unrelated provider API-key headers are+removed before any Copilot auth branch forwards the request.## Stop and undodiff --git a/headroom/copilot_auth.py b/headroom/copilot_auth.pyindex f28593158..d2290759b 100644--- a/headroom/copilot_auth.py+++ b/headroom/copilot_auth.py@@ -1069,6 +1069,14 @@ def _set_header_default(headers: dict[str, str], name: str, value: str) -> None:headers[name] = value+def _remove_headers(headers: dict[str, str], *names: str) -> None:+ """Remove every case variant of the named headers in place."""+ names_lower = {name.lower() for name in names}+ for key in list(headers):+ if key.lower() in names_lower:+ del headers[key]++def _copilot_token_exchange_headers(oauth_token: str, *, integration_id: str | None = None) -> dict[str, str]:@@ -1695,7 +1703,7 @@ def _is_forwardable_copilot_bearer_token(token: str) -> bool:def _token_kind(token: str) -> str:"""Return a non-sensitive label for the token type, safe to log."""t = token.strip()- for prefix in ("tid_", "gho_", "ghs_", "ghp_", "github_pat_"):+ for prefix in ("tid_", "ghu_", "gho_", "ghs_", "ghp_", "github_pat_"):if t.startswith(prefix):return prefix + "***"return "unknown" if t else "empty"@@ -1725,6 +1733,36 @@ async def apply_copilot_api_auth(headers: dict[str, str], *, url: str) -> dict[sif not is_copilot_upstream_url(url):return resolved+ incoming_auth = next((v for k, v in resolved.items() if k.lower() == "authorization"), None)+ scheme, _, raw_token = incoming_auth.partition(" ") if incoming_auth else ("", "", "")+ # Inline completions accept the editor's own OAuth credential. Keep its+ # account and integration metadata rather than substituting the operator's+ # seat or adding chat-specific defaults (#3352).+ if (+ scheme.lower() == "bearer"+ and raw_token.startswith("ghu_")+ and is_copilot_completions_host(url)+ and not _is_managed_copilot_seeded_bearer(raw_token)+ ):+ upstream = urlparse(url)+ base = urlparse(copilot_completions_base_url())+ path = upstream.path+ prefix = base.path.rstrip("/")+ if (+ prefix+ and upstream.scheme == base.scheme+ and upstream.netloc.lower() == base.netloc.lower()+ and path.startswith(prefix + "/")+ ):+ path = path[len(prefix) :]+ if is_copilot_completions_path(path):+ logger.info(+ "apply_copilot_api_auth: passing through inline editor token kind=%s",+ _token_kind(raw_token),+ )+ _remove_headers(resolved, "x-api-key")+ return resolved+# Read the CLIENT's integration ID before any default is applied, so the# credential we mint below can be bound to the surface that actually made# the call rather than to whatever this proxy happens to default to.@@ -1734,9 +1772,7 @@ async def apply_copilot_api_auth(headers: dict[str, str], *, url: str) -> dict[sfor name, value in _copilot_chat_header_defaults(integration_id).items():_set_header_default(resolved, name, value)- incoming_auth = next((v for k, v in resolved.items() if k.lower() == "authorization"), None)if incoming_auth:- scheme, _, raw_token = incoming_auth.partition(" ")if (scheme.lower() == "bearer"and raw_token@@ -1752,9 +1788,7 @@ async def apply_copilot_api_auth(headers: dict[str, str], *, url: str) -> dict[s"apply_copilot_api_auth: passing through client token kind=%s",_token_kind(raw_token),)- for key in list(resolved):- if key.lower() == "x-api-key":- resolved.pop(key)+ _remove_headers(resolved, "x-api-key")return resolvedlogger.info("apply_copilot_api_auth: incoming token not suitable (kind=%s), will replace",@@ -1762,9 +1796,7 @@ async def apply_copilot_api_auth(headers: dict[str, str], *, url: str) -> dict[s)token = await get_copilot_token_provider().get_api_token(integration_id=integration_id)- for key in list(resolved):- if key.lower() in {"authorization", "x-api-key"}:- resolved.pop(key)+ _remove_headers(resolved, "authorization", "x-api-key")resolved["Authorization"] = f"Bearer {token.token}"# The credential and the integration ID must leave together. Until now the# ID was applied with set-default semantics BEFORE this branch was chosen,diff --git a/tests/test_copilot_inline_client_auth.py b/tests/test_copilot_inline_client_auth.pynew file mode 100644index 000000000..e8f111fab--- /dev/null+++ b/tests/test_copilot_inline_client_auth.py@@ -0,0 +1,136 @@+"""Inline editor OAuth must not be replaced with the proxy operator's seat."""++from __future__ import annotations++import logging+import re++import httpx+import pytest++from headroom import copilot_auth++[email protected](autouse=True)+def operator_token(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setenv("GITHUB_COPILOT_API_TOKEN", "tid_operator_seat_fixture")+ monkeypatch.delenv("GITHUB_COPILOT_REFRESH_OAUTH_TOKEN", raising=False)+ monkeypatch.delenv("GITHUB_COPILOT_PROXY_URL", raising=False)+++async def test_inline_editor_uses_its_own_seat_and_integration() -> None:+ headers = await copilot_auth.apply_copilot_api_auth(+ {+ "authorization": "Bearer ghu_editor_seat_fixture",+ "Copilot-Integration-Id": "vscode",+ "x-api-key": "unrelated-provider-secret",+ "X-Api-Key": "another-provider-secret",+ },+ url="https://copilot-proxy.githubusercontent.com/v1/engines/gpt-41-copilot/completions",+ )++ assert headers["authorization"] == "Bearer ghu_editor_seat_fixture"+ assert headers["Copilot-Integration-Id"] == "vscode"+ assert "x-api-key" not in httpx.Headers(headers)+++async def test_inline_oauth_is_not_relabelled_as_the_chat_integration() -> None:+ headers = await copilot_auth.apply_copilot_api_auth(+ {"Authorization": "Bearer ghu_editor_seat_fixture"},+ url="https://proxy.business.githubcopilot.com/v1/engines/gpt-41-copilot/completions",+ )++ assert headers["Authorization"] == "Bearer ghu_editor_seat_fixture"+ assert all(name.lower() != "copilot-integration-id" for name in headers)+++async def test_inline_oauth_exception_does_not_widen_chat_auth() -> None:+ headers = await copilot_auth.apply_copilot_api_auth(+ {+ "authorization": "Bearer ghu_editor_seat_fixture",+ "AUTHORIZATION": "Bearer stale-other-seat",+ "x-api-key": "unrelated-provider-secret",+ "X-Api-Key": "another-provider-secret",+ },+ url="https://api.githubcopilot.com/chat/completions",+ )++ assert headers["Authorization"] == "Bearer tid_operator_seat_fixture"+ assert "authorization" not in headers+ assert "AUTHORIZATION" not in headers+ assert "x-api-key" not in httpx.Headers(headers)+++async def test_inline_editor_keeps_its_seat_through_a_prefixed_gateway(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ monkeypatch.setenv("GITHUB_COPILOT_PROXY_URL", "https://gateway.example.invalid/copilot")+ headers = await copilot_auth.apply_copilot_api_auth(+ {+ "authorization": "Bearer ghu_editor_seat_fixture",+ "Copilot-Integration-Id": "vscode",+ },+ url="https://gateway.example.invalid/copilot/v1/engines/gpt-41-copilot/completions",+ )++ assert headers["authorization"] == "Bearer ghu_editor_seat_fixture"+ assert headers["Copilot-Integration-Id"] == "vscode"+++ ("gateway", "upstream"),+ [+ (+ "https://gateway.example.invalid/v1",+ "https://proxy.business.githubcopilot.com/v1/engines/gpt-41-copilot/completions",+ ),+ (+ "https://proxy.business.githubcopilot.com/v1",+ "http://proxy.business.githubcopilot.com/v1/engines/gpt-41-copilot/completions",+ ),+ (+ "https://proxy.business.githubcopilot.com:8443/v1",+ "https://proxy.business.githubcopilot.com/v1/engines/gpt-41-copilot/completions",+ ),+ (+ "https://gateway.example.invalid/v",+ "https://gateway.example.invalid/v1/engines/gpt-41-copilot/completions",+ ),+ ],+ ids=["other-host", "other-scheme", "other-port", "segment-boundary"],+)+async def test_gateway_prefix_cannot_rewrite_an_unrelated_native_path(+ monkeypatch: pytest.MonkeyPatch, gateway: str, upstream: str+) -> None:+ monkeypatch.setenv("GITHUB_COPILOT_PROXY_URL", gateway)+ headers = await copilot_auth.apply_copilot_api_auth(+ {+ "authorization": "Bearer ghu_editor_seat_fixture",+ "Copilot-Integration-Id": "vscode",+ "x-api-key": "unrelated-provider-secret",+ },+ url=upstream,+ )++ header_view = httpx.Headers(headers)+ assert header_view["authorization"] == "Bearer ghu_editor_seat_fixture"+ assert header_view["Copilot-Integration-Id"] == "vscode"+ assert "x-api-key" not in header_view+++async def test_inline_debug_kind_never_discloses_the_editor_credential(caplog) -> None:+ secret = "ghu_sensitive_editor_credential_do_not_log"+ caplog.set_level(logging.INFO, logger="headroom.copilot_auth")+ await copilot_auth.apply_copilot_api_auth(+ {"authorization": f"Bearer {secret}"},+ url="https://copilot-proxy.githubusercontent.com/v1/engines/gpt-41-copilot/completions",+ )+ assert set(re.findall(r"\bkind=(\S+)", caplog.text)) == {"ghu_***"}+ assert secret not in caplog.text+ assert secret.removeprefix("ghu_") not in caplog.textdiff --git a/tests/test_proxy_copilot_auth_hooks.py b/tests/test_proxy_copilot_auth_hooks.pyindex 4bf7f33cb..abb1a0359 100644--- a/tests/test_proxy_copilot_auth_hooks.py+++ b/tests/test_proxy_copilot_auth_hooks.py@@ -1,252 +1,16 @@from __future__ import annotations-import asyncio-import importlib.util-import sys-import types-from pathlib import Path-from types import SimpleNamespace-+import httpximport pytest+from starlette.requests import Request+from starlette.responses import StreamingResponse-ROOT = Path(__file__).resolve().parents[1]-_ISOLATED_MODULE_NAMES = (- "headroom.proxy",- "headroom.proxy.handlers",- "fastapi.responses",- "tests.headroom_proxy_handlers_openai",- "tests.headroom_proxy_handlers_streaming",-)--[email protected](autouse=True)-def restore_isolated_modules() -> None:- saved_modules = {name: sys.modules.get(name) for name in _ISOLATED_MODULE_NAMES}- try:- yield- finally:- for name in _ISOLATED_MODULE_NAMES:- sys.modules.pop(name, None)- for name, module in saved_modules.items():- if module is not None:- sys.modules[name] = module---def _load_handler_module(monkeypatch: pytest.MonkeyPatch, module_name: str, relative_path: str):- proxy_pkg = types.ModuleType("headroom.proxy")- proxy_pkg.__path__ = [str(ROOT / "headroom" / "proxy")]- monkeypatch.setitem(sys.modules, "headroom.proxy", proxy_pkg)-- handlers_pkg = types.ModuleType("headroom.proxy.handlers")- handlers_pkg.__path__ = [str(ROOT / "headroom" / "proxy" / "handlers")]- monkeypatch.setitem(sys.modules, "headroom.proxy.handlers", handlers_pkg)-- # The real httpx, not a stub: it is a core dependency, and a partial stub- # breaks whatever else imports httpx during a cold import (the CCR/MCP SSE- # client needs httpx.Response). It also keeps the exception hierarchy the- # proxy catches (TransportError and its subclasses) exactly as in production.-- responses_mod = types.ModuleType("fastapi.responses")-- class Response:- def __init__(- self,- content=None,- status_code: int = 200,- headers=None,- media_type=None,- background=None,- ):- self.content = content- self.status_code = status_code- self.headers = headers or {}- self.media_type = media_type- # The streaming forwarder attaches a background task that releases the- # upstream stream when the body is never consumed (#2882); the double- # must accept and store it so the real StreamingResponse call works.- self.background = background-- class StreamingResponse(Response):- pass-- class JSONResponse(Response):- pass-- responses_mod.Response = Response- responses_mod.StreamingResponse = StreamingResponse- responses_mod.JSONResponse = JSONResponse- monkeypatch.setitem(sys.modules, "fastapi.responses", responses_mod)-- spec = importlib.util.spec_from_file_location(module_name, ROOT / relative_path)- assert spec is not None and spec.loader is not None- module = importlib.util.module_from_spec(spec)- monkeypatch.setitem(sys.modules, module_name, module)- spec.loader.exec_module(module)- return module---def test_openai_passthrough_applies_copilot_auth(monkeypatch: pytest.MonkeyPatch) -> None:- openai_mod = _load_handler_module(- monkeypatch,- "tests.headroom_proxy_handlers_openai",- "headroom/proxy/handlers/openai.py",- )-- seen: dict[str, object] = {}-- async def fake_apply(headers: dict[str, str], *, url: str) -> dict[str, str]:- seen["headers"] = dict(headers)- seen["url"] = url- return {"Authorization": "Bearer upstream-token"}-- monkeypatch.setattr(openai_mod, "apply_copilot_api_auth", fake_apply)-- class Dummy(openai_mod.OpenAIHandlerMixin):- def __init__(self) -> None:- self.metrics = SimpleNamespace(record_request=self._record_request)- self.http_client = SimpleNamespace(request=self._request)- self.cost_tracker = None- self._counter = 0-- async def _record_request(self, **kwargs) -> None: # noqa: ANN003- return None-- async def _next_request_id(self) -> str:- # The passthrough handler now allocates a request_id at end-- # of-call because it records via ``_record_request_outcome``,- # which requires one. Pre-refactor the dummy didn't need- # this method because metrics.record_request was called- # directly without a request_id.- self._counter += 1- return f"req-{self._counter}"-- async def _record_request_outcome(self, outcome) -> None: # noqa: ANN001- from headroom.proxy.outcome import emit_request_outcome-- await emit_request_outcome(self, outcome)-- def _extract_tags(self, headers: dict) -> dict[str, str]:- # Mirror of HeadroomProxy._extract_tags. The passthrough- # handler now extracts tags at entry as part of the- # outcome-tag invariant lock (PR #480).- return {- k.lower().replace("x-headroom-", ""): v- for k, v in headers.items()- if k.lower().startswith("x-headroom-")- }-- async def _request(self, **kwargs): # noqa: ANN003- seen["request_kwargs"] = kwargs- return SimpleNamespace(headers={}, content=b"{}", status_code=200)-- request = SimpleNamespace(- url=SimpleNamespace(path="/v1/models", query=""),- headers={- "authorization": "Bearer downstream",- "host": "localhost",- "accept-encoding": "gzip",- },- method="GET",- body=lambda: None,- )-- async def body() -> bytes:- return b""-- request.body = body-- handler = Dummy()- response = asyncio.run(- handler.handle_passthrough(- request,- "https://api.githubcopilot.com",- "models",- "openai",- )- )-- assert seen["url"] == "https://api.githubcopilot.com/models"- assert seen["request_kwargs"]["headers"] == {"Authorization": "Bearer upstream-token"}- assert response.status_code == 200---def test_streaming_response_applies_copilot_auth(monkeypatch: pytest.MonkeyPatch) -> None:- streaming_mod = _load_handler_module(- monkeypatch,- "tests.headroom_proxy_handlers_streaming",- "headroom/proxy/handlers/streaming.py",- )-- seen: dict[str, object] = {}+from headroom import paths+from headroom.proxy.server import HeadroomProxy, ProxyConfig- async def fake_apply(headers: dict[str, str], *, url: str) -> dict[str, str]:- seen["headers"] = dict(headers)- seen["url"] = url- return {"Authorization": "Bearer upstream-token"}-- monkeypatch.setattr(streaming_mod, "apply_copilot_api_auth", fake_apply)-- class Dummy(streaming_mod.StreamingMixin):- def __init__(self) -> None:- self.memory_handler = None- self.config = SimpleNamespace(- retry_max_attempts=1,- retry_base_delay_ms=1,- retry_max_delay_ms=1,- )- self.http_client = SimpleNamespace(- build_request=self._build_request,- send=self._send,- )- def _build_request(self, method: str, url: str, **kwargs): # noqa: ANN003- # PR-A3: streaming forwarder is byte-faithful; it now passes- # ``content=<bytes>`` instead of ``json=<dict>``.- seen["request"] = {- "method": method,- "url": url,- **kwargs,- }- return SimpleNamespace()-- async def _send(self, request, stream: bool): # noqa: ANN001, ANN003- return SimpleNamespace(headers={}, status_code=200)-- handler = Dummy()- response = asyncio.run(- handler._stream_response(- url="https://api.githubcopilot.com/v1/responses",- headers={"authorization": "Bearer downstream"},- body={"model": "gpt-4o"},- provider="openai",- model="gpt-4o",- request_id="req-test",- original_tokens=0,- optimized_tokens=0,- tokens_saved=0,- transforms_applied=[],- tags={},- optimization_latency=0.0,- )- )-- assert seen["url"] == "https://api.githubcopilot.com/v1/responses"- # PR-A3: byte-faithful forwarder always sets ``content-type`` explicitly.- sent_headers = seen["request"]["headers"]- assert sent_headers["Authorization"] == "Bearer upstream-token"- assert sent_headers["content-type"] == "application/json"- assert response.status_code == 200- # The Copilot auth hook and the #2882 upstream-stream cleanup coexist: the- # streaming response still carries its background release task.- assert response.background is not None---def test_openai_chat_routes_copilot_requests_per_model(monkeypatch: pytest.MonkeyPatch) -> None:- openai_mod = _load_handler_module(- monkeypatch,- "tests.headroom_proxy_handlers_openai",- "headroom/proxy/handlers/openai.py",- )+def test_openai_chat_routes_copilot_requests_per_model() -> None:+ from headroom.proxy.handlers import openai as openai_modcopilot_base = "https://api.githubcopilot.com"gpt54_mini_url = openai_mod.build_copilot_upstream_url(@@ -278,3 +42,137 @@ def test_openai_chat_routes_copilot_requests_per_model(monkeypatch: pytest.Monkeassert gpt54_mini_url == "https://api.githubcopilot.com/responses"assert claude_url == "https://api.githubcopilot.com/chat/completions"assert openai_url == "https://api.openai.com/v1/chat/completions"++[email protected]("entrypoint", ["passthrough", "streaming", "streaming-disconnected"])+ ("path", "expected_auth"),+ [+ (+ "/v1/engines/gpt-41-copilot/completions",+ "Bearer ghu_editor_seat_fixture",+ ),+ ("/chat/completions", "Bearer tid_operator_seat_fixture"),+ ],+ ids=["native-editor-seat", "chat-operator-seat"],+)+async def test_real_forwarders_enforce_credential_and_secret_boundaries(+ monkeypatch: pytest.MonkeyPatch, entrypoint: str, path: str, expected_auth: str+) -> None:+ monkeypatch.setenv("GITHUB_COPILOT_API_TOKEN", "tid_operator_seat_fixture")+ monkeypatch.setenv("GITHUB_COPILOT_PROXY_URL", "https://gateway.example.invalid/v1")+ monkeypatch.delenv("GITHUB_COPILOT_REFRESH_OAUTH_TOKEN", raising=False)+ monkeypatch.setattr(paths, "_PROCESS_STATELESS", paths._PROCESS_STATELESS)+ for name in (+ "ANTHROPIC_API_URL",+ "OPENAI_API_URL",+ "GEMINI_API_URL",+ "CLOUDCODE_API_URL",+ "VERTEX_API_URL",+ ):+ monkeypatch.setattr(HeadroomProxy, name, getattr(HeadroomProxy, name))+ proxy = HeadroomProxy(+ ProxyConfig(+ stateless=True,+ optimize=False,+ disable_kompress=True,+ discover_pipeline_extensions=False,+ cache_enabled=False,+ rate_limit_enabled=False,+ cost_tracking_enabled=False,+ )+ )+ content = b'data: {"choices":[{"text":"accepted"}]}\n\ndata: [DONE]\n\n'++ class UpstreamStream(httpx.AsyncByteStream):+ def __init__(self) -> None:+ self.closed = False++ async def __aiter__(self):+ yield content++ async def aclose(self) -> None:+ self.closed = True++ upstream_stream = UpstreamStream()++ def credential_gate(request: httpx.Request) -> httpx.Response:+ admitted = (+ request.headers.get("authorization") == expected_auth+ and request.headers.get("copilot-integration-id") == "vscode"+ and "x-api-key" not in request.headers+ )+ if admitted:+ return httpx.Response(+ 200,+ stream=upstream_stream,+ headers={"content-type": "text/event-stream"},+ )+ return httpx.Response(+ 401,+ content=b'{"error":"wrong credential or leaked secret"}',+ headers={"content-type": "application/json"},+ )++ headers = {+ "authorization": "Bearer ghu_editor_seat_fixture",+ "copilot-integration-id": "vscode",+ "x-api-key": "unrelated-provider-secret",+ }+ base = (+ "https://proxy.business.githubcopilot.com"+ if path.startswith("/v1/engines/")+ else "https://api.githubcopilot.com"+ )+ try:+ async with httpx.AsyncClient(transport=httpx.MockTransport(credential_gate)) as client:+ proxy.http_client = client+ if entrypoint == "passthrough":++ async def receive() -> dict[str, object]:+ return {"type": "http.request", "body": b"{}", "more_body": False}++ request = Request(+ {+ "type": "http",+ "method": "POST",+ "scheme": "http",+ "server": ("proxy.test", 80),+ "path": path,+ "query_string": b"",+ "headers": [(k.encode(), v.encode()) for k, v in headers.items()],+ },+ receive,+ )+ response = await proxy.handle_passthrough(request, base)+ else:+ response = await proxy._stream_response(+ url=base + path,+ headers=headers,+ body={"model": "gpt-41-copilot"},+ provider="openai",+ model="gpt-41-copilot",+ request_id="inline-auth-regression",+ original_tokens=0,+ optimized_tokens=0,+ tokens_saved=0,+ transforms_applied=[],+ tags={},+ optimization_latency=0.0,+ )++ assert response.status_code == 200+ if isinstance(response, StreamingResponse):+ assert not upstream_stream.closed+ if entrypoint == "streaming":+ received = b"".join([chunk async for chunk in response.body_iterator])+ assert received == content+ if response.background is not None:+ await response.background()+ else:+ assert response.body == content+ assert upstream_stream.closed+ finally:+ proxy._compression_executor.shutdown()+ proxy._background_compression_executor.shutdown()