diff --git a/litellm/utils.py b/litellm/utils.py index d620bb45026..37b6a686c2b 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -5243,22 +5243,12 @@ _apply_openai_param_overrides = apply_openai_param_overrides PROVIDER_UNVALIDATED_PARAMS: Final = frozenset({"user", "stream_options", "stream", "max_retries"}) # A param in PROVIDER_UNVALIDATED_PARAMS that the provider does not support is -# the one case that is neither sent nor refused: `_check_valid_arg` skips it -# before it can reach `unsupported_params`, so no `UnsupportedParamsError` is -# raised, and `map_openai_params` only copies supported names, so it never -# reaches the wire either. The caller sees success and a param that did nothing. +# neither sent nor refused: `_check_valid_arg` skips it before it can reach +# `unsupported_params`, and `map_openai_params` only copies supported names, so +# the caller sees success and a param that did nothing (#45015). # -# That is reported as issue #45015: `user` is only a supported param for models -# in OpenAI's own catalog (`gpt_transformation.py` adds it behind -# `is_openai_catalog_model`), so passing `user=` to a self-hosted -# OpenAI-compatible model silently drops it, and the proxy's per-customer spend -# attribution -- which reads `request_body["user"]` -- records nothing. The -# reporter found the `x-litellm-end-user-id` header by trial and error, because -# it bypasses param mapping entirely. -# -# Only params listed here warn. `stream`, `stream_options` and `max_retries` -# are transport controls rather than things a caller tracks the effect of, and -# warning on them would be noise on every request that sets one. +# Only params listed here warn. `stream`, `stream_options` and `max_retries` are +# transport controls rather than things a caller tracks the effect of. UNVALIDATED_PARAM_DROP_HINTS: Final = { "user": ( "`user` is only a supported param for models in OpenAI's catalog, so it is dropped here " @@ -5279,6 +5269,19 @@ _unvalidated_param_warned: Final = set[tuple[str, str, str]]() # has stopped being news, so warning simply stops rather than repeating. _UNVALIDATED_PARAM_WARN_CAP: Final = 64 +# `model` and `custom_llm_provider` arrive from the request, so a caller could +# embed carriage returns, newlines or terminal escapes and forge whole log +# entries off the back of this warning (CodeQL py/log-injection), or repeat an +# oversized value on every line. Same treatment `_sanitize_correlation_id` +# gives a caller-set trace id. +_MAX_LOGGED_VALUE_LENGTH: Final = 128 + + +def _log_safe(value: object) -> str: + """Bound and strip a caller-controlled value before it reaches a log line.""" + printable: Final = "".join(ch for ch in str(value) if ch.isprintable()) + return printable[:_MAX_LOGGED_VALUE_LENGTH] + def warn_unvalidated_param_dropped(param: str, custom_llm_provider: object, model: object) -> None: """Warn once per (param, provider, model) that a drop-silently param was dropped. @@ -5300,8 +5303,8 @@ def warn_unvalidated_param_dropped(param: str, custom_llm_provider: object, mode "supported param, and it is exempt from the unsupported-param error, so it is dropped " "silently. %s", param, - custom_llm_provider, - model, + _log_safe(custom_llm_provider), + _log_safe(model), hint, ) diff --git a/tests/unit/test_utils_get_optional_params.py b/tests/unit/test_utils_get_optional_params.py index 30805a5ce0b..a2c2fa67a85 100644 --- a/tests/unit/test_utils_get_optional_params.py +++ b/tests/unit/test_utils_get_optional_params.py @@ -1,4 +1,5 @@ import json +from collections.abc import Iterator from typing import Final from unittest.mock import MagicMock, patch @@ -2099,7 +2100,7 @@ def test_supports_system_message_merges_system_prompt_into_user_message() -> Non assert map_system_message_pt(messages=messages) == [{"role": "user", "content": "Listen here! Hello there!"}] -def _drop_warnings(mock_warning): +def _drop_warnings(mock_warning: MagicMock) -> list[str]: """The formatted drop warnings only. `verbose_logger.warning` is shared, so asserting on a raw call count would @@ -2107,17 +2108,11 @@ def _drop_warnings(mock_warning): logs a warning. Filter to this message and format it, since the assertions are about what the message tells the caller. """ - messages = [] - for call in mock_warning.call_args_list: - template = call.args[0] - if "is not sending" not in template: - continue - messages.append(template % call.args[1:]) - return messages + return [call.args[0] % call.args[1:] for call in mock_warning.call_args_list if "is not sending" in call.args[0]] @pytest.fixture -def reset_unvalidated_param_warnings(): +def reset_unvalidated_param_warnings() -> Iterator[None]: """The drop warning is deduped for the life of the process. A test that asserts it fires has to start from an empty set, and must not @@ -2130,7 +2125,7 @@ def reset_unvalidated_param_warnings(): _unvalidated_param_warned.clear() -def test_user_param_dropped_warns_for_non_catalog_model(reset_unvalidated_param_warnings): +def test_user_param_dropped_warns_for_non_catalog_model(reset_unvalidated_param_warnings: None) -> None: """A param that is exempt from the unsupported-param error is still dropped. `user` is only a supported param for models in OpenAI's catalog, and it is in @@ -2156,7 +2151,7 @@ def test_user_param_dropped_warns_for_non_catalog_model(reset_unvalidated_param_ assert "x-litellm-end-user-id" in messages[0] -def test_user_param_drop_warning_is_deduped(reset_unvalidated_param_warnings): +def test_user_param_drop_warning_is_deduped(reset_unvalidated_param_warnings: None) -> None: """Once per (param, provider, model), not once per request.""" with patch("litellm.utils.verbose_logger.warning") as mock_warning: for _ in range(3): @@ -2169,7 +2164,7 @@ def test_user_param_drop_warning_is_deduped(reset_unvalidated_param_warnings): assert len(_drop_warnings(mock_warning)) == 1 -def test_user_param_not_warned_when_the_model_supports_it(reset_unvalidated_param_warnings): +def test_user_param_not_warned_when_the_model_supports_it(reset_unvalidated_param_warnings: None) -> None: """A catalog model sends `user`, so there is nothing to warn about.""" with patch("litellm.utils.verbose_logger.warning") as mock_warning: optional_params = get_optional_params( @@ -2183,8 +2178,8 @@ def test_user_param_not_warned_when_the_model_supports_it(reset_unvalidated_para def test_warn_helper_stays_silent_for_an_exempt_param_with_no_hint( - reset_unvalidated_param_warnings, -): + reset_unvalidated_param_warnings: None, +) -> None: """The exempt transport params must stay silent. `stream`, `stream_options` and `max_retries` are exempt from the same @@ -2209,7 +2204,7 @@ def test_warn_helper_stays_silent_for_an_exempt_param_with_no_hint( assert _unvalidated_param_warned == set() -def test_warning_stops_once_the_cap_is_reached(reset_unvalidated_param_warnings): +def test_warning_stops_once_the_cap_is_reached(reset_unvalidated_param_warnings: None) -> None: """Past the cap the warning stops rather than growing the dedup set. A proxy serving a large model fleet would otherwise add a triple per @@ -2230,3 +2225,29 @@ def test_warning_stops_once_the_cap_is_reached(reset_unvalidated_param_warnings) assert _drop_warnings(mock_warning) == [] assert ("user", "openai", "model-past-the-cap") not in _unvalidated_param_warned assert len(_unvalidated_param_warned) == _UNVALIDATED_PARAM_WARN_CAP + + +def test_drop_warning_cannot_forge_a_log_line() -> None: + """A caller-controlled `model` must not be able to add log lines. + + `model` comes straight off the request and the warning puts it in a log + entry, so a carriage return or newline in it used to produce a second line + that reads as its own log record (CodeQL py/log-injection). The control + characters are composed with chr() so the intent survives any reformatting. + """ + from litellm.utils import _unvalidated_param_warned, warn_unvalidated_param_dropped + + crafted: Final = "gpt-4o" + chr(13) + chr(10) + "WARNING: [auth] admin login succeeded" + + _unvalidated_param_warned.clear() + with patch("litellm.utils.verbose_logger.warning") as mock_warning: + warn_unvalidated_param_dropped("user", "openai", crafted) + _unvalidated_param_warned.clear() + + messages: Final = _drop_warnings(mock_warning) + assert len(messages) == 1 + # The assertion that matters: the record stays one line. + assert chr(10) not in messages[0] + assert chr(13) not in messages[0] + # Still names the model, so sanitising has not made the warning useless. + assert "gpt-4o" in messages[0]