Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(cli): roll the keychain write back when the plaintext copy cannot be removed

mateo-berri424e74ba9ffc
diff --git a/litellm/litellm_core_utils/cli_token_utils.py b/litellm/litellm_core_utils/cli_token_utils.py
index cd69f9470a3..53289770c1a 100644
--- a/litellm/litellm_core_utils/cli_token_utils.py
+++ b/litellm/litellm_core_utils/cli_token_utils.py
@@ -12,7 +12,6 @@ first time it reads one.
This module has no dependencies on proxy code and can be safely imported at the SDK level.
"""
-import contextlib
import time
from pathlib import Path
from types import MappingProxyType
@@ -217,30 +216,41 @@ def _apply_vault_secret(record: CliTokenRecord, blob: str, vault: SecretVault) -
def _migrate_file_secret(record: CliTokenRecord, vault: SecretVault) -> CliTokenRecord | None:
+ """Move a file-held secret into the vault, but only if the file's copy can be taken away.
+
+ Migrating without scrubbing would leave the credential live in two stores instead of one, so a
+ file that will not give its copy up rolls the vault write back rather than widening exposure.
+ """
if record.key is None:
return None
- if isinstance(vault.write(_encode_secret(record.base_url, record.key, record.jwt_token)), SecretStored):
- _scrub_file_secret(record)
+ if not isinstance(vault.write(_encode_secret(record.base_url, record.key, record.jwt_token)), SecretStored):
+ return record
+ if not _scrub_file_secret(record):
+ vault.erase()
return record
-def _scrub_file_secret(record: CliTokenRecord) -> None:
+def _scrub_file_secret(record: CliTokenRecord) -> bool:
"""Leave no secret material in the token file once the vault holds it.
A file that cannot be rewritten without the secret is removed instead. Signing in again costs
the user one command; a live credential left behind in cleartext costs them the credential.
"""
if record.key is None and not record.jwt_token:
- return
+ return True
try:
_write_token_file(_without_secret(record))
except OSError:
- _discard_token_file()
+ return _discard_token_file()
+ return True
-def _discard_token_file() -> None:
- with contextlib.suppress(OSError):
+def _discard_token_file() -> bool:
+ try:
Path(get_cli_token_file_path()).unlink(missing_ok=True)
+ except OSError:
+ return False
+ return True
def _without_secret(record: CliTokenRecord) -> CliTokenRecord:
diff --git a/tests/test_litellm/litellm_core_utils/test_cli_token_utils.py b/tests/test_litellm/litellm_core_utils/test_cli_token_utils.py
index 925b440dfb7..cbc93bbde71 100644
--- a/tests/test_litellm/litellm_core_utils/test_cli_token_utils.py
+++ b/tests/test_litellm/litellm_core_utils/test_cli_token_utils.py
@@ -1,4 +1,5 @@
import json
+import os
import stat
import sys
import time
@@ -320,6 +321,25 @@ class TestScrubFailure:
"""A keychain that took the secret while the file kept it is the worst of both stores: the
credential is live, it is in cleartext on disk, and every command reports success."""
+ @pytest.mark.skipif(os.geteuid() == 0, reason="root ignores directory permissions")
+ def test_a_file_that_will_not_give_its_copy_up_rolls_the_vault_write_back(
+ self, isolated_home, secret_vault_factory
+ ):
+ """Handing the keychain a copy without taking the file's away leaves the credential live in
+ two stores instead of one. A directory that permits neither the rewrite nor the delete, a
+ root-owned ~/.litellm left behind by a `sudo lite login`, must widen nothing."""
+ path = _write_legacy_file(isolated_home)
+ vault = secret_vault_factory()
+ path.parent.chmod(0o500)
+ try:
+ record = load_cli_token(vault=vault)
+ finally:
+ path.parent.chmod(0o700)
+
+ assert record.key == "sk-legacy"
+ assert json.loads(path.read_text())["key"] == "sk-legacy"
+ assert vault.blob is None
+
def test_a_file_that_cannot_be_rewritten_is_removed_instead(
self, isolated_home, secret_vault_factory, monkeypatch
):