mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
fix(openrouter): enforce https transport and prevent structured voice bypass
This commit is contained in:
parent
508ba3cbc2
commit
bcb64f4abe
5 changed files with 208 additions and 5 deletions
|
|
@ -32,7 +32,7 @@ class OpenrouterTextToSpeechConfig(BaseTextToSpeechConfig):
|
|||
drop_params: bool = False,
|
||||
kwargs: dict | None = None, # mutable-ok: override abstract method signature
|
||||
) -> tuple[str | None, dict]: # mutable-ok: override abstract method signature
|
||||
mapped_voice: Final[str | None] = str(voice) if voice is not None else None
|
||||
mapped_voice: Final[str | None] = voice if isinstance(voice, str) else None
|
||||
return mapped_voice, optional_params
|
||||
|
||||
def validate_environment(
|
||||
|
|
@ -47,11 +47,20 @@ class OpenrouterTextToSpeechConfig(BaseTextToSpeechConfig):
|
|||
def is_trusted_api_base(self, api_base: str | None) -> bool:
|
||||
if not api_base:
|
||||
return True
|
||||
configured_base: Final = get_secret_str("OPENROUTER_API_BASE") or litellm.api_base or self.DEFAULT_BASE_URL
|
||||
if api_base.rstrip("/") == configured_base.rstrip("/"):
|
||||
return True
|
||||
parsed: Final = urlparse(api_base)
|
||||
return parsed.netloc == "openrouter.ai" or parsed.netloc.endswith(".openrouter.ai")
|
||||
scheme: Final = parsed.scheme.lower()
|
||||
if scheme != "https":
|
||||
configured_base: Final = get_secret_str("OPENROUTER_API_BASE") or litellm.api_base
|
||||
return bool(
|
||||
configured_base is not None
|
||||
and api_base.rstrip("/") == configured_base.rstrip("/")
|
||||
and parsed.hostname in ("localhost", "127.0.0.1")
|
||||
)
|
||||
configured_base: Final = get_secret_str("OPENROUTER_API_BASE") or litellm.api_base
|
||||
if configured_base is not None and api_base.rstrip("/") == configured_base.rstrip("/"):
|
||||
return True
|
||||
hostname: Final = parsed.hostname or ""
|
||||
return hostname == "openrouter.ai" or hostname.endswith(".openrouter.ai")
|
||||
|
||||
def resolve_api_base_and_key(
|
||||
self,
|
||||
|
|
|
|||
|
|
@ -809,3 +809,39 @@ def test_openrouter_speech_custom_api_base_without_key_does_not_leak_server_key(
|
|||
assert kwargs["api_base"] == "https://untrusted-external-proxy.com/v1"
|
||||
assert kwargs["api_key"] is None
|
||||
|
||||
|
||||
def test_openrouter_speech_http_api_base_does_not_leak_server_key(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
):
|
||||
monkeypatch.setenv("OPENROUTER_API_KEY", "sk-or-secret-server-key")
|
||||
with patch(
|
||||
"litellm.main.openai_chat_completions.audio_speech"
|
||||
) as mock_audio_speech:
|
||||
from litellm.types.llms.openai import HttpxBinaryResponseContent
|
||||
|
||||
mock_audio_speech.return_value = MagicMock(spec=HttpxBinaryResponseContent)
|
||||
|
||||
litellm.speech(
|
||||
model="google/gemini-3.1-flash-tts-preview",
|
||||
custom_llm_provider="openrouter",
|
||||
voice="alloy",
|
||||
input="custom config speech without key over cleartext http",
|
||||
api_base="http://openrouter.ai/v1",
|
||||
)
|
||||
|
||||
mock_audio_speech.assert_called_once()
|
||||
_, kwargs = mock_audio_speech.call_args
|
||||
assert kwargs["model"] == "google/gemini-3.1-flash-tts-preview"
|
||||
assert kwargs["api_base"] == "http://openrouter.ai/v1"
|
||||
assert kwargs["api_key"] is None
|
||||
|
||||
|
||||
def test_openrouter_speech_structured_voice_rejected():
|
||||
with pytest.raises(litellm.BadRequestError):
|
||||
litellm.speech(
|
||||
model="openrouter/google/gemini-3.1-flash-tts-preview",
|
||||
voice={"voice_id": "alloy"}, # pyright: ignore[reportArgumentType] # test invalid voice type
|
||||
input="testing structured voice rejection",
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
1
tests/test_litellm/llms/openrouter/__init__.py
Normal file
1
tests/test_litellm/llms/openrouter/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
"""OpenRouter unit tests."""
|
||||
|
|
@ -0,0 +1 @@
|
|||
"""OpenRouter text-to-speech unit tests."""
|
||||
|
|
@ -0,0 +1,156 @@
|
|||
from typing import Final
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
import litellm
|
||||
from litellm.llms.openrouter.text_to_speech.transformation import OpenrouterTextToSpeechConfig
|
||||
from litellm.types.llms.openai import HttpxBinaryResponseContent
|
||||
from litellm.utils import ProviderConfigManager
|
||||
|
||||
|
||||
def test_openrouter_text_to_speech_registered():
|
||||
config: Final = ProviderConfigManager.get_provider_text_to_speech_config(
|
||||
model="google/gemini-3.1-flash-tts-preview",
|
||||
provider=litellm.LlmProviders.OPENROUTER,
|
||||
)
|
||||
assert isinstance(config, OpenrouterTextToSpeechConfig)
|
||||
|
||||
|
||||
def test_openrouter_text_to_speech_supported_params():
|
||||
config: Final = OpenrouterTextToSpeechConfig()
|
||||
params: Final = config.get_supported_openai_params("any-model")
|
||||
assert "voice" in params
|
||||
assert "response_format" in params
|
||||
assert "speed" in params
|
||||
assert "instructions" in params
|
||||
|
||||
|
||||
def test_openrouter_text_to_speech_map_params_voice():
|
||||
config: Final = OpenrouterTextToSpeechConfig()
|
||||
mapped_voice, _ = config.map_openai_params(
|
||||
model="any-model",
|
||||
optional_params={},
|
||||
voice="alloy",
|
||||
)
|
||||
assert mapped_voice == "alloy"
|
||||
|
||||
structured_voice, _ = config.map_openai_params(
|
||||
model="any-model",
|
||||
optional_params={},
|
||||
voice={"voice_id": "alloy"},
|
||||
)
|
||||
assert structured_voice is None
|
||||
|
||||
|
||||
def test_openrouter_text_to_speech_trusted_api_base():
|
||||
config: Final = OpenrouterTextToSpeechConfig()
|
||||
assert config.is_trusted_api_base(None) is True
|
||||
assert config.is_trusted_api_base("https://openrouter.ai/api/v1") is True
|
||||
assert config.is_trusted_api_base("https://subdomain.openrouter.ai/api/v1") is True
|
||||
assert config.is_trusted_api_base("http://openrouter.ai/api/v1") is False
|
||||
assert config.is_trusted_api_base("https://untrusted-domain.com/v1") is False
|
||||
|
||||
|
||||
def test_openrouter_text_to_speech_resolve_credentials_isolation(monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.setenv("OPENROUTER_API_KEY", "sk-or-server-key")
|
||||
config: Final = OpenrouterTextToSpeechConfig()
|
||||
|
||||
base_trusted, key_trusted = config.resolve_api_base_and_key()
|
||||
assert base_trusted == "https://openrouter.ai/api/v1"
|
||||
assert key_trusted == "sk-or-server-key"
|
||||
|
||||
base_untrusted, key_untrusted = config.resolve_api_base_and_key(api_base="https://untrusted-host.com/v1")
|
||||
assert base_untrusted == "https://untrusted-host.com/v1"
|
||||
assert key_untrusted is None
|
||||
|
||||
base_http, key_http = config.resolve_api_base_and_key(api_base="http://openrouter.ai/api/v1")
|
||||
assert base_http == "http://openrouter.ai/api/v1"
|
||||
assert key_http is None
|
||||
|
||||
|
||||
def test_openrouter_text_to_speech_url_and_transforms():
|
||||
config: Final = OpenrouterTextToSpeechConfig()
|
||||
url: Final = config.get_complete_url(
|
||||
model="google/gemini-3.1-flash-tts-preview",
|
||||
api_base=None,
|
||||
litellm_params={},
|
||||
)
|
||||
assert url == "https://openrouter.ai/api/v1/audio/speech"
|
||||
|
||||
request_data: Final = config.transform_text_to_speech_request(
|
||||
model="google/gemini-3.1-flash-tts-preview",
|
||||
input="hello world",
|
||||
voice="alloy",
|
||||
optional_params={"speed": 1.2},
|
||||
litellm_params={},
|
||||
headers={"custom-header": "value"},
|
||||
)
|
||||
assert request_data["dict_body"]["model"] == "google/gemini-3.1-flash-tts-preview"
|
||||
assert request_data["dict_body"]["input"] == "hello world"
|
||||
assert request_data["dict_body"]["voice"] == "alloy"
|
||||
assert request_data["dict_body"]["speed"] == 1.2
|
||||
assert request_data["headers"]["custom-header"] == "value"
|
||||
|
||||
raw_response: Final = httpx.Response(status_code=200, content=b"audio-bytes")
|
||||
wrapped: Final = config.transform_text_to_speech_response(
|
||||
model="google/gemini-3.1-flash-tts-preview",
|
||||
raw_response=raw_response,
|
||||
logging_obj=MagicMock(),
|
||||
)
|
||||
assert isinstance(wrapped, HttpxBinaryResponseContent)
|
||||
|
||||
|
||||
def test_litellm_speech_openrouter_full_routing(monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.setenv("OPENROUTER_API_KEY", "sk-or-test-key")
|
||||
with patch("litellm.main.openai_chat_completions.audio_speech") as mock_audio:
|
||||
mock_audio.return_value = MagicMock(spec=HttpxBinaryResponseContent)
|
||||
|
||||
litellm.speech(
|
||||
model="openrouter/google/gemini-3.1-flash-tts-preview",
|
||||
voice="alloy",
|
||||
input="testing speech",
|
||||
)
|
||||
|
||||
mock_audio.assert_called_once()
|
||||
_, kwargs = mock_audio.call_args
|
||||
assert kwargs["model"] == "google/gemini-3.1-flash-tts-preview"
|
||||
assert kwargs["api_base"] == "https://openrouter.ai/api/v1"
|
||||
assert kwargs["api_key"] == "sk-or-test-key"
|
||||
assert kwargs["voice"] == "alloy"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_litellm_aspeech_openrouter_full_routing(monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.setenv("OR_API_KEY", "sk-or-alt-key")
|
||||
monkeypatch.delenv("OPENROUTER_API_KEY", raising=False)
|
||||
with patch("litellm.main.openai_chat_completions.audio_speech") as mock_audio:
|
||||
mock_response: Final = MagicMock(spec=HttpxBinaryResponseContent)
|
||||
|
||||
async def _coro(*args, **kwargs):
|
||||
return mock_response
|
||||
|
||||
mock_audio.side_effect = _coro
|
||||
|
||||
await litellm.aspeech(
|
||||
model="openrouter/google/gemini-3.1-flash-tts-preview",
|
||||
voice="alloy",
|
||||
input="testing async speech",
|
||||
)
|
||||
|
||||
mock_audio.assert_called_once()
|
||||
_, kwargs = mock_audio.call_args
|
||||
assert kwargs["model"] == "google/gemini-3.1-flash-tts-preview"
|
||||
assert kwargs["api_base"] == "https://openrouter.ai/api/v1"
|
||||
assert kwargs["api_key"] == "sk-or-alt-key"
|
||||
assert kwargs["aspeech"] is True
|
||||
|
||||
|
||||
def test_litellm_speech_openrouter_rejects_dict_voice():
|
||||
with pytest.raises(litellm.BadRequestError):
|
||||
litellm.speech(
|
||||
model="openrouter/google/gemini-3.1-flash-tts-preview",
|
||||
voice={"voice_id": "alloy"}, # pyright: ignore[reportArgumentType] # test invalid voice input
|
||||
input="testing structured voice rejection",
|
||||
)
|
||||
Loading…
Add table
Reference in a new issue