From 4310791550845562bf2f4786a88ecf78b62c9415 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Tue, 20 Oct 2026 21:04:12 -0700 Subject: [PATCH] fix(cli): expire a CLI token with no recorded proxy and stop treating 404 as rejected A token file without base_url cannot be checked, so it keeps the client window's verdict instead of printing forever. A 404 also comes from a proxy that does not serve /v1/models, so only 401 and 403 count as a rejected key, matching the CLI's existing key check. The timeout test now runs in process. --- litellm/proxy/client/cli/commands/auth.py | 6 ++-- .../proxy/client/cli/test_auth_commands.py | 29 ++++++++++++++----- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/litellm/proxy/client/cli/commands/auth.py b/litellm/proxy/client/cli/commands/auth.py index db4787040ce..e38f54adc4f 100644 --- a/litellm/proxy/client/cli/commands/auth.py +++ b/litellm/proxy/client/cli/commands/auth.py @@ -143,7 +143,7 @@ _TeamMapping: Final = TypeVar("_TeamMapping", bound=Mapping[str, object]) _JSON_OBJECT: Final = TypeAdapter(Mapping[str, object], config=ConfigDict(hide_input_in_errors=True)) _PROXY_KEY_CHECK_TIMEOUT_SECONDS: Final = 3 -_PROXY_REJECTED_KEY_STATUSES: Final = frozenset({401, 403, 404}) +_PROXY_REJECTED_KEY_STATUSES: Final = frozenset({401, 403}) KEYRING_INSTALL_HINT: Final = "pip install 'litellm[cli]'" @@ -1130,8 +1130,8 @@ def token_expired(token_data: Mapping[str, object]) -> bool: def _proxy_rejects_key(token_data: Mapping[str, object]) -> bool: key: Final = token_data.get("key") base_url: Final = token_data.get("base_url") - if not isinstance(key, str) or not isinstance(base_url, str): - return False + if not isinstance(key, str) or not isinstance(base_url, str) or not base_url: + return True try: response: Final = requests.get( f"{base_url}/v1/models", diff --git a/tests/unit/proxy/client/cli/test_auth_commands.py b/tests/unit/proxy/client/cli/test_auth_commands.py index 0872457e4ec..fbababa0d2d 100644 --- a/tests/unit/proxy/client/cli/test_auth_commands.py +++ b/tests/unit/proxy/client/cli/test_auth_commands.py @@ -1,6 +1,5 @@ import json import os -import socket import stat import time from pathlib import Path @@ -1077,7 +1076,7 @@ class TestPrintTokenCommand: (200, 0, "sk-stale-key\n", ""), (401, 1, "", "Token expired. Run 'lite login' again.\n"), (403, 1, "", "Token expired. Run 'lite login' again.\n"), - (404, 1, "", "Token expired. Run 'lite login' again.\n"), + (404, 0, "sk-stale-key\n", ""), (500, 0, "sk-stale-key\n", ""), (requests.ConnectionError("proxy unreachable"), 0, "sk-stale-key\n", ""), ], @@ -1107,22 +1106,38 @@ class TestPrintTokenCommand: assert result.stderr == expected_stderr assert len(responses.calls) == 1 + @responses.activate def test_proxy_check_waits_only_a_few_seconds_for_a_silent_proxy(self): - with socket.create_server(("127.0.0.1", 0)) as silent_proxy, patch( + _register_models_answer("sk-stale-key", requests.ReadTimeout("proxy sent nothing")) + + with patch( "litellm.proxy.client.cli.commands.auth.load_cli_token", return_value=CliTokenRecord( - base_url=f"http://127.0.0.1:{silent_proxy.getsockname()[1]}", + base_url=STALE_TOKEN_BASE_URL, key="sk-stale-key", timestamp=STALE_TOKEN_TIMESTAMP, ), ): - started: Final = time.monotonic() result: Final = self.runner.invoke(print_token, obj={}) - elapsed: Final = time.monotonic() - started + timeout: Final = responses.calls[0].request.req_kwargs["timeout"] assert result.exit_code == 0 assert result.stdout == "sk-stale-key\n" - assert elapsed < 5 + assert isinstance(timeout, int | float) + assert 0 < timeout <= 5 + + @responses.activate + def test_key_without_a_recorded_proxy_still_expires_after_the_client_window(self): + with patch( + "litellm.proxy.client.cli.commands.auth.load_cli_token", + return_value=CliTokenRecord(key="sk-old-format", timestamp=STALE_TOKEN_TIMESTAMP), + ): + result: Final = self.runner.invoke(print_token, obj={}) + + assert result.exit_code == 1 + assert result.stdout == "" + assert result.stderr == "Token expired. Run 'lite login' again.\n" + assert len(responses.calls) == 0 class TestPrintTokenWithConfigFile: