mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(logging): escape the forwarded client IP on the auth failure log line
With use_x_forwarded_for on, the proxy printed the raw X-Forwarded-For header into the auth failure message, so a dict-shaped header still added made-up fields to that JSON log line
This commit is contained in:
parent
0dc3851a91
commit
8ab3d129a1
2 changed files with 42 additions and 3 deletions
|
|
@ -9,7 +9,7 @@ from typing import TYPE_CHECKING, Any, Final
|
|||
from fastapi import HTTPException, Request, status
|
||||
|
||||
import litellm
|
||||
from litellm._logging import verbose_proxy_logger, verbose_proxy_stdout_logger
|
||||
from litellm._logging import logfmt_value, verbose_proxy_logger, verbose_proxy_stdout_logger
|
||||
from litellm.constants import EMPTY_MAPPING
|
||||
from litellm.integrations.otel.runtime import seed_request_identity
|
||||
from litellm.litellm_core_utils.core_helpers import as_str_mapping, is_expected_client_error
|
||||
|
|
@ -211,7 +211,7 @@ class UserAPIKeyAuthExceptionHandler:
|
|||
logging.WARNING if is_quiet_log else logging.ERROR,
|
||||
"litellm.proxy.proxy_server.user_api_key_auth(): Exception occured - %s\nRequester IP Address:%s%s",
|
||||
_auth_failure_log_message(e),
|
||||
requester_ip,
|
||||
logfmt_value(requester_ip),
|
||||
_identity_log_suffix(resolved_identity),
|
||||
exc_info=True if litellm.log_client_error_tracebacks or not is_expected_client_error(e) else None,
|
||||
extra=log_extra,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import asyncio
|
||||
import json
|
||||
from collections.abc import Mapping
|
||||
from collections.abc import Callable, Mapping
|
||||
from typing import Final
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
|
|
@ -979,6 +979,45 @@ async def test_auth_failure_keeps_existing_requester_ip_address():
|
|||
assert logged_request_data["metadata"]["requester_ip_address"] == "198.51.100.4"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_a_dict_shaped_x_forwarded_for_cannot_forge_fields_on_the_auth_failure_json_log(
|
||||
proxy_json_log_lines: Callable[[], tuple[dict[str, object], ...]],
|
||||
) -> None:
|
||||
forwarded_for: Final = "{'requester_ip':'forged-ip','component':'forged-comp','logger':'forged','injected':'yes'}"
|
||||
with (
|
||||
patch("litellm.proxy.auth.auth_exception_handler.seed_request_identity"),
|
||||
patch(
|
||||
"litellm.proxy.proxy_server.proxy_logging_obj.post_call_failure_hook",
|
||||
new_callable=AsyncMock,
|
||||
return_value=None,
|
||||
),
|
||||
patch(
|
||||
"litellm.proxy.proxy_server.general_settings",
|
||||
{"allow_requests_on_db_unavailable": False, "use_x_forwarded_for": True},
|
||||
),
|
||||
):
|
||||
with pytest.raises(ProxyException):
|
||||
await UserAPIKeyAuthExceptionHandler._handle_authentication_error(
|
||||
ProxyException(
|
||||
message="Invalid API key",
|
||||
type=ProxyErrorTypes.auth_error,
|
||||
param=None,
|
||||
code=status.HTTP_401_UNAUTHORIZED,
|
||||
),
|
||||
_http_request(headers={"x-forwarded-for": forwarded_for}),
|
||||
{"model": "gpt-4o"},
|
||||
"/v1/chat/completions",
|
||||
None,
|
||||
"sk-bad-key",
|
||||
)
|
||||
|
||||
(line,) = (line for line in proxy_json_log_lines() if "user_api_key_auth()" in str(line["message"]))
|
||||
assert str(line["component"]).startswith(verbose_proxy_logger.name)
|
||||
assert str(line["logger"]).startswith("auth_exception_handler.py:")
|
||||
assert line["requester_ip"] == forwarded_for
|
||||
assert "injected" not in line
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_auth_failure_ip_uses_litellm_metadata_when_present():
|
||||
"""Routes that keep proxy metadata under `litellm_metadata` (e.g. /responses) must
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue