diff --git a/litellm/proxy/management_endpoints/common_utils.py b/litellm/proxy/management_endpoints/common_utils.py index 983c3d24513..fd13d0c5c45 100644 --- a/litellm/proxy/management_endpoints/common_utils.py +++ b/litellm/proxy/management_endpoints/common_utils.py @@ -25,6 +25,21 @@ def validate_finite_spend(spend: float | None) -> None: ) +def validate_no_null_bytes(params: Mapping[str, str | None]) -> None: + """Reject null bytes in string params before they reach the DB. + + Postgres rejects strings containing ``\\x00`` outright, and the raw + driver error used to reach management endpoint clients in a 500 body, + leaking database internals (issue #46076). + """ + for name, value in params.items(): + if value is not None and "\x00" in value: + raise HTTPException( + status_code=400, + detail={"error": f"{name} must not contain null bytes"}, + ) + + def validate_budget_duration(budget_duration: str | None, status_code: int = 400) -> None: """Reject budget durations that can't be parsed, are non-positive, or overflow date math, so a bad value can't be persisted and later crash the diff --git a/litellm/proxy/management_endpoints/key_management_endpoints.py b/litellm/proxy/management_endpoints/key_management_endpoints.py index 0ad4258fb04..f934ccaa008 100644 --- a/litellm/proxy/management_endpoints/key_management_endpoints.py +++ b/litellm/proxy/management_endpoints/key_management_endpoints.py @@ -105,6 +105,7 @@ from litellm.proxy.management_endpoints.common_utils import ( # noqa: F401 # l user_api_key_has_admin_view, validate_budget_duration, validate_finite_spend, + validate_no_null_bytes, ) from litellm.proxy.management_endpoints.key_budget_change import ( KeyBudgetAdminOnly, @@ -4775,6 +4776,8 @@ async def info_key_fn_v2( ) # Resolve key_aliases to tokens so we never pass token=None (unbounded query) tokens_to_query: Final = list(data.keys) if data.keys else [] + for token in tokens_to_query: + validate_no_null_bytes({"keys": token}) if data.key_aliases: alias_rows: Final = await _prisma_table(VerificationTokenRepository(prisma_client)).find_many( where={"key_alias": {"in": data.key_aliases}}, @@ -4898,6 +4901,8 @@ async def info_key_fn( prisma_client, ) + validate_no_null_bytes({"key": key}) + try: if prisma_client is None: raise Exception( diff --git a/litellm/proxy/spend_tracking/spend_management_endpoints.py b/litellm/proxy/spend_tracking/spend_management_endpoints.py index 3c65fc0bf06..301e53748dd 100644 --- a/litellm/proxy/spend_tracking/spend_management_endpoints.py +++ b/litellm/proxy/spend_tracking/spend_management_endpoints.py @@ -51,6 +51,7 @@ from litellm.proxy.auth.authorization_dependencies import ( ) from litellm.proxy.auth.user_api_key_auth import user_api_key_auth from litellm.proxy.litellm_pre_call_utils import LiteLLMProxyRequestSetup +from litellm.proxy.management_endpoints.common_utils import validate_no_null_bytes from litellm.proxy.spend_tracking.spend_capture_rate import ( ProviderBillingCredentialMissing, ProviderBillingRequestFailed, @@ -3562,6 +3563,16 @@ async def view_spend_logs( """ from litellm.proxy.proxy_server import prisma_client + validate_no_null_bytes( + { + "api_key": api_key, + "user_id": user_id, + "request_id": request_id, + "start_date": start_date, + "end_date": end_date, + } + ) + if ( user_api_key_dict.user_role == LitellmUserRoles.INTERNAL_USER or user_api_key_dict.user_role == LitellmUserRoles.INTERNAL_USER_VIEW_ONLY @@ -3684,8 +3695,9 @@ async def view_spend_logs( ) elif isinstance(e, ProxyException): raise e + verbose_proxy_logger.exception("Unhandled error in /spend/logs") raise ProxyException( - message="/spend/logs Error" + str(e), + message="Internal server error from /spend/logs. Details are in the proxy server logs.", type="internal_error", param=getattr(e, "param", "None"), code=status.HTTP_500_INTERNAL_SERVER_ERROR, diff --git a/osv-scanner.toml b/osv-scanner.toml index 33e2f614edc..9ea1fe14ddf 100644 --- a/osv-scanner.toml +++ b/osv-scanner.toml @@ -8,15 +8,25 @@ id = "GHSA-v4x8-3wr5-3wq8" ignoreUntil = 2026-11-20 reason = "the only fixed filelock is 4.2.1, a new major that requires Python >= 3.11 while litellm still supports 3.10, and the advisory is in ReadWriteLock, which litellm never uses (only FileLock); bump filelock and remove this entry once 3.10 is dropped or a 3.x fix ships" +[[IgnoredVulns]] +id = "GHSA-567q-5qvw-pf92" +ignoreUntil = 2026-11-20 +reason = "Windows-only local DoS (CVSS 2.5) in filelock soft locks, fixed only in 4.2.1, a new major that requires Python >= 3.11 while litellm still supports 3.10 and pins filelock<4.0; litellm only uses FileLock, which takes an OS lock rather than a soft lock; bump filelock and remove this entry once 3.10 is dropped or a 3.x fix ships" + +[[IgnoredVulns]] +id = "GHSA-9vhv-jxww-xfm5" +ignoreUntil = 2026-11-20 +reason = "Windows-only local DoS (CVSS 3.3) in filelock ReadWriteLock, fixed only in 4.2.1, a new major that requires Python >= 3.11 while litellm still supports 3.10 and pins filelock<4.0; litellm never uses ReadWriteLock (only FileLock); bump filelock and remove this entry once 3.10 is dropped or a 3.x fix ships" + [[IgnoredVulns]] id = "GHSA-vfj7-8cjw-p6xm" ignoreUntil = 2026-11-03 reason = "braces has no fixed release published (3.0.3, 2024-05-21, is latest and last_affected); dev-only via knip > fast-glob > micromatch; remove this entry once one exists" [[IgnoredVulns]] -id = "GHSA-6q48-6c7w-9gw2" -ignoreUntil = 2026-10-23 -reason = "certifi 2026.10.19 (published 2026-10-20) is inside the 3-day uv exclude-newer window until 2026-10-23 02:24 UTC; bump certifi and remove this entry once it clears" +id = "GHSA-xcx6-jx8p-x3h5" +ignoreUntil = 2026-11-24 +reason = "@azure/msal-node is fixed only in 7.1.0 (published 2026-10-13), but it is dev-only in vscode-extension via @vscode/vsce > @azure/identity, and the latest @azure/identity (4.14.0) still requires msal-node ^6.0.0, so the lock cannot move without forcing a major override; the advisory is in the auth_code flow, which extension packaging does not run; bump and remove this entry once @azure/identity accepts msal-node >= 7.1.0" [[PackageOverrides]] name = "pillow" diff --git a/tests/unit/proxy/management_endpoints/test_key_management_endpoints.py b/tests/unit/proxy/management_endpoints/test_key_management_endpoints.py index d6ee9b1d29e..9b44ce9299a 100644 --- a/tests/unit/proxy/management_endpoints/test_key_management_endpoints.py +++ b/tests/unit/proxy/management_endpoints/test_key_management_endpoints.py @@ -23182,6 +23182,27 @@ async def test_key_generate_reads_the_team_from_the_writer_database_not_the_cach assert raised.value.message == "Team not found for team_id=1234. Non-admin users cannot create keys for non-existent teams." +def test_key_info_null_byte_key_rejected(): + """Regression test for https://github.com/BerriAI/litellm/issues/46076 + + A null byte in the key query param used to reach Prisma, whose raw + driver error came back to the caller in the 500 body. It must be + rejected up front with a 400 that carries no database internals. + """ + from litellm.proxy.auth.user_api_key_auth import user_api_key_auth + + app.dependency_overrides[user_api_key_auth] = lambda: UserAPIKeyAuth( + user_role=LitellmUserRoles.PROXY_ADMIN, api_key="sk-test", user_id="1234" + ) + try: + response = client.get("/key/info", params={"key": "test\x00"}) + + assert response.status_code == 400 + assert "null bytes" in response.text + finally: + app.dependency_overrides.pop(user_api_key_auth, None) + + def test_key_info_unexpected_database_exception_sanitized( monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, @@ -23304,6 +23325,3 @@ def test_key_info_database_not_connected_sanitized_500( assert "Database not connected" in caplog.text finally: proxy_server.app.dependency_overrides.pop(user_api_key_auth, None) - - - diff --git a/tests/unit/proxy/spend_tracking/test_spend_management_endpoints.py b/tests/unit/proxy/spend_tracking/test_spend_management_endpoints.py index 8d53817f51e..5cc582ca775 100644 --- a/tests/unit/proxy/spend_tracking/test_spend_management_endpoints.py +++ b/tests/unit/proxy/spend_tracking/test_spend_management_endpoints.py @@ -8268,3 +8268,60 @@ async def test_global_spend_refresh_runs_the_refresh_on_the_writer_when_a_reader assert [statement for statement in journal.statements if statement[1].startswith("REFRESH")] == [ ("writer", 'REFRESH MATERIALIZED VIEW "MonthlyGlobalSpend";') ] + + +def test_view_spend_logs_null_byte_api_key_rejected(client): + """Regression test for https://github.com/BerriAI/litellm/issues/46076 + + A null byte in the api_key query param used to reach Prisma, whose raw + driver error came back to the caller in the 500 body. It must be + rejected up front with a 400 that carries no database internals. + """ + app.dependency_overrides[ps.user_api_key_auth] = lambda: UserAPIKeyAuth( + user_role=LitellmUserRoles.PROXY_ADMIN + ) + try: + response = client.get("/spend/logs", params={"api_key": "test\x00"}) + + assert response.status_code == 400 + assert "null bytes" in response.text + finally: + app.dependency_overrides.pop(ps.user_api_key_auth, None) + + +def test_view_spend_logs_db_error_returns_generic_message(client, monkeypatch): + """Regression test for https://github.com/BerriAI/litellm/issues/46076 + + When the database does fail under /spend/logs, the raw driver error + must stay in the server logs and the 500 body must carry a generic + message instead of database internals. + """ + + class RaisingDB: + def __init__(self): + self.litellm_spendlogs = self + + async def find_many(self, *args, **kwargs): + raise Exception('Postgres rejected "test": invalid byte sequence for encoding "UTF8"') + + async def query_raw(self, *args, **kwargs): + raise Exception('Postgres rejected "test": invalid byte sequence for encoding "UTF8"') + + class RaisingPrismaClient: + def __init__(self): + self.db = RaisingDB() + + monkeypatch.setattr( + "litellm.proxy.proxy_server.prisma_client", RaisingPrismaClient() + ) + app.dependency_overrides[ps.user_api_key_auth] = lambda: UserAPIKeyAuth( + user_role=LitellmUserRoles.PROXY_ADMIN + ) + try: + response = client.get("/spend/logs", params={"api_key": "sk-normal"}) + + assert response.status_code == 500 + assert "Postgres" not in response.text + assert "server logs" in response.text + finally: + app.dependency_overrides.pop(ps.user_api_key_auth, None)