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.
This commit is contained in:
Nguyen Anh Binh 2026-09-14 09:02:33 +07:00
parent 8a54ce569c
commit 75d5c64293
3 changed files with 37 additions and 3 deletions

View file

@ -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)

View file

@ -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(

View file

@ -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)