mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
fix(spend-logs): mask token-shaped secrets in stored request and response content
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
parent
5171b3fbd8
commit
8df62ad5c7
3 changed files with 123 additions and 5 deletions
|
|
@ -14,11 +14,13 @@ from litellm.rust_bridge import diagnostics
|
|||
|
||||
REDACTED: Final = "REDACTED"
|
||||
|
||||
_PEM_PRIVATE_KEY_BLOCK: Final = r"-----BEGIN[A-Z \-]*PRIVATE KEY-----[\s\S]*?-----END[A-Z \-]*PRIVATE KEY-----"
|
||||
|
||||
|
||||
def _build_secret_patterns() -> "re.Pattern[str]":
|
||||
patterns: Final[list[str]] = [
|
||||
# PEM private key / certificate blocks
|
||||
r"-----BEGIN[A-Z \-]*PRIVATE KEY-----[\s\S]*?-----END[A-Z \-]*PRIVATE KEY-----",
|
||||
_PEM_PRIVATE_KEY_BLOCK,
|
||||
# GCP OAuth2 access tokens (ya29.*)
|
||||
r"\bya29\.[A-Za-z0-9_.~+/-]+",
|
||||
# Credential %s formatting (space separator, no key= prefix)
|
||||
|
|
@ -100,6 +102,37 @@ def redact_string(value: str) -> str:
|
|||
return diagnostics.run(lambda native: native.redact_text(value), lambda: python_redact_string(value))
|
||||
|
||||
|
||||
_TOKEN_SHAPES: Final = (
|
||||
r"ya29\.[A-Za-z0-9_.~+/-]{20,}",
|
||||
r"(?:AKIA|ASIA)[0-9A-Z]{16}\b",
|
||||
rf"sk-[A-Za-z0-9\-_]{{{MINIMUM_CUSTOM_KEY_LENGTH - len('sk-')},}}",
|
||||
r"(?:sk|rk)_(?:live|test)_[A-Za-z0-9]{24,}",
|
||||
r"AIza[0-9A-Za-z\-_]{35}",
|
||||
r"dapi[0-9a-f]{32}\b",
|
||||
r"(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{36,}",
|
||||
r"github_pat_[A-Za-z0-9_]{82}",
|
||||
r"glpat-[A-Za-z0-9\-_]{20,}",
|
||||
r"xox[abposr]-[A-Za-z0-9-]{10,}",
|
||||
r"hf_[A-Za-z0-9]{34,}",
|
||||
r"npm_[A-Za-z0-9]{36}\b",
|
||||
r"pypi-AgEIcHlwaS5vcmc[A-Za-z0-9\-_]{50,}",
|
||||
r"SG\.[A-Za-z0-9_-]{22}\.[A-Za-z0-9_-]{43}\b",
|
||||
r"eyJ[A-Za-z0-9_-]{10,}\.eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]+",
|
||||
)
|
||||
# Tokens start at a word boundary or right after a JSON escape like "\n", since tool output is often serialized JSON
|
||||
_TOKEN_START: Final = r"(?:(?<!\w)|(?<=\\[nrt]))"
|
||||
_TOKEN_RE: Final = re.compile(f"{_PEM_PRIVATE_KEY_BLOCK}|{_TOKEN_START}(?:{'|'.join(_TOKEN_SHAPES)})")
|
||||
|
||||
|
||||
def redact_tokens(value: str, replacement: str = REDACTED) -> str:
|
||||
"""Replace substrings shaped like a credential (provider key prefixes, JWTs, PEM private keys).
|
||||
|
||||
Unlike redact_string() this never matches on surrounding words such as ``password:`` or ``Basic``,
|
||||
so it is safe to run over free-form prompt and tool content.
|
||||
"""
|
||||
return _TOKEN_RE.sub(replacement, value)
|
||||
|
||||
|
||||
_UNIX_SYSTEM_PATH: Final = r"/(?:etc|var|opt|usr|home|root|private|Users|tmp|mnt|srv)/[^\s'\"\)\]}>,]+"
|
||||
_WINDOWS_DRIVE_PATH: Final = r"[A-Za-z]:\\[^\s'\"\)\]}>,]+"
|
||||
_PRIVATE_OR_LOOPBACK_IPV4: Final = (
|
||||
|
|
|
|||
|
|
@ -60,6 +60,7 @@ from litellm.litellm_core_utils.litellm_logging import (
|
|||
)
|
||||
from litellm.litellm_core_utils.ptu_pricing import azure_spillover
|
||||
from litellm.litellm_core_utils.safe_json_dumps import safe_dumps, strip_null_bytes
|
||||
from litellm.litellm_core_utils.secret_redaction import redact_tokens
|
||||
from litellm.litellm_core_utils.sensitive_data_masker import SensitiveDataMasker
|
||||
from litellm.llms.anthropic.common_utils import resolve_used_client_oauth_token
|
||||
from litellm.proxy._types import (
|
||||
|
|
@ -1204,7 +1205,7 @@ def _get_messages_for_spend_logs_payload(
|
|||
messages: Final = standard_logging_payload.get("messages")
|
||||
if messages is not None:
|
||||
try:
|
||||
return safe_dumps(messages)
|
||||
return _without_tokens(safe_dumps(messages))
|
||||
except Exception:
|
||||
return "{}"
|
||||
return "{}"
|
||||
|
|
@ -1233,6 +1234,10 @@ def _is_spend_log_content(parent: Mapping[str, object], key: str) -> bool:
|
|||
)
|
||||
|
||||
|
||||
def _without_tokens(value: str) -> str:
|
||||
return redact_tokens(value, REDACTED_BY_LITELM_STRING)
|
||||
|
||||
|
||||
def _truncated_for_db(value: str, max_length: int) -> str:
|
||||
if len(value) <= max_length:
|
||||
return value
|
||||
|
|
@ -1248,7 +1253,7 @@ def _truncated_for_db(value: str, max_length: int) -> str:
|
|||
|
||||
def _compaction_summary_for_db(summary: str) -> str:
|
||||
return _truncated_for_db(
|
||||
summary, max(_get_max_string_length_prompt_in_db(), MAX_STRING_LENGTH_COMPACTION_SUMMARY_IN_DB)
|
||||
_without_tokens(summary), max(_get_max_string_length_prompt_in_db(), MAX_STRING_LENGTH_COMPACTION_SUMMARY_IN_DB)
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -1277,7 +1282,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.
|
||||
and logprobs. Token-shaped substrings (e.g. a GitHub token in a tool result) are masked everywhere.
|
||||
"""
|
||||
if visited is None:
|
||||
visited = set()
|
||||
|
|
@ -1298,7 +1303,7 @@ def _sanitize_request_body_for_spend_logs_payload(
|
|||
elif isinstance(value, list):
|
||||
return [_sanitize_value(item, mask_credentials) for item in value]
|
||||
elif isinstance(value, str):
|
||||
return _truncated_for_db(value, max_string_length_prompt_in_db)
|
||||
return _truncated_for_db(_without_tokens(value), max_string_length_prompt_in_db)
|
||||
return value
|
||||
|
||||
def _stored_value(key: str, value: object) -> object:
|
||||
|
|
|
|||
|
|
@ -1374,6 +1374,86 @@ def test_get_response_for_spend_logs_payload_strips_null_bytes(mock_should_store
|
|||
assert json.loads(response_json)["content"] == "answerhere"
|
||||
|
||||
|
||||
_GITHUB_TOKEN: Final = "ghp_" + "a1B2c3D4e5" * 3 + "F6g7H8"
|
||||
_TOKEN_SHAPED_SECRETS: Final = (
|
||||
_GITHUB_TOKEN,
|
||||
"github_pat_" + "A1b2C3d4E5" * 8 + "Z9",
|
||||
"sk-ant-api03-" + "Qw3rTy" * 7,
|
||||
"AKIAIOSFODNN7EXAMPLE",
|
||||
"xoxb-1234567890-0987654321-AbCdEfGhIjKl",
|
||||
"glpat-AbCdEfGhIj1234567890",
|
||||
"hf_" + "AbCdEfGhIj" * 3 + "KlMn",
|
||||
"eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U",
|
||||
"-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEA\n-----END RSA PRIVATE KEY-----",
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("secret", _TOKEN_SHAPED_SECRETS)
|
||||
@patch("litellm.proxy.spend_tracking.spend_tracking_utils.should_store_prompts_and_responses_in_spend_logs")
|
||||
def test_stored_request_masks_a_token_returned_in_an_anthropic_tool_result(mock_should_store, secret: str):
|
||||
mock_should_store.return_value = True
|
||||
tool_output: Final = json.dumps({"stdout": f"export TOKEN={secret}\ndone"})
|
||||
request_body: Final = {
|
||||
"model": "claude-sonnet-5-5",
|
||||
"messages": [
|
||||
{"role": "user", "content": [{"type": "tool_result", "tool_use_id": "toolu_1", "content": tool_output}]}
|
||||
],
|
||||
}
|
||||
|
||||
stored: Final = json.dumps(_sanitize_request_body_for_spend_logs_payload(request_body))
|
||||
|
||||
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"
|
||||
]
|
||||
|
||||
|
||||
@patch("litellm.proxy.spend_tracking.spend_tracking_utils.should_store_prompts_and_responses_in_spend_logs")
|
||||
def test_stored_response_masks_a_token_in_a_tool_call_argument(mock_should_store):
|
||||
mock_should_store.return_value = True
|
||||
payload: Final = cast(
|
||||
StandardLoggingPayload,
|
||||
{
|
||||
"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"}},
|
||||
]
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
stored: Final = json.loads(_get_response_for_spend_logs_payload(payload))
|
||||
|
||||
assert stored["content"][0]["text"] == "Cloning now"
|
||||
assert stored["content"][1]["input"]["cmd"] == "git clone https://REDACTED_BY_LITELM@github.com/o/r"
|
||||
|
||||
|
||||
def test_stored_request_masks_a_token_kept_in_the_head_of_a_truncated_string():
|
||||
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)
|
||||
|
||||
assert stored["text"].startswith("REDACTED_BY_LITELM x")
|
||||
assert LITELLM_TRUNCATED_PAYLOAD_FIELD in stored["text"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"text",
|
||||
(
|
||||
"Use basic auth with password: hunter2 and a Bearer header",
|
||||
"The task-management-system and risk-assessment-framework docs",
|
||||
"See https://github.com/BerriAI/litellm and ghp_short",
|
||||
"A" * 5000,
|
||||
),
|
||||
)
|
||||
def test_stored_request_keeps_text_that_only_resembles_credentials(text: str):
|
||||
stored: Final = _sanitize_request_body_for_spend_logs_payload({"text": text}, max_string_length_prompt_in_db=10_000)
|
||||
|
||||
assert stored["text"] == text
|
||||
|
||||
|
||||
@patch("litellm.proxy.spend_tracking.spend_tracking_utils.should_store_prompts_and_responses_in_spend_logs")
|
||||
def test_get_response_for_spend_logs_payload_truncates_large_embedding(
|
||||
mock_should_store,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue