mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(copilot): preserve native inline OAuth identity (#4144)
## Description Closes #3352. Preserve the native inline editor OAuth identity without widening Chat/Responses auth. Current head `4609ac6b6162e81e4471492cb4c056065ce7f450` includes upstream main `f1bc2609`. ## Type of Change - [x] Bug fix - [x] Documentation update - [x] Code refactoring ## Changes Made - Share one case-insensitive in-place header-removal helper across all three auth branches; remove every unrelated provider API-key spelling and every replaced Authorization spelling. - Classify `ghu_` as the safe `ghu_***` kind and log that kind in the inline branch, never the editor credential. - Preserve the fetched same-origin/segment-boundary gateway-prefix fix and its host/scheme/port regressions; this was already present in `c368f1d`, not reimplemented here. - Retain managed-seed exclusion and the existing Chat/Responses token policy. No new live-account fallback or provider mock was introduced. - Keep the optional path-helper extraction out; the small existing origin guard remains explicit. ## Testing - [x] Relevant unit/API regressions pass - [x] Changed-file Ruff check and format pass - [x] Manual auth operation/privacy smoke performed ### Test Output ```text 222 passed 181 passed, 5 warnings ci-precheck PASSED — safe to push. ``` Focused command: `pytest -q tests/test_copilot_inline_client_auth.py tests/test_copilot_auth.py tests/test_proxy_copilot_auth_hooks.py tests/test_copilot_vscode_completions_routing.py tests/test_copilot_integration_id_hmac.py`. Normal commit and full pre-push hooks passed, including workspace Rust gates, genuine native rebuild/import, critical Python and commitlint. No hooks/tests were bypassed or filtered. The first normal push hit the unchanged native `expired_get_does_not_wipe_concurrent_refresh` final-live assertion: its 20 ms idle TTL can expire legitimately before the assertion under scheduling load. This is the already-documented main stress-test issue, not an auth-source change. A subsequent complete normal gate run used `RUST_TEST_THREADS=1` and passed; no Rust fix or general flake elimination is claimed. ## Real Behavior Proof - Environment: Linux x86_64, Python 3.13.14, current published source, real `apply_copilot_api_auth` and logging, synthetic credential environment, isolated HOME, `DO_NOT_TRACK=1`. - Exact command / steps: Execute `python artifacts/review-20261013/run-native.py WORKTREE TARGET/release/lib_core.so PROBE OUTPUT.json`. Invoke actual auth operations for native inline on a different origin, same-origin prefixed gateway inline, client-owned Chat gho, and Chat ghu requiring operator replacement; include mixed-case duplicate API-key headers and capture actual module logs. - Observed result: Four auth cases preserve/select the correct identity, remove all unrelated provider secrets, and log only safe kinds. Captured kinds are `ghu_***`, `ghu_***`, `gho_***`, `ghu_***`; the full editor credential and its suffix are absent. Existing real-proxy MockTransport credential-gate and streaming/disconnect regressions passed in the focused suite. - Not tested: Live Copilot or VS Code signed-in end-to-end. The isolated verification environment has no live Copilot credential; #3352 has no reporter confirmation of this branch. The synthetic upstream/credential gate is not presented as live entitlement proof. Full standalone mypy and hosted-CI success are not asserted. ## Runtime Rollout Safety - Rollout-managed feature(s): Existing Copilot auth routes; no feature rollout added. - Minimum rollout channel: Unchanged stable/default auth. - Stable/default behavior changed: Inline token-kind diagnostics and one shared secret-removal rule; native inline identity exception remains host/path constrained. - Kill switch / disable path: Existing Copilot upstream/auth configuration; no new switch. - Unsafe override required: No. - Qualification impact: Auth selection, case-variant secret removal, log privacy, gateway origin boundaries and existing proxy streaming gates requalified; live entitlement remains external. - Rollback path: Revert this auth helper/logging revision; no data migration. ## Review Readiness - [x] I have performed a self-review - [x] This PR is ready for human review ## Checklist - [x] Documentation updated - [x] Did not edit `CHANGELOG.md` --------- Co-authored-by: Tejas Chopra <tejas@headroomlabs.ai> Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
This commit is contained in:
co-authored by
Tejas Chopra
JerrettDavis
parent
33e5f67464
commit
ad62166ba3
@@ -117,8 +117,20 @@ snapshot in response metadata (for example, a request for `gpt-5.5` returned
|
||||
Copilot 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 undo
|
||||
|
||||
|
||||
@@ -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[s
|
||||
if 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[s
|
||||
for 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 resolved
|
||||
logger.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,
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@pytest.fixture(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)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
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)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
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)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
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)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
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"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
("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
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
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.text
|
||||
@@ -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 httpx
|
||||
import 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",
|
||||
)
|
||||
from headroom import paths
|
||||
from headroom.proxy.server import HeadroomProxy, ProxyConfig
|
||||
|
||||
|
||||
@pytest.fixture(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] = {}
|
||||
|
||||
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_mod
|
||||
|
||||
copilot_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.Monke
|
||||
assert 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"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("entrypoint", ["passthrough", "streaming", "streaming-disconnected"])
|
||||
@pytest.mark.parametrize(
|
||||
("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()
|
||||
|
||||
Reference in New Issue
Block a user