From 62cf355d9f6142498bf42a17a69ffbaa5df3b934 Mon Sep 17 00:00:00 2001 From: moe-berri Date: Sat, 14 Nov 2026 06:41:12 +0800 Subject: [PATCH] fix(spend-logs): make token redaction opt-in (#51729) --- litellm/proxy/_types.py | 4 +- .../spend_tracking/spend_tracking_utils.py | 6 +- .../src/lib/http/schema.d.ts | 4 +- .../test_spend_log_tool_payload_content.py | 4 +- .../test_spend_tracking_utils.py | 64 ++++++++++++++----- 5 files changed, 57 insertions(+), 25 deletions(-) diff --git a/litellm/proxy/_types.py b/litellm/proxy/_types.py index ed4660d5635..3d8a142636f 100644 --- a/litellm/proxy/_types.py +++ b/litellm/proxy/_types.py @@ -2962,8 +2962,8 @@ class ConfigGeneralSettings(LiteLLMPydanticObjectBase): description="If True, stores request messages and responses in spend logs. Default is False.", ) redact_tokens_in_spend_logs: bool = Field( - True, - description="Masks token-shaped secrets in stored spend log request and response content by default. Set False to opt out.", + False, + description="Set True to mask token-shaped secrets in stored spend log request and response content. Default is False.", ) spend_logs_prompt_storage: Literal["latest_turn"] | None = Field( None, diff --git a/litellm/proxy/spend_tracking/spend_tracking_utils.py b/litellm/proxy/spend_tracking/spend_tracking_utils.py index 31fc669568a..a4c6d8a40c7 100644 --- a/litellm/proxy/spend_tracking/spend_tracking_utils.py +++ b/litellm/proxy/spend_tracking/spend_tracking_utils.py @@ -668,7 +668,7 @@ async def get_logging_payload_async( from litellm.proxy.proxy_server import general_settings_view call_kwargs: Final[Mapping[str, object]] = kwargs if kwargs is not None else MappingProxyType({}) - offload: Final = general_settings_view().get("redact_tokens_in_spend_logs") is not False + offload: Final = general_settings_view().get("redact_tokens_in_spend_logs") is True build: Final = partial( get_logging_payload, kwargs=dict(_spend_log_worker_kwargs(call_kwargs)) if offload else dict(call_kwargs), @@ -1535,7 +1535,7 @@ def _is_spend_log_content(parent: Mapping[str, object], key: str) -> bool: def _without_tokens(value: str) -> str: from litellm.proxy.proxy_server import general_settings_view - if general_settings_view().get("redact_tokens_in_spend_logs") is False: + if general_settings_view().get("redact_tokens_in_spend_logs") is not True: return value return redact_tokens(strip_null_bytes(value), REDACTED_BY_LITELM_STRING) @@ -1584,7 +1584,7 @@ def _sanitize_request_body_for_spend_logs_payload( At every nesting level, also strips keys listed in _SENSITIVE_REQUEST_BODY_KEYS (e.g. secret_fields, which holds raw HTTP headers including Authorization tokens), and replaces string values under keys SensitiveDataMasker classifies as credentials with REDACTED_BY_LITELM_STRING, except inside tool payloads - and logprobs. Token-shaped substrings (e.g. a GitHub token in a tool result) are masked everywhere. + and logprobs. When enabled, token-shaped substrings (e.g. a GitHub token in a tool result) are masked everywhere. """ if visited is None: visited = set() diff --git a/litellm/ui/litellm-dashboard/src/lib/http/schema.d.ts b/litellm/ui/litellm-dashboard/src/lib/http/schema.d.ts index 805b543e95b..2cafcccd2d3 100644 --- a/litellm/ui/litellm-dashboard/src/lib/http/schema.d.ts +++ b/litellm/ui/litellm-dashboard/src/lib/http/schema.d.ts @@ -32383,8 +32383,8 @@ export interface components { proxy_config_reload_interval_seconds: number; /** * Redact Tokens In Spend Logs - * @description Masks token-shaped secrets in stored spend log request and response content by default. Set False to opt out. - * @default true + * @description Set True to mask token-shaped secrets in stored spend log request and response content. Default is False. + * @default false */ redact_tokens_in_spend_logs: boolean; /** diff --git a/tests/integration/spend/test_spend_log_tool_payload_content.py b/tests/integration/spend/test_spend_log_tool_payload_content.py index d4f124172c0..898b00ac524 100644 --- a/tests/integration/spend/test_spend_log_tool_payload_content.py +++ b/tests/integration/spend/test_spend_log_tool_payload_content.py @@ -34,6 +34,7 @@ def _prompt_storage_config( tmp_path: Path, *, store_prompts: bool = True, + redact_tokens: bool = False, local_cache: bool = False, model_list: tuple[dict[str, JsonValue], ...] = (), ) -> Path: @@ -49,6 +50,7 @@ def _prompt_storage_config( "disable_responses_id_security": True, "store_model_in_db": True, "store_prompts_in_spend_logs": store_prompts, + "redact_tokens_in_spend_logs": redact_tokens, "proxy_batch_write_at": 1, "proxy_batch_polling_interval": 1, }, @@ -504,7 +506,7 @@ def test_stored_messages_mask_a_github_token_inside_tool_output(gateway: Gateway with ( wire_server(_answering_model_listing(respond)) as wire, - owned_proxy(gateway, tmp_path, {}, config=_prompt_storage_config(tmp_path)) as isolated, + owned_proxy(gateway, tmp_path, {}, config=_prompt_storage_config(tmp_path, redact_tokens=True)) as isolated, isolated.scenario() as scenario, ): model: Final = scenario.model(model=ANTHROPIC_MODEL, api_base=wire.url, api_key="synthetic-anthropic-key") diff --git a/tests/unit/proxy/spend_tracking/test_spend_tracking_utils.py b/tests/unit/proxy/spend_tracking/test_spend_tracking_utils.py index 20abaf578df..69c1ed09950 100644 --- a/tests/unit/proxy/spend_tracking/test_spend_tracking_utils.py +++ b/tests/unit/proxy/spend_tracking/test_spend_tracking_utils.py @@ -1691,13 +1691,17 @@ def test_get_response_for_spend_logs_payload_strips_null_bytes(mock_should_store _GITHUB_TOKEN: Final = "ghp_" + "a1B2c3D4e5" * 3 + "F6g7H8" -@pytest.mark.parametrize("prefix,serialized", (("\u2022", True), ("\u00e9", True), ("\U0001f600", True), ("\u2022", False))) +@pytest.mark.parametrize( + "prefix,serialized", (("\u2022", True), ("\u00e9", True), ("\U0001f600", True), ("\u2022", False)) +) def test_payload_masks_tokens_after_json_unicode_escapes( monkeypatch: pytest.MonkeyPatch, prefix: str, serialized: bool ) -> None: from litellm.proxy import proxy_server - monkeypatch.setattr(proxy_server, "general_settings", {"store_prompts_in_spend_logs": True}) + monkeypatch.setattr( + proxy_server, "general_settings", {"store_prompts_in_spend_logs": True, "redact_tokens_in_spend_logs": True} + ) raw: Final = prefix + "ghp_" + "A" * 36 content: Final = json.dumps({"stdout": raw}) if serialized else raw messages: Final = [{"role": "tool", "content": content}] @@ -1726,7 +1730,9 @@ def test_payload_masks_tokens_after_json_unicode_escapes( def test_payload_masks_tokens_joined_by_null_byte_removal(monkeypatch: pytest.MonkeyPatch) -> None: from litellm.proxy import proxy_server - monkeypatch.setattr(proxy_server, "general_settings", {"store_prompts_in_spend_logs": True}) + monkeypatch.setattr( + proxy_server, "general_settings", {"store_prompts_in_spend_logs": True, "redact_tokens_in_spend_logs": True} + ) secret: Final = "ghp_\x00" + "A" * 36 messages: Final = [{"role": "tool", "content": secret}] compaction: Final = {"type": "compaction", "content": secret} @@ -1756,17 +1762,30 @@ def test_payload_masks_tokens_joined_by_null_byte_removal(monkeypatch: pytest.Mo assert compaction == {"type": "compaction", "content": secret} +def test_spend_log_token_redaction_config_is_opt_in() -> None: + from litellm.proxy._types import ConfigGeneralSettings + + assert ConfigGeneralSettings.model_validate({}).redact_tokens_in_spend_logs is False + assert ( + ConfigGeneralSettings.model_validate({"redact_tokens_in_spend_logs": True}).redact_tokens_in_spend_logs is True + ) + + @pytest.mark.asyncio @pytest.mark.parametrize("redact", (None, False, True)) +@pytest.mark.parametrize("store_prompts", (False, True)) async def test_token_redaction_thread_and_inline_payloads_match( - monkeypatch: pytest.MonkeyPatch, redact: bool | None + monkeypatch: pytest.MonkeyPatch, redact: bool | None, store_prompts: bool ) -> None: from litellm.proxy import proxy_server monkeypatch.setattr( proxy_server, "general_settings", - {"store_prompts_in_spend_logs": True, **({"redact_tokens_in_spend_logs": redact} if redact is not None else {})}, + { + "store_prompts_in_spend_logs": store_prompts, + **({"redact_tokens_in_spend_logs": redact} if redact is not None else {}), + }, ) loop_thread: Final = threading.get_ident() monkeypatch.setattr(litellm, "cache", litellm.Cache(type="local")) @@ -1795,11 +1814,12 @@ async def test_token_redaction_thread_and_inline_payloads_match( expected: Final = get_logging_payload(deepcopy(original), response, now, now) assert payload == expected - assert redact is False or kwargs == original - assert (len(worker_threads) == 1) is (redact is not False) + assert redact is not True or kwargs == original + assert (len(worker_threads) == 1) is (redact is True) assert all(thread_id != loop_thread for thread_id in worker_threads) - stored: Final = json.loads(payload["proxy_server_request"])["messages"][0]["content"] - assert stored == ("TOKEN=REDACTED_BY_LITELM ordinary tool output" if redact is not False else text) + stored: Final = json.loads(payload["proxy_server_request"]) + expected_text: Final = "TOKEN=REDACTED_BY_LITELM ordinary tool output" if redact is True else text + assert stored == ({"messages": [{"role": "tool", "content": expected_text}]} if store_prompts else {}) assert body["messages"][0]["content"] == text @@ -1823,7 +1843,9 @@ def test_stored_request_masks_a_token_returned_in_an_anthropic_tool_result( ) -> None: from litellm.proxy import proxy_server - monkeypatch.setattr(proxy_server, "general_settings", {"store_prompts_in_spend_logs": True}) + monkeypatch.setattr( + proxy_server, "general_settings", {"store_prompts_in_spend_logs": True, "redact_tokens_in_spend_logs": True} + ) tool_output: Final = json.dumps({"stdout": f"export TOKEN={secret}\ndone"}) request_body: Final = { "model": "claude-sonnet-5-5", @@ -1836,9 +1858,10 @@ def test_stored_request_masks_a_token_returned_in_an_anthropic_tool_result( assert secret not in stored assert secret[:12] not in stored - assert "export TOKEN=REDACTED_BY_LITELM" in json.loads(json.loads(stored)["messages"][0]["content"][0]["content"])[ - "stdout" - ] + assert ( + "export TOKEN=REDACTED_BY_LITELM" + in json.loads(json.loads(stored)["messages"][0]["content"][0]["content"])["stdout"] + ) def test_stored_response_masks_a_token_in_a_tool_call_argument(monkeypatch: pytest.MonkeyPatch) -> None: @@ -1853,7 +1876,12 @@ def test_stored_response_masks_a_token_in_a_tool_call_argument(monkeypatch: pyte "response": { "content": [ {"type": "text", "text": "Cloning now"}, - {"type": "tool_use", "id": "toolu_1", "name": "bash", "input": {"cmd": f"git clone https://{_GITHUB_TOKEN}@github.com/o/r"}}, + { + "type": "tool_use", + "id": "toolu_1", + "name": "bash", + "input": {"cmd": f"git clone https://{_GITHUB_TOKEN}@github.com/o/r"}, + }, ] } }, @@ -1868,10 +1896,12 @@ def test_stored_response_masks_a_token_in_a_tool_call_argument(monkeypatch: pyte def test_stored_request_masks_a_token_kept_in_the_head_of_a_truncated_string(monkeypatch: pytest.MonkeyPatch) -> None: from litellm.proxy import proxy_server - monkeypatch.setattr(proxy_server, "general_settings", {}) + monkeypatch.setattr(proxy_server, "general_settings", {"redact_tokens_in_spend_logs": True}) long_text: Final = f"{_GITHUB_TOKEN} " + "x" * 10_000 - stored: Final = _sanitize_request_body_for_spend_logs_payload({"text": long_text}, max_string_length_prompt_in_db=200) + stored: Final = _sanitize_request_body_for_spend_logs_payload( + {"text": long_text}, max_string_length_prompt_in_db=200 + ) assert stored["text"].startswith("REDACTED_BY_LITELM x") assert LITELLM_TRUNCATED_PAYLOAD_FIELD in stored["text"] @@ -1889,7 +1919,7 @@ def test_stored_request_masks_a_token_kept_in_the_head_of_a_truncated_string(mon def test_stored_request_keeps_text_that_only_resembles_credentials(monkeypatch: pytest.MonkeyPatch, text: str) -> None: from litellm.proxy import proxy_server - monkeypatch.setattr(proxy_server, "general_settings", {}) + monkeypatch.setattr(proxy_server, "general_settings", {"redact_tokens_in_spend_logs": True}) stored: Final = _sanitize_request_body_for_spend_logs_payload({"text": text}, max_string_length_prompt_in_db=10_000) assert stored["text"] == text