mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
fix(proxy): reject passthrough envelope controls on tinyfish route, raise blocking-run timeout
The generic passthrough unwraps a caller-supplied custom_body as the forwarded request and honors a caller stream flag, so custom_body.use_vault bypassed the credentialed-run 403 and stream: true flipped a blocking run into the streaming pipeline. The route now 400s the envelope fields (custom_body, stream, query_params); streaming comes from the endpoint. Blocking runs also get a 1500s default timeout covering the upstream 1200s run cap, unless the operator configured pass_through_request_timeout. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Pxz11TQKYvCXvKawNUa2g4
This commit is contained in:
parent
5ac385572d
commit
fa1f2188d0
3 changed files with 90 additions and 8 deletions
|
|
@ -14,6 +14,7 @@ import json
|
|||
import os
|
||||
import posixpath
|
||||
import re
|
||||
import sys
|
||||
from collections.abc import AsyncGenerator, Callable, Mapping, Sequence
|
||||
from dataclasses import dataclass
|
||||
from functools import partial
|
||||
|
|
@ -102,6 +103,8 @@ from litellm.types.passthrough_endpoints.pass_through_endpoints import (
|
|||
)
|
||||
from litellm.types.passthrough_endpoints.tinyfish import (
|
||||
TINYFISH_AUTHENTICATED_RUN_FIELDS,
|
||||
TINYFISH_PASSTHROUGH_TIMEOUT_SECONDS,
|
||||
TINYFISH_REJECTED_ENVELOPE_FIELDS,
|
||||
is_allowed_tinyfish_endpoint,
|
||||
)
|
||||
from litellm.types.passthrough_endpoints.vertex_ai import VertexPassThroughCredentials
|
||||
|
|
@ -3274,17 +3277,28 @@ async def cursor_proxy_route(
|
|||
return received_value
|
||||
|
||||
|
||||
async def _tinyfish_blocked_body_fields(request: Request) -> tuple[str, ...]:
|
||||
async def _tinyfish_body_field_names(request: Request) -> frozenset[str]:
|
||||
raw_body: Final = await request.body()
|
||||
if not raw_body:
|
||||
return ()
|
||||
return frozenset()
|
||||
try:
|
||||
parsed: Final[object] = json.loads(raw_body) # any-ok: json.loads -> Any
|
||||
except (json.JSONDecodeError, UnicodeDecodeError):
|
||||
return ()
|
||||
return frozenset()
|
||||
if not isinstance(parsed, dict):
|
||||
return ()
|
||||
return tuple(sorted(key for key in parsed if key in TINYFISH_AUTHENTICATED_RUN_FIELDS))
|
||||
return frozenset()
|
||||
return frozenset(parsed)
|
||||
|
||||
|
||||
def _tinyfish_route_timeout() -> float | None:
|
||||
# only raise the 600s default to cover legal 1200s runs; an operator's configured timeout still wins
|
||||
proxy_server: Final = sys.modules.get("litellm.proxy.proxy_server")
|
||||
operator_timeout: Final = (
|
||||
getattr(proxy_server, "general_settings", {}).get("pass_through_request_timeout")
|
||||
if proxy_server is not None
|
||||
else None
|
||||
)
|
||||
return None if operator_timeout is not None else TINYFISH_PASSTHROUGH_TIMEOUT_SECONDS
|
||||
|
||||
|
||||
@router.api_route(
|
||||
|
|
@ -3333,9 +3347,22 @@ async def tinyfish_proxy_route(
|
|||
"GET /v1/runs/{id}, POST /v1/runs/{id}/cancel.",
|
||||
)
|
||||
|
||||
if request.method == "POST" and encoded_endpoint.startswith("/v1/automation/"):
|
||||
blocked_fields: Final = await _tinyfish_blocked_body_fields(request)
|
||||
if blocked_fields and str_to_bool(os.getenv("TINYFISH_ALLOW_AUTHENTICATED_RUNS")) is not True:
|
||||
if request.method == "POST":
|
||||
body_fields: Final = await _tinyfish_body_field_names(request)
|
||||
envelope_fields: Final = tuple(sorted(body_fields & TINYFISH_REJECTED_ENVELOPE_FIELDS))
|
||||
if envelope_fields:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"Request fields [{', '.join(envelope_fields)}] are LiteLLM pass-through envelope controls "
|
||||
"and are not accepted on the TinyFish route. Send the native TinyFish request body; streaming is "
|
||||
"determined by the endpoint.",
|
||||
)
|
||||
blocked_fields: Final = tuple(sorted(body_fields & TINYFISH_AUTHENTICATED_RUN_FIELDS))
|
||||
if (
|
||||
blocked_fields
|
||||
and encoded_endpoint.startswith("/v1/automation/")
|
||||
and str_to_bool(os.getenv("TINYFISH_ALLOW_AUTHENTICATED_RUNS")) is not True
|
||||
):
|
||||
raise HTTPException(
|
||||
status_code=403,
|
||||
detail=f"Request fields [{', '.join(blocked_fields)}] run with the shared TinyFish account's saved "
|
||||
|
|
@ -3364,6 +3391,7 @@ async def tinyfish_proxy_route(
|
|||
target=str(updated_url),
|
||||
custom_headers=MappingProxyType({"X-API-Key": tinyfish_api_key}),
|
||||
custom_llm_provider="tinyfish",
|
||||
timeout=_tinyfish_route_timeout(),
|
||||
)
|
||||
received_value: Final = await endpoint_func(
|
||||
request,
|
||||
|
|
|
|||
|
|
@ -18,6 +18,13 @@ TINYFISH_TERMINAL_RUN_STATUSES: Final = frozenset({"COMPLETED", "FAILED", "CANCE
|
|||
# these fields use the shared account's saved logins/vault, so they 403 unless TINYFISH_ALLOW_AUTHENTICATED_RUNS=true
|
||||
TINYFISH_AUTHENTICATED_RUN_FIELDS: Final = frozenset({"use_profile", "profile_id", "use_vault", "credential_item_ids"})
|
||||
|
||||
# litellm's pass-through envelope controls; rejected here or custom_body smuggles past the field gate and
|
||||
# a caller stream flag flips the billing mode away from what the endpoint dictates
|
||||
TINYFISH_REJECTED_ENVELOPE_FIELDS: Final = frozenset({"custom_body", "stream", "query_params"})
|
||||
|
||||
# covers the upstream 1200s max run duration plus response headroom for blocking runs
|
||||
TINYFISH_PASSTHROUGH_TIMEOUT_SECONDS: Final = 1500.0
|
||||
|
||||
_RUN_SUBMIT_PATHS: Final = frozenset(
|
||||
{("v1", "automation", "run"), ("v1", "automation", "run-async"), ("v1", "automation", "run-sse")}
|
||||
)
|
||||
|
|
|
|||
|
|
@ -7207,3 +7207,50 @@ class TestTinyFishProxyRoute:
|
|||
response = tinyfish_client.get("/tinyfish/v1/runs/run-123")
|
||||
|
||||
assert (response.status_code, response.json()["status"]) == (200, "RUNNING")
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"body",
|
||||
[
|
||||
{"custom_body": {"url": "https://scrapeme.live/shop", "goal": "g", "use_vault": True}},
|
||||
{"url": "https://scrapeme.live/shop", "goal": "g", "stream": True},
|
||||
{"url": "https://scrapeme.live/shop", "goal": "g", "query_params": {"x": "1"}},
|
||||
],
|
||||
)
|
||||
def test_rejects_passthrough_envelope_controls(self, tinyfish_client: TestClient, body: dict) -> None:
|
||||
"""custom_body smuggled vault fields past the 403 gate and a stream flag flipped the
|
||||
billing mode, because the generic passthrough honors both from the caller's body."""
|
||||
with respx.mock as upstream:
|
||||
route = upstream.post("https://agent.tinyfish.ai/v1/automation/run").mock(
|
||||
return_value=httpx.Response(200, json={"run_id": "run-1", "status": "COMPLETED", "num_of_steps": 1})
|
||||
)
|
||||
response = tinyfish_client.post("/tinyfish/v1/automation/run", json=body)
|
||||
|
||||
assert response.status_code == 400
|
||||
assert "envelope" in response.json()["detail"]
|
||||
assert not route.called
|
||||
|
||||
def test_rejects_envelope_stream_on_cancel(self, tinyfish_client: TestClient) -> None:
|
||||
with respx.mock as upstream:
|
||||
route = upstream.post("https://agent.tinyfish.ai/v1/runs/run-1/cancel").mock(
|
||||
return_value=httpx.Response(200, json={"run_id": "run-1", "status": "CANCELLED"})
|
||||
)
|
||||
response = tinyfish_client.post("/tinyfish/v1/runs/run-1/cancel", json={"stream": True})
|
||||
|
||||
assert response.status_code == 400
|
||||
assert not route.called
|
||||
|
||||
|
||||
class TestTinyFishRouteTimeout:
|
||||
def test_default_covers_upstream_run_cap(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
from litellm.proxy import proxy_server
|
||||
from litellm.proxy.pass_through_endpoints.llm_passthrough_endpoints import _tinyfish_route_timeout
|
||||
|
||||
monkeypatch.setattr(proxy_server, "general_settings", {}, raising=False)
|
||||
assert _tinyfish_route_timeout() == 1500.0
|
||||
|
||||
def test_operator_configured_timeout_wins(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
from litellm.proxy import proxy_server
|
||||
from litellm.proxy.pass_through_endpoints.llm_passthrough_endpoints import _tinyfish_route_timeout
|
||||
|
||||
monkeypatch.setattr(proxy_server, "general_settings", {"pass_through_request_timeout": 30}, raising=False)
|
||||
assert _tinyfish_route_timeout() is None
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue