From 75d5c642936ee38cb404a88ee5c0cd0bd83eb573 Mon Sep 17 00:00:00 2001 From: Nguyen Anh Binh Date: Mon, 14 Sep 2026 09:02:33 +0700 Subject: [PATCH] fix(guardrails): offload DNS validation and sync API types Run URL validation in a worker thread so slow DNS does not block unrelated guardrail requests. Add a concurrency regression and regenerate the two stale API description lines reported by CI. --- .../guardrail_hooks/custom_code/primitives.py | 3 +- .../guardrails/test_custom_code_security.py | 35 +++++++++++++++++++ ui/litellm-dashboard/src/lib/http/schema.d.ts | 2 -- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/litellm/proxy/guardrails/guardrail_hooks/custom_code/primitives.py b/litellm/proxy/guardrails/guardrail_hooks/custom_code/primitives.py index 9c8ea6ec7b8..1af921f8891 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/custom_code/primitives.py +++ b/litellm/proxy/guardrails/guardrail_hooks/custom_code/primitives.py @@ -5,6 +5,7 @@ These functions are injected into the custom code execution environment and provide safe, sandboxed functionality for common guardrail operations. """ +import asyncio import json import re from collections.abc import Mapping, Sequence @@ -514,7 +515,7 @@ async def http_request( request_headers = headers if getattr(litellm, "user_url_validation", True): try: - validated_url, host_header = validate_url(url) + validated_url, host_header = await asyncio.to_thread(validate_url, url) request_headers = {**(headers or {}), "Host": host_header} except (SSRFError, ValueError) as e: verbose_proxy_logger.warning("Custom code http_request SSRF blocked: %s", e) diff --git a/tests/test_litellm/proxy/guardrails/test_custom_code_security.py b/tests/test_litellm/proxy/guardrails/test_custom_code_security.py index 2a227fe0995..0c01ab06747 100644 --- a/tests/test_litellm/proxy/guardrails/test_custom_code_security.py +++ b/tests/test_litellm/proxy/guardrails/test_custom_code_security.py @@ -1,4 +1,6 @@ +import asyncio import socket +import threading from collections.abc import AsyncIterator from typing import Final @@ -314,6 +316,39 @@ def _resolve_to(monkeypatch: pytest.MonkeyPatch, address: str) -> None: monkeypatch.setattr("litellm.litellm_core_utils.url_utils.socket.getaddrinfo", getaddrinfo) +@pytest.mark.asyncio +async def test_slow_dns_does_not_block_other_guardrail_requests( + monkeypatch: pytest.MonkeyPatch, _http_requests: list[httpx.Request] +) -> None: + loop: Final = asyncio.get_running_loop() + dns_started: Final = asyncio.Event() + release_dns: Final = threading.Event() + + def getaddrinfo(host: str, port: int, *, proto: int) -> list[tuple[int, int, int, str, tuple[str, int]]]: + if host == "slow.example.com": + loop.call_soon_threadsafe(dns_started.set) + release_dns.wait(timeout=5) + return [(socket.AF_INET, socket.SOCK_STREAM, proto, "", ("93.184.216.34", port))] + + monkeypatch.setattr("litellm.litellm_core_utils.url_utils.socket.getaddrinfo", getaddrinfo) + slow_request: Final = asyncio.create_task(primitives.http_get("http://slow.example.com/check")) + try: + await asyncio.wait_for(dns_started.wait(), timeout=5) + assert not slow_request.done() + fast_result: Final = await asyncio.wait_for(primitives.http_get("http://fast.example.com/check"), timeout=5) + assert fast_result["success"] is True + assert not slow_request.done() + assert len(_http_requests) == 1 + assert _http_requests[0].headers["Host"] == "fast.example.com" + finally: + release_dns.set() + slow_result: Final = await asyncio.wait_for(slow_request, timeout=5) + + assert slow_result["success"] is True + assert len(_http_requests) == 2 + assert _http_requests[1].headers["Host"] == "slow.example.com" + + @pytest.mark.asyncio @pytest.mark.parametrize("method", ["GET", "POST", "PUT", "DELETE", "PATCH"]) @pytest.mark.parametrize( diff --git a/ui/litellm-dashboard/src/lib/http/schema.d.ts b/ui/litellm-dashboard/src/lib/http/schema.d.ts index 7eadaa6c991..839aa52fa84 100644 --- a/ui/litellm-dashboard/src/lib/http/schema.d.ts +++ b/ui/litellm-dashboard/src/lib/http/schema.d.ts @@ -16781,7 +16781,6 @@ export interface paths { * - permissions: Optional[dict] - [Not Implemented Yet] User-specific permissions, eg. turning off pii masking. * - metadata: Optional[dict] - Metadata for user, store information for user. Example metadata = {"team": "core-infra", "app": "app2", "email": "ishaan@berri.ai" } * - max_parallel_requests: Optional[int] - Rate limit a user based on the number of parallel requests. Raises 429 error, if user's parallel requests > x. - * - soft_budget: Optional[float] - Get alerts when user crosses given budget, doesn't block requests. * - model_max_budget: Optional[dict] - Model-specific max budget for user. [Docs](https://docs.litellm.ai/docs/proxy/users#add-model-specific-budgets-to-keys) * - budget_fallbacks: Optional[Dict[str, List[str]]] - Per-model fallback chain tried in order when that model's own `model_max_budget` is exceeded, e.g. {"gpt-4o": ["gpt-4o-mini"]}. * - model_rpm_limit: Optional[float] - Model-specific rpm limit for user. [Docs](https://docs.litellm.ai/docs/proxy/users#add-model-specific-limits-to-keys) @@ -16887,7 +16886,6 @@ export interface paths { * - permissions: Optional[dict] - [Not Implemented Yet] User-specific permissions, eg. turning off pii masking. * - metadata: Optional[dict] - Metadata for user, store information for user. Example metadata = {"team": "core-infra", "app": "app2", "email": "ishaan@berri.ai" } * - max_parallel_requests: Optional[int] - Rate limit a user based on the number of parallel requests. Raises 429 error, if user's parallel requests > x. - * - soft_budget: Optional[float] - Get alerts when user crosses given budget, doesn't block requests. * - model_max_budget: Optional[dict] - Model-specific max budget for user. [Docs](https://docs.litellm.ai/docs/proxy/users#add-model-specific-budgets-to-keys) * - budget_fallbacks: Optional[Dict[str, List[str]]] - Per-model fallback chain tried in order when that model's own `model_max_budget` is exceeded, e.g. {"gpt-4o": ["gpt-4o-mini"]}. * - model_rpm_limit: Optional[float] - Model-specific rpm limit for user. [Docs](https://docs.litellm.ai/docs/proxy/users#add-model-specific-limits-to-keys)