From 797f74efbc6f8692d7cb555f84c575618f02f398 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 16 Oct 2026 12:12:50 -0700 Subject: [PATCH 01/25] feat(cli): sign in through the browser when a lite command gets a 401 without a key When a command in the keys, models, model-groups, users, teams, or credentials groups gets a 401 and no key was passed with --api-key or LITELLM_PROXY_API_KEY, lite now runs the PKCE browser sign-in, stores the credential, and runs the command again once with the new key. All sign-in output goes to stderr, so stdout keeps only the command's output. Before it opens a browser, lite checks whether the proxy accepts the rejected key on /v1/models (a 401 can be a role refusal for a valid key), tries a newer key another lite process stored, and refuses to replace a stored login that belongs to a different proxy. An explicit key that gets a 401 fails with a clean error and never opens a browser. --- litellm/proxy/client/cli/README.md | 14 + litellm/proxy/client/cli/commands/auth.py | 8 +- .../proxy/client/cli/commands/auto_sign_in.py | 181 ++++++++ .../proxy/client/cli/commands/credentials.py | 3 +- litellm/proxy/client/cli/commands/keys.py | 5 +- .../proxy/client/cli/commands/model_groups.py | 3 +- litellm/proxy/client/cli/commands/models.py | 11 +- litellm/proxy/client/cli/commands/teams.py | 10 +- litellm/proxy/client/cli/commands/users.py | 3 +- .../proxy/client/cli/test_auto_sign_in.py | 397 ++++++++++++++++++ 10 files changed, 623 insertions(+), 12 deletions(-) create mode 100644 litellm/proxy/client/cli/commands/auto_sign_in.py create mode 100644 tests/unit/proxy/client/cli/test_auto_sign_in.py diff --git a/litellm/proxy/client/cli/README.md b/litellm/proxy/client/cli/README.md index 842705ad5a7..e1ec65136e5 100644 --- a/litellm/proxy/client/cli/README.md +++ b/litellm/proxy/client/cli/README.md @@ -49,6 +49,20 @@ lite config unset hidden_commands # list everything again Hidden commands drop out of both `lite --help` and the interactive shell's "Available commands" block, and stay runnable so existing scripts keep working +## Automatic Browser Sign-In for Agents + +When a command that calls the proxy (`lite keys`, `lite models`, `lite model-groups`, `lite users`, `lite teams`, `lite credentials`) gets a 401 and you did not pass a key with `--api-key` or `LITELLM_PROXY_API_KEY`, `lite` signs you in through your browser and then runs the same command again with the new key. That covers a proxy you never logged in to and a stored login the proxy no longer accepts. It needs no terminal, so an AI agent that runs `lite` through its shell tool finishes the command on its own once you approve the sign-in in the browser: + +```bash +lite --base-url https://your-proxy.example.com keys generate --models gpt-5.4-mini --key-alias experiment-1 +``` + +The sign-in is the browser flow of `lite login --pkce`, so you pick your team on the consent page, the credential is stored where `lite login` stores it, and its refresh token renews the key on its own from then on. Every line about the sign-in goes to stderr, so stdout carries only the command's own output, such as the JSON from `lite keys generate`. If another `lite` process stored a newer login for the same proxy in the meantime, that key is tried first and no browser opens. `lite` keeps one stored login, so when that login is for a different proxy, the command fails and tells you to run `lite --base-url login` to switch, instead of replacing and revoking the other proxy's login on its own + +Nothing signs in ahead of time. A proxy that accepts the request without a key never opens a browser, and neither does `--help`. The proxy also answers 401 when a valid key lacks the role a command needs, such as a non-admin key running `lite credentials list`, so before it signs in, `lite` checks the rejected key against `/v1/models`. When the proxy answers that check with a success, the command fails with an error that says the key likely lacks the role, and no browser opens. A key you passed yourself is never swapped out, and a 401 for it fails with an error that names `--api-key` and `LITELLM_PROXY_API_KEY`. When the browser sign-in cannot finish (the machine has no web browser, the proxy does not serve `/.well-known/litellm-cli-auth`, you deny the request, or nobody approves it within 5 minutes), the command fails and tells you to run `lite login` from a terminal or set `LITELLM_PROXY_API_KEY` + +`lite chat`, `lite http request`, `lite keys import`, and `lite teams assign-key` do not sign in on their own. `lite chat` and `lite teams assign-key` are interactive, `lite keys import` also talks to a second proxy, and `lite http request` cannot tell a 401 from the proxy apart from a provider's 401 that the proxy passes through + ## Global Options - `--version`, `-v`: Print the LiteLLM Proxy client and server version and exit. diff --git a/litellm/proxy/client/cli/commands/auth.py b/litellm/proxy/client/cli/commands/auth.py index ad54cab6460..05c9300fd61 100644 --- a/litellm/proxy/client/cli/commands/auth.py +++ b/litellm/proxy/client/cli/commands/auth.py @@ -849,14 +849,14 @@ def _finish_login(base_url: str, api_key: str, config_claude: bool, stored: Secr show_commands() -def _replace_stored_token(record: CliTokenData, http: Http, vault: SecretVault) -> SecretSave: +def replace_stored_token(record: CliTokenData, http: Http, vault: SecretVault) -> SecretSave: previous: Final = load_token(vault=vault) stored: Final = save_token(record, vault=vault) if previous is None or isinstance(stored, CredentialNotSaved): return stored revocation: Final = revoke_stored_credential(previous, http) if revocation is not None: - click.echo( + _warn( f"Could not revoke the previous login's refresh token on the proxy ({revocation.reason}); " "it expires on its own." ) @@ -869,7 +869,7 @@ def _pkce_login(base_url: str, config_claude: bool, vault: SecretVault) -> None: if isinstance(credential, PkceFailure): click.echo(f"Authentication failed: {credential.reason}") return - stored: Final = _replace_stored_token(pkce_token_record(base_url, credential), http, vault) + stored: Final = replace_stored_token(pkce_token_record(base_url, credential), http, vault) _finish_login(base_url, credential.access_token, config_claude, stored) @@ -948,7 +948,7 @@ def login(ctx: click.Context, config_claude: bool, pkce: bool) -> None: # Save token data. base_url is stored so we can verify origin # before reusing the key on a subsequent CLI invocation. - stored: Final = _replace_stored_token( + stored: Final = replace_stored_token( { "base_url": base_url.rstrip("/"), "key": api_key, diff --git a/litellm/proxy/client/cli/commands/auto_sign_in.py b/litellm/proxy/client/cli/commands/auto_sign_in.py new file mode 100644 index 00000000000..fa4eb4b506e --- /dev/null +++ b/litellm/proxy/client/cli/commands/auto_sign_in.py @@ -0,0 +1,181 @@ +import webbrowser +from collections.abc import Callable +from dataclasses import dataclass +from typing import Final, Literal, Protocol + +import click +import requests +from typing_extensions import NotRequired, ReadOnly, TypedDict + +from litellm.litellm_core_utils.cli_token_utils import load_cli_token + +from ...exceptions import UnauthorizedError +from .auth import context_secret_vault, get_stored_api_key, replace_stored_token, storage_notice +from .pkce_login import PkceCredential, PkceFailure, pkce_token_record, run_pkce_login + + +class BrowserSignIn(Protocol): + def __call__(self, base_url: str, echo: Callable[[str], None]) -> PkceCredential | PkceFailure: ... + + +def pkce_browser_sign_in( + base_url: str, echo: Callable[[str], None], find_browser: Callable[[], object] = webbrowser.get +) -> PkceCredential | PkceFailure: + try: + _ = find_browser() + except webbrowser.Error: + return PkceFailure("no web browser is available on this machine") + return run_pkce_login(base_url, requests.Session(), echo=echo) + + +class _SignInContext(TypedDict): + base_url: ReadOnly[str] + api_key: ReadOnly[NotRequired[str | None]] + api_key_from_token_file: ReadOnly[NotRequired[bool]] + browser_sign_in: ReadOnly[NotRequired[BrowserSignIn]] + + +class _SignInContextView(Protocol): + @property + def obj(self) -> _SignInContext | None: ... + + +class _ApiKeySlot(Protocol): + def __setitem__(self, key: Literal["api_key"], value: str, /) -> None: ... + + +class _ApiKeySlotView(Protocol): + @property + def obj(self) -> _ApiKeySlot: ... + + +def _sign_in_context(view: _SignInContextView) -> _SignInContext | None: + return view.obj + + +def _api_key_slot(view: _ApiKeySlotView) -> _ApiKeySlot: + return view.obj + + +@dataclass(frozen=True, slots=True) +class _Completed: + value: object + + +@dataclass(frozen=True, slots=True) +class _Unauthorized: + error: UnauthorizedError + + +def _attempt(run: Callable[[click.Context], object], ctx: click.Context) -> _Completed | _Unauthorized: + try: + return _Completed(run(ctx)) + except UnauthorizedError as error: + return _Unauthorized(error) + + +def _attempt_with_key( + run: Callable[[click.Context], object], ctx: click.Context, api_key: str +) -> _Completed | _Unauthorized: + slot: Final = _api_key_slot(ctx) + slot["api_key"] = api_key + return _attempt(run, ctx) + + +def _echo_to_stderr(message: str) -> None: + click.echo(message, err=True) + + +def _attempt_with_newer_stored_key( + run: Callable[[click.Context], object], ctx: click.Context, base_url: str, rejected_key: str | None +) -> _Completed | _Unauthorized | None: + stored_key: Final = get_stored_api_key(expected_base_url=base_url, vault=context_secret_vault(ctx)) + if stored_key is None or stored_key == rejected_key: + return None + return _attempt_with_key(run, ctx, stored_key) + + +def _sign_in(ctx: click.Context, context: _SignInContext) -> str: + base_url: Final = context["base_url"] + _echo_to_stderr(f"The proxy at {base_url} needs a sign-in, so lite is opening your browser to sign in.") + sign_in: Final = context.get("browser_sign_in") or pkce_browser_sign_in + credential: Final = sign_in(base_url, _echo_to_stderr) + if isinstance(credential, PkceFailure): + raise click.ClickException( + f"Could not sign in to {base_url} through the browser: {credential.reason}. " + "Run `lite login` from a terminal, or set LITELLM_PROXY_API_KEY." + ) + stored: Final = replace_stored_token( + pkce_token_record(base_url, credential), requests.Session(), context_secret_vault(ctx) + ) + _echo_to_stderr(storage_notice(stored)) + _echo_to_stderr(f"Signed in. Continuing `{ctx.command_path}`.") + return credential.access_token + + +def _proxy_accepts(base_url: str, api_key: str) -> bool: + try: + response: Final = requests.get( + f"{base_url.rstrip('/')}/v1/models", headers={"Authorization": f"Bearer {api_key}"}, timeout=10 + ) + except requests.RequestException: + return False + return response.ok + + +def _login_for_another_proxy(ctx: click.Context, base_url: str) -> str | None: + login: Final = load_cli_token(vault=context_secret_vault(ctx)) + if login is None or not login.base_url or login.base_url == base_url.rstrip("/"): + return None + return login.base_url + + +def _rerun_after_sign_in(run: Callable[[click.Context], object], ctx: click.Context, context: _SignInContext) -> object: + base_url: Final = context["base_url"] + rejected_key: Final = context.get("api_key") + if rejected_key is not None and _proxy_accepts(base_url, rejected_key): + raise click.ClickException( + f"The proxy at {base_url} accepts this API key but refused `{ctx.command_path}` with HTTP 401, " + "so the key likely lacks the role this command needs." + ) + if not context.get("api_key_from_token_file", False): + raise click.ClickException( + f"The proxy at {base_url} rejected the API key from --api-key or LITELLM_PROXY_API_KEY. " + "Check that key, or unset both to sign in through your browser instead." + ) + retried: Final = _attempt_with_newer_stored_key(run, ctx, base_url, rejected_key) + if isinstance(retried, _Completed): + return retried.value + other_proxy: Final = _login_for_another_proxy(ctx, base_url) + if other_proxy is not None: + raise click.ClickException( + f"The proxy at {base_url} needs a sign-in, but lite keeps one stored login and it is for {other_proxy}, " + f"so lite did not replace it on its own. Run `lite --base-url {base_url} login` to switch proxies, " + "or set LITELLM_PROXY_API_KEY." + ) + match _attempt_with_key(run, ctx, _sign_in(ctx, context)): + case _Completed(value=value): + return value + case _Unauthorized(): + raise click.ClickException( + f"The proxy at {base_url} refused `{ctx.command_path}` with HTTP 401 again after the browser sign-in, " + "so the signed-in user likely lacks the role this command needs. " + "Set LITELLM_PROXY_API_KEY to a key that has it." + ) + + +class SignInOnUnauthorizedCommand(click.Command): + def invoke(self, ctx: click.Context) -> object: + run: Final = super().invoke + match _attempt(run, ctx): + case _Completed(value=value): + return value + case _Unauthorized(error=error): + context: Final = _sign_in_context(ctx) + if context is None: + raise error + return _rerun_after_sign_in(run, ctx, context) + + +class SignInOnUnauthorizedGroup(click.Group): + command_class = SignInOnUnauthorizedCommand diff --git a/litellm/proxy/client/cli/commands/credentials.py b/litellm/proxy/client/cli/commands/credentials.py index 2028bdb5599..a970f46909e 100644 --- a/litellm/proxy/client/cli/commands/credentials.py +++ b/litellm/proxy/client/cli/commands/credentials.py @@ -10,6 +10,7 @@ from typing_extensions import NotRequired, ReadOnly, TypedDict from ...credentials import CredentialsManagementClient from ._cli_context import cli_context_values +from .auto_sign_in import SignInOnUnauthorizedGroup class _CredentialInfo(TypedDict): @@ -39,7 +40,7 @@ def _print_json(data: object) -> None: rich.print_json(data=data) -@click.group() +@click.group(cls=SignInOnUnauthorizedGroup) def credentials(): """Manage credentials for the LiteLLM proxy server""" diff --git a/litellm/proxy/client/cli/commands/keys.py b/litellm/proxy/client/cli/commands/keys.py index 558c9139f5b..1033ec8f6d4 100644 --- a/litellm/proxy/client/cli/commands/keys.py +++ b/litellm/proxy/client/cli/commands/keys.py @@ -11,6 +11,7 @@ from rich.table import Table from typing_extensions import ReadOnly, TypedDict from ...keys import KeysManagementClient +from .auto_sign_in import SignInOnUnauthorizedGroup class _CliContext(TypedDict): @@ -37,7 +38,7 @@ def _cli_context(view: _CliContextView) -> _CliContext: return view.obj -@click.group() +@click.group(cls=SignInOnUnauthorizedGroup) def keys(): """Manage API keys for the LiteLLM proxy server""" @@ -352,7 +353,7 @@ def _import_keys_to_destination( return imported_count, failed_count -@keys.command(name="import") +@keys.command(name="import", cls=click.Command) @click.option( "--source-base-url", required=True, diff --git a/litellm/proxy/client/cli/commands/model_groups.py b/litellm/proxy/client/cli/commands/model_groups.py index 367c2063b6b..baa7c59ae3a 100644 --- a/litellm/proxy/client/cli/commands/model_groups.py +++ b/litellm/proxy/client/cli/commands/model_groups.py @@ -7,6 +7,7 @@ import rich.table from ... import Client from ._cli_context import cli_context_values +from .auto_sign_in import SignInOnUnauthorizedGroup def create_client(ctx: click.Context) -> Client: @@ -19,7 +20,7 @@ def _rendered_field(group: Mapping[str, object], key: str, default: str) -> str: return str(group.get(key, default)) -@click.group(name="model-groups") +@click.group(name="model-groups", cls=SignInOnUnauthorizedGroup) def model_groups() -> None: """Inspect model groups your key can access on the proxy""" diff --git a/litellm/proxy/client/cli/commands/models.py b/litellm/proxy/client/cli/commands/models.py index ac7d5ee45ea..63128c44f38 100644 --- a/litellm/proxy/client/cli/commands/models.py +++ b/litellm/proxy/client/cli/commands/models.py @@ -14,7 +14,9 @@ from typing_extensions import NotRequired, ReadOnly, TypedDict # local imports from ... import Client +from ...exceptions import UnauthorizedError from ._cli_context import cli_context_values +from .auto_sign_in import SignInOnUnauthorizedGroup if TYPE_CHECKING: from rich.console import JustifyMethod @@ -103,7 +105,7 @@ def create_client(ctx: click.Context) -> Client: return Client(base_url=context["base_url"], api_key=context["api_key"]) -@click.group() +@click.group(cls=SignInOnUnauthorizedGroup) def models() -> None: """Manage models on your LiteLLM proxy server""" @@ -449,6 +451,13 @@ def import_models( model_params=model_info_obj.model_params, model_info=model_info_obj.model_info, ) + except UnauthorizedError as error: + if added_models: + raise click.ClickException( + f"The proxy rejected the API key after {len(added_models)} of {len(filtered_model_list)} " + "models were sent, so the import stopped there." + ) from error + raise except Exception: pass # For summary, ignore errors added_models.append(model_info_obj) diff --git a/litellm/proxy/client/cli/commands/teams.py b/litellm/proxy/client/cli/commands/teams.py index 1a941786f19..2fcf4c638c8 100644 --- a/litellm/proxy/client/cli/commands/teams.py +++ b/litellm/proxy/client/cli/commands/teams.py @@ -10,8 +10,10 @@ from rich.table import Table from typing_extensions import ReadOnly, TypedDict from litellm.proxy.client import Client +from litellm.proxy.client.exceptions import UnauthorizedError from ._cli_context import cli_context_values +from .auto_sign_in import SignInOnUnauthorizedGroup class _TeamRow(TypedDict): @@ -29,7 +31,7 @@ class _ErrorBodyView(TypedDict): body: ReadOnly[Mapping[str, object]] -@click.group() +@click.group(cls=SignInOnUnauthorizedGroup) def teams(): """Manage teams and team assignments""" @@ -94,6 +96,8 @@ def list(ctx: click.Context): # Use list() for simpler response structure (returns array directly) teams: Final = client.teams.list() display_teams_table(teams) + except UnauthorizedError: + raise except requests.exceptions.HTTPError as e: click.echo(f"Error: HTTP {e.response.status_code}", err=True) error_body: Final[_ErrorBodyView] = {"body": e.response.json()} @@ -119,6 +123,8 @@ def available(ctx: click.Context): display_teams_table(teams) else: click.echo("No available teams to join.") + except UnauthorizedError: + raise except requests.exceptions.HTTPError as e: click.echo(f"Error: HTTP {e.response.status_code}", err=True) error_body: Final[_ErrorBodyView] = {"body": e.response.json()} @@ -128,7 +134,7 @@ def available(ctx: click.Context): raise click.Abort() -@teams.command() +@teams.command(cls=click.Command) @click.option("--team-id", type=str, help="Team ID to assign the key to") @click.pass_context def assign_key(ctx: click.Context, team_id: str | None): diff --git a/litellm/proxy/client/cli/commands/users.py b/litellm/proxy/client/cli/commands/users.py index a5ebefd1c4a..0d96f15a975 100644 --- a/litellm/proxy/client/cli/commands/users.py +++ b/litellm/proxy/client/cli/commands/users.py @@ -5,9 +5,10 @@ import rich from ... import UsersManagementClient from ._cli_context import cli_context_values +from .auto_sign_in import SignInOnUnauthorizedGroup -@click.group() +@click.group(cls=SignInOnUnauthorizedGroup) def users(): """Manage users on your LiteLLM proxy server""" diff --git a/tests/unit/proxy/client/cli/test_auto_sign_in.py b/tests/unit/proxy/client/cli/test_auto_sign_in.py new file mode 100644 index 00000000000..6312979d07c --- /dev/null +++ b/tests/unit/proxy/client/cli/test_auto_sign_in.py @@ -0,0 +1,397 @@ +import json +import time +import webbrowser +from collections.abc import Callable, Sequence +from pathlib import Path +from typing import Final + +import pytest +import requests +from click.testing import CliRunner, Result + +from litellm.proxy.client.cli import cli +from litellm.proxy.client.cli.commands.auth import CliTokenData, get_stored_api_key, save_token +from litellm.proxy.client.cli.commands.auto_sign_in import pkce_browser_sign_in +from litellm.proxy.client.cli.commands.pkce_login import PkceCredential, PkceFailure, pkce_token_record + +BASE_URL: Final = "https://proxy.example.com" +SIGNED_IN_KEY: Final = "sk-signed-in" +GENERATED: Final = {"key": "sk-generated", "key_alias": "experiment-1"} +REJECTED: Final = {"error": {"message": "Authentication Error, No api key passed in.", "type": "auth_error"}} + + +class FakeBrowserSignIn: + def __init__(self, outcome: PkceCredential | PkceFailure) -> None: + self.outcome = outcome + self.calls: list[str] = [] + + def __call__(self, base_url: str, echo: Callable[[str], None]) -> PkceCredential | PkceFailure: + self.calls.append(base_url) + echo(f"Opening browser to: {base_url}/v1/cli/authorize?state=fake") + return self.outcome + + +def _credential(access_token: str = SIGNED_IN_KEY) -> PkceCredential: + return PkceCredential( + access_token=access_token, + refresh_token="refresh-token", + expires_at=time.time() + 3600, + client_id="cli-client", + token_endpoint=f"{BASE_URL}/v1/cli/token", + revocation_endpoint=f"{BASE_URL}/v1/cli/revoke", + resource=BASE_URL, + user_id="agent-user", + team_id=None, + ) + + +def _classic_login(key: str) -> CliTokenData: + return { + "base_url": BASE_URL, + "key": key, + "user_id": "agent-user", + "user_email": "unknown", + "user_role": "cli", + "auth_header_name": "Authorization", + "jwt_token": "", + "timestamp": time.time(), + } + + +@pytest.fixture(autouse=True) +def no_key_in_the_environment(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.delenv("LITELLM_PROXY_API_KEY", raising=False) + monkeypatch.delenv("LITELLM_PROXY_URL", raising=False) + + +def _lite(args: Sequence[str], vault: object, sign_in: FakeBrowserSignIn, global_args: Sequence[str] = ()) -> Result: + return CliRunner().invoke( + cli, + [*global_args, "--base-url", BASE_URL, *args], + obj={"secret_vault": vault, "browser_sign_in": sign_in}, + ) + + +def _bearers(route) -> list[str | None]: + return [request.headers.get("Authorization") for request in route.request_history] + + +def _key_check(requests_mock, status_code: int): + return requests_mock.get(f"{BASE_URL}/v1/models", status_code=status_code, json={"data": []}) + + +def test_a_401_without_a_login_signs_in_once_stores_the_key_and_finishes_the_command( + requests_mock, secret_vault_factory +): + route = requests_mock.post( + f"{BASE_URL}/key/generate", [{"status_code": 401, "json": REJECTED}, {"status_code": 200, "json": GENERATED}] + ) + vault = secret_vault_factory() + sign_in = FakeBrowserSignIn(_credential()) + + result = _lite(["keys", "generate", "--models", "gpt-5.4-mini", "--key-alias", "experiment-1"], vault, sign_in) + + assert result.exit_code == 0, result.output + assert json.loads(result.stdout) == GENERATED + assert sign_in.calls == [BASE_URL] + assert _bearers(route) == [None, f"Bearer {SIGNED_IN_KEY}"] + assert get_stored_api_key(BASE_URL, vault=vault) == SIGNED_IN_KEY + assert f"The proxy at {BASE_URL} needs a sign-in" in result.stderr + assert f"Opening browser to: {BASE_URL}/v1/cli/authorize" in result.stderr + assert "Credential stored in your OS keychain." in result.stderr + assert "Continuing `cli keys generate`" in result.stderr + assert SIGNED_IN_KEY not in result.output + + +@pytest.mark.parametrize( + "key_check", + [{"status_code": 401}, {"status_code": 503}, {"exc": requests.exceptions.ConnectionError}], + ids=["rejected", "unavailable", "unreachable"], +) +def test_a_stored_login_the_proxy_rejects_is_replaced_by_one_sign_in_with_only_json_on_stdout( + requests_mock, secret_vault_factory, key_check +): + vault = secret_vault_factory() + save_token(pkce_token_record(BASE_URL, _credential("sk-stale")), vault=vault) + requests_mock.get(f"{BASE_URL}/v1/models", **key_check) + revoke = requests_mock.post(f"{BASE_URL}/v1/cli/revoke", status_code=500) + route = requests_mock.post( + f"{BASE_URL}/key/generate", [{"status_code": 401, "json": REJECTED}, {"status_code": 200, "json": GENERATED}] + ) + sign_in = FakeBrowserSignIn(_credential()) + + result = _lite(["keys", "generate"], vault, sign_in) + + assert result.exit_code == 0, result.output + assert json.loads(result.stdout) == GENERATED + assert sign_in.calls == [BASE_URL] + assert _bearers(route) == ["Bearer sk-stale", f"Bearer {SIGNED_IN_KEY}"] + assert revoke.call_count == 1 + assert "Could not revoke the previous login's refresh token" in result.stderr + assert get_stored_api_key(BASE_URL, vault=vault) == SIGNED_IN_KEY + + +def test_a_login_for_another_proxy_is_never_replaced_by_an_automatic_sign_in(requests_mock, secret_vault_factory): + other_proxy = "https://other.example.com" + vault = secret_vault_factory() + save_token({**_classic_login("sk-other"), "base_url": other_proxy}, vault=vault) + route = requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, json=REJECTED) + sign_in = FakeBrowserSignIn(_credential()) + + result = _lite(["keys", "generate"], vault, sign_in) + + assert result.exit_code == 1 + assert isinstance(result.exception, SystemExit) + assert f"lite keeps one stored login and it is for {other_proxy}" in result.stderr + assert f"Run `lite --base-url {BASE_URL} login`" in result.stderr + assert sign_in.calls == [] + assert _bearers(route) == [None] + assert get_stored_api_key(other_proxy, vault=vault) == "sk-other" + + +def test_the_browser_sign_in_fails_at_once_on_a_machine_without_a_browser(requests_mock): + def no_browser() -> object: + raise webbrowser.Error("could not locate runnable browser") + + echoed: list[str] = [] + + outcome = pkce_browser_sign_in(BASE_URL, echoed.append, find_browser=no_browser) + + assert outcome == PkceFailure("no web browser is available on this machine") + assert requests_mock.call_count == 0 + assert echoed == [] + + +@pytest.mark.parametrize( + ("command", "method", "path", "body"), + [ + (["keys", "list", "--format", "json"], "GET", "/key/list", {"keys": []}), + (["models", "list", "--format", "json"], "GET", "/models", {"data": []}), + (["model-groups", "list", "--format", "json"], "GET", "/model_group/info", {"data": []}), + (["users", "list"], "GET", "/user/list", {"users": []}), + (["teams", "list"], "GET", "/team/list", []), + (["teams", "available"], "GET", "/team/available", []), + (["credentials", "list", "--format", "json"], "GET", "/credentials", {"credentials": []}), + ], +) +def test_every_proxy_command_group_signs_in_on_a_401_and_reruns_with_the_new_key( + requests_mock, secret_vault_factory, command, method, path, body +): + route = requests_mock.register_uri( + method, f"{BASE_URL}{path}", [{"status_code": 401, "json": REJECTED}, {"status_code": 200, "json": body}] + ) + sign_in = FakeBrowserSignIn(_credential()) + + result = _lite(command, secret_vault_factory(), sign_in) + + assert result.exit_code == 0, result.output + assert sign_in.calls == [BASE_URL] + assert _bearers(route) == [None, f"Bearer {SIGNED_IN_KEY}"] + + +def test_models_import_signs_in_instead_of_reporting_models_it_never_added( + requests_mock, secret_vault_factory, tmp_path: Path +): + models_file = tmp_path / "models.yaml" + models_file.write_text( + "model_list:\n - model_name: gpt-5.4-mini\n litellm_params:\n model: openai/gpt-5.4-mini\n" + ) + route = requests_mock.post( + f"{BASE_URL}/model/new", [{"status_code": 401, "json": REJECTED}, {"status_code": 200, "json": {}}] + ) + sign_in = FakeBrowserSignIn(_credential()) + + result = _lite(["models", "import", str(models_file)], secret_vault_factory(), sign_in) + + assert result.exit_code == 0, result.output + assert sign_in.calls == [BASE_URL] + assert _bearers(route) == [None, f"Bearer {SIGNED_IN_KEY}"] + + +def test_a_401_partway_through_models_import_stops_without_signing_in_or_adding_models_twice( + requests_mock, secret_vault_factory, tmp_path: Path +): + models_file = tmp_path / "models.yaml" + models_file.write_text( + "model_list:\n" + " - model_name: gpt-5.4-mini\n litellm_params:\n model: openai/gpt-5.4-mini\n" + " - model_name: gpt-5.4\n litellm_params:\n model: openai/gpt-5.4\n" + ) + route = requests_mock.post( + f"{BASE_URL}/model/new", [{"status_code": 200, "json": {}}, {"status_code": 401, "json": REJECTED}] + ) + sign_in = FakeBrowserSignIn(_credential()) + + result = _lite(["models", "import", str(models_file)], secret_vault_factory(), sign_in) + + assert result.exit_code == 1 + assert isinstance(result.exception, SystemExit) + assert "rejected the API key after 1 of 2 models were sent" in result.stderr + assert sign_in.calls == [] + assert [request.json()["model_name"] for request in route.request_history] == ["gpt-5.4-mini", "gpt-5.4"] + + +def test_a_401_from_the_keys_import_source_never_signs_in_to_the_destination(requests_mock, secret_vault_factory): + source = "https://source.example.com" + requests_mock.get(f"{source}/key/list", status_code=401, json=REJECTED) + sign_in = FakeBrowserSignIn(_credential()) + + result = _lite( + ["keys", "import", "--source-base-url", source, "--source-api-key", "sk-source"], + secret_vault_factory(), + sign_in, + ) + + assert result.exit_code != 0 + assert sign_in.calls == [] + assert {request.netloc for request in requests_mock.request_history} == {"source.example.com"} + + +@pytest.mark.parametrize( + ("global_args", "env"), + [(["--api-key", "sk-explicit"], {}), ([], {"LITELLM_PROXY_API_KEY": "sk-explicit"})], + ids=["flag", "env"], +) +def test_a_rejected_explicit_key_fails_cleanly_without_opening_a_browser( + requests_mock, secret_vault_factory, monkeypatch: pytest.MonkeyPatch, global_args, env +): + for name, value in env.items(): + monkeypatch.setenv(name, value) + route = requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, json=REJECTED) + _key_check(requests_mock, 401) + vault = secret_vault_factory() + sign_in = FakeBrowserSignIn(_credential()) + + result = _lite(["keys", "generate"], vault, sign_in, global_args) + + assert result.exit_code == 1 + assert isinstance(result.exception, SystemExit) + assert "rejected the API key from --api-key or LITELLM_PROXY_API_KEY" in result.stderr + assert sign_in.calls == [] + assert _bearers(route) == ["Bearer sk-explicit"] + assert vault.writes == [] + + +def test_a_failed_sign_in_names_the_reason_and_points_at_lite_login(requests_mock, secret_vault_factory): + route = requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, json=REJECTED) + vault = secret_vault_factory() + sign_in = FakeBrowserSignIn(PkceFailure("timed out waiting for the browser sign-in to finish")) + + result = _lite(["keys", "generate"], vault, sign_in) + + assert result.exit_code == 1 + assert isinstance(result.exception, SystemExit) + assert "timed out waiting for the browser sign-in to finish" in result.stderr + assert "Run `lite login` from a terminal, or set LITELLM_PROXY_API_KEY." in result.stderr + assert sign_in.calls == [BASE_URL] + assert route.call_count == 1 + assert get_stored_api_key(BASE_URL, vault=vault) is None + + +def test_a_signed_in_key_the_proxy_also_rejects_is_not_followed_by_another_sign_in(requests_mock, secret_vault_factory): + route = requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, json=REJECTED) + sign_in = FakeBrowserSignIn(_credential()) + + result = _lite(["keys", "generate"], secret_vault_factory(), sign_in) + + assert result.exit_code == 1 + assert isinstance(result.exception, SystemExit) + assert "refused `cli keys generate` with HTTP 401 again after the browser sign-in" in result.stderr + assert sign_in.calls == [BASE_URL] + assert _bearers(route) == [None, f"Bearer {SIGNED_IN_KEY}"] + + +def _rejected_while_a_sibling_signs_in(vault): + def respond(request, context): + save_token(_classic_login("sk-sibling"), vault=vault) + context.status_code = 401 + return REJECTED + + return respond + + +def test_a_key_a_sibling_process_stored_meanwhile_is_used_before_any_browser(requests_mock, secret_vault_factory): + vault = secret_vault_factory() + save_token(_classic_login("sk-stale"), vault=vault) + _key_check(requests_mock, 401) + route = requests_mock.post( + f"{BASE_URL}/key/generate", + [{"json": _rejected_while_a_sibling_signs_in(vault)}, {"status_code": 200, "json": GENERATED}], + ) + sign_in = FakeBrowserSignIn(_credential()) + + result = _lite(["keys", "generate"], vault, sign_in) + + assert result.exit_code == 0, result.output + assert json.loads(result.stdout) == GENERATED + assert sign_in.calls == [] + assert _bearers(route) == ["Bearer sk-stale", "Bearer sk-sibling"] + + +def test_a_sibling_key_the_proxy_also_rejects_falls_through_to_one_sign_in(requests_mock, secret_vault_factory): + vault = secret_vault_factory() + save_token(_classic_login("sk-stale"), vault=vault) + _key_check(requests_mock, 401) + route = requests_mock.post( + f"{BASE_URL}/key/generate", + [ + {"json": _rejected_while_a_sibling_signs_in(vault)}, + {"status_code": 401, "json": REJECTED}, + {"status_code": 200, "json": GENERATED}, + ], + ) + sign_in = FakeBrowserSignIn(_credential()) + + result = _lite(["keys", "generate"], vault, sign_in) + + assert result.exit_code == 0, result.output + assert sign_in.calls == [BASE_URL] + assert _bearers(route) == ["Bearer sk-stale", "Bearer sk-sibling", f"Bearer {SIGNED_IN_KEY}"] + assert get_stored_api_key(BASE_URL, vault=vault) == SIGNED_IN_KEY + + +@pytest.mark.parametrize("explicit", [False, True], ids=["stored", "explicit"]) +def test_a_401_for_a_key_the_proxy_accepts_elsewhere_reports_a_missing_role_without_a_browser( + requests_mock, secret_vault_factory, explicit +): + vault = secret_vault_factory() + save_token(_classic_login("sk-member"), vault=vault) + route = requests_mock.get(f"{BASE_URL}/credentials", status_code=401, json=REJECTED) + key_check = _key_check(requests_mock, 200) + sign_in = FakeBrowserSignIn(_credential()) + writes_before = list(vault.writes) + + result = _lite(["credentials", "list"], vault, sign_in, ["--api-key", "sk-member"] if explicit else []) + + assert result.exit_code == 1 + assert isinstance(result.exception, SystemExit) + assert "accepts this API key but refused `cli credentials list` with HTTP 401" in result.stderr + assert sign_in.calls == [] + assert route.call_count == 1 + assert _bearers(key_check) == ["Bearer sk-member"] + assert vault.writes == writes_before + assert "sk-member" not in result.output + + +def test_help_never_signs_in(requests_mock, secret_vault_factory): + sign_in = FakeBrowserSignIn(_credential()) + + result = _lite(["keys", "generate", "--help"], secret_vault_factory(), sign_in) + + assert result.exit_code == 0 + assert "Generate a new API key" in result.stdout + assert sign_in.calls == [] + assert requests_mock.call_count == 0 + + +@pytest.mark.parametrize("status", [403, 500]) +def test_an_error_other_than_401_never_signs_in(requests_mock, secret_vault_factory, status): + route = requests_mock.post(f"{BASE_URL}/key/generate", status_code=status, json={"error": "refused"}) + sign_in = FakeBrowserSignIn(_credential()) + + result = _lite(["keys", "generate"], secret_vault_factory(), sign_in) + + assert result.exit_code == 1 + assert f"Error: HTTP {status}" in result.stderr + assert sign_in.calls == [] + assert route.call_count == 1 From 9449655e1fa096487f6daea0847522a26822c1d3 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 16 Oct 2026 13:10:14 -0700 Subject: [PATCH 02/25] fix(cli): skip the browser sign-in in CI and with text-mode browsers, and name how long it waits --- litellm/proxy/client/cli/README.md | 6 ++- .../proxy/client/cli/commands/auto_sign_in.py | 29 +++++++++-- .../html_forms/native_client_consent.py | 2 +- .../proxy/client/cli/test_auto_sign_in.py | 52 ++++++++++++++++++- 4 files changed, 79 insertions(+), 10 deletions(-) diff --git a/litellm/proxy/client/cli/README.md b/litellm/proxy/client/cli/README.md index e1ec65136e5..ff5343c0640 100644 --- a/litellm/proxy/client/cli/README.md +++ b/litellm/proxy/client/cli/README.md @@ -59,9 +59,11 @@ lite --base-url https://your-proxy.example.com keys generate --models gpt-5.4-mi The sign-in is the browser flow of `lite login --pkce`, so you pick your team on the consent page, the credential is stored where `lite login` stores it, and its refresh token renews the key on its own from then on. Every line about the sign-in goes to stderr, so stdout carries only the command's own output, such as the JSON from `lite keys generate`. If another `lite` process stored a newer login for the same proxy in the meantime, that key is tried first and no browser opens. `lite` keeps one stored login, so when that login is for a different proxy, the command fails and tells you to run `lite --base-url login` to switch, instead of replacing and revoking the other proxy's login on its own -Nothing signs in ahead of time. A proxy that accepts the request without a key never opens a browser, and neither does `--help`. The proxy also answers 401 when a valid key lacks the role a command needs, such as a non-admin key running `lite credentials list`, so before it signs in, `lite` checks the rejected key against `/v1/models`. When the proxy answers that check with a success, the command fails with an error that says the key likely lacks the role, and no browser opens. A key you passed yourself is never swapped out, and a 401 for it fails with an error that names `--api-key` and `LITELLM_PROXY_API_KEY`. When the browser sign-in cannot finish (the machine has no web browser, the proxy does not serve `/.well-known/litellm-cli-auth`, you deny the request, or nobody approves it within 5 minutes), the command fails and tells you to run `lite login` from a terminal or set `LITELLM_PROXY_API_KEY` +Nothing signs in ahead of time. A proxy that accepts the request without a key never opens a browser, and neither does `--help`. The proxy also answers 401 when a valid key lacks the role a command needs, such as a non-admin key running `lite credentials list`, so before it signs in, `lite` checks the rejected key against `/v1/models`. When the proxy answers that check with a success, the command fails with an error that says the key likely lacks the role, and no browser opens. A key you passed yourself is never swapped out, and a 401 for it fails with an error that names `--api-key` and `LITELLM_PROXY_API_KEY`. When the browser sign-in cannot finish (the machine has no web browser or only a text-mode one such as `w3m` or `lynx`, the proxy does not serve `/.well-known/litellm-cli-auth`, you deny the request, or nobody approves it within 5 minutes), the command fails and tells you to run `lite login` from a terminal or set `LITELLM_PROXY_API_KEY`. `lite` prints that 5-minute wait on stderr when the sign-in starts, so an agent whose shell tool stops commands sooner knows to run the command again with a longer timeout -`lite chat`, `lite http request`, `lite keys import`, and `lite teams assign-key` do not sign in on their own. `lite chat` and `lite teams assign-key` are interactive, `lite keys import` also talks to a second proxy, and `lite http request` cannot tell a 401 from the proxy apart from a provider's 401 that the proxy passes through +When the `CI` environment variable is set to anything but empty, `0`, or `false`, `lite` never opens a browser and the command fails at once with the same message. Set `CI=1` for a script or cron job that should fail instead of waiting for a sign-in + +`lite chat`, `lite encryption migrate`, `lite http request`, `lite keys import`, and `lite teams assign-key` do not sign in on their own. `lite chat` and `lite teams assign-key` are interactive, `lite encryption migrate` is a one-time admin migration you run with an admin key, `lite keys import` also talks to a second proxy, and `lite http request` cannot tell a 401 from the proxy apart from a provider's 401 that the proxy passes through ## Global Options diff --git a/litellm/proxy/client/cli/commands/auto_sign_in.py b/litellm/proxy/client/cli/commands/auto_sign_in.py index fa4eb4b506e..e2bd6c6a999 100644 --- a/litellm/proxy/client/cli/commands/auto_sign_in.py +++ b/litellm/proxy/client/cli/commands/auto_sign_in.py @@ -1,5 +1,6 @@ +import os import webbrowser -from collections.abc import Callable +from collections.abc import Callable, Mapping from dataclasses import dataclass from typing import Final, Literal, Protocol @@ -11,20 +12,38 @@ from litellm.litellm_core_utils.cli_token_utils import load_cli_token from ...exceptions import UnauthorizedError from .auth import context_secret_vault, get_stored_api_key, replace_stored_token, storage_notice -from .pkce_login import PkceCredential, PkceFailure, pkce_token_record, run_pkce_login +from .pkce_login import LOGIN_TIMEOUT_SECONDS, PkceCredential, PkceFailure, pkce_token_record, run_pkce_login class BrowserSignIn(Protocol): def __call__(self, base_url: str, echo: Callable[[str], None]) -> PkceCredential | PkceFailure: ... +_TEXT_MODE_BROWSERS: Final = frozenset({"www-browser", "links", "elinks", "lynx", "w3m"}) + + +def _runs_in_ci(environ: Mapping[str, str]) -> bool: + return environ.get("CI", "").strip().lower() not in {"", "0", "false"} + + def pkce_browser_sign_in( - base_url: str, echo: Callable[[str], None], find_browser: Callable[[], object] = webbrowser.get + base_url: str, + echo: Callable[[str], None], + find_browser: Callable[[], webbrowser.BaseBrowser] = webbrowser.get, + environ: Mapping[str, str] = os.environ, ) -> PkceCredential | PkceFailure: + if _runs_in_ci(environ): + return PkceFailure("the CI environment variable is set, so lite does not open a browser") try: - _ = find_browser() + browser: Final = find_browser() except webbrowser.Error: return PkceFailure("no web browser is available on this machine") + if browser.name in _TEXT_MODE_BROWSERS and "BROWSER" not in environ: + return PkceFailure("only a text-mode web browser is available on this machine") + echo( + f"lite is opening your browser to sign in and waits up to {LOGIN_TIMEOUT_SECONDS // 60} minutes " + "for you to approve it. A shell or agent that stops commands sooner should run this one with a longer timeout." + ) return run_pkce_login(base_url, requests.Session(), echo=echo) @@ -97,7 +116,7 @@ def _attempt_with_newer_stored_key( def _sign_in(ctx: click.Context, context: _SignInContext) -> str: base_url: Final = context["base_url"] - _echo_to_stderr(f"The proxy at {base_url} needs a sign-in, so lite is opening your browser to sign in.") + _echo_to_stderr(f"The proxy at {base_url} needs a sign-in.") sign_in: Final = context.get("browser_sign_in") or pkce_browser_sign_in credential: Final = sign_in(base_url, _echo_to_stderr) if isinstance(credential, PkceFailure): diff --git a/litellm/proxy/common_utils/html_forms/native_client_consent.py b/litellm/proxy/common_utils/html_forms/native_client_consent.py index dac92c4e787..0cbedcd0960 100644 --- a/litellm/proxy/common_utils/html_forms/native_client_consent.py +++ b/litellm/proxy/common_utils/html_forms/native_client_consent.py @@ -59,7 +59,7 @@ button {{ flex: 1; padding: 10px; border-radius: 6px; font-size: 15px; cursor: p

Authorize CLI access

A command-line client at {escape(client_origin)} wants to call LiteLLM as {escape(user_id)}.

-

Approving issues it a personal credential that expires within {CLI_JWT_EXPIRATION_HOURS} hours. lite logout stops it from being renewed. Only approve if you started this sign-in yourself.

+

Approving issues it a personal credential that expires within {CLI_JWT_EXPIRATION_HOURS} hours. lite logout stops it from being renewed. Only approve if you started this sign-in, yourself or through an agent you are running.

{_team_field(teams)} diff --git a/tests/unit/proxy/client/cli/test_auto_sign_in.py b/tests/unit/proxy/client/cli/test_auto_sign_in.py index 6312979d07c..70c2b6dc389 100644 --- a/tests/unit/proxy/client/cli/test_auto_sign_in.py +++ b/tests/unit/proxy/client/cli/test_auto_sign_in.py @@ -150,18 +150,66 @@ def test_a_login_for_another_proxy_is_never_replaced_by_an_automatic_sign_in(req def test_the_browser_sign_in_fails_at_once_on_a_machine_without_a_browser(requests_mock): - def no_browser() -> object: + def no_browser() -> webbrowser.BaseBrowser: raise webbrowser.Error("could not locate runnable browser") echoed: list[str] = [] - outcome = pkce_browser_sign_in(BASE_URL, echoed.append, find_browser=no_browser) + outcome = pkce_browser_sign_in(BASE_URL, echoed.append, find_browser=no_browser, environ={}) assert outcome == PkceFailure("no web browser is available on this machine") assert requests_mock.call_count == 0 assert echoed == [] +@pytest.mark.parametrize("ci", ["true", "1", "True"]) +def test_the_browser_sign_in_never_looks_for_a_browser_in_ci(requests_mock, ci): + def browser_lookup() -> webbrowser.BaseBrowser: + raise AssertionError("looked for a browser while CI is set") + + echoed: list[str] = [] + + outcome = pkce_browser_sign_in(BASE_URL, echoed.append, find_browser=browser_lookup, environ={"CI": ci}) + + assert outcome == PkceFailure("the CI environment variable is set, so lite does not open a browser") + assert requests_mock.call_count == 0 + assert echoed == [] + + +@pytest.mark.parametrize("name", ["www-browser", "links", "elinks", "lynx", "w3m"]) +def test_a_text_mode_browser_counts_as_no_browser(requests_mock, name): + echoed: list[str] = [] + + outcome = pkce_browser_sign_in( + BASE_URL, echoed.append, find_browser=lambda: webbrowser.GenericBrowser(name), environ={"TERM": "xterm"} + ) + + assert outcome == PkceFailure("only a text-mode web browser is available on this machine") + assert requests_mock.call_count == 0 + assert echoed == [] + + +@pytest.mark.parametrize( + "environ", + [{"BROWSER": "w3m"}, {"CI": "false", "BROWSER": "w3m"}, {"CI": "0", "BROWSER": "w3m"}], + ids=["browser-picks-it", "ci-false", "ci-zero"], +) +def test_a_browser_the_user_picked_starts_the_sign_in_and_names_how_long_it_waits(requests_mock, environ): + discovery = requests_mock.get(f"{BASE_URL}/.well-known/litellm-cli-auth", status_code=404) + echoed: list[str] = [] + + outcome = pkce_browser_sign_in( + BASE_URL, echoed.append, find_browser=lambda: webbrowser.GenericBrowser("w3m"), environ=environ + ) + + assert isinstance(outcome, PkceFailure) + assert discovery.call_count == 1 + assert echoed == [ + "lite is opening your browser to sign in and waits up to 5 minutes for you to approve it. " + "A shell or agent that stops commands sooner should run this one with a longer timeout." + ] + + @pytest.mark.parametrize( ("command", "method", "path", "body"), [ From 7140b49a9794f30020517441581aa825fcdc1d07 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 16 Oct 2026 14:01:27 -0700 Subject: [PATCH 03/25] fix(cli): keep the browser launcher off stdout, check the role probe without a key, and point whoami at the automatic sign-in --- litellm/proxy/client/cli/README.md | 2 +- litellm/proxy/client/cli/commands/auth.py | 5 +- .../proxy/client/cli/commands/auto_sign_in.py | 24 +++++++--- .../proxy/client/cli/test_auth_commands.py | 3 +- .../proxy/client/cli/test_auto_sign_in.py | 46 +++++++++++++++---- 5 files changed, 61 insertions(+), 19 deletions(-) diff --git a/litellm/proxy/client/cli/README.md b/litellm/proxy/client/cli/README.md index ff5343c0640..54e3ca5470c 100644 --- a/litellm/proxy/client/cli/README.md +++ b/litellm/proxy/client/cli/README.md @@ -59,7 +59,7 @@ lite --base-url https://your-proxy.example.com keys generate --models gpt-5.4-mi The sign-in is the browser flow of `lite login --pkce`, so you pick your team on the consent page, the credential is stored where `lite login` stores it, and its refresh token renews the key on its own from then on. Every line about the sign-in goes to stderr, so stdout carries only the command's own output, such as the JSON from `lite keys generate`. If another `lite` process stored a newer login for the same proxy in the meantime, that key is tried first and no browser opens. `lite` keeps one stored login, so when that login is for a different proxy, the command fails and tells you to run `lite --base-url login` to switch, instead of replacing and revoking the other proxy's login on its own -Nothing signs in ahead of time. A proxy that accepts the request without a key never opens a browser, and neither does `--help`. The proxy also answers 401 when a valid key lacks the role a command needs, such as a non-admin key running `lite credentials list`, so before it signs in, `lite` checks the rejected key against `/v1/models`. When the proxy answers that check with a success, the command fails with an error that says the key likely lacks the role, and no browser opens. A key you passed yourself is never swapped out, and a 401 for it fails with an error that names `--api-key` and `LITELLM_PROXY_API_KEY`. When the browser sign-in cannot finish (the machine has no web browser or only a text-mode one such as `w3m` or `lynx`, the proxy does not serve `/.well-known/litellm-cli-auth`, you deny the request, or nobody approves it within 5 minutes), the command fails and tells you to run `lite login` from a terminal or set `LITELLM_PROXY_API_KEY`. `lite` prints that 5-minute wait on stderr when the sign-in starts, so an agent whose shell tool stops commands sooner knows to run the command again with a longer timeout +Nothing signs in ahead of time. A proxy that accepts the request without a key never opens a browser, and neither does `--help`. The proxy also answers 401 when a valid key lacks the role a command needs, such as a non-admin key running `lite credentials list`, so before it signs in, `lite` checks the rejected key against `/v1/models`. When the proxy accepts the key there but refuses the same check without a key, the command fails with an error that says the key likely lacks the role, and no browser opens. A key you passed yourself is never swapped out, and a 401 for it fails with an error that names `--api-key` and `LITELLM_PROXY_API_KEY`. When the browser sign-in cannot finish (the machine has no web browser or only a text-mode one such as `w3m` or `lynx`, the proxy does not serve `/.well-known/litellm-cli-auth`, you deny the request, or nobody approves it within 5 minutes), the command fails and tells you to run `lite login` from a terminal or set `LITELLM_PROXY_API_KEY`. `lite` prints that 5-minute wait on stderr when the sign-in starts, so an agent whose shell tool stops commands sooner knows to run the command again with a longer timeout When the `CI` environment variable is set to anything but empty, `0`, or `false`, `lite` never opens a browser and the command fails at once with the same message. Set `CI=1` for a script or cron job that should fail instead of waiting for a sign-in diff --git a/litellm/proxy/client/cli/commands/auth.py b/litellm/proxy/client/cli/commands/auth.py index 05c9300fd61..8b23a5bcfa1 100644 --- a/litellm/proxy/client/cli/commands/auth.py +++ b/litellm/proxy/client/cli/commands/auth.py @@ -1089,7 +1089,10 @@ def whoami(ctx: click.Context): token_data: Final = load_token(vault=vault) if not token_data: - click.echo("Not authenticated. Run 'lite login' to authenticate.") + click.echo( + "Not authenticated. Run the command you need, such as 'lite keys generate', " + "and lite opens your browser to sign in. To sign in first from a terminal, run 'lite login'." + ) return key_readable: Final = token_data.get("key") is not None diff --git a/litellm/proxy/client/cli/commands/auto_sign_in.py b/litellm/proxy/client/cli/commands/auto_sign_in.py index e2bd6c6a999..aca26989057 100644 --- a/litellm/proxy/client/cli/commands/auto_sign_in.py +++ b/litellm/proxy/client/cli/commands/auto_sign_in.py @@ -1,4 +1,6 @@ import os +import subprocess +import sys import webbrowser from collections.abc import Callable, Mapping from dataclasses import dataclass @@ -26,11 +28,16 @@ def _runs_in_ci(environ: Mapping[str, str]) -> bool: return environ.get("CI", "").strip().lower() not in {"", "0", "false"} +def _open_browser_off_stdout(url: str) -> bool: + return subprocess.run((sys.executable, "-I", "-m", "webbrowser", url), stdout=2, check=False).returncode == 0 + + def pkce_browser_sign_in( base_url: str, echo: Callable[[str], None], find_browser: Callable[[], webbrowser.BaseBrowser] = webbrowser.get, environ: Mapping[str, str] = os.environ, + open_browser: Callable[[str], object] = _open_browser_off_stdout, ) -> PkceCredential | PkceFailure: if _runs_in_ci(environ): return PkceFailure("the CI environment variable is set, so lite does not open a browser") @@ -44,7 +51,7 @@ def pkce_browser_sign_in( f"lite is opening your browser to sign in and waits up to {LOGIN_TIMEOUT_SECONDS // 60} minutes " "for you to approve it. A shell or agent that stops commands sooner should run this one with a longer timeout." ) - return run_pkce_login(base_url, requests.Session(), echo=echo) + return run_pkce_login(base_url, requests.Session(), open_browser=open_browser, echo=echo) class _SignInContext(TypedDict): @@ -132,16 +139,18 @@ def _sign_in(ctx: click.Context, context: _SignInContext) -> str: return credential.access_token -def _proxy_accepts(base_url: str, api_key: str) -> bool: +def _lists_models(base_url: str, headers: Mapping[str, str]) -> bool: try: - response: Final = requests.get( - f"{base_url.rstrip('/')}/v1/models", headers={"Authorization": f"Bearer {api_key}"}, timeout=10 - ) + response: Final = requests.get(f"{base_url.rstrip('/')}/v1/models", headers=headers, timeout=10) except requests.RequestException: return False return response.ok +def _proxy_accepts(base_url: str, api_key: str) -> bool: + return _lists_models(base_url, {"Authorization": f"Bearer {api_key}"}) and not _lists_models(base_url, {}) + + def _login_for_another_proxy(ctx: click.Context, base_url: str) -> str | None: login: Final = load_cli_token(vault=context_secret_vault(ctx)) if login is None or not login.base_url or login.base_url == base_url.rstrip("/"): @@ -153,9 +162,10 @@ def _rerun_after_sign_in(run: Callable[[click.Context], object], ctx: click.Cont base_url: Final = context["base_url"] rejected_key: Final = context.get("api_key") if rejected_key is not None and _proxy_accepts(base_url, rejected_key): + accepted: Final = "your stored login" if context.get("api_key_from_token_file", False) else "this API key" raise click.ClickException( - f"The proxy at {base_url} accepts this API key but refused `{ctx.command_path}` with HTTP 401, " - "so the key likely lacks the role this command needs." + f"The proxy at {base_url} accepts {accepted} but refused `{ctx.command_path}` with HTTP 401, " + "so it likely lacks the role this command needs. Set LITELLM_PROXY_API_KEY to a key that has it." ) if not context.get("api_key_from_token_file", False): raise click.ClickException( diff --git a/tests/unit/proxy/client/cli/test_auth_commands.py b/tests/unit/proxy/client/cli/test_auth_commands.py index 27edf9f3eb3..57e73522b5f 100644 --- a/tests/unit/proxy/client/cli/test_auth_commands.py +++ b/tests/unit/proxy/client/cli/test_auth_commands.py @@ -695,7 +695,8 @@ class TestWhoamiCommand: assert result.exit_code == 0 assert "Not authenticated" in result.output - assert "Run 'lite login'" in result.output + assert "lite opens your browser to sign in" in result.output + assert "run 'lite login'" in result.output def test_whoami_old_token(self): """Test whoami with old token showing warning""" diff --git a/tests/unit/proxy/client/cli/test_auto_sign_in.py b/tests/unit/proxy/client/cli/test_auto_sign_in.py index 70c2b6dc389..36bb5a31d3b 100644 --- a/tests/unit/proxy/client/cli/test_auto_sign_in.py +++ b/tests/unit/proxy/client/cli/test_auto_sign_in.py @@ -1,5 +1,4 @@ import json -import time import webbrowser from collections.abc import Callable, Sequence from pathlib import Path @@ -18,6 +17,8 @@ BASE_URL: Final = "https://proxy.example.com" SIGNED_IN_KEY: Final = "sk-signed-in" GENERATED: Final = {"key": "sk-generated", "key_alias": "experiment-1"} REJECTED: Final = {"error": {"message": "Authentication Error, No api key passed in.", "type": "auth_error"}} +SIGNED_IN_AT: Final = 1_790_000_000.0 +EXPIRES_AT: Final = 4_102_444_800.0 class FakeBrowserSignIn: @@ -35,7 +36,7 @@ def _credential(access_token: str = SIGNED_IN_KEY) -> PkceCredential: return PkceCredential( access_token=access_token, refresh_token="refresh-token", - expires_at=time.time() + 3600, + expires_at=EXPIRES_AT, client_id="cli-client", token_endpoint=f"{BASE_URL}/v1/cli/token", revocation_endpoint=f"{BASE_URL}/v1/cli/revoke", @@ -54,7 +55,7 @@ def _classic_login(key: str) -> CliTokenData: "user_role": "cli", "auth_header_name": "Authorization", "jwt_token": "", - "timestamp": time.time(), + "timestamp": SIGNED_IN_AT, } @@ -76,8 +77,12 @@ def _bearers(route) -> list[str | None]: return [request.headers.get("Authorization") for request in route.request_history] -def _key_check(requests_mock, status_code: int): - return requests_mock.get(f"{BASE_URL}/v1/models", status_code=status_code, json={"data": []}) +def _key_check(requests_mock, status_code: int, public: bool = False): + def respond(request, context) -> dict[str, list[str]]: + context.status_code = status_code if public or "Authorization" in request.headers else 401 + return {"data": []} + + return requests_mock.get(f"{BASE_URL}/v1/models", json=respond) def test_a_401_without_a_login_signs_in_once_stores_the_key_and_finishes_the_command( @@ -398,9 +403,11 @@ def test_a_sibling_key_the_proxy_also_rejects_falls_through_to_one_sign_in(reque assert get_stored_api_key(BASE_URL, vault=vault) == SIGNED_IN_KEY -@pytest.mark.parametrize("explicit", [False, True], ids=["stored", "explicit"]) +@pytest.mark.parametrize( + ("explicit", "accepted"), [(False, "your stored login"), (True, "this API key")], ids=["stored", "explicit"] +) def test_a_401_for_a_key_the_proxy_accepts_elsewhere_reports_a_missing_role_without_a_browser( - requests_mock, secret_vault_factory, explicit + requests_mock, secret_vault_factory, explicit, accepted ): vault = secret_vault_factory() save_token(_classic_login("sk-member"), vault=vault) @@ -413,14 +420,35 @@ def test_a_401_for_a_key_the_proxy_accepts_elsewhere_reports_a_missing_role_with assert result.exit_code == 1 assert isinstance(result.exception, SystemExit) - assert "accepts this API key but refused `cli credentials list` with HTTP 401" in result.stderr + assert f"accepts {accepted} but refused `cli credentials list` with HTTP 401" in result.stderr + assert "Set LITELLM_PROXY_API_KEY to a key that has it" in result.stderr assert sign_in.calls == [] assert route.call_count == 1 - assert _bearers(key_check) == ["Bearer sk-member"] + assert _bearers(key_check) == ["Bearer sk-member", None] assert vault.writes == writes_before assert "sk-member" not in result.output +def test_a_public_models_route_never_passes_a_rejected_stored_login_off_as_a_missing_role( + requests_mock, secret_vault_factory +): + vault = secret_vault_factory() + save_token(_classic_login("sk-stale"), vault=vault) + _key_check(requests_mock, 200, public=True) + route = requests_mock.get( + f"{BASE_URL}/credentials", + [{"status_code": 401, "json": REJECTED}, {"status_code": 200, "json": {"credentials": []}}], + ) + sign_in = FakeBrowserSignIn(_credential()) + + result = _lite(["credentials", "list", "--format", "json"], vault, sign_in) + + assert result.exit_code == 0, result.output + assert sign_in.calls == [BASE_URL] + assert _bearers(route) == ["Bearer sk-stale", f"Bearer {SIGNED_IN_KEY}"] + assert get_stored_api_key(BASE_URL, vault=vault) == SIGNED_IN_KEY + + def test_help_never_signs_in(requests_mock, secret_vault_factory): sign_in = FakeBrowserSignIn(_credential()) From f883b064574176055af56769ef43db8a556434e1 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 16 Oct 2026 14:27:48 -0700 Subject: [PATCH 04/25] fix(cli): open the sign-in browser without ringing the terminal bell --- litellm/proxy/client/cli/commands/auto_sign_in.py | 9 ++++++--- tests/unit/proxy/client/cli/test_auto_sign_in.py | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/litellm/proxy/client/cli/commands/auto_sign_in.py b/litellm/proxy/client/cli/commands/auto_sign_in.py index aca26989057..ea769a4a717 100644 --- a/litellm/proxy/client/cli/commands/auto_sign_in.py +++ b/litellm/proxy/client/cli/commands/auto_sign_in.py @@ -28,8 +28,11 @@ def _runs_in_ci(environ: Mapping[str, str]) -> bool: return environ.get("CI", "").strip().lower() not in {"", "0", "false"} -def _open_browser_off_stdout(url: str) -> bool: - return subprocess.run((sys.executable, "-I", "-m", "webbrowser", url), stdout=2, check=False).returncode == 0 +_OPEN_URL_IN_BROWSER: Final = "import sys, webbrowser; webbrowser.open(sys.argv[1])" + + +def open_browser_off_stdout(url: str) -> None: + subprocess.run((sys.executable, "-I", "-c", _OPEN_URL_IN_BROWSER, url), stdout=2, check=False) def pkce_browser_sign_in( @@ -37,7 +40,7 @@ def pkce_browser_sign_in( echo: Callable[[str], None], find_browser: Callable[[], webbrowser.BaseBrowser] = webbrowser.get, environ: Mapping[str, str] = os.environ, - open_browser: Callable[[str], object] = _open_browser_off_stdout, + open_browser: Callable[[str], object] = open_browser_off_stdout, ) -> PkceCredential | PkceFailure: if _runs_in_ci(environ): return PkceFailure("the CI environment variable is set, so lite does not open a browser") diff --git a/tests/unit/proxy/client/cli/test_auto_sign_in.py b/tests/unit/proxy/client/cli/test_auto_sign_in.py index 36bb5a31d3b..949a0321f75 100644 --- a/tests/unit/proxy/client/cli/test_auto_sign_in.py +++ b/tests/unit/proxy/client/cli/test_auto_sign_in.py @@ -10,7 +10,7 @@ from click.testing import CliRunner, Result from litellm.proxy.client.cli import cli from litellm.proxy.client.cli.commands.auth import CliTokenData, get_stored_api_key, save_token -from litellm.proxy.client.cli.commands.auto_sign_in import pkce_browser_sign_in +from litellm.proxy.client.cli.commands.auto_sign_in import open_browser_off_stdout, pkce_browser_sign_in from litellm.proxy.client.cli.commands.pkce_login import PkceCredential, PkceFailure, pkce_token_record BASE_URL: Final = "https://proxy.example.com" @@ -215,6 +215,19 @@ def test_a_browser_the_user_picked_starts_the_sign_in_and_names_how_long_it_wait ] +def test_the_browser_launcher_keeps_stdout_clean_and_never_rings_the_terminal_bell( + capfd: pytest.CaptureFixture[str], monkeypatch: pytest.MonkeyPatch, tmp_path: Path +): + chatty_browser = tmp_path / "chatty-browser" + chatty_browser.write_text('#!/bin/sh\necho "Opening in existing browser session."\n') + chatty_browser.chmod(0o755) + monkeypatch.setenv("BROWSER", str(chatty_browser)) + + open_browser_off_stdout(f"{BASE_URL}/v1/cli/authorize?state=fake") + + assert capfd.readouterr() == ("", "Opening in existing browser session.\n") + + @pytest.mark.parametrize( ("command", "method", "path", "body"), [ From 8c90ab653434cdb278cae06aa43eaa6635ab9610 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 16 Oct 2026 14:45:06 -0700 Subject: [PATCH 05/25] test(cli): drop the browser launcher test that started a subprocess --- tests/unit/proxy/client/cli/test_auto_sign_in.py | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/tests/unit/proxy/client/cli/test_auto_sign_in.py b/tests/unit/proxy/client/cli/test_auto_sign_in.py index 949a0321f75..36bb5a31d3b 100644 --- a/tests/unit/proxy/client/cli/test_auto_sign_in.py +++ b/tests/unit/proxy/client/cli/test_auto_sign_in.py @@ -10,7 +10,7 @@ from click.testing import CliRunner, Result from litellm.proxy.client.cli import cli from litellm.proxy.client.cli.commands.auth import CliTokenData, get_stored_api_key, save_token -from litellm.proxy.client.cli.commands.auto_sign_in import open_browser_off_stdout, pkce_browser_sign_in +from litellm.proxy.client.cli.commands.auto_sign_in import pkce_browser_sign_in from litellm.proxy.client.cli.commands.pkce_login import PkceCredential, PkceFailure, pkce_token_record BASE_URL: Final = "https://proxy.example.com" @@ -215,19 +215,6 @@ def test_a_browser_the_user_picked_starts_the_sign_in_and_names_how_long_it_wait ] -def test_the_browser_launcher_keeps_stdout_clean_and_never_rings_the_terminal_bell( - capfd: pytest.CaptureFixture[str], monkeypatch: pytest.MonkeyPatch, tmp_path: Path -): - chatty_browser = tmp_path / "chatty-browser" - chatty_browser.write_text('#!/bin/sh\necho "Opening in existing browser session."\n') - chatty_browser.chmod(0o755) - monkeypatch.setenv("BROWSER", str(chatty_browser)) - - open_browser_off_stdout(f"{BASE_URL}/v1/cli/authorize?state=fake") - - assert capfd.readouterr() == ("", "Opening in existing browser session.\n") - - @pytest.mark.parametrize( ("command", "method", "path", "body"), [ From 594bb8c9592573de2a1e5c24a54b137d7a2c6890 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 16 Oct 2026 15:50:48 -0700 Subject: [PATCH 06/25] fix(cli): check a stored login with /key/info, sign in once across parallel commands, and fail fast when no browser can open --- litellm/proxy/client/cli/README.md | 4 +- litellm/proxy/client/cli/commands/auth.py | 6 +- .../proxy/client/cli/commands/auto_sign_in.py | 63 ++-- .../proxy/client/cli/commands/pkce_login.py | 12 +- .../proxy/client/cli/test_auth_commands.py | 4 +- .../proxy/client/cli/test_auto_sign_in.py | 321 +++++++++++------- .../unit/proxy/client/cli/test_pkce_login.py | 16 + 7 files changed, 274 insertions(+), 152 deletions(-) diff --git a/litellm/proxy/client/cli/README.md b/litellm/proxy/client/cli/README.md index 54e3ca5470c..9a51891cef0 100644 --- a/litellm/proxy/client/cli/README.md +++ b/litellm/proxy/client/cli/README.md @@ -57,9 +57,9 @@ When a command that calls the proxy (`lite keys`, `lite models`, `lite model-gro lite --base-url https://your-proxy.example.com keys generate --models gpt-5.4-mini --key-alias experiment-1 ``` -The sign-in is the browser flow of `lite login --pkce`, so you pick your team on the consent page, the credential is stored where `lite login` stores it, and its refresh token renews the key on its own from then on. Every line about the sign-in goes to stderr, so stdout carries only the command's own output, such as the JSON from `lite keys generate`. If another `lite` process stored a newer login for the same proxy in the meantime, that key is tried first and no browser opens. `lite` keeps one stored login, so when that login is for a different proxy, the command fails and tells you to run `lite --base-url login` to switch, instead of replacing and revoking the other proxy's login on its own +The sign-in is the browser flow of `lite login --pkce`, so you pick your team on the consent page, the credential is stored where `lite login` stores it, and its refresh token renews the key on its own from then on. Every line about the sign-in goes to stderr, so stdout carries only the command's own output, such as the JSON from `lite keys generate`. If another `lite` process is already signing in, the command waits for it to finish and then uses the login it stored, so parallel commands open one browser tab. If another `lite` process stored a newer login for the same proxy in the meantime, that key is tried first and no browser opens. `lite` keeps one stored login, so when that login is for a different proxy, the command fails and tells you to run `lite --base-url login --pkce` to switch, instead of replacing and revoking the other proxy's login on its own -Nothing signs in ahead of time. A proxy that accepts the request without a key never opens a browser, and neither does `--help`. The proxy also answers 401 when a valid key lacks the role a command needs, such as a non-admin key running `lite credentials list`, so before it signs in, `lite` checks the rejected key against `/v1/models`. When the proxy accepts the key there but refuses the same check without a key, the command fails with an error that says the key likely lacks the role, and no browser opens. A key you passed yourself is never swapped out, and a 401 for it fails with an error that names `--api-key` and `LITELLM_PROXY_API_KEY`. When the browser sign-in cannot finish (the machine has no web browser or only a text-mode one such as `w3m` or `lynx`, the proxy does not serve `/.well-known/litellm-cli-auth`, you deny the request, or nobody approves it within 5 minutes), the command fails and tells you to run `lite login` from a terminal or set `LITELLM_PROXY_API_KEY`. `lite` prints that 5-minute wait on stderr when the sign-in starts, so an agent whose shell tool stops commands sooner knows to run the command again with a longer timeout +Nothing signs in ahead of time. A proxy that accepts the request without a key never opens a browser, and neither does `--help`. The proxy also answers 401 when a valid key lacks the role a command needs, such as a non-admin key running `lite credentials list`, so before it signs in, `lite` checks a rejected stored login against `/key/info`. When the proxy accepts the login there, the command fails with an error that says the login likely lacks the role, and no browser opens. A key you passed yourself is never swapped out or checked again, and a 401 for it fails with an error that names `--api-key` and `LITELLM_PROXY_API_KEY`. When the browser sign-in cannot finish (the machine has no web browser or only a text-mode one such as `w3m` or `lynx`, the shell runs over SSH with no display and `BROWSER` is unset, the browser fails to open, the proxy does not serve `/.well-known/litellm-cli-auth`, you deny the request, or nobody approves it within 5 minutes), the command fails and tells you to run `lite login --pkce` from a terminal on a machine with a browser, `lite login` on one without, or set `LITELLM_PROXY_API_KEY`. `lite` prints that 5-minute wait on stderr when the sign-in starts, so an agent whose shell tool stops commands sooner knows to run the command again with a longer timeout When the `CI` environment variable is set to anything but empty, `0`, or `false`, `lite` never opens a browser and the command fails at once with the same message. Set `CI=1` for a script or cron job that should fail instead of waiting for a sign-in diff --git a/litellm/proxy/client/cli/commands/auth.py b/litellm/proxy/client/cli/commands/auth.py index 8b23a5bcfa1..b2bb76f9fb1 100644 --- a/litellm/proxy/client/cli/commands/auth.py +++ b/litellm/proxy/client/cli/commands/auth.py @@ -53,6 +53,7 @@ from .claude_settings import ( settings_file_owners, ) from .pkce_login import ( + LOGIN_TIMEOUT_SECONDS, Http, PkceFailure, RevocationUnavailable, @@ -1090,8 +1091,9 @@ def whoami(ctx: click.Context): if not token_data: click.echo( - "Not authenticated. Run the command you need, such as 'lite keys generate', " - "and lite opens your browser to sign in. To sign in first from a terminal, run 'lite login'." + "Not authenticated. Run the command you need, such as 'lite keys generate', and lite opens your browser " + f"to sign in and waits up to {LOGIN_TIMEOUT_SECONDS // 60} minutes for you to approve it. " + "To sign in first from a terminal, run 'lite login --pkce'." ) return diff --git a/litellm/proxy/client/cli/commands/auto_sign_in.py b/litellm/proxy/client/cli/commands/auto_sign_in.py index ea769a4a717..74aef486933 100644 --- a/litellm/proxy/client/cli/commands/auto_sign_in.py +++ b/litellm/proxy/client/cli/commands/auto_sign_in.py @@ -3,14 +3,17 @@ import subprocess import sys import webbrowser from collections.abc import Callable, Mapping +from contextlib import AbstractContextManager from dataclasses import dataclass +from pathlib import Path from typing import Final, Literal, Protocol import click import requests +from filelock import FileLock from typing_extensions import NotRequired, ReadOnly, TypedDict -from litellm.litellm_core_utils.cli_token_utils import load_cli_token +from litellm.litellm_core_utils.cli_token_utils import get_cli_token_file_path, load_cli_token from ...exceptions import UnauthorizedError from .auth import context_secret_vault, get_stored_api_key, replace_stored_token, storage_notice @@ -22,17 +25,25 @@ class BrowserSignIn(Protocol): _TEXT_MODE_BROWSERS: Final = frozenset({"www-browser", "links", "elinks", "lynx", "w3m"}) +_SSH_SESSION_VARIABLES: Final = ("SSH_CONNECTION", "SSH_TTY") +_DISPLAY_VARIABLES: Final = ("DISPLAY", "WAYLAND_DISPLAY") def _runs_in_ci(environ: Mapping[str, str]) -> bool: return environ.get("CI", "").strip().lower() not in {"", "0", "false"} -_OPEN_URL_IN_BROWSER: Final = "import sys, webbrowser; webbrowser.open(sys.argv[1])" +def _ssh_session_without_a_display(environ: Mapping[str, str]) -> bool: + over_ssh: Final = any(environ.get(name) for name in _SSH_SESSION_VARIABLES) + return over_ssh and not any(environ.get(name) for name in _DISPLAY_VARIABLES) -def open_browser_off_stdout(url: str) -> None: - subprocess.run((sys.executable, "-I", "-c", _OPEN_URL_IN_BROWSER, url), stdout=2, check=False) +_OPEN_URL_IN_BROWSER: Final = "import sys, webbrowser; sys.exit(0 if webbrowser.open(sys.argv[1]) else 1)" + + +def open_browser_off_stdout(url: str) -> PkceFailure | None: + launcher: Final = subprocess.run((sys.executable, "-I", "-c", _OPEN_URL_IN_BROWSER, url), stdout=2, check=False) + return None if launcher.returncode == 0 else PkceFailure("the web browser did not open") def pkce_browser_sign_in( @@ -44,6 +55,8 @@ def pkce_browser_sign_in( ) -> PkceCredential | PkceFailure: if _runs_in_ci(environ): return PkceFailure("the CI environment variable is set, so lite does not open a browser") + if "BROWSER" not in environ and _ssh_session_without_a_display(environ): + return PkceFailure("this is an SSH session without a display, so a browser would open where you cannot see it") try: browser: Final = find_browser() except webbrowser.Error: @@ -62,6 +75,7 @@ class _SignInContext(TypedDict): api_key: ReadOnly[NotRequired[str | None]] api_key_from_token_file: ReadOnly[NotRequired[bool]] browser_sign_in: ReadOnly[NotRequired[BrowserSignIn]] + sign_in_lock: ReadOnly[NotRequired[Callable[[], AbstractContextManager[object]]]] class _SignInContextView(Protocol): @@ -132,7 +146,8 @@ def _sign_in(ctx: click.Context, context: _SignInContext) -> str: if isinstance(credential, PkceFailure): raise click.ClickException( f"Could not sign in to {base_url} through the browser: {credential.reason}. " - "Run `lite login` from a terminal, or set LITELLM_PROXY_API_KEY." + "Run `lite login --pkce` from a terminal on a machine with a browser, `lite login` on one without, " + "or set LITELLM_PROXY_API_KEY." ) stored: Final = replace_stored_token( pkce_token_record(base_url, credential), requests.Session(), context_secret_vault(ctx) @@ -142,16 +157,18 @@ def _sign_in(ctx: click.Context, context: _SignInContext) -> str: return credential.access_token -def _lists_models(base_url: str, headers: Mapping[str, str]) -> bool: +def _proxy_accepts(base_url: str, api_key: str) -> bool: try: - response: Final = requests.get(f"{base_url.rstrip('/')}/v1/models", headers=headers, timeout=10) + response: Final = requests.get( + f"{base_url.rstrip('/')}/key/info", headers={"Authorization": f"Bearer {api_key}"}, timeout=10 + ) except requests.RequestException: return False return response.ok -def _proxy_accepts(base_url: str, api_key: str) -> bool: - return _lists_models(base_url, {"Authorization": f"Bearer {api_key}"}) and not _lists_models(base_url, {}) +def _sign_in_lock() -> FileLock: + return FileLock(Path(get_cli_token_file_path()).with_name("sign-in.lock")) def _login_for_another_proxy(ctx: click.Context, base_url: str) -> str | None: @@ -163,18 +180,26 @@ def _login_for_another_proxy(ctx: click.Context, base_url: str) -> str | None: def _rerun_after_sign_in(run: Callable[[click.Context], object], ctx: click.Context, context: _SignInContext) -> object: base_url: Final = context["base_url"] - rejected_key: Final = context.get("api_key") - if rejected_key is not None and _proxy_accepts(base_url, rejected_key): - accepted: Final = "your stored login" if context.get("api_key_from_token_file", False) else "this API key" - raise click.ClickException( - f"The proxy at {base_url} accepts {accepted} but refused `{ctx.command_path}` with HTTP 401, " - "so it likely lacks the role this command needs. Set LITELLM_PROXY_API_KEY to a key that has it." - ) if not context.get("api_key_from_token_file", False): raise click.ClickException( - f"The proxy at {base_url} rejected the API key from --api-key or LITELLM_PROXY_API_KEY. " - "Check that key, or unset both to sign in through your browser instead." + f"The proxy at {base_url} refused `{ctx.command_path}` with HTTP 401 for the API key from --api-key " + "or LITELLM_PROXY_API_KEY. Check that the key is valid and has the role this command needs, " + "or unset both to sign in through your browser instead." ) + rejected_key: Final = context.get("api_key") + if rejected_key is not None and _proxy_accepts(base_url, rejected_key): + raise click.ClickException( + f"The proxy at {base_url} accepts your stored login but refused `{ctx.command_path}` with HTTP 401, " + "so it likely lacks the role this command needs. Set LITELLM_PROXY_API_KEY to a key that has it." + ) + with context.get("sign_in_lock", _sign_in_lock)(): + return _rerun_once_signed_in(run, ctx, context, rejected_key) + + +def _rerun_once_signed_in( + run: Callable[[click.Context], object], ctx: click.Context, context: _SignInContext, rejected_key: str | None +) -> object: + base_url: Final = context["base_url"] retried: Final = _attempt_with_newer_stored_key(run, ctx, base_url, rejected_key) if isinstance(retried, _Completed): return retried.value @@ -182,7 +207,7 @@ def _rerun_after_sign_in(run: Callable[[click.Context], object], ctx: click.Cont if other_proxy is not None: raise click.ClickException( f"The proxy at {base_url} needs a sign-in, but lite keeps one stored login and it is for {other_proxy}, " - f"so lite did not replace it on its own. Run `lite --base-url {base_url} login` to switch proxies, " + f"so lite did not replace it on its own. Run `lite --base-url {base_url} login --pkce` to switch proxies, " "or set LITELLM_PROXY_API_KEY." ) match _attempt_with_key(run, ctx, _sign_in(ctx, context)): diff --git a/litellm/proxy/client/cli/commands/pkce_login.py b/litellm/proxy/client/cli/commands/pkce_login.py index 7cf846bdf9a..9e4bf02cf13 100644 --- a/litellm/proxy/client/cli/commands/pkce_login.py +++ b/litellm/proxy/client/cli/commands/pkce_login.py @@ -128,6 +128,7 @@ class LoopbackServer(HTTPServer): self.expected_state: Final = expected_state self.connection_timeout_seconds: Final = connection_timeout_seconds self.outcome: CallbackOutcome | None = None + self.launch_failure: PkceFailure | None = None self.timeout = 1 @property @@ -139,11 +140,18 @@ class LoopbackServer(HTTPServer): accepted[0].settimeout(self.connection_timeout_seconds) return accepted + def launch_browser(self, open_browser: Callable[[str], object], url: str) -> None: + opened: Final = open_browser(url) + if isinstance(opened, PkceFailure): + self.launch_failure = opened + def wait( self, timeout_seconds: float, clock: Callable[[], float] = time.monotonic ) -> CallbackOutcome | PkceFailure: deadline: Final = clock() + timeout_seconds while self.outcome is None: + if self.launch_failure is not None: + return self.launch_failure if clock() >= deadline: return PkceFailure("timed out waiting for the browser sign-in to finish") self.handle_request() @@ -454,7 +462,9 @@ def run_pkce_login( url: Final = authorize_url(contract, client_id, server.redirect_uri, state, challenge) echo(f"Opening browser to: {url}") echo("Approve the sign-in in your browser. Waiting...") - threading.Thread(target=open_browser, args=(url,), name="lite-login-browser", daemon=True).start() + threading.Thread( + target=server.launch_browser, args=(open_browser, url), name="lite-login-browser", daemon=True + ).start() outcome: Final = server.wait(timeout_seconds) match outcome: case PkceFailure(): diff --git a/tests/unit/proxy/client/cli/test_auth_commands.py b/tests/unit/proxy/client/cli/test_auth_commands.py index 57e73522b5f..83793fa841c 100644 --- a/tests/unit/proxy/client/cli/test_auth_commands.py +++ b/tests/unit/proxy/client/cli/test_auth_commands.py @@ -695,8 +695,8 @@ class TestWhoamiCommand: assert result.exit_code == 0 assert "Not authenticated" in result.output - assert "lite opens your browser to sign in" in result.output - assert "run 'lite login'" in result.output + assert "lite opens your browser to sign in and waits up to 5 minutes" in result.output + assert "run 'lite login --pkce'" in result.output def test_whoami_old_token(self): """Test whoami with old token showing warning""" diff --git a/tests/unit/proxy/client/cli/test_auto_sign_in.py b/tests/unit/proxy/client/cli/test_auto_sign_in.py index 36bb5a31d3b..c4ae588e78e 100644 --- a/tests/unit/proxy/client/cli/test_auto_sign_in.py +++ b/tests/unit/proxy/client/cli/test_auto_sign_in.py @@ -7,11 +7,13 @@ from typing import Final import pytest import requests from click.testing import CliRunner, Result +from requests_mock import Mocker from litellm.proxy.client.cli import cli from litellm.proxy.client.cli.commands.auth import CliTokenData, get_stored_api_key, save_token from litellm.proxy.client.cli.commands.auto_sign_in import pkce_browser_sign_in from litellm.proxy.client.cli.commands.pkce_login import PkceCredential, PkceFailure, pkce_token_record +from tests.unit.litellm_core_utils.fake_secret_vault import FakeSecretVault BASE_URL: Final = "https://proxy.example.com" SIGNED_IN_KEY: Final = "sk-signed-in" @@ -77,24 +79,22 @@ def _bearers(route) -> list[str | None]: return [request.headers.get("Authorization") for request in route.request_history] -def _key_check(requests_mock, status_code: int, public: bool = False): - def respond(request, context) -> dict[str, list[str]]: - context.status_code = status_code if public or "Authorization" in request.headers else 401 - return {"data": []} - - return requests_mock.get(f"{BASE_URL}/v1/models", json=respond) +def _key_check(requests_mock, status_code: int): + return requests_mock.get(f"{BASE_URL}/key/info", status_code=status_code, json={"info": {}}) def test_a_401_without_a_login_signs_in_once_stores_the_key_and_finishes_the_command( - requests_mock, secret_vault_factory -): - route = requests_mock.post( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] +) -> None: + route: Final = requests_mock.post( f"{BASE_URL}/key/generate", [{"status_code": 401, "json": REJECTED}, {"status_code": 200, "json": GENERATED}] ) - vault = secret_vault_factory() - sign_in = FakeBrowserSignIn(_credential()) + vault: Final = secret_vault_factory() + sign_in: Final = FakeBrowserSignIn(_credential()) - result = _lite(["keys", "generate", "--models", "gpt-5.4-mini", "--key-alias", "experiment-1"], vault, sign_in) + result: Final = _lite( + ["keys", "generate", "--models", "gpt-5.4-mini", "--key-alias", "experiment-1"], vault, sign_in + ) assert result.exit_code == 0, result.output assert json.loads(result.stdout) == GENERATED @@ -114,18 +114,18 @@ def test_a_401_without_a_login_signs_in_once_stores_the_key_and_finishes_the_com ids=["rejected", "unavailable", "unreachable"], ) def test_a_stored_login_the_proxy_rejects_is_replaced_by_one_sign_in_with_only_json_on_stdout( - requests_mock, secret_vault_factory, key_check -): - vault = secret_vault_factory() + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault], key_check: dict[str, object] +) -> None: + vault: Final = secret_vault_factory() save_token(pkce_token_record(BASE_URL, _credential("sk-stale")), vault=vault) - requests_mock.get(f"{BASE_URL}/v1/models", **key_check) - revoke = requests_mock.post(f"{BASE_URL}/v1/cli/revoke", status_code=500) - route = requests_mock.post( + requests_mock.get(f"{BASE_URL}/key/info", **key_check) + revoke: Final = requests_mock.post(f"{BASE_URL}/v1/cli/revoke", status_code=500) + route: Final = requests_mock.post( f"{BASE_URL}/key/generate", [{"status_code": 401, "json": REJECTED}, {"status_code": 200, "json": GENERATED}] ) - sign_in = FakeBrowserSignIn(_credential()) + sign_in: Final = FakeBrowserSignIn(_credential()) - result = _lite(["keys", "generate"], vault, sign_in) + result: Final = _lite(["keys", "generate"], vault, sign_in) assert result.exit_code == 0, result.output assert json.loads(result.stdout) == GENERATED @@ -136,31 +136,33 @@ def test_a_stored_login_the_proxy_rejects_is_replaced_by_one_sign_in_with_only_j assert get_stored_api_key(BASE_URL, vault=vault) == SIGNED_IN_KEY -def test_a_login_for_another_proxy_is_never_replaced_by_an_automatic_sign_in(requests_mock, secret_vault_factory): - other_proxy = "https://other.example.com" - vault = secret_vault_factory() +def test_a_login_for_another_proxy_is_never_replaced_by_an_automatic_sign_in( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] +) -> None: + other_proxy: Final = "https://other.example.com" + vault: Final = secret_vault_factory() save_token({**_classic_login("sk-other"), "base_url": other_proxy}, vault=vault) - route = requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, json=REJECTED) - sign_in = FakeBrowserSignIn(_credential()) + route: Final = requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, json=REJECTED) + sign_in: Final = FakeBrowserSignIn(_credential()) - result = _lite(["keys", "generate"], vault, sign_in) + result: Final = _lite(["keys", "generate"], vault, sign_in) assert result.exit_code == 1 assert isinstance(result.exception, SystemExit) assert f"lite keeps one stored login and it is for {other_proxy}" in result.stderr - assert f"Run `lite --base-url {BASE_URL} login`" in result.stderr + assert f"Run `lite --base-url {BASE_URL} login --pkce`" in result.stderr assert sign_in.calls == [] assert _bearers(route) == [None] assert get_stored_api_key(other_proxy, vault=vault) == "sk-other" -def test_the_browser_sign_in_fails_at_once_on_a_machine_without_a_browser(requests_mock): +def test_the_browser_sign_in_fails_at_once_on_a_machine_without_a_browser(requests_mock: Mocker) -> None: def no_browser() -> webbrowser.BaseBrowser: raise webbrowser.Error("could not locate runnable browser") echoed: list[str] = [] - outcome = pkce_browser_sign_in(BASE_URL, echoed.append, find_browser=no_browser, environ={}) + outcome: Final = pkce_browser_sign_in(BASE_URL, echoed.append, find_browser=no_browser, environ={}) assert outcome == PkceFailure("no web browser is available on this machine") assert requests_mock.call_count == 0 @@ -168,13 +170,13 @@ def test_the_browser_sign_in_fails_at_once_on_a_machine_without_a_browser(reques @pytest.mark.parametrize("ci", ["true", "1", "True"]) -def test_the_browser_sign_in_never_looks_for_a_browser_in_ci(requests_mock, ci): +def test_the_browser_sign_in_never_looks_for_a_browser_in_ci(requests_mock: Mocker, ci: str) -> None: def browser_lookup() -> webbrowser.BaseBrowser: raise AssertionError("looked for a browser while CI is set") echoed: list[str] = [] - outcome = pkce_browser_sign_in(BASE_URL, echoed.append, find_browser=browser_lookup, environ={"CI": ci}) + outcome: Final = pkce_browser_sign_in(BASE_URL, echoed.append, find_browser=browser_lookup, environ={"CI": ci}) assert outcome == PkceFailure("the CI environment variable is set, so lite does not open a browser") assert requests_mock.call_count == 0 @@ -182,10 +184,10 @@ def test_the_browser_sign_in_never_looks_for_a_browser_in_ci(requests_mock, ci): @pytest.mark.parametrize("name", ["www-browser", "links", "elinks", "lynx", "w3m"]) -def test_a_text_mode_browser_counts_as_no_browser(requests_mock, name): +def test_a_text_mode_browser_counts_as_no_browser(requests_mock: Mocker, name: str) -> None: echoed: list[str] = [] - outcome = pkce_browser_sign_in( + outcome: Final = pkce_browser_sign_in( BASE_URL, echoed.append, find_browser=lambda: webbrowser.GenericBrowser(name), environ={"TERM": "xterm"} ) @@ -194,16 +196,44 @@ def test_a_text_mode_browser_counts_as_no_browser(requests_mock, name): assert echoed == [] -@pytest.mark.parametrize( - "environ", - [{"BROWSER": "w3m"}, {"CI": "false", "BROWSER": "w3m"}, {"CI": "0", "BROWSER": "w3m"}], - ids=["browser-picks-it", "ci-false", "ci-zero"], -) -def test_a_browser_the_user_picked_starts_the_sign_in_and_names_how_long_it_waits(requests_mock, environ): - discovery = requests_mock.get(f"{BASE_URL}/.well-known/litellm-cli-auth", status_code=404) +@pytest.mark.parametrize("ssh_variable", ["SSH_CONNECTION", "SSH_TTY"]) +def test_an_ssh_session_without_a_display_never_opens_a_browser_on_the_remote_screen( + requests_mock: Mocker, ssh_variable: str +) -> None: + def browser_lookup() -> webbrowser.BaseBrowser: + raise AssertionError("looked for a browser in an SSH session without a display") + echoed: list[str] = [] - outcome = pkce_browser_sign_in( + outcome: Final = pkce_browser_sign_in( + BASE_URL, echoed.append, find_browser=browser_lookup, environ={ssh_variable: "203.0.113.7 52144 10.0.0.5 22"} + ) + + assert outcome == PkceFailure( + "this is an SSH session without a display, so a browser would open where you cannot see it" + ) + assert requests_mock.call_count == 0 + assert echoed == [] + + +@pytest.mark.parametrize( + "environ", + [ + {"BROWSER": "w3m"}, + {"CI": "false", "BROWSER": "w3m"}, + {"CI": "0", "BROWSER": "w3m"}, + {"SSH_CONNECTION": "203.0.113.7 52144 10.0.0.5 22", "BROWSER": "w3m"}, + {"SSH_CONNECTION": "203.0.113.7 52144 10.0.0.5 22", "DISPLAY": "localhost:10.0", "BROWSER": "w3m"}, + ], + ids=["browser-picks-it", "ci-false", "ci-zero", "ssh-with-a-browser-forwarder", "ssh-with-x11-forwarding"], +) +def test_a_browser_the_user_picked_starts_the_sign_in_and_names_how_long_it_waits( + requests_mock: Mocker, environ: dict[str, str] +) -> None: + discovery: Final = requests_mock.get(f"{BASE_URL}/.well-known/litellm-cli-auth", status_code=404) + echoed: list[str] = [] + + outcome: Final = pkce_browser_sign_in( BASE_URL, echoed.append, find_browser=lambda: webbrowser.GenericBrowser("w3m"), environ=environ ) @@ -228,14 +258,19 @@ def test_a_browser_the_user_picked_starts_the_sign_in_and_names_how_long_it_wait ], ) def test_every_proxy_command_group_signs_in_on_a_401_and_reruns_with_the_new_key( - requests_mock, secret_vault_factory, command, method, path, body -): - route = requests_mock.register_uri( + requests_mock: Mocker, + secret_vault_factory: type[FakeSecretVault], + command: list[str], + method: str, + path: str, + body: object, +) -> None: + route: Final = requests_mock.register_uri( method, f"{BASE_URL}{path}", [{"status_code": 401, "json": REJECTED}, {"status_code": 200, "json": body}] ) - sign_in = FakeBrowserSignIn(_credential()) + sign_in: Final = FakeBrowserSignIn(_credential()) - result = _lite(command, secret_vault_factory(), sign_in) + result: Final = _lite(command, secret_vault_factory(), sign_in) assert result.exit_code == 0, result.output assert sign_in.calls == [BASE_URL] @@ -243,18 +278,18 @@ def test_every_proxy_command_group_signs_in_on_a_401_and_reruns_with_the_new_key def test_models_import_signs_in_instead_of_reporting_models_it_never_added( - requests_mock, secret_vault_factory, tmp_path: Path -): - models_file = tmp_path / "models.yaml" + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault], tmp_path: Path +) -> None: + models_file: Final = tmp_path / "models.yaml" models_file.write_text( "model_list:\n - model_name: gpt-5.4-mini\n litellm_params:\n model: openai/gpt-5.4-mini\n" ) - route = requests_mock.post( + route: Final = requests_mock.post( f"{BASE_URL}/model/new", [{"status_code": 401, "json": REJECTED}, {"status_code": 200, "json": {}}] ) - sign_in = FakeBrowserSignIn(_credential()) + sign_in: Final = FakeBrowserSignIn(_credential()) - result = _lite(["models", "import", str(models_file)], secret_vault_factory(), sign_in) + result: Final = _lite(["models", "import", str(models_file)], secret_vault_factory(), sign_in) assert result.exit_code == 0, result.output assert sign_in.calls == [BASE_URL] @@ -262,20 +297,20 @@ def test_models_import_signs_in_instead_of_reporting_models_it_never_added( def test_a_401_partway_through_models_import_stops_without_signing_in_or_adding_models_twice( - requests_mock, secret_vault_factory, tmp_path: Path -): - models_file = tmp_path / "models.yaml" + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault], tmp_path: Path +) -> None: + models_file: Final = tmp_path / "models.yaml" models_file.write_text( "model_list:\n" " - model_name: gpt-5.4-mini\n litellm_params:\n model: openai/gpt-5.4-mini\n" " - model_name: gpt-5.4\n litellm_params:\n model: openai/gpt-5.4\n" ) - route = requests_mock.post( + route: Final = requests_mock.post( f"{BASE_URL}/model/new", [{"status_code": 200, "json": {}}, {"status_code": 401, "json": REJECTED}] ) - sign_in = FakeBrowserSignIn(_credential()) + sign_in: Final = FakeBrowserSignIn(_credential()) - result = _lite(["models", "import", str(models_file)], secret_vault_factory(), sign_in) + result: Final = _lite(["models", "import", str(models_file)], secret_vault_factory(), sign_in) assert result.exit_code == 1 assert isinstance(result.exception, SystemExit) @@ -284,12 +319,14 @@ def test_a_401_partway_through_models_import_stops_without_signing_in_or_adding_ assert [request.json()["model_name"] for request in route.request_history] == ["gpt-5.4-mini", "gpt-5.4"] -def test_a_401_from_the_keys_import_source_never_signs_in_to_the_destination(requests_mock, secret_vault_factory): - source = "https://source.example.com" +def test_a_401_from_the_keys_import_source_never_signs_in_to_the_destination( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] +) -> None: + source: Final = "https://source.example.com" requests_mock.get(f"{source}/key/list", status_code=401, json=REJECTED) - sign_in = FakeBrowserSignIn(_credential()) + sign_in: Final = FakeBrowserSignIn(_credential()) - result = _lite( + result: Final = _lite( ["keys", "import", "--source-base-url", source, "--source-api-key", "sk-source"], secret_vault_factory(), sign_in, @@ -306,46 +343,55 @@ def test_a_401_from_the_keys_import_source_never_signs_in_to_the_destination(req ids=["flag", "env"], ) def test_a_rejected_explicit_key_fails_cleanly_without_opening_a_browser( - requests_mock, secret_vault_factory, monkeypatch: pytest.MonkeyPatch, global_args, env -): + requests_mock: Mocker, + secret_vault_factory: type[FakeSecretVault], + monkeypatch: pytest.MonkeyPatch, + global_args: list[str], + env: dict[str, str], +) -> None: for name, value in env.items(): monkeypatch.setenv(name, value) - route = requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, json=REJECTED) - _key_check(requests_mock, 401) - vault = secret_vault_factory() - sign_in = FakeBrowserSignIn(_credential()) + route: Final = requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, json=REJECTED) + vault: Final = secret_vault_factory() + sign_in: Final = FakeBrowserSignIn(_credential()) - result = _lite(["keys", "generate"], vault, sign_in, global_args) + result: Final = _lite(["keys", "generate"], vault, sign_in, global_args) assert result.exit_code == 1 assert isinstance(result.exception, SystemExit) - assert "rejected the API key from --api-key or LITELLM_PROXY_API_KEY" in result.stderr + assert "with HTTP 401 for the API key from --api-key or LITELLM_PROXY_API_KEY" in result.stderr + assert "has the role this command needs" in result.stderr assert sign_in.calls == [] assert _bearers(route) == ["Bearer sk-explicit"] + assert [request.path for request in requests_mock.request_history] == ["/key/generate"] assert vault.writes == [] -def test_a_failed_sign_in_names_the_reason_and_points_at_lite_login(requests_mock, secret_vault_factory): - route = requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, json=REJECTED) - vault = secret_vault_factory() - sign_in = FakeBrowserSignIn(PkceFailure("timed out waiting for the browser sign-in to finish")) +def test_a_failed_sign_in_names_the_reason_and_points_at_lite_login( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] +) -> None: + route: Final = requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, json=REJECTED) + vault: Final = secret_vault_factory() + sign_in: Final = FakeBrowserSignIn(PkceFailure("timed out waiting for the browser sign-in to finish")) - result = _lite(["keys", "generate"], vault, sign_in) + result: Final = _lite(["keys", "generate"], vault, sign_in) assert result.exit_code == 1 assert isinstance(result.exception, SystemExit) assert "timed out waiting for the browser sign-in to finish" in result.stderr - assert "Run `lite login` from a terminal, or set LITELLM_PROXY_API_KEY." in result.stderr + assert "Run `lite login --pkce` from a terminal on a machine with a browser" in result.stderr assert sign_in.calls == [BASE_URL] assert route.call_count == 1 assert get_stored_api_key(BASE_URL, vault=vault) is None -def test_a_signed_in_key_the_proxy_also_rejects_is_not_followed_by_another_sign_in(requests_mock, secret_vault_factory): - route = requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, json=REJECTED) - sign_in = FakeBrowserSignIn(_credential()) +def test_a_signed_in_key_the_proxy_also_rejects_is_not_followed_by_another_sign_in( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] +) -> None: + route: Final = requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, json=REJECTED) + sign_in: Final = FakeBrowserSignIn(_credential()) - result = _lite(["keys", "generate"], secret_vault_factory(), sign_in) + result: Final = _lite(["keys", "generate"], secret_vault_factory(), sign_in) assert result.exit_code == 1 assert isinstance(result.exception, SystemExit) @@ -363,17 +409,19 @@ def _rejected_while_a_sibling_signs_in(vault): return respond -def test_a_key_a_sibling_process_stored_meanwhile_is_used_before_any_browser(requests_mock, secret_vault_factory): - vault = secret_vault_factory() +def test_a_key_a_sibling_process_stored_meanwhile_is_used_before_any_browser( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] +) -> None: + vault: Final = secret_vault_factory() save_token(_classic_login("sk-stale"), vault=vault) _key_check(requests_mock, 401) - route = requests_mock.post( + route: Final = requests_mock.post( f"{BASE_URL}/key/generate", [{"json": _rejected_while_a_sibling_signs_in(vault)}, {"status_code": 200, "json": GENERATED}], ) - sign_in = FakeBrowserSignIn(_credential()) + sign_in: Final = FakeBrowserSignIn(_credential()) - result = _lite(["keys", "generate"], vault, sign_in) + result: Final = _lite(["keys", "generate"], vault, sign_in) assert result.exit_code == 0, result.output assert json.loads(result.stdout) == GENERATED @@ -381,11 +429,13 @@ def test_a_key_a_sibling_process_stored_meanwhile_is_used_before_any_browser(req assert _bearers(route) == ["Bearer sk-stale", "Bearer sk-sibling"] -def test_a_sibling_key_the_proxy_also_rejects_falls_through_to_one_sign_in(requests_mock, secret_vault_factory): - vault = secret_vault_factory() +def test_a_sibling_key_the_proxy_also_rejects_falls_through_to_one_sign_in( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] +) -> None: + vault: Final = secret_vault_factory() save_token(_classic_login("sk-stale"), vault=vault) _key_check(requests_mock, 401) - route = requests_mock.post( + route: Final = requests_mock.post( f"{BASE_URL}/key/generate", [ {"json": _rejected_while_a_sibling_signs_in(vault)}, @@ -393,9 +443,9 @@ def test_a_sibling_key_the_proxy_also_rejects_falls_through_to_one_sign_in(reque {"status_code": 200, "json": GENERATED}, ], ) - sign_in = FakeBrowserSignIn(_credential()) + sign_in: Final = FakeBrowserSignIn(_credential()) - result = _lite(["keys", "generate"], vault, sign_in) + result: Final = _lite(["keys", "generate"], vault, sign_in) assert result.exit_code == 0, result.output assert sign_in.calls == [BASE_URL] @@ -403,56 +453,73 @@ def test_a_sibling_key_the_proxy_also_rejects_falls_through_to_one_sign_in(reque assert get_stored_api_key(BASE_URL, vault=vault) == SIGNED_IN_KEY -@pytest.mark.parametrize( - ("explicit", "accepted"), [(False, "your stored login"), (True, "this API key")], ids=["stored", "explicit"] -) -def test_a_401_for_a_key_the_proxy_accepts_elsewhere_reports_a_missing_role_without_a_browser( - requests_mock, secret_vault_factory, explicit, accepted -): - vault = secret_vault_factory() +def test_a_stored_login_the_proxy_accepts_but_a_command_refuses_reports_a_missing_role_without_a_browser( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] +) -> None: + vault: Final = secret_vault_factory() save_token(_classic_login("sk-member"), vault=vault) - route = requests_mock.get(f"{BASE_URL}/credentials", status_code=401, json=REJECTED) - key_check = _key_check(requests_mock, 200) - sign_in = FakeBrowserSignIn(_credential()) - writes_before = list(vault.writes) + requests_mock.get(f"{BASE_URL}/v1/models", status_code=200, json={"data": []}) + route: Final = requests_mock.get(f"{BASE_URL}/credentials", status_code=401, json=REJECTED) + key_check: Final = _key_check(requests_mock, 200) + sign_in: Final = FakeBrowserSignIn(_credential()) + writes_before: Final = list(vault.writes) - result = _lite(["credentials", "list"], vault, sign_in, ["--api-key", "sk-member"] if explicit else []) + result: Final = _lite(["credentials", "list"], vault, sign_in) assert result.exit_code == 1 assert isinstance(result.exception, SystemExit) - assert f"accepts {accepted} but refused `cli credentials list` with HTTP 401" in result.stderr + assert "accepts your stored login but refused `cli credentials list` with HTTP 401" in result.stderr assert "Set LITELLM_PROXY_API_KEY to a key that has it" in result.stderr assert sign_in.calls == [] assert route.call_count == 1 - assert _bearers(key_check) == ["Bearer sk-member", None] + assert _bearers(key_check) == ["Bearer sk-member"] assert vault.writes == writes_before assert "sk-member" not in result.output -def test_a_public_models_route_never_passes_a_rejected_stored_login_off_as_a_missing_role( - requests_mock, secret_vault_factory -): - vault = secret_vault_factory() - save_token(_classic_login("sk-stale"), vault=vault) - _key_check(requests_mock, 200, public=True) - route = requests_mock.get( - f"{BASE_URL}/credentials", - [{"status_code": 401, "json": REJECTED}, {"status_code": 200, "json": {"credentials": []}}], - ) - sign_in = FakeBrowserSignIn(_credential()) +class SiblingSignsInWhileThisCommandWaits: + def __init__(self, vault) -> None: + self.vault = vault + self.held = False - result = _lite(["credentials", "list", "--format", "json"], vault, sign_in) + def __call__(self) -> "SiblingSignsInWhileThisCommandWaits": + return self + + def __enter__(self) -> None: + save_token(_classic_login("sk-sibling"), vault=self.vault) + self.held = True + + def __exit__(self, *exc_info: object) -> None: + self.held = False + + +def test_a_command_that_waits_for_another_commands_sign_in_uses_its_login_instead_of_a_second_browser( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] +) -> None: + vault: Final = secret_vault_factory() + route: Final = requests_mock.post( + f"{BASE_URL}/key/generate", [{"status_code": 401, "json": REJECTED}, {"status_code": 200, "json": GENERATED}] + ) + sign_in: Final = FakeBrowserSignIn(_credential()) + sibling: Final = SiblingSignsInWhileThisCommandWaits(vault) + + result: Final = CliRunner().invoke( + cli, + ["--base-url", BASE_URL, "keys", "generate"], + obj={"secret_vault": vault, "browser_sign_in": sign_in, "sign_in_lock": sibling}, + ) assert result.exit_code == 0, result.output - assert sign_in.calls == [BASE_URL] - assert _bearers(route) == ["Bearer sk-stale", f"Bearer {SIGNED_IN_KEY}"] - assert get_stored_api_key(BASE_URL, vault=vault) == SIGNED_IN_KEY + assert json.loads(result.stdout) == GENERATED + assert sign_in.calls == [] + assert _bearers(route) == [None, "Bearer sk-sibling"] + assert sibling.held is False -def test_help_never_signs_in(requests_mock, secret_vault_factory): - sign_in = FakeBrowserSignIn(_credential()) +def test_help_never_signs_in(requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault]) -> None: + sign_in: Final = FakeBrowserSignIn(_credential()) - result = _lite(["keys", "generate", "--help"], secret_vault_factory(), sign_in) + result: Final = _lite(["keys", "generate", "--help"], secret_vault_factory(), sign_in) assert result.exit_code == 0 assert "Generate a new API key" in result.stdout @@ -461,11 +528,13 @@ def test_help_never_signs_in(requests_mock, secret_vault_factory): @pytest.mark.parametrize("status", [403, 500]) -def test_an_error_other_than_401_never_signs_in(requests_mock, secret_vault_factory, status): - route = requests_mock.post(f"{BASE_URL}/key/generate", status_code=status, json={"error": "refused"}) - sign_in = FakeBrowserSignIn(_credential()) +def test_an_error_other_than_401_never_signs_in( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault], status: int +) -> None: + route: Final = requests_mock.post(f"{BASE_URL}/key/generate", status_code=status, json={"error": "refused"}) + sign_in: Final = FakeBrowserSignIn(_credential()) - result = _lite(["keys", "generate"], secret_vault_factory(), sign_in) + result: Final = _lite(["keys", "generate"], secret_vault_factory(), sign_in) assert result.exit_code == 1 assert f"Error: HTTP {status}" in result.stderr diff --git a/tests/unit/proxy/client/cli/test_pkce_login.py b/tests/unit/proxy/client/cli/test_pkce_login.py index f58bd0ff412..270beb36b3c 100644 --- a/tests/unit/proxy/client/cli/test_pkce_login.py +++ b/tests/unit/proxy/client/cli/test_pkce_login.py @@ -549,6 +549,22 @@ def test_run_pkce_login_stops_before_the_browser_when_registration_fails(): assert opened == [] +def test_run_pkce_login_stops_waiting_when_the_browser_launcher_reports_it_did_not_open(): + http = _FakeHttp( + { + ("GET", DISCOVERY_URL): _FakeResponse(200, CONTRACT_DOC), + ("POST", f"{BASE}/register"): _FakeResponse(201, {"client_id": "llm_dcrc_abc"}), + } + ) + launch_failure = PkceFailure("the web browser did not open") + + result = run_pkce_login( + BASE, http, open_browser=lambda _url: launch_failure, echo=lambda _: None, timeout_seconds=30 + ) + + assert result == launch_failure + + def test_pkce_token_record_keeps_every_refresh_input_next_to_the_key(): credential = PkceCredential( access_token="sk-cli-new", From d3ad6dd4a6fd05e8cb86e52b059fc086caf72cb0 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 16 Oct 2026 16:19:25 -0700 Subject: [PATCH 07/25] fix(cli): treat a 404 from /key/info as an accepted browser login --- litellm/proxy/client/cli/commands/auto_sign_in.py | 4 +++- tests/unit/proxy/client/cli/test_auto_sign_in.py | 5 +++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/litellm/proxy/client/cli/commands/auto_sign_in.py b/litellm/proxy/client/cli/commands/auto_sign_in.py index 74aef486933..62afc561db7 100644 --- a/litellm/proxy/client/cli/commands/auto_sign_in.py +++ b/litellm/proxy/client/cli/commands/auto_sign_in.py @@ -5,6 +5,7 @@ import webbrowser from collections.abc import Callable, Mapping from contextlib import AbstractContextManager from dataclasses import dataclass +from http import HTTPStatus from pathlib import Path from typing import Final, Literal, Protocol @@ -27,6 +28,7 @@ class BrowserSignIn(Protocol): _TEXT_MODE_BROWSERS: Final = frozenset({"www-browser", "links", "elinks", "lynx", "w3m"}) _SSH_SESSION_VARIABLES: Final = ("SSH_CONNECTION", "SSH_TTY") _DISPLAY_VARIABLES: Final = ("DISPLAY", "WAYLAND_DISPLAY") +_SIGNED_IN_LOGIN_WITHOUT_KEY_ROW: Final = HTTPStatus.NOT_FOUND def _runs_in_ci(environ: Mapping[str, str]) -> bool: @@ -164,7 +166,7 @@ def _proxy_accepts(base_url: str, api_key: str) -> bool: ) except requests.RequestException: return False - return response.ok + return response.ok or response.status_code == _SIGNED_IN_LOGIN_WITHOUT_KEY_ROW def _sign_in_lock() -> FileLock: diff --git a/tests/unit/proxy/client/cli/test_auto_sign_in.py b/tests/unit/proxy/client/cli/test_auto_sign_in.py index c4ae588e78e..e37aac42390 100644 --- a/tests/unit/proxy/client/cli/test_auto_sign_in.py +++ b/tests/unit/proxy/client/cli/test_auto_sign_in.py @@ -453,14 +453,15 @@ def test_a_sibling_key_the_proxy_also_rejects_falls_through_to_one_sign_in( assert get_stored_api_key(BASE_URL, vault=vault) == SIGNED_IN_KEY +@pytest.mark.parametrize("key_check_status", [200, 404], ids=["virtual_key", "browser_login_without_a_key_row"]) def test_a_stored_login_the_proxy_accepts_but_a_command_refuses_reports_a_missing_role_without_a_browser( - requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault], key_check_status: int ) -> None: vault: Final = secret_vault_factory() save_token(_classic_login("sk-member"), vault=vault) requests_mock.get(f"{BASE_URL}/v1/models", status_code=200, json={"data": []}) route: Final = requests_mock.get(f"{BASE_URL}/credentials", status_code=401, json=REJECTED) - key_check: Final = _key_check(requests_mock, 200) + key_check: Final = _key_check(requests_mock, key_check_status) sign_in: Final = FakeBrowserSignIn(_credential()) writes_before: Final = list(vault.writes) From 86d90e1be489cc16b67f25fc8456c37672e7e51d Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 16 Oct 2026 17:16:27 -0700 Subject: [PATCH 08/25] fix(cli): keep the browser off lite's pipes and name an old proxy on sign-in failure The browser launcher now runs with stdin, stdout and stderr on /dev/null, so a browser that lite starts on Linux no longer holds an agent's output pipe open after lite exits. A proxy that answers 404 for browser sign-in discovery now gets an error saying it is likely older than v1.99.0 instead of a hint at lite login --pkce, which that proxy cannot run --- .../proxy/client/cli/commands/auto_sign_in.py | 31 ++++++++++++++----- .../proxy/client/cli/commands/pkce_login.py | 5 ++- .../proxy/client/cli/test_auto_sign_in.py | 29 +++++++++++++++++ 3 files changed, 56 insertions(+), 9 deletions(-) diff --git a/litellm/proxy/client/cli/commands/auto_sign_in.py b/litellm/proxy/client/cli/commands/auto_sign_in.py index 62afc561db7..a7afe9a1429 100644 --- a/litellm/proxy/client/cli/commands/auto_sign_in.py +++ b/litellm/proxy/client/cli/commands/auto_sign_in.py @@ -43,8 +43,14 @@ def _ssh_session_without_a_display(environ: Mapping[str, str]) -> bool: _OPEN_URL_IN_BROWSER: Final = "import sys, webbrowser; sys.exit(0 if webbrowser.open(sys.argv[1]) else 1)" -def open_browser_off_stdout(url: str) -> PkceFailure | None: - launcher: Final = subprocess.run((sys.executable, "-I", "-c", _OPEN_URL_IN_BROWSER, url), stdout=2, check=False) +def open_browser_detached(url: str) -> PkceFailure | None: + launcher: Final = subprocess.run( + (sys.executable, "-I", "-c", _OPEN_URL_IN_BROWSER, url), + stdin=subprocess.DEVNULL, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + check=False, + ) return None if launcher.returncode == 0 else PkceFailure("the web browser did not open") @@ -53,7 +59,7 @@ def pkce_browser_sign_in( echo: Callable[[str], None], find_browser: Callable[[], webbrowser.BaseBrowser] = webbrowser.get, environ: Mapping[str, str] = os.environ, - open_browser: Callable[[str], object] = open_browser_off_stdout, + open_browser: Callable[[str], object] = open_browser_detached, ) -> PkceCredential | PkceFailure: if _runs_in_ci(environ): return PkceFailure("the CI environment variable is set, so lite does not open a browser") @@ -140,17 +146,26 @@ def _attempt_with_newer_stored_key( return _attempt_with_key(run, ctx, stored_key) +def _sign_in_failure(base_url: str, failure: PkceFailure) -> str: + if failure.proxy_predates_pkce: + return ( + f"The proxy at {base_url} has no browser sign-in for lite, so it is likely older than v1.99.0. " + "Upgrade it, run `lite login` if it uses SSO, or set LITELLM_PROXY_API_KEY." + ) + return ( + f"Could not sign in to {base_url} through the browser: {failure.reason}. " + "Run `lite login --pkce` from a terminal on a machine with a browser, `lite login` on one without, " + "or set LITELLM_PROXY_API_KEY." + ) + + def _sign_in(ctx: click.Context, context: _SignInContext) -> str: base_url: Final = context["base_url"] _echo_to_stderr(f"The proxy at {base_url} needs a sign-in.") sign_in: Final = context.get("browser_sign_in") or pkce_browser_sign_in credential: Final = sign_in(base_url, _echo_to_stderr) if isinstance(credential, PkceFailure): - raise click.ClickException( - f"Could not sign in to {base_url} through the browser: {credential.reason}. " - "Run `lite login --pkce` from a terminal on a machine with a browser, `lite login` on one without, " - "or set LITELLM_PROXY_API_KEY." - ) + raise click.ClickException(_sign_in_failure(base_url, credential)) stored: Final = replace_stored_token( pkce_token_record(base_url, credential), requests.Session(), context_secret_vault(ctx) ) diff --git a/litellm/proxy/client/cli/commands/pkce_login.py b/litellm/proxy/client/cli/commands/pkce_login.py index 9e4bf02cf13..087d5d91c81 100644 --- a/litellm/proxy/client/cli/commands/pkce_login.py +++ b/litellm/proxy/client/cli/commands/pkce_login.py @@ -14,6 +14,7 @@ import webbrowser from base64 import urlsafe_b64encode from collections.abc import Callable, Mapping, Sequence from dataclasses import dataclass +from http import HTTPStatus from http.server import BaseHTTPRequestHandler, HTTPServer from types import MappingProxyType from typing import TYPE_CHECKING, Final, Literal, Protocol @@ -68,6 +69,7 @@ class _TokenResponse(LiteLLMBaseModel): @dataclass(frozen=True, slots=True) class PkceFailure: reason: str + proxy_predates_pkce: bool = False @dataclass(frozen=True, slots=True) @@ -208,7 +210,8 @@ def discover_cli_auth(base_url: str, http: Http) -> CliAuthContract | PkceFailur return PkceFailure(f"could not reach {url}: {exc}") if response.status_code != 200: return PkceFailure( - f"{url} answered {response.status_code}; this proxy version does not support `lite login --pkce`" + f"{url} answered {response.status_code}; this proxy version does not support `lite login --pkce`", + proxy_predates_pkce=response.status_code == HTTPStatus.NOT_FOUND, ) try: contract: Final = CliAuthContract.model_validate(response.json()) diff --git a/tests/unit/proxy/client/cli/test_auto_sign_in.py b/tests/unit/proxy/client/cli/test_auto_sign_in.py index e37aac42390..9ae26e4e873 100644 --- a/tests/unit/proxy/client/cli/test_auto_sign_in.py +++ b/tests/unit/proxy/client/cli/test_auto_sign_in.py @@ -385,6 +385,35 @@ def test_a_failed_sign_in_names_the_reason_and_points_at_lite_login( assert get_stored_api_key(BASE_URL, vault=vault) is None +@pytest.mark.parametrize( + ("discovery_status", "advice"), + [ + (404, "so it is likely older than v1.99.0. Upgrade it, run `lite login` if it uses SSO"), + (503, "Run `lite login --pkce` from a terminal on a machine with a browser"), + ], + ids=["proxy_without_browser_sign_in", "proxy_erroring"], +) +def test_a_proxy_without_browser_sign_in_is_named_as_too_old_instead_of_pointing_at_lite_login_pkce( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault], discovery_status: int, advice: str +) -> None: + requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, json=REJECTED) + requests_mock.get(f"{BASE_URL}/.well-known/litellm-cli-auth", status_code=discovery_status) + + def sign_in_with_a_picked_browser(base_url: str, echo: Callable[[str], None]) -> PkceCredential | PkceFailure: + return pkce_browser_sign_in( + base_url, echo, find_browser=lambda: webbrowser.GenericBrowser("w3m"), environ={"BROWSER": "w3m"} + ) + + result: Final = CliRunner().invoke( + cli, + ["--base-url", BASE_URL, "keys", "generate"], + obj={"secret_vault": secret_vault_factory(), "browser_sign_in": sign_in_with_a_picked_browser}, + ) + + assert result.exit_code == 1 + assert advice in result.stderr + + def test_a_signed_in_key_the_proxy_also_rejects_is_not_followed_by_another_sign_in( requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] ) -> None: From a4055fdd64cc95db8aaf048a01e10738111a1e4b Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 16 Oct 2026 18:00:53 -0700 Subject: [PATCH 09/25] fix(cli): fail at once on a text-mode browser lite cannot show --- .../proxy/client/cli/commands/auto_sign_in.py | 4 +-- .../proxy/client/cli/test_auto_sign_in.py | 35 +++++++++++++------ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/litellm/proxy/client/cli/commands/auto_sign_in.py b/litellm/proxy/client/cli/commands/auto_sign_in.py index a7afe9a1429..45d4e3bc493 100644 --- a/litellm/proxy/client/cli/commands/auto_sign_in.py +++ b/litellm/proxy/client/cli/commands/auto_sign_in.py @@ -69,8 +69,8 @@ def pkce_browser_sign_in( browser: Final = find_browser() except webbrowser.Error: return PkceFailure("no web browser is available on this machine") - if browser.name in _TEXT_MODE_BROWSERS and "BROWSER" not in environ: - return PkceFailure("only a text-mode web browser is available on this machine") + if Path(browser.name).name in _TEXT_MODE_BROWSERS: + return PkceFailure("the web browser is a text-mode one, which lite cannot show while a command waits") echo( f"lite is opening your browser to sign in and waits up to {LOGIN_TIMEOUT_SECONDS // 60} minutes " "for you to approve it. A shell or agent that stops commands sooner should run this one with a longer timeout." diff --git a/tests/unit/proxy/client/cli/test_auto_sign_in.py b/tests/unit/proxy/client/cli/test_auto_sign_in.py index 9ae26e4e873..1c8702505ea 100644 --- a/tests/unit/proxy/client/cli/test_auto_sign_in.py +++ b/tests/unit/proxy/client/cli/test_auto_sign_in.py @@ -183,15 +183,28 @@ def test_the_browser_sign_in_never_looks_for_a_browser_in_ci(requests_mock: Mock assert echoed == [] -@pytest.mark.parametrize("name", ["www-browser", "links", "elinks", "lynx", "w3m"]) -def test_a_text_mode_browser_counts_as_no_browser(requests_mock: Mocker, name: str) -> None: +@pytest.mark.parametrize( + ("name", "environ"), + [ + ("www-browser", {"TERM": "xterm"}), + ("links", {"TERM": "xterm"}), + ("elinks", {"TERM": "xterm"}), + ("lynx", {"TERM": "xterm"}), + ("w3m", {"TERM": "xterm"}), + ("lynx", {"BROWSER": "lynx"}), + ("/usr/bin/w3m", {"BROWSER": "/usr/bin/w3m"}), + ], +) +def test_a_text_mode_browser_fails_at_once_even_when_browser_picks_it( + requests_mock: Mocker, name: str, environ: dict[str, str] +) -> None: echoed: list[str] = [] outcome: Final = pkce_browser_sign_in( - BASE_URL, echoed.append, find_browser=lambda: webbrowser.GenericBrowser(name), environ={"TERM": "xterm"} + BASE_URL, echoed.append, find_browser=lambda: webbrowser.GenericBrowser(name), environ=environ ) - assert outcome == PkceFailure("only a text-mode web browser is available on this machine") + assert outcome == PkceFailure("the web browser is a text-mode one, which lite cannot show while a command waits") assert requests_mock.call_count == 0 assert echoed == [] @@ -219,11 +232,11 @@ def test_an_ssh_session_without_a_display_never_opens_a_browser_on_the_remote_sc @pytest.mark.parametrize( "environ", [ - {"BROWSER": "w3m"}, - {"CI": "false", "BROWSER": "w3m"}, - {"CI": "0", "BROWSER": "w3m"}, - {"SSH_CONNECTION": "203.0.113.7 52144 10.0.0.5 22", "BROWSER": "w3m"}, - {"SSH_CONNECTION": "203.0.113.7 52144 10.0.0.5 22", "DISPLAY": "localhost:10.0", "BROWSER": "w3m"}, + {"BROWSER": "firefox"}, + {"CI": "false", "BROWSER": "firefox"}, + {"CI": "0", "BROWSER": "firefox"}, + {"SSH_CONNECTION": "203.0.113.7 52144 10.0.0.5 22", "BROWSER": "firefox"}, + {"SSH_CONNECTION": "203.0.113.7 52144 10.0.0.5 22", "DISPLAY": "localhost:10.0", "BROWSER": "firefox"}, ], ids=["browser-picks-it", "ci-false", "ci-zero", "ssh-with-a-browser-forwarder", "ssh-with-x11-forwarding"], ) @@ -234,7 +247,7 @@ def test_a_browser_the_user_picked_starts_the_sign_in_and_names_how_long_it_wait echoed: list[str] = [] outcome: Final = pkce_browser_sign_in( - BASE_URL, echoed.append, find_browser=lambda: webbrowser.GenericBrowser("w3m"), environ=environ + BASE_URL, echoed.append, find_browser=lambda: webbrowser.GenericBrowser("firefox"), environ=environ ) assert isinstance(outcome, PkceFailure) @@ -401,7 +414,7 @@ def test_a_proxy_without_browser_sign_in_is_named_as_too_old_instead_of_pointing def sign_in_with_a_picked_browser(base_url: str, echo: Callable[[str], None]) -> PkceCredential | PkceFailure: return pkce_browser_sign_in( - base_url, echo, find_browser=lambda: webbrowser.GenericBrowser("w3m"), environ={"BROWSER": "w3m"} + base_url, echo, find_browser=lambda: webbrowser.GenericBrowser("firefox"), environ={"BROWSER": "firefox"} ) result: Final = CliRunner().invoke( From e63339c7014e6e72bd73680a5b23c07dc3ec904a Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 16 Oct 2026 19:28:02 -0700 Subject: [PATCH 10/25] fix(cli): check for a browser before the key check and lock, cap the sign-in wait, and sign in without the lock when the login dir is read-only --- litellm/proxy/client/cli/README.md | 4 +- .../proxy/client/cli/commands/auto_sign_in.py | 146 ++++++++++---- .../proxy/client/cli/test_auto_sign_in.py | 181 ++++++++++++++---- 3 files changed, 251 insertions(+), 80 deletions(-) diff --git a/litellm/proxy/client/cli/README.md b/litellm/proxy/client/cli/README.md index 9a51891cef0..07a6aa0077c 100644 --- a/litellm/proxy/client/cli/README.md +++ b/litellm/proxy/client/cli/README.md @@ -57,11 +57,11 @@ When a command that calls the proxy (`lite keys`, `lite models`, `lite model-gro lite --base-url https://your-proxy.example.com keys generate --models gpt-5.4-mini --key-alias experiment-1 ``` -The sign-in is the browser flow of `lite login --pkce`, so you pick your team on the consent page, the credential is stored where `lite login` stores it, and its refresh token renews the key on its own from then on. Every line about the sign-in goes to stderr, so stdout carries only the command's own output, such as the JSON from `lite keys generate`. If another `lite` process is already signing in, the command waits for it to finish and then uses the login it stored, so parallel commands open one browser tab. If another `lite` process stored a newer login for the same proxy in the meantime, that key is tried first and no browser opens. `lite` keeps one stored login, so when that login is for a different proxy, the command fails and tells you to run `lite --base-url login --pkce` to switch, instead of replacing and revoking the other proxy's login on its own +The sign-in is the browser flow of `lite login --pkce`, so you pick your team on the consent page, the credential is stored where `lite login` stores it, and its refresh token renews the key on its own from then on. Every line about the sign-in goes to stderr, so stdout carries only the command's own output, such as the JSON from `lite keys generate`. If another `lite` process is already signing in, the command waits for it to finish and then uses the login it stored, so parallel commands open one browser tab. When that sign-in ends without a login the proxy accepts, or is still waiting after 6 minutes, the command fails instead of opening a second tab. If another `lite` process stored a newer login for the same proxy in the meantime, that key is tried first and no browser opens. `lite` keeps one stored login, so when that login is for a different proxy, the command fails and tells you to run `lite --base-url login --pkce` to switch, instead of replacing and revoking the other proxy's login on its own Nothing signs in ahead of time. A proxy that accepts the request without a key never opens a browser, and neither does `--help`. The proxy also answers 401 when a valid key lacks the role a command needs, such as a non-admin key running `lite credentials list`, so before it signs in, `lite` checks a rejected stored login against `/key/info`. When the proxy accepts the login there, the command fails with an error that says the login likely lacks the role, and no browser opens. A key you passed yourself is never swapped out or checked again, and a 401 for it fails with an error that names `--api-key` and `LITELLM_PROXY_API_KEY`. When the browser sign-in cannot finish (the machine has no web browser or only a text-mode one such as `w3m` or `lynx`, the shell runs over SSH with no display and `BROWSER` is unset, the browser fails to open, the proxy does not serve `/.well-known/litellm-cli-auth`, you deny the request, or nobody approves it within 5 minutes), the command fails and tells you to run `lite login --pkce` from a terminal on a machine with a browser, `lite login` on one without, or set `LITELLM_PROXY_API_KEY`. `lite` prints that 5-minute wait on stderr when the sign-in starts, so an agent whose shell tool stops commands sooner knows to run the command again with a longer timeout -When the `CI` environment variable is set to anything but empty, `0`, or `false`, `lite` never opens a browser and the command fails at once with the same message. Set `CI=1` for a script or cron job that should fail instead of waiting for a sign-in +When the `CI` environment variable is set to anything but empty, `0`, or `false`, `lite` never opens a browser and the command fails at once with the same message, without checking the stored login against the proxy first. Set `CI=1` for a script or cron job that should fail instead of waiting for a sign-in `lite chat`, `lite encryption migrate`, `lite http request`, `lite keys import`, and `lite teams assign-key` do not sign in on their own. `lite chat` and `lite teams assign-key` are interactive, `lite encryption migrate` is a one-time admin migration you run with an admin key, `lite keys import` also talks to a second proxy, and `lite http request` cannot tell a 401 from the proxy apart from a provider's 401 that the proxy passes through diff --git a/litellm/proxy/client/cli/commands/auto_sign_in.py b/litellm/proxy/client/cli/commands/auto_sign_in.py index 45d4e3bc493..51f5a86ce85 100644 --- a/litellm/proxy/client/cli/commands/auto_sign_in.py +++ b/litellm/proxy/client/cli/commands/auto_sign_in.py @@ -2,16 +2,16 @@ import os import subprocess import sys import webbrowser -from collections.abc import Callable, Mapping -from contextlib import AbstractContextManager -from dataclasses import dataclass +from collections.abc import Callable, Generator, Mapping +from contextlib import contextmanager +from dataclasses import dataclass, field from http import HTTPStatus from pathlib import Path from typing import Final, Literal, Protocol import click import requests -from filelock import FileLock +from filelock import FileLock, Timeout from typing_extensions import NotRequired, ReadOnly, TypedDict from litellm.litellm_core_utils.cli_token_utils import get_cli_token_file_path, load_cli_token @@ -22,13 +22,22 @@ from .pkce_login import LOGIN_TIMEOUT_SECONDS, PkceCredential, PkceFailure, pkce class BrowserSignIn(Protocol): + def unavailable_because(self) -> PkceFailure | None: ... + def __call__(self, base_url: str, echo: Callable[[str], None]) -> PkceCredential | PkceFailure: ... +class SignInLock(Protocol): + def acquire(self, timeout: float) -> object: ... + + def release(self) -> object: ... + + _TEXT_MODE_BROWSERS: Final = frozenset({"www-browser", "links", "elinks", "lynx", "w3m"}) _SSH_SESSION_VARIABLES: Final = ("SSH_CONNECTION", "SSH_TTY") _DISPLAY_VARIABLES: Final = ("DISPLAY", "WAYLAND_DISPLAY") _SIGNED_IN_LOGIN_WITHOUT_KEY_ROW: Final = HTTPStatus.NOT_FOUND +_WAIT_FOR_ANOTHER_SIGN_IN_SECONDS: Final = LOGIN_TIMEOUT_SECONDS + 60 def _runs_in_ci(environ: Mapping[str, str]) -> bool: @@ -54,28 +63,38 @@ def open_browser_detached(url: str) -> PkceFailure | None: return None if launcher.returncode == 0 else PkceFailure("the web browser did not open") -def pkce_browser_sign_in( - base_url: str, - echo: Callable[[str], None], - find_browser: Callable[[], webbrowser.BaseBrowser] = webbrowser.get, - environ: Mapping[str, str] = os.environ, - open_browser: Callable[[str], object] = open_browser_detached, -) -> PkceCredential | PkceFailure: - if _runs_in_ci(environ): - return PkceFailure("the CI environment variable is set, so lite does not open a browser") - if "BROWSER" not in environ and _ssh_session_without_a_display(environ): - return PkceFailure("this is an SSH session without a display, so a browser would open where you cannot see it") - try: - browser: Final = find_browser() - except webbrowser.Error: - return PkceFailure("no web browser is available on this machine") - if Path(browser.name).name in _TEXT_MODE_BROWSERS: - return PkceFailure("the web browser is a text-mode one, which lite cannot show while a command waits") - echo( - f"lite is opening your browser to sign in and waits up to {LOGIN_TIMEOUT_SECONDS // 60} minutes " - "for you to approve it. A shell or agent that stops commands sooner should run this one with a longer timeout." - ) - return run_pkce_login(base_url, requests.Session(), open_browser=open_browser, echo=echo) +def _live_environ() -> Mapping[str, str]: + return os.environ + + +@dataclass(frozen=True, slots=True) +class PkceBrowserSignIn: + find_browser: Callable[[], webbrowser.BaseBrowser] = webbrowser.get + environ: Mapping[str, str] = field(default_factory=_live_environ) + open_browser: Callable[[str], object] = open_browser_detached + + def unavailable_because(self) -> PkceFailure | None: + if _runs_in_ci(self.environ): + return PkceFailure("the CI environment variable is set, so lite does not open a browser") + if "BROWSER" not in self.environ and _ssh_session_without_a_display(self.environ): + return PkceFailure( + "this is an SSH session without a display, so a browser would open where you cannot see it" + ) + try: + browser: Final = self.find_browser() + except webbrowser.Error: + return PkceFailure("no web browser is available on this machine") + if Path(browser.name).name in _TEXT_MODE_BROWSERS: + return PkceFailure("the web browser is a text-mode one, which lite cannot show while a command waits") + return None + + def __call__(self, base_url: str, echo: Callable[[str], None]) -> PkceCredential | PkceFailure: + echo( + f"lite is opening your browser to sign in and waits up to {LOGIN_TIMEOUT_SECONDS // 60} minutes " + "for you to approve it. " + "A shell or agent that stops commands sooner should run this one with a longer timeout." + ) + return run_pkce_login(base_url, requests.Session(), open_browser=self.open_browser, echo=echo) class _SignInContext(TypedDict): @@ -83,7 +102,7 @@ class _SignInContext(TypedDict): api_key: ReadOnly[NotRequired[str | None]] api_key_from_token_file: ReadOnly[NotRequired[bool]] browser_sign_in: ReadOnly[NotRequired[BrowserSignIn]] - sign_in_lock: ReadOnly[NotRequired[Callable[[], AbstractContextManager[object]]]] + sign_in_lock: ReadOnly[NotRequired[Callable[[], SignInLock]]] class _SignInContextView(Protocol): @@ -159,10 +178,8 @@ def _sign_in_failure(base_url: str, failure: PkceFailure) -> str: ) -def _sign_in(ctx: click.Context, context: _SignInContext) -> str: - base_url: Final = context["base_url"] +def _sign_in(ctx: click.Context, base_url: str, sign_in: BrowserSignIn) -> str: _echo_to_stderr(f"The proxy at {base_url} needs a sign-in.") - sign_in: Final = context.get("browser_sign_in") or pkce_browser_sign_in credential: Final = sign_in(base_url, _echo_to_stderr) if isinstance(credential, PkceFailure): raise click.ClickException(_sign_in_failure(base_url, credential)) @@ -184,10 +201,52 @@ def _proxy_accepts(base_url: str, api_key: str) -> bool: return response.ok or response.status_code == _SIGNED_IN_LOGIN_WITHOUT_KEY_ROW -def _sign_in_lock() -> FileLock: +def _sign_in_lock() -> SignInLock: return FileLock(Path(get_cli_token_file_path()).with_name("sign-in.lock")) +def _try_to_lock(lock: SignInLock, timeout: float) -> Literal["held", "busy", "unlockable"]: + try: + lock.acquire(timeout=timeout) + except Timeout: + return "busy" + except OSError: + return "unlockable" + return "held" + + +def _lock_sign_in(lock: SignInLock) -> Literal["held", "waited", "unlockable"]: + first_try: Final = _try_to_lock(lock, timeout=0) + if first_try != "busy": + return first_try + _echo_to_stderr( + "Another lite command is waiting for a browser sign-in, so this one waits for it instead of opening a second one." + ) + match _try_to_lock(lock, timeout=_WAIT_FOR_ANOTHER_SIGN_IN_SECONDS): + case "held": + return "waited" + case "unlockable": + return "unlockable" + case "busy": + raise click.ClickException( + "Another lite command has been waiting for a browser sign-in for over " + f"{_WAIT_FOR_ANOTHER_SIGN_IN_SECONDS // 60} minutes, so this one stopped waiting. " + "Finish or stop that sign-in, then run this command again." + ) + + +@contextmanager +def _sign_in_lock_held(lock: SignInLock) -> Generator[bool]: + outcome: Final = _lock_sign_in(lock) + if outcome == "unlockable": + yield False + return + try: + yield outcome == "waited" + finally: + lock.release() + + def _login_for_another_proxy(ctx: click.Context, base_url: str) -> str | None: login: Final = load_cli_token(vault=context_secret_vault(ctx)) if login is None or not login.base_url or login.base_url == base_url.rstrip("/"): @@ -203,20 +262,29 @@ def _rerun_after_sign_in(run: Callable[[click.Context], object], ctx: click.Cont "or LITELLM_PROXY_API_KEY. Check that the key is valid and has the role this command needs, " "or unset both to sign in through your browser instead." ) + sign_in: Final = context.get("browser_sign_in") or PkceBrowserSignIn() + unavailable: Final = sign_in.unavailable_because() + if unavailable is not None: + _echo_to_stderr(f"The proxy at {base_url} needs a sign-in.") + raise click.ClickException(_sign_in_failure(base_url, unavailable)) rejected_key: Final = context.get("api_key") if rejected_key is not None and _proxy_accepts(base_url, rejected_key): raise click.ClickException( f"The proxy at {base_url} accepts your stored login but refused `{ctx.command_path}` with HTTP 401, " "so it likely lacks the role this command needs. Set LITELLM_PROXY_API_KEY to a key that has it." ) - with context.get("sign_in_lock", _sign_in_lock)(): - return _rerun_once_signed_in(run, ctx, context, rejected_key) + with _sign_in_lock_held(context.get("sign_in_lock", _sign_in_lock)()) as waited_for_another_sign_in: + return _rerun_once_signed_in(run, ctx, base_url, sign_in, rejected_key, waited_for_another_sign_in) def _rerun_once_signed_in( - run: Callable[[click.Context], object], ctx: click.Context, context: _SignInContext, rejected_key: str | None + run: Callable[[click.Context], object], + ctx: click.Context, + base_url: str, + sign_in: BrowserSignIn, + rejected_key: str | None, + waited_for_another_sign_in: bool, ) -> object: - base_url: Final = context["base_url"] retried: Final = _attempt_with_newer_stored_key(run, ctx, base_url, rejected_key) if isinstance(retried, _Completed): return retried.value @@ -227,7 +295,13 @@ def _rerun_once_signed_in( f"so lite did not replace it on its own. Run `lite --base-url {base_url} login --pkce` to switch proxies, " "or set LITELLM_PROXY_API_KEY." ) - match _attempt_with_key(run, ctx, _sign_in(ctx, context)): + if waited_for_another_sign_in: + raise click.ClickException( + "The browser sign-in another lite command started ended while this one waited, " + f"and the proxy at {base_url} still refuses `{ctx.command_path}`, so lite did not open a second one. " + "Run the command again to sign in." + ) + match _attempt_with_key(run, ctx, _sign_in(ctx, base_url, sign_in)): case _Completed(value=value): return value case _Unauthorized(): diff --git a/tests/unit/proxy/client/cli/test_auto_sign_in.py b/tests/unit/proxy/client/cli/test_auto_sign_in.py index 1c8702505ea..a67106f6cb1 100644 --- a/tests/unit/proxy/client/cli/test_auto_sign_in.py +++ b/tests/unit/proxy/client/cli/test_auto_sign_in.py @@ -7,11 +7,12 @@ from typing import Final import pytest import requests from click.testing import CliRunner, Result +from filelock import Timeout from requests_mock import Mocker from litellm.proxy.client.cli import cli from litellm.proxy.client.cli.commands.auth import CliTokenData, get_stored_api_key, save_token -from litellm.proxy.client.cli.commands.auto_sign_in import pkce_browser_sign_in +from litellm.proxy.client.cli.commands.auto_sign_in import PkceBrowserSignIn from litellm.proxy.client.cli.commands.pkce_login import PkceCredential, PkceFailure, pkce_token_record from tests.unit.litellm_core_utils.fake_secret_vault import FakeSecretVault @@ -28,6 +29,9 @@ class FakeBrowserSignIn: self.outcome = outcome self.calls: list[str] = [] + def unavailable_because(self) -> PkceFailure | None: + return None + def __call__(self, base_url: str, echo: Callable[[str], None]) -> PkceCredential | PkceFailure: self.calls.append(base_url) echo(f"Opening browser to: {base_url}/v1/cli/authorize?state=fake") @@ -160,13 +164,10 @@ def test_the_browser_sign_in_fails_at_once_on_a_machine_without_a_browser(reques def no_browser() -> webbrowser.BaseBrowser: raise webbrowser.Error("could not locate runnable browser") - echoed: list[str] = [] - - outcome: Final = pkce_browser_sign_in(BASE_URL, echoed.append, find_browser=no_browser, environ={}) + outcome: Final = PkceBrowserSignIn(find_browser=no_browser, environ={}).unavailable_because() assert outcome == PkceFailure("no web browser is available on this machine") assert requests_mock.call_count == 0 - assert echoed == [] @pytest.mark.parametrize("ci", ["true", "1", "True"]) @@ -174,13 +175,10 @@ def test_the_browser_sign_in_never_looks_for_a_browser_in_ci(requests_mock: Mock def browser_lookup() -> webbrowser.BaseBrowser: raise AssertionError("looked for a browser while CI is set") - echoed: list[str] = [] - - outcome: Final = pkce_browser_sign_in(BASE_URL, echoed.append, find_browser=browser_lookup, environ={"CI": ci}) + outcome: Final = PkceBrowserSignIn(find_browser=browser_lookup, environ={"CI": ci}).unavailable_because() assert outcome == PkceFailure("the CI environment variable is set, so lite does not open a browser") assert requests_mock.call_count == 0 - assert echoed == [] @pytest.mark.parametrize( @@ -198,15 +196,12 @@ def test_the_browser_sign_in_never_looks_for_a_browser_in_ci(requests_mock: Mock def test_a_text_mode_browser_fails_at_once_even_when_browser_picks_it( requests_mock: Mocker, name: str, environ: dict[str, str] ) -> None: - echoed: list[str] = [] - - outcome: Final = pkce_browser_sign_in( - BASE_URL, echoed.append, find_browser=lambda: webbrowser.GenericBrowser(name), environ=environ - ) + outcome: Final = PkceBrowserSignIn( + find_browser=lambda: webbrowser.GenericBrowser(name), environ=environ + ).unavailable_because() assert outcome == PkceFailure("the web browser is a text-mode one, which lite cannot show while a command waits") assert requests_mock.call_count == 0 - assert echoed == [] @pytest.mark.parametrize("ssh_variable", ["SSH_CONNECTION", "SSH_TTY"]) @@ -216,17 +211,14 @@ def test_an_ssh_session_without_a_display_never_opens_a_browser_on_the_remote_sc def browser_lookup() -> webbrowser.BaseBrowser: raise AssertionError("looked for a browser in an SSH session without a display") - echoed: list[str] = [] - - outcome: Final = pkce_browser_sign_in( - BASE_URL, echoed.append, find_browser=browser_lookup, environ={ssh_variable: "203.0.113.7 52144 10.0.0.5 22"} - ) + outcome: Final = PkceBrowserSignIn( + find_browser=browser_lookup, environ={ssh_variable: "203.0.113.7 52144 10.0.0.5 22"} + ).unavailable_because() assert outcome == PkceFailure( "this is an SSH session without a display, so a browser would open where you cannot see it" ) assert requests_mock.call_count == 0 - assert echoed == [] @pytest.mark.parametrize( @@ -245,10 +237,10 @@ def test_a_browser_the_user_picked_starts_the_sign_in_and_names_how_long_it_wait ) -> None: discovery: Final = requests_mock.get(f"{BASE_URL}/.well-known/litellm-cli-auth", status_code=404) echoed: list[str] = [] + sign_in: Final = PkceBrowserSignIn(find_browser=lambda: webbrowser.GenericBrowser("firefox"), environ=environ) - outcome: Final = pkce_browser_sign_in( - BASE_URL, echoed.append, find_browser=lambda: webbrowser.GenericBrowser("firefox"), environ=environ - ) + assert sign_in.unavailable_because() is None + outcome: Final = sign_in(BASE_URL, echoed.append) assert isinstance(outcome, PkceFailure) assert discovery.call_count == 1 @@ -412,10 +404,9 @@ def test_a_proxy_without_browser_sign_in_is_named_as_too_old_instead_of_pointing requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, json=REJECTED) requests_mock.get(f"{BASE_URL}/.well-known/litellm-cli-auth", status_code=discovery_status) - def sign_in_with_a_picked_browser(base_url: str, echo: Callable[[str], None]) -> PkceCredential | PkceFailure: - return pkce_browser_sign_in( - base_url, echo, find_browser=lambda: webbrowser.GenericBrowser("firefox"), environ={"BROWSER": "firefox"} - ) + sign_in_with_a_picked_browser: Final = PkceBrowserSignIn( + find_browser=lambda: webbrowser.GenericBrowser("firefox"), environ={"BROWSER": "firefox"} + ) result: Final = CliRunner().invoke( cli, @@ -520,20 +511,46 @@ def test_a_stored_login_the_proxy_accepts_but_a_command_refuses_reports_a_missin assert "sk-member" not in result.output -class SiblingSignsInWhileThisCommandWaits: - def __init__(self, vault) -> None: - self.vault = vault +class AnotherCommandHoldsTheSignInLock: + def __init__(self, when_it_lets_go: Callable[[], None], lets_go: bool = True) -> None: + self.when_it_lets_go = when_it_lets_go + self.lets_go = lets_go + self.attempts = 0 self.held = False - def __call__(self) -> "SiblingSignsInWhileThisCommandWaits": + def __call__(self) -> "AnotherCommandHoldsTheSignInLock": return self - def __enter__(self) -> None: - save_token(_classic_login("sk-sibling"), vault=self.vault) + def acquire(self, timeout: float) -> object: + self.attempts += 1 + if self.attempts == 1 or not self.lets_go: + raise Timeout("sign-in.lock") + self.when_it_lets_go() self.held = True + return self - def __exit__(self, *exc_info: object) -> None: + def release(self) -> object: self.held = False + return None + + +class UnwritableLoginDirectory: + def __call__(self) -> "UnwritableLoginDirectory": + return self + + def acquire(self, timeout: float) -> object: + raise PermissionError(13, "Permission denied", "/home/agent/.litellm") + + def release(self) -> object: + raise AssertionError("released a sign-in lock it never held") + + +def _lite_with_lock(vault: object, sign_in: FakeBrowserSignIn, lock: object) -> Result: + return CliRunner().invoke( + cli, + ["--base-url", BASE_URL, "keys", "generate"], + obj={"secret_vault": vault, "browser_sign_in": sign_in, "sign_in_lock": lock}, + ) def test_a_command_that_waits_for_another_commands_sign_in_uses_its_login_instead_of_a_second_browser( @@ -544,19 +561,99 @@ def test_a_command_that_waits_for_another_commands_sign_in_uses_its_login_instea f"{BASE_URL}/key/generate", [{"status_code": 401, "json": REJECTED}, {"status_code": 200, "json": GENERATED}] ) sign_in: Final = FakeBrowserSignIn(_credential()) - sibling: Final = SiblingSignsInWhileThisCommandWaits(vault) + other_command: Final = AnotherCommandHoldsTheSignInLock( + lambda: save_token(_classic_login("sk-sibling"), vault=vault) + ) + + result: Final = _lite_with_lock(vault, sign_in, other_command) + + assert result.exit_code == 0, result.output + assert json.loads(result.stdout) == GENERATED + assert "Another lite command is waiting for a browser sign-in" in result.stderr + assert sign_in.calls == [] + assert _bearers(route) == [None, "Bearer sk-sibling"] + assert other_command.held is False + + +def test_a_command_that_waited_on_a_sign_in_that_ended_without_a_login_opens_no_second_browser( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] +) -> None: + route: Final = requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, json=REJECTED) + sign_in: Final = FakeBrowserSignIn(_credential()) + other_command: Final = AnotherCommandHoldsTheSignInLock(lambda: None) + + result: Final = _lite_with_lock(secret_vault_factory(), sign_in, other_command) + + assert result.exit_code == 1 + assert isinstance(result.exception, SystemExit) + assert "so lite did not open a second one. Run the command again to sign in." in result.stderr + assert sign_in.calls == [] + assert route.call_count == 1 + assert other_command.held is False + + +def test_a_command_stops_waiting_for_a_sign_in_another_command_never_finishes( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] +) -> None: + route: Final = requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, json=REJECTED) + sign_in: Final = FakeBrowserSignIn(_credential()) + other_command: Final = AnotherCommandHoldsTheSignInLock(lambda: None, lets_go=False) + + result: Final = _lite_with_lock(secret_vault_factory(), sign_in, other_command) + + assert result.exit_code == 1 + assert isinstance(result.exception, SystemExit) + assert "Another lite command has been waiting for a browser sign-in for over 6 minutes" in result.stderr + assert sign_in.calls == [] + assert route.call_count == 1 + + +def test_a_login_directory_lite_cannot_write_still_signs_in_and_finishes_the_command( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] +) -> None: + route: Final = requests_mock.post( + f"{BASE_URL}/key/generate", [{"status_code": 401, "json": REJECTED}, {"status_code": 200, "json": GENERATED}] + ) + sign_in: Final = FakeBrowserSignIn(_credential()) + + result: Final = _lite_with_lock(secret_vault_factory(), sign_in, UnwritableLoginDirectory()) + + assert result.exit_code == 0, result.output + assert json.loads(result.stdout) == GENERATED + assert sign_in.calls == [BASE_URL] + assert _bearers(route) == [None, f"Bearer {SIGNED_IN_KEY}"] + + +@pytest.mark.parametrize( + ("environ", "reason"), + [ + ({"CI": "1"}, "the CI environment variable is set"), + ({"SSH_CONNECTION": "203.0.113.7 52144 10.0.0.5 22"}, "this is an SSH session without a display"), + ], + ids=["ci", "ssh_without_a_display"], +) +def test_a_rejected_stored_login_fails_at_once_without_a_key_check_or_lock_where_no_browser_can_open( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault], environ: dict[str, str], reason: str +) -> None: + vault: Final = secret_vault_factory() + save_token(_classic_login("sk-stale"), vault=vault) + key_check: Final = _key_check(requests_mock, 401) + route: Final = requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, json=REJECTED) + lock: Final = AnotherCommandHoldsTheSignInLock(lambda: None) result: Final = CliRunner().invoke( cli, ["--base-url", BASE_URL, "keys", "generate"], - obj={"secret_vault": vault, "browser_sign_in": sign_in, "sign_in_lock": sibling}, + obj={"secret_vault": vault, "browser_sign_in": PkceBrowserSignIn(environ=environ), "sign_in_lock": lock}, ) - assert result.exit_code == 0, result.output - assert json.loads(result.stdout) == GENERATED - assert sign_in.calls == [] - assert _bearers(route) == [None, "Bearer sk-sibling"] - assert sibling.held is False + assert result.exit_code == 1 + assert isinstance(result.exception, SystemExit) + assert reason in result.stderr + assert "Run `lite login --pkce` from a terminal on a machine with a browser" in result.stderr + assert _bearers(route) == ["Bearer sk-stale"] + assert key_check.call_count == 0 + assert lock.attempts == 0 def test_help_never_signs_in(requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault]) -> None: From dc703f5d0ae5880d4630fca0d167fd2d5d2007c2 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 16 Oct 2026 20:34:07 -0700 Subject: [PATCH 11/25] fix(cli): open only the browser lite checked, so a failing BROWSER is reported instead of a hidden console browser waiting --- .../proxy/client/cli/commands/auto_sign_in.py | 2 +- .../proxy/client/cli/test_auto_sign_in.py | 31 ++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/litellm/proxy/client/cli/commands/auto_sign_in.py b/litellm/proxy/client/cli/commands/auto_sign_in.py index 51f5a86ce85..7c313531145 100644 --- a/litellm/proxy/client/cli/commands/auto_sign_in.py +++ b/litellm/proxy/client/cli/commands/auto_sign_in.py @@ -49,7 +49,7 @@ def _ssh_session_without_a_display(environ: Mapping[str, str]) -> bool: return over_ssh and not any(environ.get(name) for name in _DISPLAY_VARIABLES) -_OPEN_URL_IN_BROWSER: Final = "import sys, webbrowser; sys.exit(0 if webbrowser.open(sys.argv[1]) else 1)" +_OPEN_URL_IN_BROWSER: Final = "import sys, webbrowser; sys.exit(0 if webbrowser.get().open(sys.argv[1]) else 1)" def open_browser_detached(url: str) -> PkceFailure | None: diff --git a/tests/unit/proxy/client/cli/test_auto_sign_in.py b/tests/unit/proxy/client/cli/test_auto_sign_in.py index a67106f6cb1..a53ba4cd496 100644 --- a/tests/unit/proxy/client/cli/test_auto_sign_in.py +++ b/tests/unit/proxy/client/cli/test_auto_sign_in.py @@ -12,7 +12,7 @@ from requests_mock import Mocker from litellm.proxy.client.cli import cli from litellm.proxy.client.cli.commands.auth import CliTokenData, get_stored_api_key, save_token -from litellm.proxy.client.cli.commands.auto_sign_in import PkceBrowserSignIn +from litellm.proxy.client.cli.commands.auto_sign_in import PkceBrowserSignIn, open_browser_detached from litellm.proxy.client.cli.commands.pkce_login import PkceCredential, PkceFailure, pkce_token_record from tests.unit.litellm_core_utils.fake_secret_vault import FakeSecretVault @@ -250,6 +250,35 @@ def test_a_browser_the_user_picked_starts_the_sign_in_and_names_how_long_it_wait ] + +def _browser_script(path: Path, ran: Path, exit_code: int) -> Path: + path.write_text(f'#!/bin/sh\n: > "{ran}"\nexit {exit_code}\n') + path.chmod(0o755) + return path + + +@pytest.mark.parametrize( + ("picked_exit_code", "expected"), + [(0, None), (1, PkceFailure("the web browser did not open"))], + ids=["picked-browser-opens", "picked-browser-fails"], +) +def test_the_browser_launcher_opens_only_the_browser_lite_picked_and_never_a_hidden_console_one( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, picked_exit_code: int, expected: PkceFailure | None +) -> None: + picked_ran: Final = tmp_path / "picked-browser-ran" + console_ran: Final = tmp_path / "console-browser-ran" + picked: Final = _browser_script(tmp_path / "picked-browser", picked_ran, picked_exit_code) + _browser_script(tmp_path / "www-browser", console_ran, 0) + monkeypatch.setenv("PATH", str(tmp_path)) + monkeypatch.setenv("TERM", "xterm") + monkeypatch.setenv("BROWSER", str(picked)) + monkeypatch.delenv("DISPLAY", raising=False) + monkeypatch.delenv("WAYLAND_DISPLAY", raising=False) + + assert open_browser_detached(f"{BASE_URL}/v1/cli/authorize?state=abc") == expected + assert picked_ran.exists() + assert not console_ran.exists() + @pytest.mark.parametrize( ("command", "method", "path", "body"), [ From 23406d9be8c941a0f3d9dc24cb9d648d28b1a5ab Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 16 Oct 2026 22:51:41 -0700 Subject: [PATCH 12/25] feat(cli): let the browser launcher end lite's sign-in wait when it cannot open --- litellm/proxy/client/cli/commands/pkce_login.py | 12 +++++++++++- tests/unit/proxy/client/cli/test_pkce_login.py | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/litellm/proxy/client/cli/commands/pkce_login.py b/litellm/proxy/client/cli/commands/pkce_login.py index c9dd542eb36..21823b26530 100644 --- a/litellm/proxy/client/cli/commands/pkce_login.py +++ b/litellm/proxy/client/cli/commands/pkce_login.py @@ -128,6 +128,7 @@ class LoopbackServer(HTTPServer): self.expected_state: Final = expected_state self.connection_timeout_seconds: Final = connection_timeout_seconds self.outcome: CallbackOutcome | None = None + self.launch_failure: PkceFailure | None = None self.timeout = 1 @property @@ -139,11 +140,18 @@ class LoopbackServer(HTTPServer): accepted[0].settimeout(self.connection_timeout_seconds) return accepted + def launch_browser(self, open_browser: Callable[[str], object], url: str) -> None: + opened: Final = open_browser(url) + if isinstance(opened, PkceFailure): + self.launch_failure = opened + def wait( self, timeout_seconds: float, clock: Callable[[], float] = time.monotonic ) -> CallbackOutcome | PkceFailure: deadline: Final = clock() + timeout_seconds while self.outcome is None: + if self.launch_failure is not None: + return self.launch_failure if clock() >= deadline: return PkceFailure("timed out waiting for the browser sign-in to finish") self.handle_request() @@ -454,7 +462,9 @@ def run_pkce_login( url: Final = authorize_url(contract, client_id, server.redirect_uri, state, challenge) echo(f"Opening browser to: {url}") echo("Approve the sign-in in your browser. Waiting...") - threading.Thread(target=open_browser, args=(url,), name="lite-login-browser", daemon=True).start() + threading.Thread( + target=server.launch_browser, args=(open_browser, url), name="lite-login-browser", daemon=True + ).start() outcome: Final = server.wait(timeout_seconds) match outcome: case PkceFailure(): diff --git a/tests/unit/proxy/client/cli/test_pkce_login.py b/tests/unit/proxy/client/cli/test_pkce_login.py index f58bd0ff412..270beb36b3c 100644 --- a/tests/unit/proxy/client/cli/test_pkce_login.py +++ b/tests/unit/proxy/client/cli/test_pkce_login.py @@ -549,6 +549,22 @@ def test_run_pkce_login_stops_before_the_browser_when_registration_fails(): assert opened == [] +def test_run_pkce_login_stops_waiting_when_the_browser_launcher_reports_it_did_not_open(): + http = _FakeHttp( + { + ("GET", DISCOVERY_URL): _FakeResponse(200, CONTRACT_DOC), + ("POST", f"{BASE}/register"): _FakeResponse(201, {"client_id": "llm_dcrc_abc"}), + } + ) + launch_failure = PkceFailure("the web browser did not open") + + result = run_pkce_login( + BASE, http, open_browser=lambda _url: launch_failure, echo=lambda _: None, timeout_seconds=30 + ) + + assert result == launch_failure + + def test_pkce_token_record_keeps_every_refresh_input_next_to_the_key(): credential = PkceCredential( access_token="sk-cli-new", From fe7080b6b80e3582517db06666e21f75d148a396 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Sat, 17 Oct 2026 04:26:35 -0700 Subject: [PATCH 13/25] test(cli): type the mocked-route helpers in the browser sign-in tests --- .../unit/proxy/client/cli/test_auto_sign_in.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tests/unit/proxy/client/cli/test_auto_sign_in.py b/tests/unit/proxy/client/cli/test_auto_sign_in.py index a53ba4cd496..2de56ef108f 100644 --- a/tests/unit/proxy/client/cli/test_auto_sign_in.py +++ b/tests/unit/proxy/client/cli/test_auto_sign_in.py @@ -2,7 +2,7 @@ import json import webbrowser from collections.abc import Callable, Sequence from pathlib import Path -from typing import Final +from typing import TYPE_CHECKING, Final, Protocol import pytest import requests @@ -16,6 +16,9 @@ from litellm.proxy.client.cli.commands.auto_sign_in import PkceBrowserSignIn, op from litellm.proxy.client.cli.commands.pkce_login import PkceCredential, PkceFailure, pkce_token_record from tests.unit.litellm_core_utils.fake_secret_vault import FakeSecretVault +if TYPE_CHECKING: + from requests_mock.request import Request + BASE_URL: Final = "https://proxy.example.com" SIGNED_IN_KEY: Final = "sk-signed-in" GENERATED: Final = {"key": "sk-generated", "key_alias": "experiment-1"} @@ -79,11 +82,19 @@ def _lite(args: Sequence[str], vault: object, sign_in: FakeBrowserSignIn, global ) -def _bearers(route) -> list[str | None]: +class _MockedRoute(Protocol): + @property + def request_history(self) -> Sequence["Request"]: ... + + @property + def call_count(self) -> int: ... + + +def _bearers(route: _MockedRoute) -> list[str | None]: return [request.headers.get("Authorization") for request in route.request_history] -def _key_check(requests_mock, status_code: int): +def _key_check(requests_mock: Mocker, status_code: int) -> _MockedRoute: return requests_mock.get(f"{BASE_URL}/key/info", status_code=status_code, json={"info": {}}) From c5ed701362a06eaebee9ce6628170f6c2da584d7 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Thu, 29 Oct 2026 01:33:06 -0700 Subject: [PATCH 14/25] fix(cli): renew an expired login once, show the proxy's 401 reason, read a models file once, try each BROWSER entry --- .../proxy/client/cli/commands/auto_sign_in.py | 98 +++++++--- litellm/proxy/client/cli/commands/models.py | 20 ++- .../proxy/client/cli/test_auto_sign_in.py | 170 +++++++++++++++++- 3 files changed, 253 insertions(+), 35 deletions(-) diff --git a/litellm/proxy/client/cli/commands/auto_sign_in.py b/litellm/proxy/client/cli/commands/auto_sign_in.py index 7c313531145..729efa835f4 100644 --- a/litellm/proxy/client/cli/commands/auto_sign_in.py +++ b/litellm/proxy/client/cli/commands/auto_sign_in.py @@ -1,8 +1,9 @@ import os import subprocess import sys +import time import webbrowser -from collections.abc import Callable, Generator, Mapping +from collections.abc import Callable, Generator, Mapping, Sequence from contextlib import contextmanager from dataclasses import dataclass, field from http import HTTPStatus @@ -15,9 +16,10 @@ from filelock import FileLock, Timeout from typing_extensions import NotRequired, ReadOnly, TypedDict from litellm.litellm_core_utils.cli_token_utils import get_cli_token_file_path, load_cli_token +from litellm.litellm_core_utils.secret_redaction import redact_string from ...exceptions import UnauthorizedError -from .auth import context_secret_vault, get_stored_api_key, replace_stored_token, storage_notice +from .auth import context_secret_vault, replace_stored_token, storage_notice from .pkce_login import LOGIN_TIMEOUT_SECONDS, PkceCredential, PkceFailure, pkce_token_record, run_pkce_login @@ -49,12 +51,15 @@ def _ssh_session_without_a_display(environ: Mapping[str, str]) -> bool: return over_ssh and not any(environ.get(name) for name in _DISPLAY_VARIABLES) -_OPEN_URL_IN_BROWSER: Final = "import sys, webbrowser; sys.exit(0 if webbrowser.get().open(sys.argv[1]) else 1)" +_OPEN_URL_IN_BROWSER: Final = ( + "import sys, webbrowser; " + "sys.exit(0 if any(webbrowser.get(name).open(sys.argv[1]) for name in sys.argv[2:] or [None]) else 1)" +) -def open_browser_detached(url: str) -> PkceFailure | None: +def open_browser_detached(url: str, browsers: Sequence[str]) -> PkceFailure | None: launcher: Final = subprocess.run( - (sys.executable, "-I", "-c", _OPEN_URL_IN_BROWSER, url), + (sys.executable, "-I", "-c", _OPEN_URL_IN_BROWSER, url, *browsers), stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, @@ -67,11 +72,40 @@ def _live_environ() -> Mapping[str, str]: return os.environ +def _listed_browsers(environ: Mapping[str, str]) -> tuple[str, ...]: + return tuple(name for name in environ.get("BROWSER", "").split(os.pathsep) if name) + + +def _locate_browser( + find_browser: Callable[[str | None], webbrowser.BaseBrowser], name: str | None +) -> webbrowser.BaseBrowser | None: + try: + return find_browser(name) + except webbrowser.Error: + return None + + +def _shows_a_window(browser: webbrowser.BaseBrowser) -> bool: + return Path(browser.name).name not in _TEXT_MODE_BROWSERS + + @dataclass(frozen=True, slots=True) class PkceBrowserSignIn: - find_browser: Callable[[], webbrowser.BaseBrowser] = webbrowser.get + find_browser: Callable[[str | None], webbrowser.BaseBrowser] = webbrowser.get environ: Mapping[str, str] = field(default_factory=_live_environ) - open_browser: Callable[[str], object] = open_browser_detached + open_browser: Callable[[str, Sequence[str]], object] = open_browser_detached + + def _browsers_to_open(self) -> tuple[str, ...] | PkceFailure: + names: Final = _listed_browsers(self.environ) or (None,) + located: Final = tuple( + (name, browser) for name in names if (browser := _locate_browser(self.find_browser, name)) is not None + ) + if not located: + return PkceFailure("no web browser is available on this machine") + windowed: Final = tuple(name for name, browser in located if _shows_a_window(browser)) + if not windowed: + return PkceFailure("the web browser is a text-mode one, which lite cannot show while a command waits") + return tuple(name for name in windowed if name is not None) def unavailable_because(self) -> PkceFailure | None: if _runs_in_ci(self.environ): @@ -80,21 +114,21 @@ class PkceBrowserSignIn: return PkceFailure( "this is an SSH session without a display, so a browser would open where you cannot see it" ) - try: - browser: Final = self.find_browser() - except webbrowser.Error: - return PkceFailure("no web browser is available on this machine") - if Path(browser.name).name in _TEXT_MODE_BROWSERS: - return PkceFailure("the web browser is a text-mode one, which lite cannot show while a command waits") - return None + browsers: Final = self._browsers_to_open() + return browsers if isinstance(browsers, PkceFailure) else None def __call__(self, base_url: str, echo: Callable[[str], None]) -> PkceCredential | PkceFailure: + browsers: Final = self._browsers_to_open() + if isinstance(browsers, PkceFailure): + return browsers echo( f"lite is opening your browser to sign in and waits up to {LOGIN_TIMEOUT_SECONDS // 60} minutes " "for you to approve it. " "A shell or agent that stops commands sooner should run this one with a longer timeout." ) - return run_pkce_login(base_url, requests.Session(), open_browser=self.open_browser, echo=echo) + return run_pkce_login( + base_url, requests.Session(), open_browser=lambda url: self.open_browser(url, browsers), echo=echo + ) class _SignInContext(TypedDict): @@ -156,15 +190,30 @@ def _echo_to_stderr(message: str) -> None: click.echo(message, err=True) +def _newer_stored_key(ctx: click.Context, base_url: str, rejected_key: str | None) -> str | None: + login: Final = load_cli_token(vault=context_secret_vault(ctx)) + if login is None or login.base_url != base_url.rstrip("/") or not login.key or login.key == rejected_key: + return None + expired: Final = login.expires_at is not None and time.time() >= login.expires_at + return None if expired else login.key + + def _attempt_with_newer_stored_key( run: Callable[[click.Context], object], ctx: click.Context, base_url: str, rejected_key: str | None ) -> _Completed | _Unauthorized | None: - stored_key: Final = get_stored_api_key(expected_base_url=base_url, vault=context_secret_vault(ctx)) - if stored_key is None or stored_key == rejected_key: + stored_key: Final = _newer_stored_key(ctx, base_url, rejected_key) + if stored_key is None: return None return _attempt_with_key(run, ctx, stored_key) +def _proxy_reason(error: UnauthorizedError) -> str: + original: Final = error.orig_exception + response: Final = original.response if isinstance(original, requests.HTTPError) else None + body: Final = response.text.strip() if response is not None else "" + return redact_string(body) if body else str(error) + + def _sign_in_failure(base_url: str, failure: PkceFailure) -> str: if failure.proxy_predates_pkce: return ( @@ -254,13 +303,15 @@ def _login_for_another_proxy(ctx: click.Context, base_url: str) -> str | None: return login.base_url -def _rerun_after_sign_in(run: Callable[[click.Context], object], ctx: click.Context, context: _SignInContext) -> object: +def _rerun_after_sign_in( + run: Callable[[click.Context], object], ctx: click.Context, context: _SignInContext, error: UnauthorizedError +) -> object: base_url: Final = context["base_url"] if not context.get("api_key_from_token_file", False): raise click.ClickException( f"The proxy at {base_url} refused `{ctx.command_path}` with HTTP 401 for the API key from --api-key " "or LITELLM_PROXY_API_KEY. Check that the key is valid and has the role this command needs, " - "or unset both to sign in through your browser instead." + f"or unset both to sign in through your browser instead. The proxy said: {_proxy_reason(error)}" ) sign_in: Final = context.get("browser_sign_in") or PkceBrowserSignIn() unavailable: Final = sign_in.unavailable_because() @@ -271,7 +322,8 @@ def _rerun_after_sign_in(run: Callable[[click.Context], object], ctx: click.Cont if rejected_key is not None and _proxy_accepts(base_url, rejected_key): raise click.ClickException( f"The proxy at {base_url} accepts your stored login but refused `{ctx.command_path}` with HTTP 401, " - "so it likely lacks the role this command needs. Set LITELLM_PROXY_API_KEY to a key that has it." + "so it likely lacks the role this command needs. Set LITELLM_PROXY_API_KEY to a key that has it. " + f"The proxy said: {_proxy_reason(error)}" ) with _sign_in_lock_held(context.get("sign_in_lock", _sign_in_lock)()) as waited_for_another_sign_in: return _rerun_once_signed_in(run, ctx, base_url, sign_in, rejected_key, waited_for_another_sign_in) @@ -304,11 +356,11 @@ def _rerun_once_signed_in( match _attempt_with_key(run, ctx, _sign_in(ctx, base_url, sign_in)): case _Completed(value=value): return value - case _Unauthorized(): + case _Unauthorized(error=error): raise click.ClickException( f"The proxy at {base_url} refused `{ctx.command_path}` with HTTP 401 again after the browser sign-in, " "so the signed-in user likely lacks the role this command needs. " - "Set LITELLM_PROXY_API_KEY to a key that has it." + f"Set LITELLM_PROXY_API_KEY to a key that has it. The proxy said: {_proxy_reason(error)}" ) @@ -322,7 +374,7 @@ class SignInOnUnauthorizedCommand(click.Command): context: Final = _sign_in_context(ctx) if context is None: raise error - return _rerun_after_sign_in(run, ctx, context) + return _rerun_after_sign_in(run, ctx, context, error) class SignInOnUnauthorizedGroup(click.Group): diff --git a/litellm/proxy/client/cli/commands/models.py b/litellm/proxy/client/cli/commands/models.py index 63128c44f38..c1afa37f07c 100644 --- a/litellm/proxy/client/cli/commands/models.py +++ b/litellm/proxy/client/cli/commands/models.py @@ -1,7 +1,7 @@ # stdlib imports import re from collections import defaultdict -from collections.abc import Callable +from collections.abc import Callable, Mapping, Sequence from dataclasses import dataclass from datetime import datetime from typing import TYPE_CHECKING, Any, Final, Literal @@ -45,7 +45,7 @@ class ModelYamlInfo: return ", ".join(self.access_groups) if self.access_groups else "" -def _get_model_info_obj_from_yaml(model: dict[str, Any]) -> ModelYamlInfo: +def _get_model_info_obj_from_yaml(model: Mapping[str, Any]) -> ModelYamlInfo: """Extract model info from a model dict and return as ModelYamlInfo dataclass.""" model_name: Final[str] = model["model_name"] model_params: Final[dict[str, Any]] = model["litellm_params"] @@ -392,6 +392,12 @@ def get_model_list_from_yaml_file(yaml_file: str) -> list[dict[str, object]]: return model_list +def _model_list_from_yaml_argument( + _ctx: click.Context, _param: click.Parameter, yaml_file: str +) -> Sequence[Mapping[str, object]]: + return get_model_list_from_yaml_file(yaml_file) + + def _get_filtered_model_list(model_list, only_models_matching_regex, only_access_groups_matching_regex): """Return a list of models that pass the filter criteria.""" model_regex: Final = re.compile(only_models_matching_regex) if only_models_matching_regex else None @@ -407,7 +413,12 @@ def _import_models_get_table_title(dry_run: bool) -> str: @models.command("import") -@click.argument("yaml_file", type=click.Path(exists=True, dir_okay=False, readable=True)) +@click.argument( + "model_list", + metavar="YAML_FILE", + type=click.Path(exists=True, dir_okay=False, readable=True), + callback=_model_list_from_yaml_argument, +) @click.option( "--dry-run", is_flag=True, @@ -426,7 +437,7 @@ def _import_models_get_table_title(dry_run: bool) -> str: @click.pass_context def import_models( ctx: click.Context, - yaml_file: str, + model_list: Sequence[Mapping[str, object]], dry_run: bool, only_models_matching_regex: str | None, only_access_groups_matching_regex: str | None, @@ -434,7 +445,6 @@ def import_models( """Import models from a YAML file and add them to the proxy.""" provider_counts: Final[dict[str, int]] = defaultdict(int) added_models: Final[list[ModelYamlInfo]] = [] - model_list: Final = get_model_list_from_yaml_file(yaml_file) filtered_model_list: Final = _get_filtered_model_list( model_list, only_models_matching_regex, only_access_groups_matching_regex ) diff --git a/tests/unit/proxy/client/cli/test_auto_sign_in.py b/tests/unit/proxy/client/cli/test_auto_sign_in.py index 2de56ef108f..f63a07fc5e4 100644 --- a/tests/unit/proxy/client/cli/test_auto_sign_in.py +++ b/tests/unit/proxy/client/cli/test_auto_sign_in.py @@ -1,6 +1,8 @@ import json +import os import webbrowser from collections.abc import Callable, Sequence +from dataclasses import replace from pathlib import Path from typing import TYPE_CHECKING, Final, Protocol @@ -23,6 +25,9 @@ BASE_URL: Final = "https://proxy.example.com" SIGNED_IN_KEY: Final = "sk-signed-in" GENERATED: Final = {"key": "sk-generated", "key_alias": "experiment-1"} REJECTED: Final = {"error": {"message": "Authentication Error, No api key passed in.", "type": "auth_error"}} +INVALID_KEY: Final = { + "error": {"message": "Authentication Error, Invalid API key provided", "type": "token_not_found_in_db"} +} SIGNED_IN_AT: Final = 1_790_000_000.0 EXPIRES_AT: Final = 4_102_444_800.0 @@ -151,6 +156,28 @@ def test_a_stored_login_the_proxy_rejects_is_replaced_by_one_sign_in_with_only_j assert get_stored_api_key(BASE_URL, vault=vault) == SIGNED_IN_KEY +def test_an_expired_login_whose_renewal_the_proxy_refuses_is_renewed_once_and_then_signs_in( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] +) -> None: + vault: Final = secret_vault_factory() + save_token(pkce_token_record(BASE_URL, replace(_credential("sk-expired"), expires_at=SIGNED_IN_AT)), vault=vault) + renewal: Final = requests_mock.post(f"{BASE_URL}/v1/cli/token", status_code=400, json={"error": "invalid_grant"}) + requests_mock.post(f"{BASE_URL}/v1/cli/revoke", status_code=200) + route: Final = requests_mock.post( + f"{BASE_URL}/key/generate", [{"status_code": 401, "json": REJECTED}, {"status_code": 200, "json": GENERATED}] + ) + sign_in: Final = FakeBrowserSignIn(_credential()) + + result: Final = _lite(["keys", "generate"], vault, sign_in) + + assert result.exit_code == 0, result.output + assert json.loads(result.stdout) == GENERATED + assert renewal.call_count == 1 + assert result.stderr.count("Could not renew the key") == 1 + assert sign_in.calls == [BASE_URL] + assert _bearers(route) == [None, f"Bearer {SIGNED_IN_KEY}"] + + def test_a_login_for_another_proxy_is_never_replaced_by_an_automatic_sign_in( requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] ) -> None: @@ -172,7 +199,7 @@ def test_a_login_for_another_proxy_is_never_replaced_by_an_automatic_sign_in( def test_the_browser_sign_in_fails_at_once_on_a_machine_without_a_browser(requests_mock: Mocker) -> None: - def no_browser() -> webbrowser.BaseBrowser: + def no_browser(_name: str | None) -> webbrowser.BaseBrowser: raise webbrowser.Error("could not locate runnable browser") outcome: Final = PkceBrowserSignIn(find_browser=no_browser, environ={}).unavailable_because() @@ -183,7 +210,7 @@ def test_the_browser_sign_in_fails_at_once_on_a_machine_without_a_browser(reques @pytest.mark.parametrize("ci", ["true", "1", "True"]) def test_the_browser_sign_in_never_looks_for_a_browser_in_ci(requests_mock: Mocker, ci: str) -> None: - def browser_lookup() -> webbrowser.BaseBrowser: + def browser_lookup(_name: str | None) -> webbrowser.BaseBrowser: raise AssertionError("looked for a browser while CI is set") outcome: Final = PkceBrowserSignIn(find_browser=browser_lookup, environ={"CI": ci}).unavailable_because() @@ -202,13 +229,14 @@ def test_the_browser_sign_in_never_looks_for_a_browser_in_ci(requests_mock: Mock ("w3m", {"TERM": "xterm"}), ("lynx", {"BROWSER": "lynx"}), ("/usr/bin/w3m", {"BROWSER": "/usr/bin/w3m"}), + ("lynx", {"BROWSER": f"lynx{os.pathsep}/usr/bin/w3m"}), ], ) def test_a_text_mode_browser_fails_at_once_even_when_browser_picks_it( requests_mock: Mocker, name: str, environ: dict[str, str] ) -> None: outcome: Final = PkceBrowserSignIn( - find_browser=lambda: webbrowser.GenericBrowser(name), environ=environ + find_browser=lambda listed: webbrowser.GenericBrowser(listed or name), environ=environ ).unavailable_because() assert outcome == PkceFailure("the web browser is a text-mode one, which lite cannot show while a command waits") @@ -219,7 +247,7 @@ def test_a_text_mode_browser_fails_at_once_even_when_browser_picks_it( def test_an_ssh_session_without_a_display_never_opens_a_browser_on_the_remote_screen( requests_mock: Mocker, ssh_variable: str ) -> None: - def browser_lookup() -> webbrowser.BaseBrowser: + def browser_lookup(_name: str | None) -> webbrowser.BaseBrowser: raise AssertionError("looked for a browser in an SSH session without a display") outcome: Final = PkceBrowserSignIn( @@ -248,7 +276,9 @@ def test_a_browser_the_user_picked_starts_the_sign_in_and_names_how_long_it_wait ) -> None: discovery: Final = requests_mock.get(f"{BASE_URL}/.well-known/litellm-cli-auth", status_code=404) echoed: list[str] = [] - sign_in: Final = PkceBrowserSignIn(find_browser=lambda: webbrowser.GenericBrowser("firefox"), environ=environ) + sign_in: Final = PkceBrowserSignIn( + find_browser=lambda _name: webbrowser.GenericBrowser("firefox"), environ=environ + ) assert sign_in.unavailable_because() is None outcome: Final = sign_in(BASE_URL, echoed.append) @@ -262,6 +292,46 @@ def test_a_browser_the_user_picked_starts_the_sign_in_and_names_how_long_it_wait +def test_a_browser_list_hands_the_launcher_every_listed_browser_with_a_window_in_order( + requests_mock: Mocker, +) -> None: + requests_mock.get(f"{BASE_URL}/.well-known/litellm-cli-auth", json=_cli_auth_contract()) + requests_mock.post(f"{BASE_URL}/register", status_code=201, json={"client_id": "cli-client"}) + launched: list[tuple[str, Sequence[str]]] = [] + + def launch(url: str, browsers: Sequence[str]) -> PkceFailure: + launched.append((url, browsers)) + return PkceFailure("the web browser did not open") + + sign_in: Final = PkceBrowserSignIn( + find_browser=lambda name: webbrowser.GenericBrowser(name or "xdg-open"), + environ={"BROWSER": os.pathsep.join(("lynx", "firefox", "/usr/bin/w3m", "chromium"))}, + open_browser=launch, + ) + + assert sign_in.unavailable_because() is None + assert sign_in(BASE_URL, lambda _line: None) == PkceFailure("the web browser did not open") + assert [browsers for _url, browsers in launched] == [("firefox", "chromium")] + assert launched[0][0].startswith(f"{BASE_URL}/authorize?") + + +def _cli_auth_contract() -> dict[str, object]: + return { + "contract_version": 1, + "issuer": BASE_URL, + "authorization_endpoint": f"{BASE_URL}/authorize", + "token_endpoint": f"{BASE_URL}/token", + "registration_endpoint": f"{BASE_URL}/register", + "revocation_endpoint": f"{BASE_URL}/revoke", + "resource": BASE_URL, + "response_types_supported": ["code"], + "grant_types_supported": ["authorization_code", "refresh_token"], + "code_challenge_methods_supported": ["S256"], + "token_endpoint_auth_methods_supported": ["none"], + "revocation_endpoint_auth_methods_supported": ["none"], + } + + def _browser_script(path: Path, ran: Path, exit_code: int) -> Path: path.write_text(f'#!/bin/sh\n: > "{ran}"\nexit {exit_code}\n') path.chmod(0o755) @@ -286,10 +356,36 @@ def test_the_browser_launcher_opens_only_the_browser_lite_picked_and_never_a_hid monkeypatch.delenv("DISPLAY", raising=False) monkeypatch.delenv("WAYLAND_DISPLAY", raising=False) - assert open_browser_detached(f"{BASE_URL}/v1/cli/authorize?state=abc") == expected + assert open_browser_detached(f"{BASE_URL}/v1/cli/authorize?state=abc", ()) == expected assert picked_ran.exists() assert not console_ran.exists() + +@pytest.mark.parametrize( + ("second_exit_code", "expected"), + [(0, None), (1, PkceFailure("the web browser did not open"))], + ids=["second-browser-opens", "every-browser-fails"], +) +def test_the_browser_launcher_tries_each_listed_browser_in_order_and_never_a_console_one( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, second_exit_code: int, expected: PkceFailure | None +) -> None: + first_ran: Final = tmp_path / "first-browser-ran" + second_ran: Final = tmp_path / "second-browser-ran" + console_ran: Final = tmp_path / "console-browser-ran" + first: Final = str(_browser_script(tmp_path / "first-browser", first_ran, 1)) + second: Final = str(_browser_script(tmp_path / "second-browser", second_ran, second_exit_code)) + _browser_script(tmp_path / "www-browser", console_ran, 0) + monkeypatch.setenv("PATH", str(tmp_path)) + monkeypatch.setenv("TERM", "xterm") + monkeypatch.setenv("BROWSER", os.pathsep.join((first, second))) + monkeypatch.delenv("DISPLAY", raising=False) + monkeypatch.delenv("WAYLAND_DISPLAY", raising=False) + + assert open_browser_detached(f"{BASE_URL}/v1/cli/authorize?state=abc", (first, second)) == expected + assert first_ran.exists() + assert second_ran.exists() + assert not console_ran.exists() + @pytest.mark.parametrize( ("command", "method", "path", "body"), [ @@ -341,6 +437,37 @@ def test_models_import_signs_in_instead_of_reporting_models_it_never_added( assert _bearers(route) == [None, f"Bearer {SIGNED_IN_KEY}"] +def _rejected_once_it_is_read(models_file: Path): + def respond(request, context): + models_file.unlink() + context.status_code = 401 + return REJECTED + + return respond + + +def test_models_import_reads_its_file_once_so_a_piped_file_still_imports_after_the_sign_in( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault], tmp_path: Path +) -> None: + models_file: Final = tmp_path / "models.yaml" + models_file.write_text( + "model_list:\n - model_name: gpt-5.4-mini\n litellm_params:\n model: openai/gpt-5.4-mini\n" + ) + route: Final = requests_mock.post( + f"{BASE_URL}/model/new", + [{"json": _rejected_once_it_is_read(models_file)}, {"status_code": 200, "json": {}}], + ) + sign_in: Final = FakeBrowserSignIn(_credential()) + + result: Final = _lite(["models", "import", str(models_file)], secret_vault_factory(), sign_in) + + assert result.exit_code == 0, result.output + assert sign_in.calls == [BASE_URL] + assert [request.json()["model_name"] for request in route.request_history] == ["gpt-5.4-mini", "gpt-5.4-mini"] + assert _bearers(route) == [None, f"Bearer {SIGNED_IN_KEY}"] + assert "gpt-5.4-mini" in result.stdout + + def test_a_401_partway_through_models_import_stops_without_signing_in_or_adding_models_twice( requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault], tmp_path: Path ) -> None: @@ -412,6 +539,33 @@ def test_a_rejected_explicit_key_fails_cleanly_without_opening_a_browser( assert vault.writes == [] +@pytest.mark.parametrize( + ("command", "method", "path"), + [(["users", "list"], "GET", "/user/list"), (["keys", "generate"], "POST", "/key/generate")], + ids=["users", "keys"], +) +def test_a_rejected_explicit_key_names_the_reason_the_proxy_gave_without_echoing_the_key( + requests_mock: Mocker, + secret_vault_factory: type[FakeSecretVault], + command: list[str], + method: str, + path: str, +) -> None: + passed_key: Final = "sk-" + "x" * 48 + echoed_key: Final = {"error": {**INVALID_KEY["error"], "param": f"Received API Key = {passed_key}"}} + requests_mock.request(method, f"{BASE_URL}{path}", status_code=401, json=echoed_key) + sign_in: Final = FakeBrowserSignIn(_credential()) + + result: Final = _lite(command, secret_vault_factory(), sign_in, ["--api-key", passed_key]) + + assert result.exit_code == 1 + assert "for the API key from --api-key or LITELLM_PROXY_API_KEY" in result.stderr + assert "Authentication Error, Invalid API key provided" in result.stderr + assert "token_not_found_in_db" in result.stderr + assert passed_key not in result.output + assert sign_in.calls == [] + + def test_a_failed_sign_in_names_the_reason_and_points_at_lite_login( requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] ) -> None: @@ -445,7 +599,7 @@ def test_a_proxy_without_browser_sign_in_is_named_as_too_old_instead_of_pointing requests_mock.get(f"{BASE_URL}/.well-known/litellm-cli-auth", status_code=discovery_status) sign_in_with_a_picked_browser: Final = PkceBrowserSignIn( - find_browser=lambda: webbrowser.GenericBrowser("firefox"), environ={"BROWSER": "firefox"} + find_browser=lambda _name: webbrowser.GenericBrowser("firefox"), environ={"BROWSER": "firefox"} ) result: Final = CliRunner().invoke( @@ -469,6 +623,7 @@ def test_a_signed_in_key_the_proxy_also_rejects_is_not_followed_by_another_sign_ assert result.exit_code == 1 assert isinstance(result.exception, SystemExit) assert "refused `cli keys generate` with HTTP 401 again after the browser sign-in" in result.stderr + assert REJECTED["error"]["message"] in result.stderr assert sign_in.calls == [BASE_URL] assert _bearers(route) == [None, f"Bearer {SIGNED_IN_KEY}"] @@ -544,6 +699,7 @@ def test_a_stored_login_the_proxy_accepts_but_a_command_refuses_reports_a_missin assert isinstance(result.exception, SystemExit) assert "accepts your stored login but refused `cli credentials list` with HTTP 401" in result.stderr assert "Set LITELLM_PROXY_API_KEY to a key that has it" in result.stderr + assert REJECTED["error"]["message"] in result.stderr assert sign_in.calls == [] assert route.call_count == 1 assert _bearers(key_check) == ["Bearer sk-member"] From 0b6e0960469dbeea79de86ade30605498fe10b8c Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Thu, 29 Oct 2026 02:50:39 -0700 Subject: [PATCH 15/25] fix(cli): keep the proxy's response on a teams 401 so lite shows the proxy's reason --- litellm/proxy/client/teams.py | 12 +++++++++--- tests/unit/proxy/client/cli/test_auto_sign_in.py | 8 ++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/litellm/proxy/client/teams.py b/litellm/proxy/client/teams.py index dc6fa1e228a..38d494f70e4 100644 --- a/litellm/proxy/client/teams.py +++ b/litellm/proxy/client/teams.py @@ -65,7 +65,9 @@ class TeamsManagementClient: response: Final = requests.get(url, headers=self._get_headers(), params=params, timeout=self._timeout) if response.status_code == 401: - raise UnauthorizedError("Authentication failed. Check your API key.") + raise UnauthorizedError( + requests.exceptions.HTTPError("Authentication failed. Check your API key.", response=response) + ) response.raise_for_status() return response.json() @@ -122,7 +124,9 @@ class TeamsManagementClient: response: Final = requests.get(url, headers=self._get_headers(), params=params, timeout=self._timeout) if response.status_code == 401: - raise UnauthorizedError("Authentication failed. Check your API key.") + raise UnauthorizedError( + requests.exceptions.HTTPError("Authentication failed. Check your API key.", response=response) + ) response.raise_for_status() return response.json() @@ -143,7 +147,9 @@ class TeamsManagementClient: response: Final = requests.get(url, headers=self._get_headers(), timeout=self._timeout) if response.status_code == 401: - raise UnauthorizedError("Authentication failed. Check your API key.") + raise UnauthorizedError( + requests.exceptions.HTTPError("Authentication failed. Check your API key.", response=response) + ) response.raise_for_status() return response.json() diff --git a/tests/unit/proxy/client/cli/test_auto_sign_in.py b/tests/unit/proxy/client/cli/test_auto_sign_in.py index f63a07fc5e4..e01ac6eb762 100644 --- a/tests/unit/proxy/client/cli/test_auto_sign_in.py +++ b/tests/unit/proxy/client/cli/test_auto_sign_in.py @@ -541,8 +541,12 @@ def test_a_rejected_explicit_key_fails_cleanly_without_opening_a_browser( @pytest.mark.parametrize( ("command", "method", "path"), - [(["users", "list"], "GET", "/user/list"), (["keys", "generate"], "POST", "/key/generate")], - ids=["users", "keys"], + [ + (["users", "list"], "GET", "/user/list"), + (["keys", "generate"], "POST", "/key/generate"), + (["teams", "list"], "GET", "/team/list"), + ], + ids=["users", "keys", "teams"], ) def test_a_rejected_explicit_key_names_the_reason_the_proxy_gave_without_echoing_the_key( requests_mock: Mocker, From ef91316f1a3efe26da8e9c1f2c46d6f0323caf1f Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Thu, 29 Oct 2026 04:09:13 -0700 Subject: [PATCH 16/25] fix(cli): keep a login for another proxy that was stored while the browser sign-in waited --- .../proxy/client/cli/commands/auto_sign_in.py | 7 +++ .../proxy/client/cli/test_auto_sign_in.py | 57 +++++++++++++++++-- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/litellm/proxy/client/cli/commands/auto_sign_in.py b/litellm/proxy/client/cli/commands/auto_sign_in.py index 729efa835f4..18b9a64bc15 100644 --- a/litellm/proxy/client/cli/commands/auto_sign_in.py +++ b/litellm/proxy/client/cli/commands/auto_sign_in.py @@ -232,6 +232,13 @@ def _sign_in(ctx: click.Context, base_url: str, sign_in: BrowserSignIn) -> str: credential: Final = sign_in(base_url, _echo_to_stderr) if isinstance(credential, PkceFailure): raise click.ClickException(_sign_in_failure(base_url, credential)) + other_proxy: Final = _login_for_another_proxy(ctx, base_url) + if other_proxy is not None: + _echo_to_stderr( + f"A login for {other_proxy} was stored while this sign-in waited, so lite kept it " + f"and used this sign-in for `{ctx.command_path}` only." + ) + return credential.access_token stored: Final = replace_stored_token( pkce_token_record(base_url, credential), requests.Session(), context_secret_vault(ctx) ) diff --git a/tests/unit/proxy/client/cli/test_auto_sign_in.py b/tests/unit/proxy/client/cli/test_auto_sign_in.py index e01ac6eb762..f8c8a3855ce 100644 --- a/tests/unit/proxy/client/cli/test_auto_sign_in.py +++ b/tests/unit/proxy/client/cli/test_auto_sign_in.py @@ -32,9 +32,16 @@ SIGNED_IN_AT: Final = 1_790_000_000.0 EXPIRES_AT: Final = 4_102_444_800.0 +def _nothing_else_happens() -> None: + return None + + class FakeBrowserSignIn: - def __init__(self, outcome: PkceCredential | PkceFailure) -> None: + def __init__( + self, outcome: PkceCredential | PkceFailure, meanwhile: Callable[[], None] = _nothing_else_happens + ) -> None: self.outcome = outcome + self.meanwhile = meanwhile self.calls: list[str] = [] def unavailable_because(self) -> PkceFailure | None: @@ -43,6 +50,7 @@ class FakeBrowserSignIn: def __call__(self, base_url: str, echo: Callable[[str], None]) -> PkceCredential | PkceFailure: self.calls.append(base_url) echo(f"Opening browser to: {base_url}/v1/cli/authorize?state=fake") + self.meanwhile() return self.outcome @@ -87,6 +95,13 @@ def _lite(args: Sequence[str], vault: object, sign_in: FakeBrowserSignIn, global ) +class _ResponseContext(Protocol): + status_code: int + + +_Respond = Callable[["Request", _ResponseContext], dict[str, dict[str, str]]] + + class _MockedRoute(Protocol): @property def request_history(self) -> Sequence["Request"]: ... @@ -198,6 +213,38 @@ def test_a_login_for_another_proxy_is_never_replaced_by_an_automatic_sign_in( assert get_stored_api_key(other_proxy, vault=vault) == "sk-other" +def test_a_login_for_another_proxy_stored_during_the_browser_wait_is_kept_and_not_revoked( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] +) -> None: + other_proxy: Final = "https://other.example.com" + other_login: Final = replace( + _credential("sk-other"), + token_endpoint=f"{other_proxy}/v1/cli/token", + revocation_endpoint=f"{other_proxy}/v1/cli/revoke", + resource=other_proxy, + ) + other_revoke: Final = requests_mock.post(f"{other_proxy}/v1/cli/revoke", status_code=200) + vault: Final = secret_vault_factory() + route: Final = requests_mock.post( + f"{BASE_URL}/key/generate", [{"status_code": 401, "json": REJECTED}, {"status_code": 200, "json": GENERATED}] + ) + + def explicit_login_for_another_proxy() -> None: + save_token(pkce_token_record(other_proxy, other_login), vault=vault) + + sign_in: Final = FakeBrowserSignIn(_credential(), meanwhile=explicit_login_for_another_proxy) + + result: Final = _lite(["keys", "generate"], vault, sign_in) + + assert result.exit_code == 0, result.output + assert json.loads(result.stdout) == GENERATED + assert _bearers(route) == [None, f"Bearer {SIGNED_IN_KEY}"] + assert get_stored_api_key(other_proxy, vault=vault) == "sk-other" + assert get_stored_api_key(BASE_URL, vault=vault) is None + assert other_revoke.call_count == 0 + assert f"A login for {other_proxy} was stored while this sign-in waited" in result.stderr + + def test_the_browser_sign_in_fails_at_once_on_a_machine_without_a_browser(requests_mock: Mocker) -> None: def no_browser(_name: str | None) -> webbrowser.BaseBrowser: raise webbrowser.Error("could not locate runnable browser") @@ -437,8 +484,8 @@ def test_models_import_signs_in_instead_of_reporting_models_it_never_added( assert _bearers(route) == [None, f"Bearer {SIGNED_IN_KEY}"] -def _rejected_once_it_is_read(models_file: Path): - def respond(request, context): +def _rejected_once_it_is_read(models_file: Path) -> _Respond: + def respond(_request: "Request", context: _ResponseContext) -> dict[str, dict[str, str]]: models_file.unlink() context.status_code = 401 return REJECTED @@ -632,8 +679,8 @@ def test_a_signed_in_key_the_proxy_also_rejects_is_not_followed_by_another_sign_ assert _bearers(route) == [None, f"Bearer {SIGNED_IN_KEY}"] -def _rejected_while_a_sibling_signs_in(vault): - def respond(request, context): +def _rejected_while_a_sibling_signs_in(vault: FakeSecretVault) -> _Respond: + def respond(_request: "Request", context: _ResponseContext) -> dict[str, dict[str, str]]: save_token(_classic_login("sk-sibling"), vault=vault) context.status_code = 401 return REJECTED From 8613c1a5d36f3716df527b9b9e37bd7d3b4f63f9 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Thu, 29 Oct 2026 21:39:00 -0700 Subject: [PATCH 17/25] fix(cli): report a missing role before the browser checks, cut long 401 bodies, and sign in on lite debug claude A stored login the proxy accepts but a command refuses now gets the role error with the proxy's reason over SSH and in CI too, the 401 text lite prints is cut at 500 characters, lite debug claude opens the browser sign-in on a 401 like the other proxy commands, and lite whoami names lite login for machines without a browser --- litellm/proxy/client/cli/commands/auth.py | 2 +- .../proxy/client/cli/commands/auto_sign_in.py | 14 ++--- litellm/proxy/client/cli/commands/debug.py | 16 +++++- .../proxy/client/cli/test_auth_commands.py | 1 + .../proxy/client/cli/test_auto_sign_in.py | 53 ++++++++++++++++++- 5 files changed, 75 insertions(+), 11 deletions(-) diff --git a/litellm/proxy/client/cli/commands/auth.py b/litellm/proxy/client/cli/commands/auth.py index 487322b47a2..9abe580cd8d 100644 --- a/litellm/proxy/client/cli/commands/auth.py +++ b/litellm/proxy/client/cli/commands/auth.py @@ -1097,7 +1097,7 @@ def whoami(ctx: click.Context): click.echo( "Not authenticated. Run the command you need, such as 'lite keys generate', and lite opens your browser " f"to sign in and waits up to {LOGIN_TIMEOUT_SECONDS // 60} minutes for you to approve it. " - "To sign in first from a terminal, run 'lite login --pkce'." + "To sign in first from a terminal, run 'lite login --pkce', or 'lite login' where no browser can open." ) return diff --git a/litellm/proxy/client/cli/commands/auto_sign_in.py b/litellm/proxy/client/cli/commands/auto_sign_in.py index 18b9a64bc15..10f5955bbac 100644 --- a/litellm/proxy/client/cli/commands/auto_sign_in.py +++ b/litellm/proxy/client/cli/commands/auto_sign_in.py @@ -40,6 +40,7 @@ _SSH_SESSION_VARIABLES: Final = ("SSH_CONNECTION", "SSH_TTY") _DISPLAY_VARIABLES: Final = ("DISPLAY", "WAYLAND_DISPLAY") _SIGNED_IN_LOGIN_WITHOUT_KEY_ROW: Final = HTTPStatus.NOT_FOUND _WAIT_FOR_ANOTHER_SIGN_IN_SECONDS: Final = LOGIN_TIMEOUT_SECONDS + 60 +_PROXY_REASON_CHARS: Final = 500 def _runs_in_ci(environ: Mapping[str, str]) -> bool: @@ -211,7 +212,8 @@ def _proxy_reason(error: UnauthorizedError) -> str: original: Final = error.orig_exception response: Final = original.response if isinstance(original, requests.HTTPError) else None body: Final = response.text.strip() if response is not None else "" - return redact_string(body) if body else str(error) + reason: Final = redact_string(body) if body else str(error) + return reason if len(reason) <= _PROXY_REASON_CHARS else f"{reason[:_PROXY_REASON_CHARS]}..." def _sign_in_failure(base_url: str, failure: PkceFailure) -> str: @@ -320,11 +322,6 @@ def _rerun_after_sign_in( "or LITELLM_PROXY_API_KEY. Check that the key is valid and has the role this command needs, " f"or unset both to sign in through your browser instead. The proxy said: {_proxy_reason(error)}" ) - sign_in: Final = context.get("browser_sign_in") or PkceBrowserSignIn() - unavailable: Final = sign_in.unavailable_because() - if unavailable is not None: - _echo_to_stderr(f"The proxy at {base_url} needs a sign-in.") - raise click.ClickException(_sign_in_failure(base_url, unavailable)) rejected_key: Final = context.get("api_key") if rejected_key is not None and _proxy_accepts(base_url, rejected_key): raise click.ClickException( @@ -332,6 +329,11 @@ def _rerun_after_sign_in( "so it likely lacks the role this command needs. Set LITELLM_PROXY_API_KEY to a key that has it. " f"The proxy said: {_proxy_reason(error)}" ) + sign_in: Final = context.get("browser_sign_in") or PkceBrowserSignIn() + unavailable: Final = sign_in.unavailable_because() + if unavailable is not None: + _echo_to_stderr(f"The proxy at {base_url} needs a sign-in.") + raise click.ClickException(_sign_in_failure(base_url, unavailable)) with _sign_in_lock_held(context.get("sign_in_lock", _sign_in_lock)()) as waited_for_another_sign_in: return _rerun_once_signed_in(run, ctx, base_url, sign_in, rejected_key, waited_for_another_sign_in) diff --git a/litellm/proxy/client/cli/commands/debug.py b/litellm/proxy/client/cli/commands/debug.py index 89b93e2bbb8..f2871e68d7c 100644 --- a/litellm/proxy/client/cli/commands/debug.py +++ b/litellm/proxy/client/cli/commands/debug.py @@ -12,6 +12,7 @@ import re from collections.abc import Mapping, Sequence from dataclasses import dataclass from datetime import datetime, timezone +from http import HTTPStatus from pathlib import Path from types import MappingProxyType from typing import Final @@ -23,8 +24,10 @@ from pydantic import ConfigDict, Field, JsonValue, TypeAdapter, ValidationError, from litellm.types.llms.base_model import LiteLLMBaseModel +from ...exceptions import UnauthorizedError from ...http_client import HTTPClient from ._cli_context import cli_context_values +from .auto_sign_in import SignInOnUnauthorizedGroup CLAUDE_DIR: Final = Path.home() / ".claude" REPORT_DIR: Final = Path.home() / ".litellm" / "debug" @@ -45,6 +48,7 @@ report was saved to so I can hand it off. If nothing failed, say so. @dataclass(frozen=True, slots=True) class DebugFailure: message: str + unauthorized: requests.HTTPError | None = None class ErrorInformation(LiteLLMBaseModel): @@ -141,7 +145,13 @@ def detect_claude_session_id(env: Mapping[str, str], claude_dir: Path) -> str | def _transport_failure(uri: str, error: requests.exceptions.RequestException) -> DebugFailure: body: Final = error.response.text[:_TRANSPORT_BODY_CHARS] if error.response is not None else "" detail: Final = f"\n{body}" if body else "" - return DebugFailure(f"GET {uri} failed: {error}{detail}") + return DebugFailure(f"GET {uri} failed: {error}{detail}", _unauthorized(error)) + + +def _unauthorized(error: requests.exceptions.RequestException) -> requests.HTTPError | None: + if not isinstance(error, requests.HTTPError) or error.response is None: + return None + return error if error.response.status_code == HTTPStatus.UNAUTHORIZED else None class SpendLogsFetcher: @@ -311,7 +321,7 @@ def install_slash_command(claude_dir: Path) -> Path: return path -@click.group() +@click.group(cls=SignInOnUnauthorizedGroup) def debug() -> None: """Pull debug reports (spend, request, response, error) for coding-agent sessions""" @@ -360,6 +370,8 @@ def debug_claude( recent_bodies=recent_bodies, max_chars=max_body_chars, ) + if isinstance(outcome, DebugFailure) and outcome.unauthorized is not None: + raise UnauthorizedError(outcome.unauthorized) if isinstance(outcome, DebugFailure): raise click.ClickException(outcome.message) click.echo(outcome) diff --git a/tests/unit/proxy/client/cli/test_auth_commands.py b/tests/unit/proxy/client/cli/test_auth_commands.py index bbe31f2672a..acda1f4a255 100644 --- a/tests/unit/proxy/client/cli/test_auth_commands.py +++ b/tests/unit/proxy/client/cli/test_auth_commands.py @@ -712,6 +712,7 @@ class TestWhoamiCommand: assert "Not authenticated" in result.output assert "lite opens your browser to sign in and waits up to 5 minutes" in result.output assert "run 'lite login --pkce'" in result.output + assert "or 'lite login' where no browser can open" in result.output @pytest.mark.parametrize( ("proxy_status", "expected_last_line_start"), diff --git a/tests/unit/proxy/client/cli/test_auto_sign_in.py b/tests/unit/proxy/client/cli/test_auto_sign_in.py index f8c8a3855ce..869066e1404 100644 --- a/tests/unit/proxy/client/cli/test_auto_sign_in.py +++ b/tests/unit/proxy/client/cli/test_auto_sign_in.py @@ -443,6 +443,12 @@ def test_the_browser_launcher_tries_each_listed_browser_in_order_and_never_a_con (["teams", "list"], "GET", "/team/list", []), (["teams", "available"], "GET", "/team/available", []), (["credentials", "list", "--format", "json"], "GET", "/credentials", {"credentials": []}), + ( + ["debug", "claude", "--session-id", "session-1", "--recent-bodies", "0", "--no-save"], + "GET", + "/spend/logs/session/ui", + {"data": [{"request_id": "req-1"}], "total": 1, "total_pages": 1}, + ), ], ) def test_every_proxy_command_group_signs_in_on_a_401_and_reruns_with_the_new_key( @@ -617,6 +623,23 @@ def test_a_rejected_explicit_key_names_the_reason_the_proxy_gave_without_echoing assert sign_in.calls == [] +def test_a_long_401_body_is_cut_before_it_reaches_the_terminal( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] +) -> None: + long_reason: Final = "Authentication Error, " + "the key was refused " * 500 + "END-OF-BODY" + requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, json={"error": {"message": long_reason}}) + sign_in: Final = FakeBrowserSignIn(_credential()) + + result: Final = _lite(["keys", "generate"], secret_vault_factory(), sign_in, ["--api-key", "sk-explicit"]) + + assert result.exit_code == 1 + assert "The proxy said: " in result.stderr + assert "Authentication Error, the key was refused" in result.stderr + assert "END-OF-BODY" not in result.stderr + assert len(result.stderr) < 2_000 + assert sign_in.calls == [] + + def test_a_failed_sign_in_names_the_reason_and_points_at_lite_login( requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] ) -> None: @@ -879,7 +902,7 @@ def test_a_login_directory_lite_cannot_write_still_signs_in_and_finishes_the_com ], ids=["ci", "ssh_without_a_display"], ) -def test_a_rejected_stored_login_fails_at_once_without_a_key_check_or_lock_where_no_browser_can_open( +def test_a_rejected_stored_login_fails_at_once_without_a_lock_where_no_browser_can_open( requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault], environ: dict[str, str], reason: str ) -> None: vault: Final = secret_vault_factory() @@ -899,7 +922,33 @@ def test_a_rejected_stored_login_fails_at_once_without_a_key_check_or_lock_where assert reason in result.stderr assert "Run `lite login --pkce` from a terminal on a machine with a browser" in result.stderr assert _bearers(route) == ["Bearer sk-stale"] - assert key_check.call_count == 0 + assert _bearers(key_check) == ["Bearer sk-stale"] + assert lock.attempts == 0 + + +@pytest.mark.parametrize( + "environ", + [{"CI": "1"}, {"SSH_CONNECTION": "203.0.113.7 52144 10.0.0.5 22"}], + ids=["ci", "ssh_without_a_display"], +) +def test_a_stored_login_missing_a_role_is_reported_as_a_role_problem_where_no_browser_can_open( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault], environ: dict[str, str] +) -> None: + vault: Final = secret_vault_factory() + save_token(_classic_login("sk-member"), vault=vault) + _key_check(requests_mock, 200) + requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, json=REJECTED) + lock: Final = AnotherCommandHoldsTheSignInLock(lambda: None) + + result: Final = CliRunner().invoke( + cli, + ["--base-url", BASE_URL, "keys", "generate"], + obj={"secret_vault": vault, "browser_sign_in": PkceBrowserSignIn(environ=environ), "sign_in_lock": lock}, + ) + + assert result.exit_code == 1 + assert "accepts your stored login but refused `cli keys generate` with HTTP 401" in result.stderr + assert "Run `lite login --pkce`" not in result.stderr assert lock.attempts == 0 From 26a26a5b9dfcbdebe2aa23054f34988b0335232b Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 30 Oct 2026 01:35:45 -0700 Subject: [PATCH 18/25] docs(cli): say CI still checks a rejected stored login and list lite debug claude The role check now runs before the browser checks, so the CI paragraph no longer says the command skips it, and the sign-in list names lite debug claude --- litellm/proxy/client/cli/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/litellm/proxy/client/cli/README.md b/litellm/proxy/client/cli/README.md index ae5ce16c249..859cbde180b 100644 --- a/litellm/proxy/client/cli/README.md +++ b/litellm/proxy/client/cli/README.md @@ -51,7 +51,7 @@ Hidden commands drop out of both `lite --help` and the interactive shell's "Avai ## Automatic Browser Sign-In for Agents -When a command that calls the proxy (`lite keys`, `lite models`, `lite model-groups`, `lite users`, `lite teams`, `lite credentials`) gets a 401 and you did not pass a key with `--api-key` or `LITELLM_PROXY_API_KEY`, `lite` signs you in through your browser and then runs the same command again with the new key. That covers a proxy you never logged in to and a stored login the proxy no longer accepts. It needs no terminal, so an AI agent that runs `lite` through its shell tool finishes the command on its own once you approve the sign-in in the browser: +When a command that calls the proxy (`lite keys`, `lite models`, `lite model-groups`, `lite users`, `lite teams`, `lite credentials`, `lite debug claude`) gets a 401 and you did not pass a key with `--api-key` or `LITELLM_PROXY_API_KEY`, `lite` signs you in through your browser and then runs the same command again with the new key. That covers a proxy you never logged in to and a stored login the proxy no longer accepts. It needs no terminal, so an AI agent that runs `lite` through its shell tool finishes the command on its own once you approve the sign-in in the browser: ```bash lite --base-url https://your-proxy.example.com keys generate --models gpt-5.4-mini --key-alias experiment-1 @@ -61,7 +61,7 @@ The sign-in is the browser flow of `lite login --pkce`, so you pick your team on Nothing signs in ahead of time. A proxy that accepts the request without a key never opens a browser, and neither does `--help`. The proxy also answers 401 when a valid key lacks the role a command needs, such as a non-admin key running `lite credentials list`, so before it signs in, `lite` checks a rejected stored login against `/key/info`. When the proxy accepts the login there, the command fails with an error that says the login likely lacks the role, and no browser opens. A key you passed yourself is never swapped out or checked again, and a 401 for it fails with an error that names `--api-key` and `LITELLM_PROXY_API_KEY`. When the browser sign-in cannot finish (the machine has no web browser or only a text-mode one such as `w3m` or `lynx`, the shell runs over SSH with no display and `BROWSER` is unset, the browser fails to open, the proxy does not serve `/.well-known/litellm-cli-auth`, you deny the request, or nobody approves it within 5 minutes), the command fails and tells you to run `lite login --pkce` from a terminal on a machine with a browser, `lite login` on one without, or set `LITELLM_PROXY_API_KEY`. `lite` prints that 5-minute wait on stderr when the sign-in starts, so an agent whose shell tool stops commands sooner knows to run the command again with a longer timeout -When the `CI` environment variable is set to anything but empty, `0`, or `false`, `lite` never opens a browser and the command fails at once with the same message, without checking the stored login against the proxy first. Set `CI=1` for a script or cron job that should fail instead of waiting for a sign-in +When the `CI` environment variable is set to anything but empty, `0`, or `false`, `lite` never opens a browser and the command fails with the same message. A rejected stored login still gets the `/key/info` check first, which can take up to 10 seconds, so a login that lacks the role gets the role error in CI too. Set `CI=1` for a script or cron job that should fail instead of waiting for a sign-in `lite chat`, `lite encryption migrate`, `lite http request`, `lite keys import`, and `lite teams assign-key` do not sign in on their own. `lite chat` and `lite teams assign-key` are interactive, `lite encryption migrate` is a one-time admin migration you run with an admin key, `lite keys import` also talks to a second proxy, and `lite http request` cannot tell a 401 from the proxy apart from a provider's 401 that the proxy passes through From 24a0b694e479e3217fd97c6b865e7cec393ab76c Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 30 Oct 2026 04:06:05 -0700 Subject: [PATCH 19/25] fix(cli): start the browser sign-in on Python 3.10's macOS browser and strip terminal control characters from 401 text Python 3.10's macOS browser class has no name attribute, so every 401 with no passed key crashed before the sign-in, and a 401 body could put escape sequences on the terminal --- .../proxy/client/cli/commands/auto_sign_in.py | 6 ++-- .../proxy/client/cli/test_auto_sign_in.py | 29 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/litellm/proxy/client/cli/commands/auto_sign_in.py b/litellm/proxy/client/cli/commands/auto_sign_in.py index 10f5955bbac..79894abdebb 100644 --- a/litellm/proxy/client/cli/commands/auto_sign_in.py +++ b/litellm/proxy/client/cli/commands/auto_sign_in.py @@ -1,4 +1,5 @@ import os +import re import subprocess import sys import time @@ -41,6 +42,7 @@ _DISPLAY_VARIABLES: Final = ("DISPLAY", "WAYLAND_DISPLAY") _SIGNED_IN_LOGIN_WITHOUT_KEY_ROW: Final = HTTPStatus.NOT_FOUND _WAIT_FOR_ANOTHER_SIGN_IN_SECONDS: Final = LOGIN_TIMEOUT_SECONDS + 60 _PROXY_REASON_CHARS: Final = 500 +_TERMINAL_CONTROL_CHARACTERS: Final = re.compile(r"[\x00-\x08\x0b-\x1f\x7f-\x9f]") def _runs_in_ci(environ: Mapping[str, str]) -> bool: @@ -87,7 +89,7 @@ def _locate_browser( def _shows_a_window(browser: webbrowser.BaseBrowser) -> bool: - return Path(browser.name).name not in _TEXT_MODE_BROWSERS + return Path(getattr(browser, "name", "")).name not in _TEXT_MODE_BROWSERS @dataclass(frozen=True, slots=True) @@ -212,7 +214,7 @@ def _proxy_reason(error: UnauthorizedError) -> str: original: Final = error.orig_exception response: Final = original.response if isinstance(original, requests.HTTPError) else None body: Final = response.text.strip() if response is not None else "" - reason: Final = redact_string(body) if body else str(error) + reason: Final = _TERMINAL_CONTROL_CHARACTERS.sub("", redact_string(body) if body else str(error)) return reason if len(reason) <= _PROXY_REASON_CHARS else f"{reason[:_PROXY_REASON_CHARS]}..." diff --git a/tests/unit/proxy/client/cli/test_auto_sign_in.py b/tests/unit/proxy/client/cli/test_auto_sign_in.py index 869066e1404..661019e0677 100644 --- a/tests/unit/proxy/client/cli/test_auto_sign_in.py +++ b/tests/unit/proxy/client/cli/test_auto_sign_in.py @@ -307,6 +307,18 @@ def test_an_ssh_session_without_a_display_never_opens_a_browser_on_the_remote_sc assert requests_mock.call_count == 0 +class BrowserWithoutAName(webbrowser.BaseBrowser): + def __init__(self) -> None: + self._name = "default" + + +def test_a_browser_object_without_a_name_still_starts_the_sign_in(requests_mock: Mocker) -> None: + sign_in: Final = PkceBrowserSignIn(find_browser=lambda _name: BrowserWithoutAName(), environ={}) + + assert sign_in.unavailable_because() is None + assert requests_mock.call_count == 0 + + @pytest.mark.parametrize( "environ", [ @@ -640,6 +652,23 @@ def test_a_long_401_body_is_cut_before_it_reaches_the_terminal( assert sign_in.calls == [] +def test_terminal_control_characters_in_a_401_body_never_reach_the_terminal( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] +) -> None: + requests_mock.post( + f"{BASE_URL}/key/generate", status_code=401, text="\x1b]0;title\x07refused\x1b[2J\r\nsecond line\x9b31m" + ) + sign_in: Final = FakeBrowserSignIn(_credential()) + + result: Final = _lite(["keys", "generate"], secret_vault_factory(), sign_in, ["--api-key", "sk-explicit"]) + + assert result.exit_code == 1 + assert "refused" in result.stderr + assert "second line" in result.stderr + assert not {"\x1b", "\x07", "\x9b", "\r"} & set(result.stderr) + assert sign_in.calls == [] + + def test_a_failed_sign_in_names_the_reason_and_points_at_lite_login( requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] ) -> None: From 719e3fee9c7f9d97f85eb768267aaa83315d32dd Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 30 Oct 2026 05:24:31 -0700 Subject: [PATCH 20/25] fix(cli): strip control characters from a 401 body before redaction and keep a reason when nothing printable is left --- .../proxy/client/cli/commands/auto_sign_in.py | 8 +++-- .../proxy/client/cli/test_auto_sign_in.py | 34 +++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/litellm/proxy/client/cli/commands/auto_sign_in.py b/litellm/proxy/client/cli/commands/auto_sign_in.py index 79894abdebb..cd718c1f1b5 100644 --- a/litellm/proxy/client/cli/commands/auto_sign_in.py +++ b/litellm/proxy/client/cli/commands/auto_sign_in.py @@ -210,11 +210,15 @@ def _attempt_with_newer_stored_key( return _attempt_with_key(run, ctx, stored_key) +def _printable(text: str) -> str: + return _TERMINAL_CONTROL_CHARACTERS.sub("", text).strip() + + def _proxy_reason(error: UnauthorizedError) -> str: original: Final = error.orig_exception response: Final = original.response if isinstance(original, requests.HTTPError) else None - body: Final = response.text.strip() if response is not None else "" - reason: Final = _TERMINAL_CONTROL_CHARACTERS.sub("", redact_string(body) if body else str(error)) + body: Final = response.text if response is not None else "" + reason: Final = redact_string(_printable(body) or _printable(str(error))) return reason if len(reason) <= _PROXY_REASON_CHARS else f"{reason[:_PROXY_REASON_CHARS]}..." diff --git a/tests/unit/proxy/client/cli/test_auto_sign_in.py b/tests/unit/proxy/client/cli/test_auto_sign_in.py index 661019e0677..e2851132899 100644 --- a/tests/unit/proxy/client/cli/test_auto_sign_in.py +++ b/tests/unit/proxy/client/cli/test_auto_sign_in.py @@ -656,7 +656,9 @@ def test_terminal_control_characters_in_a_401_body_never_reach_the_terminal( requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] ) -> None: requests_mock.post( - f"{BASE_URL}/key/generate", status_code=401, text="\x1b]0;title\x07refused\x1b[2J\r\nsecond line\x9b31m" + f"{BASE_URL}/key/generate", + status_code=401, + text="\x1b]0;title\x07refused\x1b[2J\rnext\tcolumn\nsecond line\x9b31m", ) sign_in: Final = FakeBrowserSignIn(_credential()) @@ -664,11 +666,39 @@ def test_terminal_control_characters_in_a_401_body_never_reach_the_terminal( assert result.exit_code == 1 assert "refused" in result.stderr - assert "second line" in result.stderr + assert "next\tcolumn\nsecond line" in result.stderr assert not {"\x1b", "\x07", "\x9b", "\r"} & set(result.stderr) assert sign_in.calls == [] +def test_a_key_split_by_a_control_character_in_a_401_body_stays_redacted( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] +) -> None: + key: Final = "sk-" + "a1b2c3d4" * 4 + requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, text=f"refused {key[:12]}\x07{key[12:]}") + + result: Final = _lite( + ["keys", "generate"], secret_vault_factory(), FakeBrowserSignIn(_credential()), ["--api-key", "sk-explicit"] + ) + + assert result.exit_code == 1 + assert "refused" in result.stderr + assert key not in result.stderr + + +def test_a_401_body_of_only_control_characters_still_gives_a_reason( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] +) -> None: + requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, text="\x1b\x07\r\x9b") + + result: Final = _lite( + ["keys", "generate"], secret_vault_factory(), FakeBrowserSignIn(_credential()), ["--api-key", "sk-explicit"] + ) + + assert result.exit_code == 1 + assert result.stderr.partition("The proxy said: ")[2].strip() + + def test_a_failed_sign_in_names_the_reason_and_points_at_lite_login( requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] ) -> None: From dbacc081eab5c1d37d73bbfdd7b8eb9c69a81306 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 30 Oct 2026 07:20:16 -0700 Subject: [PATCH 21/25] fix(cli): redact a 401 body before and after removing control characters and never print an empty reason --- .../proxy/client/cli/commands/auto_sign_in.py | 7 ++- .../proxy/client/cli/test_auto_sign_in.py | 59 ++++++++++++++----- 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/litellm/proxy/client/cli/commands/auto_sign_in.py b/litellm/proxy/client/cli/commands/auto_sign_in.py index cd718c1f1b5..fce24a2aab7 100644 --- a/litellm/proxy/client/cli/commands/auto_sign_in.py +++ b/litellm/proxy/client/cli/commands/auto_sign_in.py @@ -42,6 +42,7 @@ _DISPLAY_VARIABLES: Final = ("DISPLAY", "WAYLAND_DISPLAY") _SIGNED_IN_LOGIN_WITHOUT_KEY_ROW: Final = HTTPStatus.NOT_FOUND _WAIT_FOR_ANOTHER_SIGN_IN_SECONDS: Final = LOGIN_TIMEOUT_SECONDS + 60 _PROXY_REASON_CHARS: Final = 500 +_NO_PROXY_REASON: Final = "no reason given" _TERMINAL_CONTROL_CHARACTERS: Final = re.compile(r"[\x00-\x08\x0b-\x1f\x7f-\x9f]") @@ -210,15 +211,15 @@ def _attempt_with_newer_stored_key( return _attempt_with_key(run, ctx, stored_key) -def _printable(text: str) -> str: - return _TERMINAL_CONTROL_CHARACTERS.sub("", text).strip() +def _printable_and_redacted(text: str) -> str: + return redact_string(_TERMINAL_CONTROL_CHARACTERS.sub("", redact_string(text)).strip()) def _proxy_reason(error: UnauthorizedError) -> str: original: Final = error.orig_exception response: Final = original.response if isinstance(original, requests.HTTPError) else None body: Final = response.text if response is not None else "" - reason: Final = redact_string(_printable(body) or _printable(str(error))) + reason: Final = _printable_and_redacted(body) or _printable_and_redacted(str(error)) or _NO_PROXY_REASON return reason if len(reason) <= _PROXY_REASON_CHARS else f"{reason[:_PROXY_REASON_CHARS]}..." diff --git a/tests/unit/proxy/client/cli/test_auto_sign_in.py b/tests/unit/proxy/client/cli/test_auto_sign_in.py index e2851132899..7ea67c53449 100644 --- a/tests/unit/proxy/client/cli/test_auto_sign_in.py +++ b/tests/unit/proxy/client/cli/test_auto_sign_in.py @@ -22,6 +22,7 @@ if TYPE_CHECKING: from requests_mock.request import Request BASE_URL: Final = "https://proxy.example.com" +FAKE_SECRET: Final = "a1b2c3d4" * 4 SIGNED_IN_KEY: Final = "sk-signed-in" GENERATED: Final = {"key": "sk-generated", "key_alias": "experiment-1"} REJECTED: Final = {"error": {"message": "Authentication Error, No api key passed in.", "type": "auth_error"}} @@ -652,17 +653,26 @@ def test_a_long_401_body_is_cut_before_it_reaches_the_terminal( assert sign_in.calls == [] +_ROUTES_THAT_RAISE_EACH_401_ERROR_TYPE: Final = pytest.mark.parametrize( + "command, method, route", + [(["keys", "generate"], "POST", "/key/generate"), (["users", "list"], "GET", "/user/list")], + ids=["http_error", "plain_string_error"], +) + + +@_ROUTES_THAT_RAISE_EACH_401_ERROR_TYPE def test_terminal_control_characters_in_a_401_body_never_reach_the_terminal( - requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault], command: list[str], method: str, route: str ) -> None: - requests_mock.post( - f"{BASE_URL}/key/generate", + requests_mock.register_uri( + method, + f"{BASE_URL}{route}", status_code=401, text="\x1b]0;title\x07refused\x1b[2J\rnext\tcolumn\nsecond line\x9b31m", ) sign_in: Final = FakeBrowserSignIn(_credential()) - result: Final = _lite(["keys", "generate"], secret_vault_factory(), sign_in, ["--api-key", "sk-explicit"]) + result: Final = _lite(command, secret_vault_factory(), sign_in, ["--api-key", "sk-explicit"]) assert result.exit_code == 1 assert "refused" in result.stderr @@ -671,28 +681,49 @@ def test_terminal_control_characters_in_a_401_body_never_reach_the_terminal( assert sign_in.calls == [] -def test_a_key_split_by_a_control_character_in_a_401_body_stays_redacted( - requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] +@_ROUTES_THAT_RAISE_EACH_401_ERROR_TYPE +@pytest.mark.parametrize( + "body", + [ + f"refused sk-{FAKE_SECRET[:9]}\x07{FAKE_SECRET[9:]}", + f"refused Bearer\r{FAKE_SECRET}", + f"refused client_secret\r{FAKE_SECRET}", + ], + ids=["key_split_by_the_character", "bearer_token_after_the_character", "named_secret_after_the_character"], +) +def test_a_secret_beside_a_control_character_in_a_401_body_stays_redacted( + requests_mock: Mocker, + secret_vault_factory: type[FakeSecretVault], + command: list[str], + method: str, + route: str, + body: str, ) -> None: - key: Final = "sk-" + "a1b2c3d4" * 4 - requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, text=f"refused {key[:12]}\x07{key[12:]}") + requests_mock.register_uri(method, f"{BASE_URL}{route}", status_code=401, text=body) result: Final = _lite( - ["keys", "generate"], secret_vault_factory(), FakeBrowserSignIn(_credential()), ["--api-key", "sk-explicit"] + command, secret_vault_factory(), FakeBrowserSignIn(_credential()), ["--api-key", "sk-explicit"] ) assert result.exit_code == 1 assert "refused" in result.stderr - assert key not in result.stderr + assert FAKE_SECRET[9:] not in result.stderr -def test_a_401_body_of_only_control_characters_still_gives_a_reason( - requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] +@_ROUTES_THAT_RAISE_EACH_401_ERROR_TYPE +@pytest.mark.parametrize("body", ["", " \n", "\x1b\x07\r\x9b"], ids=["empty", "blank", "control_characters"]) +def test_a_401_body_with_nothing_printable_still_gives_a_reason( + requests_mock: Mocker, + secret_vault_factory: type[FakeSecretVault], + command: list[str], + method: str, + route: str, + body: str, ) -> None: - requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, text="\x1b\x07\r\x9b") + requests_mock.register_uri(method, f"{BASE_URL}{route}", status_code=401, text=body) result: Final = _lite( - ["keys", "generate"], secret_vault_factory(), FakeBrowserSignIn(_credential()), ["--api-key", "sk-explicit"] + command, secret_vault_factory(), FakeBrowserSignIn(_credential()), ["--api-key", "sk-explicit"] ) assert result.exit_code == 1 From 0e358cae66652d42d56885777a83901b61cbf818 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 30 Oct 2026 09:10:58 -0700 Subject: [PATCH 22/25] fix(cli): print no 401 body that holds a terminal control character --- .../proxy/client/cli/commands/auto_sign_in.py | 9 +-- .../proxy/client/cli/test_auto_sign_in.py | 56 ++++++++++++++----- 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/litellm/proxy/client/cli/commands/auto_sign_in.py b/litellm/proxy/client/cli/commands/auto_sign_in.py index fce24a2aab7..cfd5d9a3ef5 100644 --- a/litellm/proxy/client/cli/commands/auto_sign_in.py +++ b/litellm/proxy/client/cli/commands/auto_sign_in.py @@ -42,7 +42,7 @@ _DISPLAY_VARIABLES: Final = ("DISPLAY", "WAYLAND_DISPLAY") _SIGNED_IN_LOGIN_WITHOUT_KEY_ROW: Final = HTTPStatus.NOT_FOUND _WAIT_FOR_ANOTHER_SIGN_IN_SECONDS: Final = LOGIN_TIMEOUT_SECONDS + 60 _PROXY_REASON_CHARS: Final = 500 -_NO_PROXY_REASON: Final = "no reason given" +_NO_PROXY_REASON: Final = "no printable reason" _TERMINAL_CONTROL_CHARACTERS: Final = re.compile(r"[\x00-\x08\x0b-\x1f\x7f-\x9f]") @@ -211,15 +211,16 @@ def _attempt_with_newer_stored_key( return _attempt_with_key(run, ctx, stored_key) -def _printable_and_redacted(text: str) -> str: - return redact_string(_TERMINAL_CONTROL_CHARACTERS.sub("", redact_string(text)).strip()) +def _displayable(text: str) -> str: + normalized: Final = text.replace("\r\n", "\n").strip() + return "" if _TERMINAL_CONTROL_CHARACTERS.search(normalized) else redact_string(normalized) def _proxy_reason(error: UnauthorizedError) -> str: original: Final = error.orig_exception response: Final = original.response if isinstance(original, requests.HTTPError) else None body: Final = response.text if response is not None else "" - reason: Final = _printable_and_redacted(body) or _printable_and_redacted(str(error)) or _NO_PROXY_REASON + reason: Final = _displayable(body) or _displayable(str(error)) or _NO_PROXY_REASON return reason if len(reason) <= _PROXY_REASON_CHARS else f"{reason[:_PROXY_REASON_CHARS]}..." diff --git a/tests/unit/proxy/client/cli/test_auto_sign_in.py b/tests/unit/proxy/client/cli/test_auto_sign_in.py index 7ea67c53449..ab33a06632a 100644 --- a/tests/unit/proxy/client/cli/test_auto_sign_in.py +++ b/tests/unit/proxy/client/cli/test_auto_sign_in.py @@ -661,37 +661,64 @@ _ROUTES_THAT_RAISE_EACH_401_ERROR_TYPE: Final = pytest.mark.parametrize( @_ROUTES_THAT_RAISE_EACH_401_ERROR_TYPE +@pytest.mark.parametrize("character", ["\x00", "\x07", "\x0b", "\r", "\x1b", "\x7f", "\x9b"]) def test_terminal_control_characters_in_a_401_body_never_reach_the_terminal( - requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault], command: list[str], method: str, route: str + requests_mock: Mocker, + secret_vault_factory: type[FakeSecretVault], + command: list[str], + method: str, + route: str, + character: str, ) -> None: - requests_mock.register_uri( - method, - f"{BASE_URL}{route}", - status_code=401, - text="\x1b]0;title\x07refused\x1b[2J\rnext\tcolumn\nsecond line\x9b31m", - ) + requests_mock.register_uri(method, f"{BASE_URL}{route}", status_code=401, text=f"forbidden{character}[2Jnext") sign_in: Final = FakeBrowserSignIn(_credential()) result: Final = _lite(command, secret_vault_factory(), sign_in, ["--api-key", "sk-explicit"]) assert result.exit_code == 1 - assert "refused" in result.stderr - assert "next\tcolumn\nsecond line" in result.stderr - assert not {"\x1b", "\x07", "\x9b", "\r"} & set(result.stderr) + assert "The proxy said: " in result.stderr + assert character not in result.stderr + assert "forbidden" not in result.stderr assert sign_in.calls == [] +@_ROUTES_THAT_RAISE_EACH_401_ERROR_TYPE +def test_tabs_and_line_breaks_in_a_401_body_stay_readable( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault], command: list[str], method: str, route: str +) -> None: + requests_mock.register_uri(method, f"{BASE_URL}{route}", status_code=401, text="refused\tcolumn\r\nsecond line\r\n") + + result: Final = _lite( + command, secret_vault_factory(), FakeBrowserSignIn(_credential()), ["--api-key", "sk-explicit"] + ) + + assert result.exit_code == 1 + assert "refused\tcolumn\nsecond line" in result.stderr + + @_ROUTES_THAT_RAISE_EACH_401_ERROR_TYPE @pytest.mark.parametrize( "body", [ - f"refused sk-{FAKE_SECRET[:9]}\x07{FAKE_SECRET[9:]}", + f"refused sk-{FAKE_SECRET[:8]}\x07{FAKE_SECRET[8:]}", + f"refused sk-{FAKE_SECRET[:24]}\x07{FAKE_SECRET[24:]}", + f"refused Bearer {FAKE_SECRET[:24]}\x00{FAKE_SECRET[24:]}", f"refused Bearer\r{FAKE_SECRET}", + f"refused Bearer\x07{FAKE_SECRET}", f"refused client_secret\r{FAKE_SECRET}", + f"refused \x1b[31m{FAKE_SECRET}\x1b[0m", + ], + ids=[ + "key_split_near_its_start", + "key_split_near_its_end", + "bearer_token_split_near_its_end", + "bearer_token_after_a_carriage_return", + "bearer_token_after_a_bell", + "named_secret_after_a_carriage_return", + "value_inside_a_color_sequence", ], - ids=["key_split_by_the_character", "bearer_token_after_the_character", "named_secret_after_the_character"], ) -def test_a_secret_beside_a_control_character_in_a_401_body_stays_redacted( +def test_a_secret_beside_a_control_character_in_a_401_body_never_prints( requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault], command: list[str], @@ -706,8 +733,7 @@ def test_a_secret_beside_a_control_character_in_a_401_body_stays_redacted( ) assert result.exit_code == 1 - assert "refused" in result.stderr - assert FAKE_SECRET[9:] not in result.stderr + assert FAKE_SECRET[-8:] not in result.stderr @_ROUTES_THAT_RAISE_EACH_401_ERROR_TYPE From 537434920bdf27515d8b610c646c39643fd9830c Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 30 Oct 2026 11:34:56 -0700 Subject: [PATCH 23/25] fix(cli): read a 401 body as UTF-8 so a bad charset cannot crash the reason response.text raises LookupError on an unknown charset such as charset=undefined, which turned a rejected key into a traceback on teams list. Decoding the bytes keeps the reason for that body and for UTF-8 bodies with no charset. A body that is not UTF-8 prints nothing from the body, like a body with a control character --- .../proxy/client/cli/commands/auto_sign_in.py | 6 +- .../proxy/client/cli/test_auto_sign_in.py | 99 +++++++++++++++++-- 2 files changed, 92 insertions(+), 13 deletions(-) diff --git a/litellm/proxy/client/cli/commands/auto_sign_in.py b/litellm/proxy/client/cli/commands/auto_sign_in.py index cfd5d9a3ef5..07e0cb1453c 100644 --- a/litellm/proxy/client/cli/commands/auto_sign_in.py +++ b/litellm/proxy/client/cli/commands/auto_sign_in.py @@ -43,7 +43,7 @@ _SIGNED_IN_LOGIN_WITHOUT_KEY_ROW: Final = HTTPStatus.NOT_FOUND _WAIT_FOR_ANOTHER_SIGN_IN_SECONDS: Final = LOGIN_TIMEOUT_SECONDS + 60 _PROXY_REASON_CHARS: Final = 500 _NO_PROXY_REASON: Final = "no printable reason" -_TERMINAL_CONTROL_CHARACTERS: Final = re.compile(r"[\x00-\x08\x0b-\x1f\x7f-\x9f]") +_UNPRINTABLE_CHARACTERS: Final = re.compile(r"[\x00-\x08\x0b-\x1f\x7f-\x9f\ufffd]") def _runs_in_ci(environ: Mapping[str, str]) -> bool: @@ -213,13 +213,13 @@ def _attempt_with_newer_stored_key( def _displayable(text: str) -> str: normalized: Final = text.replace("\r\n", "\n").strip() - return "" if _TERMINAL_CONTROL_CHARACTERS.search(normalized) else redact_string(normalized) + return "" if _UNPRINTABLE_CHARACTERS.search(normalized) else redact_string(normalized) def _proxy_reason(error: UnauthorizedError) -> str: original: Final = error.orig_exception response: Final = original.response if isinstance(original, requests.HTTPError) else None - body: Final = response.text if response is not None else "" + body: Final = response.content.decode("utf-8", errors="replace") if response is not None else "" reason: Final = _displayable(body) or _displayable(str(error)) or _NO_PROXY_REASON return reason if len(reason) <= _PROXY_REASON_CHARS else f"{reason[:_PROXY_REASON_CHARS]}..." diff --git a/tests/unit/proxy/client/cli/test_auto_sign_in.py b/tests/unit/proxy/client/cli/test_auto_sign_in.py index ab33a06632a..d0aefa4049e 100644 --- a/tests/unit/proxy/client/cli/test_auto_sign_in.py +++ b/tests/unit/proxy/client/cli/test_auto_sign_in.py @@ -661,7 +661,7 @@ _ROUTES_THAT_RAISE_EACH_401_ERROR_TYPE: Final = pytest.mark.parametrize( @_ROUTES_THAT_RAISE_EACH_401_ERROR_TYPE -@pytest.mark.parametrize("character", ["\x00", "\x07", "\x0b", "\r", "\x1b", "\x7f", "\x9b"]) +@pytest.mark.parametrize("character", ["\x00", "\x08", "\x0b", "\r", "\x1b", "\x1f", "\x7f", "\x9b", "\x9f"]) def test_terminal_control_characters_in_a_401_body_never_reach_the_terminal( requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault], @@ -686,27 +686,106 @@ def test_terminal_control_characters_in_a_401_body_never_reach_the_terminal( def test_tabs_and_line_breaks_in_a_401_body_stay_readable( requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault], command: list[str], method: str, route: str ) -> None: - requests_mock.register_uri(method, f"{BASE_URL}{route}", status_code=401, text="refused\tcolumn\r\nsecond line\r\n") + requests_mock.register_uri( + method, f"{BASE_URL}{route}", status_code=401, text="\x0c refused\tcolumn\r\nsecond line \x0c\r\n" + ) result: Final = _lite( command, secret_vault_factory(), FakeBrowserSignIn(_credential()), ["--api-key", "sk-explicit"] ) assert result.exit_code == 1 - assert "refused\tcolumn\nsecond line" in result.stderr + assert "The proxy said: refused\tcolumn\nsecond line" in result.stderr + + +def test_a_utf8_401_body_with_no_charset_prints_as_written( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] +) -> None: + reason: Final = "the key\u2019s owner ~ caf\u00e9\u00a0\u65e5\u672c" + requests_mock.post( + f"{BASE_URL}/key/generate", + status_code=401, + content=reason.encode("utf-8"), + headers={"Content-Type": "text/plain"}, + ) + + result: Final = _lite( + ["keys", "generate"], secret_vault_factory(), FakeBrowserSignIn(_credential()), ["--api-key", "sk-explicit"] + ) + + assert result.exit_code == 1 + assert reason in result.stderr + + +def test_a_401_body_that_is_not_utf8_is_never_printed( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] +) -> None: + requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, content=b"forbidden \xff\xfe") + + result: Final = _lite( + ["keys", "generate"], secret_vault_factory(), FakeBrowserSignIn(_credential()), ["--api-key", "sk-explicit"] + ) + + assert result.exit_code == 1 + assert "forbidden" not in result.stderr + assert "\ufffd" not in result.stderr + + +@pytest.mark.parametrize( + "command, method, route", + [(["keys", "generate"], "POST", "/key/generate"), (["teams", "list"], "GET", "/team/list")], + ids=["http_error", "fixed_text_error"], +) +@pytest.mark.parametrize("charset", ["undefined", "idna"]) +def test_a_401_with_an_unusable_charset_still_gives_a_reason( + requests_mock: Mocker, + secret_vault_factory: type[FakeSecretVault], + command: list[str], + method: str, + route: str, + charset: str, +) -> None: + requests_mock.register_uri( + method, + f"{BASE_URL}{route}", + status_code=401, + content=b"refused", + headers={"Content-Type": f"text/plain; charset={charset}"}, + ) + + result: Final = _lite( + command, secret_vault_factory(), FakeBrowserSignIn(_credential()), ["--api-key", "sk-explicit"] + ) + + assert result.exit_code == 1 + assert isinstance(result.exception, SystemExit) + assert "The proxy said: refused" in result.stderr + + +def test_a_401_body_stays_hidden_when_redaction_would_remove_its_control_character( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] +) -> None: + requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, text=f"forbidden Bearer\r{FAKE_SECRET}") + + result: Final = _lite( + ["keys", "generate"], secret_vault_factory(), FakeBrowserSignIn(_credential()), ["--api-key", "sk-explicit"] + ) + + assert result.exit_code == 1 + assert "forbidden" not in result.stderr @_ROUTES_THAT_RAISE_EACH_401_ERROR_TYPE @pytest.mark.parametrize( "body", [ - f"refused sk-{FAKE_SECRET[:8]}\x07{FAKE_SECRET[8:]}", - f"refused sk-{FAKE_SECRET[:24]}\x07{FAKE_SECRET[24:]}", - f"refused Bearer {FAKE_SECRET[:24]}\x00{FAKE_SECRET[24:]}", - f"refused Bearer\r{FAKE_SECRET}", - f"refused Bearer\x07{FAKE_SECRET}", - f"refused client_secret\r{FAKE_SECRET}", - f"refused \x1b[31m{FAKE_SECRET}\x1b[0m", + f"forbidden sk-{FAKE_SECRET[:8]}\x07{FAKE_SECRET[8:]}", + f"forbidden sk-{FAKE_SECRET[:24]}\x07{FAKE_SECRET[24:]}", + f"forbidden Bearer {FAKE_SECRET[:24]}\x00{FAKE_SECRET[24:]}", + f"forbidden Bearer\r{FAKE_SECRET}", + f"forbidden Bearer\x07{FAKE_SECRET}", + f"forbidden client_secret\r{FAKE_SECRET}", + f"forbidden \x1b[31m{FAKE_SECRET}\x1b[0m", ], ids=[ "key_split_near_its_start", From b827c191101a66bd32f4e8408a82c853aec0d20f Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 30 Oct 2026 13:01:06 -0700 Subject: [PATCH 24/25] fix(cli): fall back to the declared charset for a 401 body that is not UTF-8 Reading only UTF-8 hid a body sent in a declared charset such as shift_jis or windows-1252. UTF-8 stays the first read so a body with no charset still prints correctly, and a charset Python cannot read leaves the HTTP error text --- .../proxy/client/cli/commands/auto_sign_in.py | 16 ++++- .../proxy/client/cli/test_auto_sign_in.py | 72 ++++++++++++++++++- 2 files changed, 84 insertions(+), 4 deletions(-) diff --git a/litellm/proxy/client/cli/commands/auto_sign_in.py b/litellm/proxy/client/cli/commands/auto_sign_in.py index 07e0cb1453c..06ff705d3b9 100644 --- a/litellm/proxy/client/cli/commands/auto_sign_in.py +++ b/litellm/proxy/client/cli/commands/auto_sign_in.py @@ -216,10 +216,24 @@ def _displayable(text: str) -> str: return "" if _UNPRINTABLE_CHARACTERS.search(normalized) else redact_string(normalized) +def _text_in_declared_charset(response: requests.Response) -> str: + try: + return response.text + except UnicodeError: + return "" + + +def _response_text(response: requests.Response) -> str: + try: + return response.content.decode("utf-8") + except UnicodeDecodeError: + return _text_in_declared_charset(response) + + def _proxy_reason(error: UnauthorizedError) -> str: original: Final = error.orig_exception response: Final = original.response if isinstance(original, requests.HTTPError) else None - body: Final = response.content.decode("utf-8", errors="replace") if response is not None else "" + body: Final = _response_text(response) if response is not None else "" reason: Final = _displayable(body) or _displayable(str(error)) or _NO_PROXY_REASON return reason if len(reason) <= _PROXY_REASON_CHARS else f"{reason[:_PROXY_REASON_CHARS]}..." diff --git a/tests/unit/proxy/client/cli/test_auto_sign_in.py b/tests/unit/proxy/client/cli/test_auto_sign_in.py index d0aefa4049e..c245ba994ce 100644 --- a/tests/unit/proxy/client/cli/test_auto_sign_in.py +++ b/tests/unit/proxy/client/cli/test_auto_sign_in.py @@ -653,6 +653,37 @@ def test_a_long_401_body_is_cut_before_it_reaches_the_terminal( assert sign_in.calls == [] +@pytest.mark.parametrize( + "body, printed", + [("a " * 249 + "xy", "a " * 249 + "xy\n"), ("a " * 249 + "xyz", "a " * 249 + "xy...\n")], + ids=["at_the_limit", "one_past_the_limit"], +) +def test_a_401_reason_is_cut_only_past_the_limit_and_says_so( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault], body: str, printed: str +) -> None: + requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, text=body) + + result: Final = _lite( + ["keys", "generate"], secret_vault_factory(), FakeBrowserSignIn(_credential()), ["--api-key", "sk-explicit"] + ) + + assert result.exit_code == 1 + assert f"The proxy said: {printed}" in result.stderr + + +def test_a_control_character_past_the_cut_still_hides_a_401_body( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] +) -> None: + requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, text="forbidden " * 60 + "\x1b[2J") + + result: Final = _lite( + ["keys", "generate"], secret_vault_factory(), FakeBrowserSignIn(_credential()), ["--api-key", "sk-explicit"] + ) + + assert result.exit_code == 1 + assert "forbidden" not in result.stderr + + _ROUTES_THAT_RAISE_EACH_401_ERROR_TYPE: Final = pytest.mark.parametrize( "command, method, route", [(["keys", "generate"], "POST", "/key/generate"), (["users", "list"], "GET", "/user/list")], @@ -717,16 +748,51 @@ def test_a_utf8_401_body_with_no_charset_prints_as_written( assert reason in result.stderr -def test_a_401_body_that_is_not_utf8_is_never_printed( - requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault] +@pytest.mark.parametrize( + "reason, charset", + [ + ("caf\u00e9", "iso-8859-1"), + ("the key\u2019s owner", "windows-1252"), + ("\u65e5\u672c", "shift_jis"), + ("refused", "utf-16"), + ], +) +def test_a_401_body_in_a_declared_charset_prints_as_written( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault], reason: str, charset: str ) -> None: - requests_mock.post(f"{BASE_URL}/key/generate", status_code=401, content=b"forbidden \xff\xfe") + requests_mock.post( + f"{BASE_URL}/key/generate", + status_code=401, + content=reason.encode(charset), + headers={"Content-Type": f"text/plain; charset={charset}"}, + ) result: Final = _lite( ["keys", "generate"], secret_vault_factory(), FakeBrowserSignIn(_credential()), ["--api-key", "sk-explicit"] ) assert result.exit_code == 1 + assert f"The proxy said: {reason}" in result.stderr + + +@pytest.mark.parametrize("charset", ["utf-8", "undefined", "idna"]) +def test_a_401_body_its_charset_cannot_read_is_never_printed( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault], charset: str +) -> None: + requests_mock.post( + f"{BASE_URL}/key/generate", + status_code=401, + content=b"forbidden \xff\xfe", + headers={"Content-Type": f"text/plain; charset={charset}"}, + ) + + result: Final = _lite( + ["keys", "generate"], secret_vault_factory(), FakeBrowserSignIn(_credential()), ["--api-key", "sk-explicit"] + ) + + assert result.exit_code == 1 + assert isinstance(result.exception, SystemExit) + assert "The proxy said: " in result.stderr assert "forbidden" not in result.stderr assert "\ufffd" not in result.stderr From 669614c0324b4745ef9db236f28cb118b3d74ed3 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 30 Oct 2026 15:06:58 -0700 Subject: [PATCH 25/25] fix(cli): read a 401 body only as UTF-8 and print nothing from any other body The fallback to the declared charset crashed on a NUL in the charset name, and a stray Shift_JIS lead byte swallowed the first letter of a key so redaction missed it. Reading only UTF-8 avoids both. A body in another charset now shows the HTTP error text --- .../proxy/client/cli/commands/auto_sign_in.py | 16 +----- .../proxy/client/cli/test_auto_sign_in.py | 52 +++++++++---------- 2 files changed, 26 insertions(+), 42 deletions(-) diff --git a/litellm/proxy/client/cli/commands/auto_sign_in.py b/litellm/proxy/client/cli/commands/auto_sign_in.py index 06ff705d3b9..07e0cb1453c 100644 --- a/litellm/proxy/client/cli/commands/auto_sign_in.py +++ b/litellm/proxy/client/cli/commands/auto_sign_in.py @@ -216,24 +216,10 @@ def _displayable(text: str) -> str: return "" if _UNPRINTABLE_CHARACTERS.search(normalized) else redact_string(normalized) -def _text_in_declared_charset(response: requests.Response) -> str: - try: - return response.text - except UnicodeError: - return "" - - -def _response_text(response: requests.Response) -> str: - try: - return response.content.decode("utf-8") - except UnicodeDecodeError: - return _text_in_declared_charset(response) - - def _proxy_reason(error: UnauthorizedError) -> str: original: Final = error.orig_exception response: Final = original.response if isinstance(original, requests.HTTPError) else None - body: Final = _response_text(response) if response is not None else "" + body: Final = response.content.decode("utf-8", errors="replace") if response is not None else "" reason: Final = _displayable(body) or _displayable(str(error)) or _NO_PROXY_REASON return reason if len(reason) <= _PROXY_REASON_CHARS else f"{reason[:_PROXY_REASON_CHARS]}..." diff --git a/tests/unit/proxy/client/cli/test_auto_sign_in.py b/tests/unit/proxy/client/cli/test_auto_sign_in.py index c245ba994ce..aa7b3dcee37 100644 --- a/tests/unit/proxy/client/cli/test_auto_sign_in.py +++ b/tests/unit/proxy/client/cli/test_auto_sign_in.py @@ -749,40 +749,37 @@ def test_a_utf8_401_body_with_no_charset_prints_as_written( @pytest.mark.parametrize( - "reason, charset", + "content, charset", [ - ("caf\u00e9", "iso-8859-1"), - ("the key\u2019s owner", "windows-1252"), - ("\u65e5\u672c", "shift_jis"), - ("refused", "utf-16"), + (b"forbidden \xff\xfe", "utf-8"), + (b"forbidden \xff\xfe", "undefined"), + (b"forbidden \xff\xfe", "idna"), + (b"forbidden \xff\xfe", "bad\x00name"), + ("forbidden caf\u00e9".encode("iso-8859-1"), "iso-8859-1"), + ("forbidden \u2019".encode("windows-1252"), "windows-1252"), + ("forbidden \u65e5\u672c".encode("shift_jis"), "shift_jis"), + ("forbidden".encode("utf-16"), "utf-16"), + (f"forbidden \x81sk-{FAKE_SECRET}".encode("latin-1"), "shift_jis"), + ], + ids=[ + "stray_bytes_in_utf8", + "undefined_charset", + "idna_charset", + "nul_in_the_charset_name", + "latin1", + "windows_1252", + "shift_jis", + "utf16", + "lead_byte_before_a_key", ], ) -def test_a_401_body_in_a_declared_charset_prints_as_written( - requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault], reason: str, charset: str +def test_a_401_body_that_is_not_utf8_is_never_printed( + requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault], content: bytes, charset: str ) -> None: requests_mock.post( f"{BASE_URL}/key/generate", status_code=401, - content=reason.encode(charset), - headers={"Content-Type": f"text/plain; charset={charset}"}, - ) - - result: Final = _lite( - ["keys", "generate"], secret_vault_factory(), FakeBrowserSignIn(_credential()), ["--api-key", "sk-explicit"] - ) - - assert result.exit_code == 1 - assert f"The proxy said: {reason}" in result.stderr - - -@pytest.mark.parametrize("charset", ["utf-8", "undefined", "idna"]) -def test_a_401_body_its_charset_cannot_read_is_never_printed( - requests_mock: Mocker, secret_vault_factory: type[FakeSecretVault], charset: str -) -> None: - requests_mock.post( - f"{BASE_URL}/key/generate", - status_code=401, - content=b"forbidden \xff\xfe", + content=content, headers={"Content-Type": f"text/plain; charset={charset}"}, ) @@ -795,6 +792,7 @@ def test_a_401_body_its_charset_cannot_read_is_never_printed( assert "The proxy said: " in result.stderr assert "forbidden" not in result.stderr assert "\ufffd" not in result.stderr + assert FAKE_SECRET[-8:] not in result.stderr @pytest.mark.parametrize(