fix(github_copilot): keep a refreshed key that cannot be saved in memory

This commit is contained in:
mateo-berri 2026-10-28 22:26:31 -07:00
parent 9cb35091a3
commit 1d36a09134
4 changed files with 99 additions and 10 deletions

View file

@ -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.

View file

@ -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()

View file

@ -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")))

View file

@ -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",