Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion litellm/proxy/common_utils/encrypt_decrypt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def decrypt_value_helper(
verbose_proxy_logger.debug(error_message)
return value if return_original_value else None

verbose_proxy_logger.debug(f"Unable to decrypt value={value} for key: {key}, returning None")
verbose_proxy_logger.debug(f"Unable to decrypt value for key: {key}, returning None")
if return_original_value:
return value
else:
Expand Down
82 changes: 61 additions & 21 deletions litellm/proxy/proxy_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
_init_custom_logger_compatible_class,
)
from litellm.litellm_core_utils.safe_json_dumps import safe_dumps
from litellm.litellm_core_utils.safe_json_loads import safe_json_loads
from litellm.proxy._types import (
UI_TEAM_ID,
CallbackDelete,
Expand Down Expand Up @@ -637,6 +638,43 @@ def generate_feedback_box():
global_max_parallel_request_retries_env: Optional[str] = os.getenv("LITELLM_GLOBAL_MAX_PARALLEL_REQUEST_RETRIES")
proxy_state = ProxyState()
SENSITIVE_DATA_MASKER = SensitiveDataMasker()


# Secret-bearing general_settings fields the segment masker does not match by
# name: database_url and database_extra_connection_params embed DB credentials,
# pass_through_endpoints carry upstream Authorization headers, and
# alert_to_webhook_url is itself a webhook secret
_EXTRA_SECRET_GENERAL_SETTINGS_FIELDS = frozenset(
{
"database_url",
"database_extra_connection_params",
"pass_through_endpoints",
"alert_to_webhook_url",
}
)


def _redact_worker_config_for_logging(worker_config: str | dict[str, JsonValue] | None) -> JsonValue:
"""Mask sensitive fields in the worker config before it enters a log record.

`worker_config` reaches `proxy_startup_event` as either the JSON blob
persisted by `save_worker_config` (a string) or the dict passed directly
to `initialize`. Both shapes can carry `master_key`, `database_url`,
provider API keys, etc.; passing the raw value to `verbose_proxy_logger`
leaks them whenever the last-line-of-defense regex filter is bypassed
(`LITELLM_DISABLE_REDACT_SECRETS=true`, an older log sink, a downstream
handler that captures records pre-filter). Redact at the source.
"""
if worker_config is None:
return None
if isinstance(worker_config, dict):
return _redact_secret_values_in_obj(worker_config)
parsed = safe_json_loads(worker_config, default=None)
if isinstance(parsed, dict):
return safe_dumps(_redact_secret_values_in_obj(parsed))
return worker_config


if global_max_parallel_request_retries_env is None:
global_max_parallel_request_retries: int = 3
else:
Expand Down Expand Up @@ -833,7 +871,7 @@ async def proxy_startup_event(app: FastAPI):
### LOAD CONFIG ###
worker_config: Optional[Union[str, dict]] = get_secret("WORKER_CONFIG") # type: ignore
env_config_yaml: Optional[str] = get_secret_str("CONFIG_FILE_PATH")
verbose_proxy_logger.debug("worker_config: %s", worker_config)
verbose_proxy_logger.debug("worker_config: %s", _redact_worker_config_for_logging(worker_config))
# check if it's a valid file path
if env_config_yaml is not None:
if os.path.isfile(env_config_yaml) and proxy_config.is_yaml(config_file_path=env_config_yaml):
Expand Down Expand Up @@ -4273,7 +4311,9 @@ async def load_config(self, router: Optional[litellm.Router], config_file_path:
raise Exception(
f"team_id missing from default_team_settings at index={idx}\npassed in value={type(team_setting)}"
)
verbose_proxy_logger.debug(f"{blue_color_code} setting litellm.{key}={value}{reset_color_code}")
verbose_proxy_logger.debug(
f"{blue_color_code} setting litellm.{key}={_redact_general_setting_value(key, value, is_full_admin=False)}{reset_color_code}"
)
setattr(litellm, key, value)
elif key == "upperbound_key_generate_params":
if value is not None and isinstance(value, dict):
Expand All @@ -4288,7 +4328,9 @@ async def load_config(self, router: Optional[litellm.Router], config_file_path:
litellm._turn_on_json()
verbose_proxy_logger.debug(f"{blue_color_code} Enabled JSON logging via config{reset_color_code}")
else:
verbose_proxy_logger.debug(f"{blue_color_code} setting litellm.{key}={value}{reset_color_code}")
verbose_proxy_logger.debug(
f"{blue_color_code} setting litellm.{key}={_redact_general_setting_value(key, value, is_full_admin=False)}{reset_color_code}"
)
setattr(litellm, key, value)
if key == "request_timeout":
litellm.request_timeout_explicitly_set = True
Expand Down Expand Up @@ -4336,9 +4378,9 @@ async def load_config(self, router: Optional[litellm.Router], config_file_path:
### CONNECT TO DATABASE ###
database_url = general_settings.get("database_url", None)
if database_url and database_url.startswith("os.environ/"):
verbose_proxy_logger.debug("GOING INTO LITELLM.GET_SECRET!")
verbose_proxy_logger.debug("Resolving database_url via secret manager")
database_url = get_secret(database_url)
verbose_proxy_logger.debug("RETRIEVED DB URL: %s", database_url)
verbose_proxy_logger.debug("Resolved database_url from secret manager")
### MASTER KEY ###
master_key = general_settings.get("master_key", get_secret("LITELLM_MASTER_KEY", None))

Expand Down Expand Up @@ -4738,7 +4780,7 @@ def _load_alerting_settings(self, general_settings: dict):
"""

_alerting_callbacks = general_settings.get("alerting", None)
verbose_proxy_logger.debug(f"_alerting_callbacks: {general_settings}")
verbose_proxy_logger.debug("_alerting_callbacks: %s", _alerting_callbacks)
if _alerting_callbacks is None:
return

Expand Down Expand Up @@ -5646,7 +5688,11 @@ async def _update_config_from_db(

param_name = getattr(response, "param_name", None)
param_value = getattr(response, "param_value", None)
verbose_proxy_logger.debug(f"param_name={param_name}, param_value={param_value}")
verbose_proxy_logger.debug(
"param_name=%s, param_value=%s",
param_name,
_redact_config_param_value_for_logging(param_name, param_value),
)
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved

if param_name is not None and param_value is not None:
config = self._update_config_fields(
Expand Down Expand Up @@ -14218,20 +14264,6 @@ async def update_config_general_settings(
return response


# Secret-bearing general_settings fields the segment masker does not match by
# name: database_url and database_extra_connection_params embed DB credentials,
# pass_through_endpoints carry upstream Authorization headers, and
# alert_to_webhook_url is itself a webhook secret
_EXTRA_SECRET_GENERAL_SETTINGS_FIELDS = frozenset(
{
"database_url",
"database_extra_connection_params",
"pass_through_endpoints",
"alert_to_webhook_url",
}
)


def _is_secret_general_setting_field(field_name: str) -> bool:
return field_name in _EXTRA_SECRET_GENERAL_SETTINGS_FIELDS or SENSITIVE_DATA_MASKER.is_sensitive_key(field_name)

Expand Down Expand Up @@ -14261,6 +14293,14 @@ def _redact_secret_values_in_obj(value: JsonValue, depth: int = 0) -> JsonValue:
return value


def _redact_config_param_value_for_logging(param_name: Optional[str], param_value: JsonValue) -> JsonValue:
if param_name == "environment_variables" and isinstance(param_value, dict):
return {key: "REDACTED" for key in param_value}
if isinstance(param_value, (dict, list)):
return _redact_secret_values_in_obj(param_value)
return param_value


def _redact_general_setting_value(field_name: str, value: JsonValue, is_full_admin: bool) -> JsonValue:
if is_full_admin:
return value
Expand Down
63 changes: 51 additions & 12 deletions tests/test_litellm/proxy/common_utils/test_encrypt_decrypt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

def _use_aes(monkeypatch):
"""Flip the write-time algorithm to AES-256-GCM for the duration of a test."""
monkeypatch.setattr(
proxy_server, "general_settings", {"encryption_algorithm": "aes-256-gcm"}
)
monkeypatch.setattr(proxy_server, "general_settings", {"encryption_algorithm": "aes-256-gcm"})


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -96,12 +94,7 @@ def test_aes_decrypt_failure_returns_original_when_requested(monkeypatch):
_use_aes(monkeypatch)

garbled = _V2_GCM_PREFIX + "###"
assert (
decrypt_value_helper(
garbled, key="t", exception_type="debug", return_original_value=True
)
== garbled
)
assert decrypt_value_helper(garbled, key="t", exception_type="debug", return_original_value=True) == garbled


def test_empty_string_round_trips_under_aes(monkeypatch):
Expand Down Expand Up @@ -139,10 +132,56 @@ def test_callback_prefix_composes_with_v2(monkeypatch):

def test_unknown_algorithm_falls_back_to_legacy(monkeypatch):
"""An unrecognized encryption_algorithm value does not produce v2 writes."""
monkeypatch.setattr(
proxy_server, "general_settings", {"encryption_algorithm": "rot13"}
)
monkeypatch.setattr(proxy_server, "general_settings", {"encryption_algorithm": "rot13"})

ct = encrypt_value_helper("secret")
assert not ct.startswith(_V2_GCM_PREFIX)
assert decrypt_value_helper(ct, key="t") == "secret"


def test_decrypt_failure_debug_log_omits_raw_value(monkeypatch):
"""Regression for LIT-4152: the decrypt-failure debug breadcrumb must not
embed the raw value.

A DB ``environment_variables`` secret (e.g. a ``DATABASE_URL`` connection
string) reaches this path when it cannot be decrypted, for example after a
salt or master key change, and previously printed in cleartext when the
module regex scrubber was bypassed. The failing key still names the pair so
the breadcrumb keeps its debugging value. Uses a dedicated handler rather
than caplog because caplog is unreliable under pytest-xdist.
"""
import logging

import litellm._logging as _logging_module
from litellm._logging import verbose_proxy_logger

monkeypatch.setattr(_logging_module, "_ENABLE_SECRET_REDACTION", False)

secret = "postgresql://leak_user:leak_pw_decrypt@leak-host:5432/leak_db"

class LogRecordHandler(logging.Handler):
def __init__(self) -> None:
super().__init__()
self.records: list[logging.LogRecord] = []

def emit(self, record: logging.LogRecord) -> None:
self.records.append(record)

handler = LogRecordHandler()
handler.setLevel(logging.DEBUG)
original_level = verbose_proxy_logger.level
verbose_proxy_logger.setLevel(logging.DEBUG)
verbose_proxy_logger.addHandler(handler)
try:
result = decrypt_value_helper(secret, key="DATABASE_URL", return_original_value=True)
rendered = " ".join(record.getMessage() for record in handler.records)
finally:
verbose_proxy_logger.removeHandler(handler)
verbose_proxy_logger.setLevel(original_level)

assert secret not in rendered, f"raw value leaked in decrypt-failure log: {rendered!r}"
assert "leak_pw_decrypt" not in rendered
assert any("DATABASE_URL" in record.getMessage() for record in handler.records), (
"the failing key should still be named in the breadcrumb"
)
assert result == secret
139 changes: 139 additions & 0 deletions tests/test_litellm/proxy/proxy_server/test_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,145 @@ def test_save_worker_config_invalid_no_kwargs_yields_empty(monkeypatch):
assert os.environ["WORKER_CONFIG"] == "{}"


# ---------------------------------------------------------------------------
# _redact_worker_config_for_logging (LIT-4152)
# ---------------------------------------------------------------------------


_LIT4152_SECRETS = (
"sk-lit4152-regression-master-key-abcdef1234567890",
"leak_password_9090",
"sk-lit4152-provider-api-key-abcdef",
"postgresql://leak_user:leak_password_9090@leak-host.internal:5432/leak_db",
)


def _lit4152_worker_config_dict():
return {
"model": "openai/gpt-4o-mini",
"config": "/tmp/c.yaml",
"master_key": _LIT4152_SECRETS[0],
"database_url": _LIT4152_SECRETS[3],
"api_key": _LIT4152_SECRETS[2],
"telemetry": True,
}


def test__redact_worker_config_for_logging_dict_masks_all_secret_shapes():
"""LIT-4152 regression: dict-form worker_config must not embed any raw
secret. Covers the segment-matched fields (`master_key`, `api_key`) and the
URL-with-credentials field (`database_url`), which the segment masker
misses because neither segment matches its sensitive-pattern set.
"""
from litellm.proxy.proxy_server import _redact_worker_config_for_logging

redacted = _redact_worker_config_for_logging(_lit4152_worker_config_dict())
rendered = repr(redacted)
for secret in _LIT4152_SECRETS:
assert secret not in rendered, f"leak: {secret} in {rendered!r}"
assert isinstance(redacted, dict)
assert redacted["model"] == "openai/gpt-4o-mini"
assert redacted["telemetry"] is True


def test__redact_worker_config_for_logging_json_string_round_trips_masked():
"""Docker/K8s deployments hand the proxy a JSON string via ``WORKER_CONFIG``.
Confirm the string path also masks and that the returned value re-parses
into a dict with the sensitive fields masked.
"""
from litellm.proxy.proxy_server import _redact_worker_config_for_logging

payload = json.dumps(_lit4152_worker_config_dict())
redacted = _redact_worker_config_for_logging(payload)
assert isinstance(redacted, str)
for secret in _LIT4152_SECRETS:
assert secret not in redacted, f"leak: {secret} in {redacted!r}"
parsed = json.loads(redacted)
assert parsed["model"] == "openai/gpt-4o-mini"


def test__redact_worker_config_for_logging_passthrough_for_none_and_non_json_string():
"""Non-dict, non-JSON-parseable string is passed through verbatim (nothing
to mask) and ``None`` returns ``None``.
"""
from litellm.proxy.proxy_server import _redact_worker_config_for_logging

assert _redact_worker_config_for_logging(None) is None
assert _redact_worker_config_for_logging("/tmp/some_config.yaml") == "/tmp/some_config.yaml"


def test__redact_worker_config_for_logging_masks_non_string_url_webhook_values():
"""The URL/webhook fields the segment masker cannot catch by key name
(``alert_to_webhook_url``, ``pass_through_endpoints``,
``database_extra_connection_params``) can hold non-string shapes:
``alert_to_webhook_url`` is typed as ``Optional[Dict]`` and can nest
secret query params under keys the segment masker also misses. Confirm
the whole value is replaced regardless of shape so a nested webhook or
Bearer token under a non-segment-matched key does not slip through.
"""
from litellm.proxy.proxy_server import _redact_worker_config_for_logging

nested_webhook_secret = "https://hooks.slack.com/services/T0/B0/nested-webhook-secret-xyz"
data = {
"master_key": "sk-should-be-masked",
"alert_to_webhook_url": {"budget_alerts": nested_webhook_secret},
"pass_through_endpoints": [
{
"path": "/upstream",
"target": "https://api.provider.com",
"headers": {"Authorization": "Bearer nested-token-should-be-gone"},
}
],
"database_extra_connection_params": {"password": "extra-db-password-abc"},
}
redacted = _redact_worker_config_for_logging(data)
rendered = repr(redacted)
for secret in (
"sk-should-be-masked",
nested_webhook_secret,
"nested-token-should-be-gone",
"extra-db-password-abc",
):
assert secret not in rendered, f"leak: {secret} in {rendered!r}"


def test__redact_worker_config_for_logging_masks_nested_secret_fields():
"""LIT-4152 nested regression: the URL/webhook credential fields the segment
masker cannot catch by name (``database_url``,
``database_extra_connection_params``, ``pass_through_endpoints``,
``alert_to_webhook_url``) must be redacted at any depth, not just the top
level. A worker_config that nests ``general_settings`` under a parent key
must not leak a nested ``database_url`` or webhook secret; the earlier
top-level-only redaction would have passed these through raw.
"""
from litellm.proxy.proxy_server import _redact_worker_config_for_logging

nested_db_url = "postgresql://nested_user:nested_pw_4152@nested-host:5432/db"
nested_webhook = "https://hooks.slack.com/services/T0/B0/nested-4152-webhook"
nested_extra_pw = "nested-extra-conn-pw-4152"
nested_bearer = "Bearer nested-passthrough-token-4152"
data = {
"config": {
"general_settings": {
"database_url": nested_db_url,
"database_extra_connection_params": {"password": nested_extra_pw},
"alert_to_webhook_url": {"budget_alerts": nested_webhook},
"pass_through_endpoints": [
{"path": "/up", "headers": {"Authorization": nested_bearer}}
],
}
}
}
redacted = _redact_worker_config_for_logging(data)
rendered = repr(redacted)
for secret in (nested_db_url, nested_webhook, nested_extra_pw, nested_bearer):
assert secret not in rendered, f"nested leak: {secret} in {rendered!r}"

inner = redacted["config"]["general_settings"]
assert inner["database_url"] == "REDACTED"
assert inner["pass_through_endpoints"] == "REDACTED"


# ---------------------------------------------------------------------------
# initialize
# ---------------------------------------------------------------------------
Expand Down
Loading
Loading