From 0663659c55c8e1008edc891345dddba711c262ce Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:25:47 -0700 Subject: [PATCH 1/8] fix(github_copilot): treat a malformed api-key.json like a missing one --- litellm/llms/github_copilot/authenticator.py | 58 +++++++++++-------- .../test_github_copilot_authenticator.py | 51 ++++++++++++++++ 2 files changed, 85 insertions(+), 24 deletions(-) diff --git a/litellm/llms/github_copilot/authenticator.py b/litellm/llms/github_copilot/authenticator.py index 80fd4f755e7..46722534071 100644 --- a/litellm/llms/github_copilot/authenticator.py +++ b/litellm/llms/github_copilot/authenticator.py @@ -5,12 +5,12 @@ from datetime import datetime from typing import Any, Final import httpx +from pydantic import BaseModel, ConfigDict, ValidationError from litellm._logging import verbose_logger from litellm.llms.custom_httpx.http_handler import _get_httpx_client from .common_utils import ( - APIKeyExpiredError, GetAccessTokenError, GetAPIKeyError, GetDeviceCodeError, @@ -24,6 +24,20 @@ DEFAULT_GITHUB_ACCESS_TOKEN_URL: Final = "https://github.com/login/oauth/access_ DEFAULT_GITHUB_API_KEY_URL: Final = "https://api.github.com/copilot_internal/v2/token" +class CopilotEndpoints(BaseModel): + model_config = ConfigDict(frozen=True, strict=True, extra="ignore", hide_input_in_errors=True) + + api: str | None = None + + +class CopilotApiKeyFile(BaseModel): + model_config = ConfigDict(frozen=True, strict=True, extra="ignore", hide_input_in_errors=True) + + token: str + expires_at: float + endpoints: CopilotEndpoints | None = None + + class Authenticator: def __init__(self) -> None: """Initialize the GitHub Copilot authenticator with configurable token paths.""" @@ -86,23 +100,11 @@ class Authenticator: Raises: GetAPIKeyError: If unable to obtain an API key. """ - try: - with open(self.api_key_file, "r") as f: - api_key_info = json.load(f) - if api_key_info.get("expires_at", 0) > datetime.now().timestamp(): - return api_key_info.get("token") - else: - verbose_logger.warning("API key expired, refreshing") - raise APIKeyExpiredError( - message="API key expired", - status_code=401, - ) - except OSError: - verbose_logger.warning("No API key file found or error opening file") - except (json.JSONDecodeError, KeyError) as e: - verbose_logger.warning("Error reading API key from file: %s", e) - except APIKeyExpiredError: - pass # Already logged in the try block + cached: Final = self._read_api_key_file() + if cached is not None and cached.expires_at > datetime.now().timestamp(): + return cached.token + if cached is not None: + verbose_logger.warning("API key expired, refreshing") try: api_key_info = self._refresh_api_key() @@ -135,14 +137,22 @@ class Authenticator: Returns: Optional[str]: The GitHub Copilot API endpoint, or None if not found. """ + cached: Final = self._read_api_key_file() + if cached is None or cached.endpoints is None: + return None + return cached.endpoints.api + + def _read_api_key_file(self) -> CopilotApiKeyFile | None: try: with open(self.api_key_file, "r") as f: - api_key_info: Final = json.load(f) - endpoints: Final = api_key_info.get("endpoints", {}) - api_endpoint: Final = endpoints.get("api") - return api_endpoint - except (OSError, json.JSONDecodeError, KeyError) as e: - verbose_logger.warning("Error reading API endpoint from file: %s", e) + raw: Final = f.read() + except OSError: + verbose_logger.warning("No API key file found or error opening file") + return None + try: + return CopilotApiKeyFile.model_validate_json(raw) + except ValidationError as e: + verbose_logger.warning("Ignoring %s, not a Copilot API key file: %s", self.api_key_file, e) return None def _refresh_api_key(self) -> dict[str, Any]: diff --git a/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py b/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py index 6c846a90c71..a26fb1a2ef1 100644 --- a/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py +++ b/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py @@ -134,6 +134,57 @@ class TestGitHubCopilotAuthenticator: assert api_key == "new-api-key" authenticator._refresh_api_key.assert_called_once() + @pytest.mark.parametrize( + "malformed", + ["[]", json.dumps({"token": "tid=abc", "expires_at": "soon"})], + ids=["json-list", "text-expires-at"], + ) + def test_get_api_key_malformed_file_refreshes_like_missing(self, tmp_path, monkeypatch, malformed): + """A file that is not a Copilot key object is ignored, refreshed over, never answered with a parse error.""" + monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) + api_key_file = tmp_path / "api-key.json" + api_key_file.write_text(malformed) + refreshed = {"token": "new-api-key", "expires_at": (datetime.now() + timedelta(hours=1)).timestamp()} + auth = Authenticator() + + with patch.object(auth, "_refresh_api_key", return_value=refreshed) as mock_refresh: + assert auth.get_api_key() == "new-api-key" + + mock_refresh.assert_called_once() + assert json.loads(api_key_file.read_text()) == refreshed + + @pytest.mark.parametrize( + "malformed", + ["[]", json.dumps({"token": "tid=abc", "expires_at": "soon"})], + ids=["json-list", "text-expires-at"], + ) + def test_get_api_key_malformed_file_is_kept_when_refresh_fails(self, tmp_path, monkeypatch, malformed): + """Ignoring the file never deletes it, so a failed refresh leaves the operator's file for inspection.""" + monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) + api_key_file = tmp_path / "api-key.json" + api_key_file.write_text(malformed) + auth = Authenticator() + + with ( + patch.object(auth, "_refresh_api_key", side_effect=RefreshAPIKeyError(message="no login", status_code=401)), + pytest.raises(GetAPIKeyError), + ): + auth.get_api_key() + + assert api_key_file.read_text() == malformed + + @pytest.mark.parametrize( + "malformed", + ["[]", json.dumps({"token": "tid=abc", "expires_at": "soon"}), json.dumps({"token": "t", "expires_at": 1, "endpoints": []})], + ids=["json-list", "text-expires-at", "list-endpoints"], + ) + def test_get_api_base_malformed_file_returns_none(self, tmp_path, monkeypatch, malformed): + """get_api_base runs before get_api_key on every request, so it must survive the same malformed shapes.""" + monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) + (tmp_path / "api-key.json").write_text(malformed) + + assert Authenticator().get_api_base() is None + def test_refresh_api_key(self, authenticator, mock_http_client): """Test refreshing an API key.""" mock_client, mock_response = mock_http_client From ccd80f5ec277f98c048cccdb50abc78723c30d34 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:56:33 -0700 Subject: [PATCH 2/8] fix(github_copilot): read the key and the endpoints of api-key.json as separate strict views --- litellm/llms/github_copilot/authenticator.py | 24 ++++++++++++------- .../test_github_copilot_authenticator.py | 21 +++++++++++++--- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/litellm/llms/github_copilot/authenticator.py b/litellm/llms/github_copilot/authenticator.py index 46722534071..10730ce8cc2 100644 --- a/litellm/llms/github_copilot/authenticator.py +++ b/litellm/llms/github_copilot/authenticator.py @@ -2,7 +2,7 @@ import json import os import time from datetime import datetime -from typing import Any, Final +from typing import Any, Final, TypeVar import httpx from pydantic import BaseModel, ConfigDict, ValidationError @@ -24,20 +24,28 @@ DEFAULT_GITHUB_ACCESS_TOKEN_URL: Final = "https://github.com/login/oauth/access_ DEFAULT_GITHUB_API_KEY_URL: Final = "https://api.github.com/copilot_internal/v2/token" +class CopilotCachedKey(BaseModel): + model_config = ConfigDict(frozen=True, strict=True, extra="ignore", hide_input_in_errors=True) + + token: str + expires_at: float + + class CopilotEndpoints(BaseModel): model_config = ConfigDict(frozen=True, strict=True, extra="ignore", hide_input_in_errors=True) api: str | None = None -class CopilotApiKeyFile(BaseModel): +class CopilotCachedEndpoints(BaseModel): model_config = ConfigDict(frozen=True, strict=True, extra="ignore", hide_input_in_errors=True) - token: str - expires_at: float endpoints: CopilotEndpoints | None = None +ApiKeyFileView = TypeVar("ApiKeyFileView", CopilotCachedKey, CopilotCachedEndpoints) + + class Authenticator: def __init__(self) -> None: """Initialize the GitHub Copilot authenticator with configurable token paths.""" @@ -100,7 +108,7 @@ class Authenticator: Raises: GetAPIKeyError: If unable to obtain an API key. """ - cached: Final = self._read_api_key_file() + cached: Final = self._read_api_key_file(CopilotCachedKey) if cached is not None and cached.expires_at > datetime.now().timestamp(): return cached.token if cached is not None: @@ -137,12 +145,12 @@ class Authenticator: Returns: Optional[str]: The GitHub Copilot API endpoint, or None if not found. """ - cached: Final = self._read_api_key_file() + cached: Final = self._read_api_key_file(CopilotCachedEndpoints) if cached is None or cached.endpoints is None: return None return cached.endpoints.api - def _read_api_key_file(self) -> CopilotApiKeyFile | None: + def _read_api_key_file(self, view: type[ApiKeyFileView]) -> ApiKeyFileView | None: try: with open(self.api_key_file, "r") as f: raw: Final = f.read() @@ -150,7 +158,7 @@ class Authenticator: verbose_logger.warning("No API key file found or error opening file") return None try: - return CopilotApiKeyFile.model_validate_json(raw) + return view.model_validate_json(raw) except ValidationError as e: verbose_logger.warning("Ignoring %s, not a Copilot API key file: %s", self.api_key_file, e) return None diff --git a/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py b/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py index a26fb1a2ef1..f04dd91c830 100644 --- a/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py +++ b/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py @@ -175,16 +175,31 @@ class TestGitHubCopilotAuthenticator: @pytest.mark.parametrize( "malformed", - ["[]", json.dumps({"token": "tid=abc", "expires_at": "soon"}), json.dumps({"token": "t", "expires_at": 1, "endpoints": []})], - ids=["json-list", "text-expires-at", "list-endpoints"], + ["[]", json.dumps({"endpoints": {"api": 5}}), json.dumps({"token": "t", "expires_at": 1, "endpoints": []})], + ids=["json-list", "number-api", "list-endpoints"], ) def test_get_api_base_malformed_file_returns_none(self, tmp_path, monkeypatch, malformed): - """get_api_base runs before get_api_key on every request, so it must survive the same malformed shapes.""" + """get_api_base runs before get_api_key on every request, so it must survive malformed shapes too.""" monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) (tmp_path / "api-key.json").write_text(malformed) assert Authenticator().get_api_base() is None + def test_get_api_base_keeps_endpoints_when_the_key_is_malformed(self, tmp_path, monkeypatch): + """The endpoint is read on its own, so a bad key never sends the refreshed key to the default host.""" + monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) + tenant_api = "https://api.tenant.githubcopilot.com" + (tmp_path / "api-key.json").write_text( + json.dumps({"token": "tid=abc", "expires_at": "soon", "endpoints": {"api": tenant_api}}) + ) + refreshed = {"token": "new-api-key", "expires_at": (datetime.now() + timedelta(hours=1)).timestamp()} + auth = Authenticator() + + assert auth.get_api_base() == tenant_api + with patch.object(auth, "_refresh_api_key", return_value=refreshed) as mock_refresh: + assert auth.get_api_key() == "new-api-key" + mock_refresh.assert_called_once() + def test_refresh_api_key(self, authenticator, mock_http_client): """Test refreshing an API key.""" mock_client, mock_response = mock_http_client From 8dccf594c801b9d11889bb689c9c8bba319f4260 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 28 Oct 2026 16:52:32 -0700 Subject: [PATCH 3/8] fix(github_copilot): ignore a non-UTF-8 api-key.json like any other malformed file Read the cached key file as bytes so a UTF-16 file, which Windows PowerShell 5.1 writes with >, fails JSON validation instead of raising the decoder's error, and the request gets the refresh or the login hint --- litellm/llms/github_copilot/authenticator.py | 2 +- .../test_device_code_login_guard_wire.py | 19 ++++++----- .../test_github_copilot_authenticator.py | 33 +++++++++++++------ 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/litellm/llms/github_copilot/authenticator.py b/litellm/llms/github_copilot/authenticator.py index e76de266fc1..2bb79200420 100644 --- a/litellm/llms/github_copilot/authenticator.py +++ b/litellm/llms/github_copilot/authenticator.py @@ -200,7 +200,7 @@ class Authenticator: def _read_api_key_file(self, view: type[ApiKeyFileView]) -> ApiKeyFileView | None: try: - with open(self.api_key_file, "r") as f: + with open(self.api_key_file, "rb") as f: raw: Final = f.read() except OSError: verbose_logger.warning("No API key file found or error opening file") diff --git a/tests/integration/providers/test_device_code_login_guard_wire.py b/tests/integration/providers/test_device_code_login_guard_wire.py index 910332ef8c9..f3a8f55411e 100644 --- a/tests/integration/providers/test_device_code_login_guard_wire.py +++ b/tests/integration/providers/test_device_code_login_guard_wire.py @@ -655,14 +655,17 @@ def test_chatgpt_token_with_a_text_expiry_falls_back_to_the_expiry_inside_the_to assert scene.rig.peers.auth_connections() == () -_UNUSABLE_COPILOT_FILES: Final[Mapping[str, Mapping[str, str]]] = MappingProxyType( +_UNUSABLE_COPILOT_FILES: Final[Mapping[str, Mapping[str, bytes]]] = MappingProxyType( { - "empty-access-token": MappingProxyType({"access-token": ""}), - "blank-access-token": MappingProxyType({"access-token": " \n"}), - "invalid-api-key-json": MappingProxyType({"api-key.json": "{not json"}), - "api-key-list": MappingProxyType({"api-key.json": "[]"}), + "empty-access-token": MappingProxyType({"access-token": b""}), + "blank-access-token": MappingProxyType({"access-token": b" \n"}), + "invalid-api-key-json": MappingProxyType({"api-key.json": b"{not json"}), + "api-key-list": MappingProxyType({"api-key.json": b"[]"}), "api-key-expiry-text": MappingProxyType( - {"api-key.json": json.dumps({"token": "copilot-text-expiry", "expires_at": "soon"})} + {"api-key.json": json.dumps({"token": "copilot-text-expiry", "expires_at": "soon"}).encode()} + ), + "api-key-utf-16": MappingProxyType( + {"api-key.json": json.dumps({"token": "copilot-utf-16", "expires_at": 9999999999}).encode("utf-16")} ), } ) @@ -672,7 +675,7 @@ _UNUSABLE_COPILOT_FILES: Final[Mapping[str, Mapping[str, str]]] = MappingProxyTy def test_unusable_copilot_token_files_are_refused_like_missing_ones_each_time(scene: _Scene, name: str) -> None: files: Final = _UNUSABLE_COPILOT_FILES[name] for file_name, content in files.items(): - (scene.rig.copilot_dir / file_name).write_text(content) + (scene.rig.copilot_dir / file_name).write_bytes(content) first: Final = _serve(scene.call(_Cell("copilot", "chat", False, "httpx"))) second: Final = _serve(scene.call(_Cell("copilot", "chat", False, "httpx"))) for served in (first, second): @@ -682,7 +685,7 @@ def test_unusable_copilot_token_files_are_refused_like_missing_ones_each_time(sc assert scene.rig.peers.auth_connections() == () assert _describe(scene.rig.peers.api.drain()) == () for file_name, content in files.items(): - assert (scene.rig.copilot_dir / file_name).read_text() == content + assert (scene.rig.copilot_dir / file_name).read_bytes() == content @pytest.mark.parametrize("provider", _PROVIDERS) diff --git a/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py b/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py index 65cd5903353..ccdaf78b167 100644 --- a/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py +++ b/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py @@ -165,14 +165,18 @@ class TestGitHubCopilotAuthenticator: @pytest.mark.parametrize( "malformed", - ["[]", json.dumps({"token": "tid=abc", "expires_at": "soon"})], - ids=["json-list", "text-expires-at"], + [ + b"[]", + json.dumps({"token": "tid=abc", "expires_at": "soon"}).encode(), + json.dumps({"token": "tid=abc", "expires_at": 9999999999}).encode("utf-16"), + ], + ids=["json-list", "text-expires-at", "utf-16-file"], ) def test_get_api_key_malformed_file_refreshes_like_missing(self, tmp_path, monkeypatch, malformed): """A file that is not a Copilot key object is ignored, refreshed over, never answered with a parse error.""" monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) api_key_file = tmp_path / "api-key.json" - api_key_file.write_text(malformed) + api_key_file.write_bytes(malformed) refreshed = {"token": "new-api-key", "expires_at": (datetime.now() + timedelta(hours=1)).timestamp()} auth = Authenticator() @@ -184,14 +188,18 @@ class TestGitHubCopilotAuthenticator: @pytest.mark.parametrize( "malformed", - ["[]", json.dumps({"token": "tid=abc", "expires_at": "soon"})], - ids=["json-list", "text-expires-at"], + [ + b"[]", + json.dumps({"token": "tid=abc", "expires_at": "soon"}).encode(), + json.dumps({"token": "tid=abc", "expires_at": 9999999999}).encode("utf-16"), + ], + ids=["json-list", "text-expires-at", "utf-16-file"], ) def test_get_api_key_malformed_file_is_kept_when_refresh_fails(self, tmp_path, monkeypatch, malformed): """Ignoring the file never deletes it, so a failed refresh leaves the operator's file for inspection.""" monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) api_key_file = tmp_path / "api-key.json" - api_key_file.write_text(malformed) + api_key_file.write_bytes(malformed) auth = Authenticator() with ( @@ -200,17 +208,22 @@ class TestGitHubCopilotAuthenticator: ): auth.get_api_key() - assert api_key_file.read_text() == malformed + assert api_key_file.read_bytes() == malformed @pytest.mark.parametrize( "malformed", - ["[]", json.dumps({"endpoints": {"api": 5}}), json.dumps({"token": "t", "expires_at": 1, "endpoints": []})], - ids=["json-list", "number-api", "list-endpoints"], + [ + b"[]", + json.dumps({"endpoints": {"api": 5}}).encode(), + json.dumps({"token": "t", "expires_at": 1, "endpoints": []}).encode(), + json.dumps({"endpoints": {"api": "https://api.tenant.githubcopilot.com"}}).encode("utf-16"), + ], + ids=["json-list", "number-api", "list-endpoints", "utf-16-file"], ) def test_get_api_base_malformed_file_returns_none(self, tmp_path, monkeypatch, malformed): """get_api_base runs before get_api_key on every request, so it must survive malformed shapes too.""" monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) - (tmp_path / "api-key.json").write_text(malformed) + (tmp_path / "api-key.json").write_bytes(malformed) assert Authenticator().get_api_base() is None From e70aa2363b4570f4482b718f1557bc9f12ae2c44 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 28 Oct 2026 17:34:14 -0700 Subject: [PATCH 4/8] fix(github_copilot): decode UTF-16 key files and refresh over a malformed endpoints field --- litellm/llms/github_copilot/authenticator.py | 19 +-- .../test_device_code_login_guard_wire.py | 5 +- .../test_github_copilot_authenticator.py | 113 ++++++++++++------ 3 files changed, 89 insertions(+), 48 deletions(-) diff --git a/litellm/llms/github_copilot/authenticator.py b/litellm/llms/github_copilot/authenticator.py index 2bb79200420..dc5b663848b 100644 --- a/litellm/llms/github_copilot/authenticator.py +++ b/litellm/llms/github_copilot/authenticator.py @@ -5,7 +5,7 @@ from datetime import datetime from typing import Any, Final, TypeVar import httpx -from pydantic import BaseModel, ConfigDict, TypeAdapter, ValidationError, with_config +from pydantic import BaseModel, ConfigDict, TypeAdapter, with_config from typing_extensions import NotRequired, ReadOnly, TypedDict from litellm._logging import verbose_logger @@ -26,17 +26,18 @@ DEFAULT_GITHUB_ACCESS_TOKEN_URL: Final = "https://github.com/login/oauth/access_ DEFAULT_GITHUB_API_KEY_URL: Final = "https://api.github.com/copilot_internal/v2/token" +class CopilotEndpoints(BaseModel): + model_config = ConfigDict(frozen=True, strict=True, extra="ignore", hide_input_in_errors=True) + + api: str | None = None + + class CopilotCachedKey(BaseModel): model_config = ConfigDict(frozen=True, strict=True, extra="ignore", hide_input_in_errors=True) token: str expires_at: float - - -class CopilotEndpoints(BaseModel): - model_config = ConfigDict(frozen=True, strict=True, extra="ignore", hide_input_in_errors=True) - - api: str | None = None + endpoints: CopilotEndpoints | None = None class CopilotCachedEndpoints(BaseModel): @@ -206,8 +207,8 @@ class Authenticator: verbose_logger.warning("No API key file found or error opening file") return None try: - return view.model_validate_json(raw) - except ValidationError as e: + return view.model_validate_json(raw.decode(json.detect_encoding(raw))) + except ValueError as e: verbose_logger.warning("Ignoring %s, not a Copilot API key file: %s", self.api_key_file, e) return None diff --git a/tests/integration/providers/test_device_code_login_guard_wire.py b/tests/integration/providers/test_device_code_login_guard_wire.py index f3a8f55411e..627cb0b67fa 100644 --- a/tests/integration/providers/test_device_code_login_guard_wire.py +++ b/tests/integration/providers/test_device_code_login_guard_wire.py @@ -664,9 +664,10 @@ _UNUSABLE_COPILOT_FILES: Final[Mapping[str, Mapping[str, bytes]]] = MappingProxy "api-key-expiry-text": MappingProxyType( {"api-key.json": json.dumps({"token": "copilot-text-expiry", "expires_at": "soon"}).encode()} ), - "api-key-utf-16": MappingProxyType( - {"api-key.json": json.dumps({"token": "copilot-utf-16", "expires_at": 9999999999}).encode("utf-16")} + "api-key-expired-utf-16": MappingProxyType( + {"api-key.json": json.dumps({"token": "copilot-utf-16", "expires_at": 1}).encode("utf-16")} ), + "api-key-truncated-utf-16": MappingProxyType({"api-key.json": b"\xff\xfe\x00\xd8"}), } ) diff --git a/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py b/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py index ccdaf78b167..6db7f924ee4 100644 --- a/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py +++ b/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py @@ -1,11 +1,15 @@ import json +import logging import os import time from concurrent.futures import ThreadPoolExecutor from datetime import datetime, timedelta +from types import MappingProxyType +from typing import Final from unittest.mock import MagicMock, mock_open, patch import pytest +import respx from litellm.llms.github_copilot.authenticator import Authenticator from litellm.llms.github_copilot.common_utils import ( @@ -16,6 +20,19 @@ from litellm.llms.github_copilot.common_utils import ( RefreshAPIKeyError, ) +API_KEY_URL: Final = "https://copilot-key.test/copilot_internal/v2/token" +REFRESHED_KEY: Final = MappingProxyType({"token": "new-api-key", "expires_at": 4102444800}) +TENANT_API: Final = "https://api.tenant.githubcopilot.com" +TRUNCATED_UTF_16: Final = b"\xff\xfe\x00\xd8" + + +@pytest.fixture +def logged_in_token_dir(tmp_path, monkeypatch): + monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) + monkeypatch.setenv("GITHUB_COPILOT_API_KEY_URL", API_KEY_URL) + (tmp_path / "access-token").write_text("gho_logged_in") + return tmp_path + class TestGitHubCopilotAuthenticator: @pytest.fixture @@ -137,7 +154,7 @@ class TestGitHubCopilotAuthenticator: future_time = (datetime.now() + timedelta(hours=1)).timestamp() mock_api_key_data = json.dumps( {"token": "mock-api-key", "expires_at": future_time} - ) + ).encode() with patch("builtins.open", mock_open(read_data=mock_api_key_data)): api_key = authenticator.get_api_key() @@ -148,7 +165,7 @@ class TestGitHubCopilotAuthenticator: past_time = (datetime.now() - timedelta(hours=1)).timestamp() mock_expired_data = json.dumps( {"token": "expired-api-key", "expires_at": past_time} - ) + ).encode() mock_new_data = { "token": "new-api-key", "expires_at": (datetime.now() + timedelta(hours=1)).timestamp(), @@ -168,45 +185,43 @@ class TestGitHubCopilotAuthenticator: [ b"[]", json.dumps({"token": "tid=abc", "expires_at": "soon"}).encode(), - json.dumps({"token": "tid=abc", "expires_at": 9999999999}).encode("utf-16"), + TRUNCATED_UTF_16, + json.dumps({"token": "tid=abc", "expires_at": 4102444800, "endpoints": []}).encode(), ], - ids=["json-list", "text-expires-at", "utf-16-file"], + ids=["json-list", "text-expires-at", "truncated-utf-16", "list-endpoints-beside-live-key"], ) - def test_get_api_key_malformed_file_refreshes_like_missing(self, tmp_path, monkeypatch, malformed): + def test_get_api_key_malformed_file_refreshes_like_missing(self, logged_in_token_dir, malformed): """A file that is not a Copilot key object is ignored, refreshed over, never answered with a parse error.""" - monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) - api_key_file = tmp_path / "api-key.json" + api_key_file: Final = logged_in_token_dir / "api-key.json" api_key_file.write_bytes(malformed) - refreshed = {"token": "new-api-key", "expires_at": (datetime.now() + timedelta(hours=1)).timestamp()} - auth = Authenticator() - with patch.object(auth, "_refresh_api_key", return_value=refreshed) as mock_refresh: - assert auth.get_api_key() == "new-api-key" + with respx.mock(assert_all_called=True) as router: + route: Final = router.get(API_KEY_URL).respond(json=dict(REFRESHED_KEY)) + assert Authenticator().get_api_key() == "new-api-key" - mock_refresh.assert_called_once() - assert json.loads(api_key_file.read_text()) == refreshed + assert route.call_count == 1 + assert route.calls.last.request.headers["authorization"] == "token gho_logged_in" + assert json.loads(api_key_file.read_text()) == REFRESHED_KEY @pytest.mark.parametrize( "malformed", [ b"[]", json.dumps({"token": "tid=abc", "expires_at": "soon"}).encode(), - json.dumps({"token": "tid=abc", "expires_at": 9999999999}).encode("utf-16"), + TRUNCATED_UTF_16, + json.dumps({"token": "tid=abc", "expires_at": 4102444800, "endpoints": []}).encode(), ], - ids=["json-list", "text-expires-at", "utf-16-file"], + ids=["json-list", "text-expires-at", "truncated-utf-16", "list-endpoints-beside-live-key"], ) - def test_get_api_key_malformed_file_is_kept_when_refresh_fails(self, tmp_path, monkeypatch, malformed): + def test_get_api_key_malformed_file_is_kept_when_refresh_fails(self, logged_in_token_dir, malformed): """Ignoring the file never deletes it, so a failed refresh leaves the operator's file for inspection.""" - monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) - api_key_file = tmp_path / "api-key.json" + api_key_file: Final = logged_in_token_dir / "api-key.json" api_key_file.write_bytes(malformed) - auth = Authenticator() - with ( - patch.object(auth, "_refresh_api_key", side_effect=RefreshAPIKeyError(message="no login", status_code=401)), - pytest.raises(GetAPIKeyError), - ): - auth.get_api_key() + with respx.mock(assert_all_called=True) as router: + router.get(API_KEY_URL).respond(401) + with pytest.raises(GetAPIKeyError): + Authenticator().get_api_key() assert api_key_file.read_bytes() == malformed @@ -216,9 +231,9 @@ class TestGitHubCopilotAuthenticator: b"[]", json.dumps({"endpoints": {"api": 5}}).encode(), json.dumps({"token": "t", "expires_at": 1, "endpoints": []}).encode(), - json.dumps({"endpoints": {"api": "https://api.tenant.githubcopilot.com"}}).encode("utf-16"), + TRUNCATED_UTF_16, ], - ids=["json-list", "number-api", "list-endpoints", "utf-16-file"], + ids=["json-list", "number-api", "list-endpoints", "truncated-utf-16"], ) def test_get_api_base_malformed_file_returns_none(self, tmp_path, monkeypatch, malformed): """get_api_base runs before get_api_key on every request, so it must survive malformed shapes too.""" @@ -227,20 +242,44 @@ class TestGitHubCopilotAuthenticator: assert Authenticator().get_api_base() is None - def test_get_api_base_keeps_endpoints_when_the_key_is_malformed(self, tmp_path, monkeypatch): + def test_get_api_base_keeps_endpoints_when_the_key_is_malformed(self, logged_in_token_dir): """The endpoint is read on its own, so a bad key never sends the refreshed key to the default host.""" - monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) - tenant_api = "https://api.tenant.githubcopilot.com" - (tmp_path / "api-key.json").write_text( - json.dumps({"token": "tid=abc", "expires_at": "soon", "endpoints": {"api": tenant_api}}) + (logged_in_token_dir / "api-key.json").write_text( + json.dumps({"token": "tid=abc", "expires_at": "soon", "endpoints": {"api": TENANT_API}}) ) - refreshed = {"token": "new-api-key", "expires_at": (datetime.now() + timedelta(hours=1)).timestamp()} - auth = Authenticator() + auth: Final = Authenticator() - assert auth.get_api_base() == tenant_api - with patch.object(auth, "_refresh_api_key", return_value=refreshed) as mock_refresh: + assert auth.get_api_base() == TENANT_API + with respx.mock(assert_all_called=True) as router: + router.get(API_KEY_URL).respond(json=dict(REFRESHED_KEY)) assert auth.get_api_key() == "new-api-key" - mock_refresh.assert_called_once() + + @pytest.mark.parametrize("encoding", ["utf-16", "utf-16-le", "utf-8-sig"]) + def test_live_key_file_in_another_json_encoding_is_used_without_a_refresh(self, logged_in_token_dir, encoding): + """A key file a Windows editor saved as UTF-16 or with a BOM still holds a live key and tenant.""" + live_key: Final = json.dumps({"token": "tid=abc", "expires_at": 4102444800, "endpoints": {"api": TENANT_API}}) + api_key_file: Final = logged_in_token_dir / "api-key.json" + api_key_file.write_bytes(live_key.encode(encoding)) + auth: Final = Authenticator() + + with respx.mock: + assert auth.get_api_base() == TENANT_API + assert auth.get_api_key() == "tid=abc" + + assert api_key_file.read_bytes() == live_key.encode(encoding) + + def test_ignored_key_file_is_logged_without_its_contents(self, logged_in_token_dir, caplog): + """The warning names the file but never echoes a token saved in the wrong shape.""" + (logged_in_token_dir / "api-key.json").write_text( + json.dumps({"token": ["tid=secret-value"], "expires_at": 4102444800}) + ) + + with caplog.at_level(logging.WARNING, logger="LiteLLM"), respx.mock(assert_all_called=True) as router: + router.get(API_KEY_URL).respond(json=dict(REFRESHED_KEY)) + assert Authenticator().get_api_key() == "new-api-key" + + assert "not a Copilot API key file" in caplog.text + assert "tid=secret-value" not in caplog.text def test_refresh_api_key(self, authenticator, mock_http_client): """Test refreshing an API key.""" @@ -349,7 +388,7 @@ class TestGitHubCopilotAuthenticator: "expires_at": (datetime.now() + timedelta(hours=1)).timestamp(), "endpoints": {"api": "https://api.enterprise.githubcopilot.com"}, } - ) + ).encode() with patch("builtins.open", mock_open(read_data=mock_api_key_data)): api_base = authenticator.get_api_base() assert api_base == "https://api.enterprise.githubcopilot.com" From 11c96d7fc9d2376d1cbb0737920210a4e5c59921 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 28 Oct 2026 18:09:57 -0700 Subject: [PATCH 5/8] fix(github_copilot): decode a UTF-16 access-token file and refuse an undecodable one like a missing one --- litellm/llms/github_copilot/authenticator.py | 20 ++++++++----- .../test_github_copilot_authenticator.py | 30 +++++++++++++++---- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/litellm/llms/github_copilot/authenticator.py b/litellm/llms/github_copilot/authenticator.py index dc5b663848b..ab00e339985 100644 --- a/litellm/llms/github_copilot/authenticator.py +++ b/litellm/llms/github_copilot/authenticator.py @@ -49,6 +49,12 @@ class CopilotCachedEndpoints(BaseModel): ApiKeyFileView = TypeVar("ApiKeyFileView", CopilotCachedKey, CopilotCachedEndpoints) +def _read_token_file(path: str) -> str: + with open(path, "rb") as f: + raw: Final = f.read() + return raw.decode(json.detect_encoding(raw)) + + @with_config(ConfigDict(extra="allow", strict=True)) class _DeviceCode(TypedDict): device_code: ReadOnly[str] @@ -109,12 +115,13 @@ class Authenticator: GetAccessTokenError: If unable to obtain an access token after retries. """ try: - with open(self.access_token_file, "r") as f: - access_token = f.read().strip() - if access_token: - return access_token + saved_token: Final = _read_token_file(self.access_token_file).strip() + if saved_token: + return saved_token except OSError: verbose_logger.warning("No existing access token found or error reading file") + except ValueError as e: + verbose_logger.warning("Ignoring %s, not a text access token: %s", self.access_token_file, e) if not can_block_current_thread(): raise GetAccessTokenError( @@ -201,13 +208,10 @@ class Authenticator: def _read_api_key_file(self, view: type[ApiKeyFileView]) -> ApiKeyFileView | None: try: - with open(self.api_key_file, "rb") as f: - raw: Final = f.read() + return view.model_validate_json(_read_token_file(self.api_key_file)) except OSError: verbose_logger.warning("No API key file found or error opening file") return None - try: - return view.model_validate_json(raw.decode(json.detect_encoding(raw))) except ValueError as e: verbose_logger.warning("Ignoring %s, not a Copilot API key file: %s", self.api_key_file, e) return None diff --git a/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py b/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py index 6db7f924ee4..be4b8c858bb 100644 --- a/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py +++ b/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py @@ -86,13 +86,9 @@ class TestGitHubCopilotAuthenticator: headers_with_token = authenticator._get_github_headers("test-token") assert headers_with_token["authorization"] == "token test-token" - def test_get_access_token_from_file(self, authenticator): + def test_get_access_token_from_file(self, logged_in_token_dir): """Test retrieving an access token from a file.""" - mock_token = "mock-access-token" - - with patch("builtins.open", mock_open(read_data=mock_token)): - token = authenticator.get_access_token() - assert token == mock_token + assert Authenticator().get_access_token() == "gho_logged_in" def test_get_access_token_login(self, authenticator): """Test logging in to get an access token.""" @@ -135,6 +131,28 @@ class TestGitHubCopilotAuthenticator: assert authenticator.access_token_file not in str(exc.value) mock_login.assert_not_called() + @pytest.mark.parametrize("encoding", ["utf-16", "utf-16-le", "utf-8-sig"]) + def test_saved_access_token_in_another_encoding_is_sent_on_refresh(self, logged_in_token_dir, encoding): + """An access-token file a Windows editor saved as UTF-16 or with a BOM still refreshes the key.""" + (logged_in_token_dir / "access-token").write_bytes("gho_logged_in\n".encode(encoding)) + + with respx.mock(assert_all_called=True) as router: + route: Final = router.get(API_KEY_URL).respond(json=dict(REFRESHED_KEY)) + assert Authenticator().get_api_key() == "new-api-key" + + assert route.calls.last.request.headers["authorization"] == "token gho_logged_in" + + def test_undecodable_access_token_is_refused_like_a_missing_one(self, logged_in_token_dir): + """A proxy worker gets the login hint for an access-token file it cannot decode, and the file is kept.""" + access_token_file: Final = logged_in_token_dir / "access-token" + access_token_file.write_bytes(TRUNCATED_UTF_16) + + with ThreadPoolExecutor(max_workers=1) as pool, pytest.raises(GetAccessTokenError) as exc: + pool.submit(Authenticator().get_access_token).result() + + assert "worker thread" in str(exc.value) + assert access_token_file.read_bytes() == TRUNCATED_UTF_16 + def test_get_access_token_failure(self, authenticator): """Test that an exception is raised after multiple login failures.""" with ( From 9cb35091a3e79265715e22450513bb58d897219f Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 28 Oct 2026 19:03:55 -0700 Subject: [PATCH 6/8] fix(github_copilot): use the refreshed key when api-key.json cannot be written --- litellm/llms/github_copilot/authenticator.py | 32 +++++++++---------- .../test_device_code_login_guard_wire.py | 1 + .../test_github_copilot_authenticator.py | 11 +++++++ 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/litellm/llms/github_copilot/authenticator.py b/litellm/llms/github_copilot/authenticator.py index ab00e339985..3be2c30a489 100644 --- a/litellm/llms/github_copilot/authenticator.py +++ b/litellm/llms/github_copilot/authenticator.py @@ -171,28 +171,26 @@ class Authenticator: verbose_logger.warning("API key expired, refreshing") try: - api_key_info = self._refresh_api_key() - with open(self.api_key_file, "w") as f: - json.dump(api_key_info, f) - token: Final = api_key_info.get("token") - if token: - return token - else: - raise GetAPIKeyError( - message="API key response missing token", - status_code=401, - ) - except OSError as e: - verbose_logger.error("Error saving API key to file: %s", e) - raise GetAPIKeyError( - message=f"Failed to save API key: {e}", - status_code=500, - ) + api_key_info: Final = self._refresh_api_key() except RefreshAPIKeyError as e: raise GetAPIKeyError( message=f"Failed to refresh API key: {e}", status_code=401, + ) from e + try: + with open(self.api_key_file, "w") as f: + json.dump(api_key_info, f) + except OSError as e: + verbose_logger.warning( + "Using the refreshed API key without saving it, %s is not writable: %s", self.api_key_file, e ) + token: Final = api_key_info.get("token") + if not token: + raise GetAPIKeyError( + message="API key response missing token", + status_code=401, + ) + return token def get_api_base(self) -> str | None: """ diff --git a/tests/integration/providers/test_device_code_login_guard_wire.py b/tests/integration/providers/test_device_code_login_guard_wire.py index 627cb0b67fa..353b760dbf3 100644 --- a/tests/integration/providers/test_device_code_login_guard_wire.py +++ b/tests/integration/providers/test_device_code_login_guard_wire.py @@ -659,6 +659,7 @@ _UNUSABLE_COPILOT_FILES: Final[Mapping[str, Mapping[str, bytes]]] = MappingProxy { "empty-access-token": MappingProxyType({"access-token": b""}), "blank-access-token": MappingProxyType({"access-token": b" \n"}), + "access-token-truncated-utf-16": MappingProxyType({"access-token": b"\xff\xfe\x00\xd8"}), "invalid-api-key-json": MappingProxyType({"api-key.json": b"{not json"}), "api-key-list": MappingProxyType({"api-key.json": b"[]"}), "api-key-expiry-text": MappingProxyType( diff --git a/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py b/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py index be4b8c858bb..f14feb51bbf 100644 --- a/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py +++ b/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py @@ -243,6 +243,17 @@ class TestGitHubCopilotAuthenticator: assert api_key_file.read_bytes() == malformed + def test_refreshed_key_is_used_when_the_key_file_cannot_be_written(self, logged_in_token_dir): + unwritable_key_file: Final = logged_in_token_dir / "api-key.json" + unwritable_key_file.mkdir() + + with respx.mock(assert_all_called=True) as router: + route: Final = router.get(API_KEY_URL).respond(json=dict(REFRESHED_KEY)) + assert Authenticator().get_api_key() == "new-api-key" + + assert route.call_count == 1 + assert unwritable_key_file.is_dir() + @pytest.mark.parametrize( "malformed", [ From 1d36a0913412a94f49d5be8ca11d2f8aabd7fa76 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 28 Oct 2026 22:26:31 -0700 Subject: [PATCH 7/8] fix(github_copilot): keep a refreshed key that cannot be saved in memory --- litellm/llms/github_copilot/authenticator.py | 39 +++++++++++++++++-- tests/integration/_support/device_login.py | 12 +++++- .../test_device_code_login_guard_wire.py | 21 ++++++++++ .../test_github_copilot_authenticator.py | 37 ++++++++++++++++-- 4 files changed, 99 insertions(+), 10 deletions(-) diff --git a/litellm/llms/github_copilot/authenticator.py b/litellm/llms/github_copilot/authenticator.py index 3be2c30a489..7c14f8f0bc0 100644 --- a/litellm/llms/github_copilot/authenticator.py +++ b/litellm/llms/github_copilot/authenticator.py @@ -48,6 +48,8 @@ class CopilotCachedEndpoints(BaseModel): ApiKeyFileView = TypeVar("ApiKeyFileView", CopilotCachedKey, CopilotCachedEndpoints) +_UNSAVED_API_KEYS: Final[dict[str, CopilotCachedKey]] = {} # mutable-ok: refreshed keys api-key.json could not hold + def _read_token_file(path: str) -> str: with open(path, "rb") as f: @@ -167,6 +169,9 @@ class Authenticator: cached: Final = self._read_api_key_file(CopilotCachedKey) if cached is not None and cached.expires_at > datetime.now().timestamp(): return cached.token + unsaved: Final = self._unsaved_api_key() + if unsaved is not None: + return unsaved.token if cached is not None: verbose_logger.warning("API key expired, refreshing") @@ -177,13 +182,15 @@ class Authenticator: message=f"Failed to refresh API key: {e}", status_code=401, ) from e + serialized: Final = json.dumps(api_key_info) try: with open(self.api_key_file, "w") as f: - json.dump(api_key_info, f) + f.write(serialized) except OSError as e: verbose_logger.warning( - "Using the refreshed API key without saving it, %s is not writable: %s", self.api_key_file, e + "Keeping the refreshed API key in memory, it could not be saved to %s: %s", self.api_key_file, e ) + self._keep_unsaved_api_key(serialized) token: Final = api_key_info.get("token") if not token: raise GetAPIKeyError( @@ -194,12 +201,12 @@ class Authenticator: def get_api_base(self) -> str | None: """ - Get the API endpoint from the api-key.json file. + Get the API endpoint of the current API key. Returns: Optional[str]: The GitHub Copilot API endpoint, or None if not found. """ - cached: Final = self._read_api_key_file(CopilotCachedEndpoints) + cached: Final = self._unsaved_api_key() or self._read_api_key_file(CopilotCachedEndpoints) if cached is None or cached.endpoints is None: return None return cached.endpoints.api @@ -214,6 +221,30 @@ class Authenticator: verbose_logger.warning("Ignoring %s, not a Copilot API key file: %s", self.api_key_file, e) return None + def _unsaved_api_key_cache_key(self) -> str | None: + try: + access_token: Final = _read_token_file(self.access_token_file).strip() + except (OSError, ValueError): + return None + return f"{self.api_key_file}:{access_token}" if access_token else None + + def _unsaved_api_key(self) -> CopilotCachedKey | None: + cache_key: Final = self._unsaved_api_key_cache_key() + if cache_key is None: + return None + unsaved: Final = _UNSAVED_API_KEYS.get(cache_key) + return unsaved if unsaved is not None and unsaved.expires_at > time.time() else None + + def _keep_unsaved_api_key(self, serialized: str) -> None: + cache_key: Final = self._unsaved_api_key_cache_key() + if cache_key is None: + return + try: + unsaved: Final = CopilotCachedKey.model_validate_json(serialized) + except ValueError: + return + _UNSAVED_API_KEYS[cache_key] = unsaved + def _refresh_api_key(self) -> dict[str, Any]: """ Refresh the API key using the access token. diff --git a/tests/integration/_support/device_login.py b/tests/integration/_support/device_login.py index 9b990fd2167..55519678f40 100644 --- a/tests/integration/_support/device_login.py +++ b/tests/integration/_support/device_login.py @@ -73,6 +73,7 @@ COPILOT_USER_CODE: Final = "COPI-LEAK" COPILOT_ACCESS: Final = "device-login-github-access" COPILOT_REJECTED_ACCESS: Final = "device-login-github-access-rejected" COPILOT_FIRST_LOGIN_ACCESS: Final = "device-login-github-access-first-login" +COPILOT_UNSAVED_KEY_ACCESS: Final = "device-login-github-access-unsaved-key" COPILOT_STORED_KEY: Final = "tid=device-login;stored" COPILOT_MINTED_KEY: Final = "tid=device-login;minted" COPILOT_REJECTED_KEY: Final = "tid=device-login;rejected" @@ -132,7 +133,11 @@ def _oauth_token(request: Request, grant: threading.Event) -> Reply: def _copilot_api_key(request: Request, api_url: str) -> Reply: - if request.headers.get("authorization") not in (f"token {COPILOT_ACCESS}", f"token {COPILOT_FIRST_LOGIN_ACCESS}"): + if request.headers.get("authorization") not in ( + f"token {COPILOT_ACCESS}", + f"token {COPILOT_FIRST_LOGIN_ACCESS}", + f"token {COPILOT_UNSAVED_KEY_ACCESS}", + ): return _json(401, {"message": "Bad credentials"}) return _json( 200, @@ -380,4 +385,7 @@ def write_copilot_access(directory: Path, access_token: str) -> None: def clear_tokens(*directories: Path) -> None: for directory in directories: for path in directory.iterdir(): - path.unlink() + if path.is_dir(): + path.rmdir() + else: + path.unlink() diff --git a/tests/integration/providers/test_device_code_login_guard_wire.py b/tests/integration/providers/test_device_code_login_guard_wire.py index 353b760dbf3..b4044674ed5 100644 --- a/tests/integration/providers/test_device_code_login_guard_wire.py +++ b/tests/integration/providers/test_device_code_login_guard_wire.py @@ -595,6 +595,27 @@ def test_copilot_api_key_is_minted_once_from_the_stored_access_token(scene: _Sce assert json.loads((scene.rig.copilot_dir / "api-key.json").read_text())["token"] == dl.COPILOT_MINTED_KEY +def test_copilot_api_key_that_cannot_be_saved_is_minted_at_most_once_per_worker(scene: _Scene) -> None: + dl.write_copilot_access(scene.rig.copilot_dir, dl.COPILOT_UNSAVED_KEY_ACCESS) + unwritable_key_file: Final = scene.rig.copilot_dir / "api-key.json" + unwritable_key_file.mkdir() + first: Final = scene.call(_Cell("copilot", "chat", False, "httpx")) + first_served: Final = _serve(first) + assert first_served.status == 200, first_served.text + (minted,) = scene.rig.peers.auth.drain() + assert minted.headers.get("authorization") == f"token {dl.COPILOT_UNSAVED_KEY_ACCESS}" + later: Final = tuple(scene.call(_Cell("copilot", "chat", False, "httpx")) for _ in range(3)) + later_served: Final = tuple(_serve(call) for call in later) + assert [served.status for served in later_served] == [200, 200, 200], [served.text for served in later_served] + assert len(scene.rig.peers.auth.drain()) <= 1 + received: Final = scene.rig.peers.api.drain() + forwarded: Final = tuple(_carrying(received, call.marker) for call in (first, *later)) + assert [tuple(dl.bearer(request) for request in requests) for requests in forwarded] == [ + (dl.COPILOT_MINTED_KEY,) + ] * 4, _describe(received) + assert unwritable_key_file.is_dir() + + def test_copilot_access_token_github_rejects_fails_the_request_without_a_device_login(scene: _Scene) -> None: dl.write_copilot_access(scene.rig.copilot_dir, dl.COPILOT_REJECTED_ACCESS) served: Final = _serve(scene.call(_Cell("copilot", "chat", False, "httpx"))) diff --git a/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py b/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py index f14feb51bbf..e8a756b4274 100644 --- a/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py +++ b/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py @@ -243,16 +243,45 @@ class TestGitHubCopilotAuthenticator: assert api_key_file.read_bytes() == malformed - def test_refreshed_key_is_used_when_the_key_file_cannot_be_written(self, logged_in_token_dir): + def test_refreshed_key_that_cannot_be_saved_is_reused_with_its_endpoint(self, logged_in_token_dir): unwritable_key_file: Final = logged_in_token_dir / "api-key.json" unwritable_key_file.mkdir() + first: Final = Authenticator() + second: Final = Authenticator() + + with respx.mock(assert_all_called=True) as router: + route: Final = router.get(API_KEY_URL).respond(json={**REFRESHED_KEY, "endpoints": {"api": TENANT_API}}) + served: Final = (first.get_api_key(), second.get_api_key(), first.get_api_key()) + + assert served == ("new-api-key",) * 3 + assert (first.get_api_base(), second.get_api_base()) == (TENANT_API, TENANT_API) + assert route.call_count == 1 + assert unwritable_key_file.is_dir() + + def test_refreshed_key_that_cannot_be_saved_and_has_expired_is_refreshed_again(self, logged_in_token_dir): + (logged_in_token_dir / "api-key.json").mkdir() + auth: Final = Authenticator() + + with respx.mock(assert_all_called=True) as router: + route: Final = router.get(API_KEY_URL).respond(json={"token": "expired-api-key", "expires_at": 1}) + served: Final = (auth.get_api_key(), auth.get_api_key()) + + assert served == ("expired-api-key",) * 2 + assert route.call_count == 2 + + def test_unsaved_key_is_not_reused_after_the_access_token_changes(self, logged_in_token_dir): + (logged_in_token_dir / "api-key.json").mkdir() with respx.mock(assert_all_called=True) as router: route: Final = router.get(API_KEY_URL).respond(json=dict(REFRESHED_KEY)) - assert Authenticator().get_api_key() == "new-api-key" + Authenticator().get_api_key() + (logged_in_token_dir / "access-token").write_text("gho_logged_in_again") + Authenticator().get_api_key() - assert route.call_count == 1 - assert unwritable_key_file.is_dir() + assert [call.request.headers["authorization"] for call in route.calls] == [ + "token gho_logged_in", + "token gho_logged_in_again", + ] @pytest.mark.parametrize( "malformed", From be066e1ae61ac21e2202f582bb3e3efafe3543cc Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 28 Oct 2026 23:04:42 -0700 Subject: [PATCH 8/8] fix(github_copilot): serve a kept key and its endpoint together after the key file is repaired A key held in memory is now checked before api-key.json, so the token and the endpoint come from the same source, and the map holds one entry per key file with the access token beside it --- litellm/llms/github_copilot/authenticator.py | 35 +++++++++++-------- .../test_github_copilot_authenticator.py | 19 ++++++++++ 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/litellm/llms/github_copilot/authenticator.py b/litellm/llms/github_copilot/authenticator.py index 7c14f8f0bc0..ee0e7af7e9b 100644 --- a/litellm/llms/github_copilot/authenticator.py +++ b/litellm/llms/github_copilot/authenticator.py @@ -1,6 +1,7 @@ import json import os import time +from dataclasses import dataclass from datetime import datetime from typing import Any, Final, TypeVar @@ -48,7 +49,14 @@ class CopilotCachedEndpoints(BaseModel): ApiKeyFileView = TypeVar("ApiKeyFileView", CopilotCachedKey, CopilotCachedEndpoints) -_UNSAVED_API_KEYS: Final[dict[str, CopilotCachedKey]] = {} # mutable-ok: refreshed keys api-key.json could not hold + +@dataclass(frozen=True, slots=True) +class _UnsavedApiKey: + access_token: str + key: CopilotCachedKey + + +_UNSAVED_API_KEYS: Final[dict[str, _UnsavedApiKey]] = {} # mutable-ok: refreshed keys api-key.json could not hold def _read_token_file(path: str) -> str: @@ -166,12 +174,12 @@ class Authenticator: Raises: GetAPIKeyError: If unable to obtain an API key. """ - cached: Final = self._read_api_key_file(CopilotCachedKey) - if cached is not None and cached.expires_at > datetime.now().timestamp(): - return cached.token unsaved: Final = self._unsaved_api_key() if unsaved is not None: return unsaved.token + cached: Final = self._read_api_key_file(CopilotCachedKey) + if cached is not None and cached.expires_at > datetime.now().timestamp(): + return cached.token if cached is not None: verbose_logger.warning("API key expired, refreshing") @@ -221,29 +229,28 @@ class Authenticator: verbose_logger.warning("Ignoring %s, not a Copilot API key file: %s", self.api_key_file, e) return None - def _unsaved_api_key_cache_key(self) -> str | None: + def _stored_access_token(self) -> str | None: try: access_token: Final = _read_token_file(self.access_token_file).strip() except (OSError, ValueError): return None - return f"{self.api_key_file}:{access_token}" if access_token else None + return access_token or None def _unsaved_api_key(self) -> CopilotCachedKey | None: - cache_key: Final = self._unsaved_api_key_cache_key() - if cache_key is None: + unsaved: Final = _UNSAVED_API_KEYS.get(self.api_key_file) + if unsaved is None or unsaved.key.expires_at <= time.time(): return None - unsaved: Final = _UNSAVED_API_KEYS.get(cache_key) - return unsaved if unsaved is not None and unsaved.expires_at > time.time() else None + return unsaved.key if unsaved.access_token == self._stored_access_token() else None def _keep_unsaved_api_key(self, serialized: str) -> None: - cache_key: Final = self._unsaved_api_key_cache_key() - if cache_key is None: + access_token: Final = self._stored_access_token() + if access_token is None: return try: - unsaved: Final = CopilotCachedKey.model_validate_json(serialized) + key: Final = CopilotCachedKey.model_validate_json(serialized) except ValueError: return - _UNSAVED_API_KEYS[cache_key] = unsaved + _UNSAVED_API_KEYS[self.api_key_file] = _UnsavedApiKey(access_token=access_token, key=key) def _refresh_api_key(self) -> dict[str, Any]: """ diff --git a/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py b/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py index e8a756b4274..17205f520b8 100644 --- a/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py +++ b/tests/unit/llms/github_copilot/test_github_copilot_authenticator.py @@ -23,6 +23,7 @@ from litellm.llms.github_copilot.common_utils import ( API_KEY_URL: Final = "https://copilot-key.test/copilot_internal/v2/token" REFRESHED_KEY: Final = MappingProxyType({"token": "new-api-key", "expires_at": 4102444800}) TENANT_API: Final = "https://api.tenant.githubcopilot.com" +REPAIRED_API: Final = "https://api.repaired.githubcopilot.com" TRUNCATED_UTF_16: Final = b"\xff\xfe\x00\xd8" @@ -269,6 +270,24 @@ class TestGitHubCopilotAuthenticator: assert served == ("expired-api-key",) * 2 assert route.call_count == 2 + def test_kept_key_and_its_endpoint_stay_paired_after_the_key_file_is_repaired(self, logged_in_token_dir): + key_file: Final = logged_in_token_dir / "api-key.json" + key_file.mkdir() + auth: Final = Authenticator() + + with respx.mock(assert_all_called=True) as router: + router.get(API_KEY_URL).respond(json={**REFRESHED_KEY, "endpoints": {"api": TENANT_API}}) + auth.get_api_key() + key_file.rmdir() + key_file.write_text( + json.dumps({"token": "repaired-api-key", "expires_at": 9999999999, "endpoints": {"api": REPAIRED_API}}) + ) + + assert (auth.get_api_key(), auth.get_api_base()) in ( + ("new-api-key", TENANT_API), + ("repaired-api-key", REPAIRED_API), + ) + def test_unsaved_key_is_not_reused_after_the_access_token_changes(self, logged_in_token_dir): (logged_in_token_dir / "api-key.json").mkdir()