mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(proxy): keep auth_error on proxy-raised 401/403 route failures and ban the string None as an error type or param
This commit is contained in:
parent
8db3643411
commit
ed2c5c0bdc
8 changed files with 156 additions and 8 deletions
6
.github/workflows/test-linting.yml
vendored
6
.github/workflows/test-linting.yml
vendored
|
|
@ -630,6 +630,12 @@ jobs:
|
|||
- name: check_migrations_lock_timeout
|
||||
run: uv run --no-sync python ./tests/code_coverage_tests/check_migrations_lock_timeout.py
|
||||
|
||||
- name: check_no_none_string_error_fields
|
||||
run: uv run --no-sync python ./tests/code_coverage_tests/check_no_none_string_error_fields.py
|
||||
|
||||
- name: test_check_no_none_string_error_fields
|
||||
run: uv run --no-sync pytest -q --noconftest -p no:cacheprovider -c /dev/null tests/code_coverage_tests/test_check_no_none_string_error_fields.py
|
||||
|
||||
- name: check_no_publicly_known_master_key
|
||||
run: uv run --no-sync python ./tests/code_coverage_tests/check_no_publicly_known_master_key.py
|
||||
|
||||
|
|
|
|||
|
|
@ -145,7 +145,7 @@ def route_failure_proxy_exception(
|
|||
answered_status: Final = answered_status_code(message, error_status_code(exc, default_status_code))
|
||||
return ProxyException(
|
||||
message=message,
|
||||
type=openai_error_type(exc, answered_status),
|
||||
type=_catch_all_error_type(exc, answered_status),
|
||||
param=openai_error_param(exc),
|
||||
headers=headers,
|
||||
openai_code=openai_code,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,71 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import ast
|
||||
import sys
|
||||
from collections.abc import Iterator
|
||||
from itertools import chain
|
||||
from pathlib import Path
|
||||
from typing import Final
|
||||
|
||||
ERROR_FIELDS: Final = frozenset({"type", "param"})
|
||||
SOURCE_ROOT: Final = Path(__file__).resolve().parents[2] / "litellm"
|
||||
|
||||
|
||||
def _is_none_string(node: ast.expr | None) -> bool:
|
||||
return isinstance(node, ast.Constant) and node.value == "None"
|
||||
|
||||
|
||||
def _names_an_error_field(node: ast.expr | None) -> bool:
|
||||
return isinstance(node, ast.Constant) and node.value in ERROR_FIELDS
|
||||
|
||||
|
||||
def _keyword_defaults_to_none_string(call: ast.Call) -> bool:
|
||||
return any(keyword.arg in ERROR_FIELDS and _is_none_string(keyword.value) for keyword in call.keywords)
|
||||
|
||||
|
||||
def _getattr_defaults_to_none_string(call: ast.Call) -> bool:
|
||||
return (
|
||||
isinstance(call.func, ast.Name)
|
||||
and call.func.id == "getattr"
|
||||
and len(call.args) == 3
|
||||
and _names_an_error_field(call.args[1])
|
||||
and _is_none_string(call.args[2])
|
||||
)
|
||||
|
||||
|
||||
def _dict_holds_none_string(node: ast.Dict) -> bool:
|
||||
return any(_names_an_error_field(key) and _is_none_string(value) for key, value in zip(node.keys, node.values))
|
||||
|
||||
|
||||
def _is_violation(node: ast.AST) -> bool:
|
||||
match node:
|
||||
case ast.Call():
|
||||
return _keyword_defaults_to_none_string(node) or _getattr_defaults_to_none_string(node)
|
||||
case ast.Dict():
|
||||
return _dict_holds_none_string(node)
|
||||
case _:
|
||||
return False
|
||||
|
||||
|
||||
def violations(source: str, filename: str) -> Iterator[str]:
|
||||
return (f"{filename}:{node.lineno}" for node in ast.walk(ast.parse(source, filename=filename)) if _is_violation(node))
|
||||
|
||||
|
||||
def _file_violations(path: Path) -> Iterator[str]:
|
||||
return violations(path.read_text(encoding="utf-8"), str(path.relative_to(SOURCE_ROOT.parent)))
|
||||
|
||||
|
||||
def main() -> int:
|
||||
found: Final = tuple(chain.from_iterable(_file_violations(path) for path in sorted(SOURCE_ROOT.rglob("*.py"))))
|
||||
if not found:
|
||||
return 0
|
||||
print(
|
||||
'An error\'s "type" or "param" is set to the string "None", which OpenAI clients read as a real '
|
||||
"error class or field name. Use a named error type and a real None for param instead:"
|
||||
)
|
||||
print("\n".join(found))
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
import importlib.util
|
||||
from pathlib import Path
|
||||
from typing import Final
|
||||
|
||||
import pytest
|
||||
|
||||
GUARD_PATH: Final = Path(__file__).resolve().parent / "check_no_none_string_error_fields.py"
|
||||
_spec: Final = importlib.util.spec_from_file_location("check_no_none_string_error_fields", GUARD_PATH)
|
||||
assert _spec is not None and _spec.loader is not None
|
||||
guard: Final = importlib.util.module_from_spec(_spec)
|
||||
_spec.loader.exec_module(guard)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"source",
|
||||
[
|
||||
pytest.param('raise ProxyException(message="m", type="None", param=None, code=500)\n', id="type_keyword"),
|
||||
pytest.param('raise ProxyException(message="m", type="x", param="None", code=500)\n', id="param_keyword"),
|
||||
pytest.param('ProxyException(message="m", type=getattr(e, "type", "None"), param=None)\n', id="type_getattr"),
|
||||
pytest.param('ProxyException(message="m", type="x", param=getattr(e, "param", "None"))\n', id="param_getattr"),
|
||||
pytest.param('error = {"message": "m", "type": "x", "param": "None"}\n', id="dict_literal"),
|
||||
],
|
||||
)
|
||||
def test_a_none_string_error_field_is_reported_with_its_line(source: str) -> None:
|
||||
assert tuple(guard.violations("x = 1\n" + source, "proxy.py")) == ("proxy.py:2",)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"source",
|
||||
[
|
||||
pytest.param('ProxyException(message="m", type="auth_error", param=None, code=401)\n', id="real_none_param"),
|
||||
pytest.param('ProxyException(message="None", type="x", param=getattr(e, "param", None))\n', id="none_message"),
|
||||
pytest.param('label = getattr(e, "provider", "None")\n', id="other_attribute"),
|
||||
pytest.param('row = {"model": "None"}\n', id="other_dict_key"),
|
||||
],
|
||||
)
|
||||
def test_a_real_none_or_an_unrelated_none_string_passes(source: str) -> None:
|
||||
assert tuple(guard.violations(source, "proxy.py")) == ()
|
||||
|
|
@ -270,7 +270,7 @@ def test_audio_transcription_over_the_file_size_cap_is_rejected_as_a_bad_request
|
|||
assert error["param"] == "file"
|
||||
|
||||
|
||||
def test_audio_speech_tag_routing_denial_is_typed_as_an_authentication_error(
|
||||
def test_audio_speech_tag_routing_denial_is_typed_as_a_proxy_auth_error(
|
||||
client, auth_as, patched_speech_error, monkeypatch
|
||||
):
|
||||
async def _deny(*args, **kwargs):
|
||||
|
|
@ -281,10 +281,10 @@ def test_audio_speech_tag_routing_denial_is_typed_as_an_authentication_error(
|
|||
response = client.post("/v1/audio/speech", json={"model": "tts-1", "input": "Hi", "voice": "alloy"})
|
||||
assert response.status_code == 401
|
||||
error = response.json()["error"]
|
||||
assert (error["type"], error["param"]) == ("authentication_error", None)
|
||||
assert (error["type"], error["param"]) == ("auth_error", None)
|
||||
|
||||
|
||||
def test_audio_transcription_tag_routing_denial_is_typed_as_an_authentication_error(
|
||||
def test_audio_transcription_tag_routing_denial_is_typed_as_a_proxy_auth_error(
|
||||
client, auth_as, patched_transcription, monkeypatch
|
||||
):
|
||||
async def _deny(*args, **kwargs):
|
||||
|
|
@ -296,7 +296,7 @@ def test_audio_transcription_tag_routing_denial_is_typed_as_an_authentication_er
|
|||
response = client.post("/v1/audio/transcriptions", files=files, data={"model": "whisper-1"})
|
||||
assert response.status_code == 401
|
||||
error = response.json()["error"]
|
||||
assert (error["type"], error["param"]) == ("authentication_error", None)
|
||||
assert (error["type"], error["param"]) == ("auth_error", None)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
|
|
|||
|
|
@ -134,7 +134,7 @@ def test_completion_pipeline_error(client, auth_as, completion_pipeline_raises,
|
|||
pytest.param(
|
||||
ValueError(f"{RouterErrors.no_deployments_with_tag_routing.value}. Passed model=gpt-4o and tags=['x']"),
|
||||
401,
|
||||
"authentication_error",
|
||||
"auth_error",
|
||||
id="tag_routing_denial",
|
||||
),
|
||||
],
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ import litellm
|
|||
import litellm.proxy.proxy_server as ps
|
||||
from litellm.constants import RETURN_RAW_MODEL_NAME_METADATA_KEY
|
||||
from litellm.integrations.custom_logger import CustomLogger
|
||||
from litellm.proxy._types import UserAPIKeyAuth
|
||||
from litellm.proxy._types import ProxyException, UserAPIKeyAuth
|
||||
from litellm.proxy.common_request_processing import create_response
|
||||
from litellm.proxy.proxy_server import (
|
||||
_apply_streaming_chunk_hooks,
|
||||
|
|
@ -946,6 +946,39 @@ def _error_of(frame: str) -> dict[str, object]:
|
|||
return json.loads(frame.removeprefix("data: "))["error"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
"raised, expected_error",
|
||||
[
|
||||
pytest.param(
|
||||
ProxyException(message="Budget has been exceeded", type="budget_exceeded", param=None, code=429),
|
||||
{"message": "Budget has been exceeded", "type": "budget_exceeded", "param": None, "code": "429"},
|
||||
id="proxy_exception_keeps_its_status",
|
||||
),
|
||||
pytest.param(
|
||||
RuntimeError("upstream blew up"),
|
||||
{"message": "upstream blew up", "type": "internal_server_error", "param": None, "code": "500"},
|
||||
id="unexpected_failure",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_a_mid_stream_failure_frame_carries_the_status_the_error_was_raised_with(
|
||||
monkeypatch, raised: Exception, expected_error: dict[str, object]
|
||||
):
|
||||
_patch_logging_flags(monkeypatch)
|
||||
monkeypatch.setattr(ps.proxy_logging_obj, "post_call_failure_hook", AsyncMock(return_value=None))
|
||||
|
||||
frames: Final = [
|
||||
frame
|
||||
async for frame in async_data_generator(
|
||||
response=_async_iter_raises(raised), user_api_key_dict=_user_auth(), request_data={}
|
||||
)
|
||||
]
|
||||
|
||||
error_frames: Final = [frame for frame in frames if isinstance(frame, str) and frame.startswith('data: {"error":')]
|
||||
assert [_error_of(frame.removesuffix("\n\n")) for frame in error_frames] == [expected_error]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
"detail,expected_message,expected_fields",
|
||||
|
|
|
|||
|
|
@ -14262,7 +14262,7 @@ async def test_moderations_reraises_proxy_exception_unwrapped():
|
|||
pytest.param(Exception("provider down"), "internal_server_error", "500", id="bare_exception"),
|
||||
pytest.param(
|
||||
ValueError("Not allowed to access model due to tags configuration. Passed model=m and tags=['x']"),
|
||||
"authentication_error",
|
||||
"auth_error",
|
||||
"401",
|
||||
id="tag_routing_denial",
|
||||
),
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue