[Bugfix][Frontend] Constrain Anthropic cache_salt to non-empty (#50764)
Signed-off-by: Omkar Shewale <omkarshewale2001@gmail.com> Signed-off-by: Andreas Karatzas <Andreas.Karatzas@amd.com> Co-authored-by: Andreas Karatzas <akaratza@amd.com> Co-authored-by: Andreas Karatzas <Andreas.Karatzas@amd.com>
This commit is contained in:
co-authored by
Andreas Karatzas
Andreas Karatzas
parent
5e35a6f4f9
commit
f5bb701fa2
@@ -13,10 +13,16 @@ Also covers cache usage computation in ``_build_anthropic_usage``.
|
||||
"""
|
||||
|
||||
import json
|
||||
from argparse import Namespace
|
||||
from http import HTTPStatus
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
from fastapi import FastAPI
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from vllm.entrypoints.anthropic.api_router import attach_router
|
||||
from vllm.entrypoints.anthropic.protocol import (
|
||||
AnthropicMessagesRequest,
|
||||
)
|
||||
@@ -38,6 +44,7 @@ from vllm.entrypoints.openai.engine.protocol import (
|
||||
PromptTokenUsageInfo,
|
||||
UsageInfo,
|
||||
)
|
||||
from vllm.entrypoints.serve.utils.server_utils import validation_exception_handler
|
||||
|
||||
_convert = AnthropicServingMessages._convert_anthropic_to_openai_request
|
||||
_img_url = AnthropicServingMessages._convert_image_source_to_url
|
||||
@@ -1408,3 +1415,44 @@ class TestCacheSalt:
|
||||
request = _make_request([{"role": "user", "content": "Hello"}])
|
||||
result = _convert(request)
|
||||
assert result.cache_salt is None
|
||||
|
||||
@staticmethod
|
||||
def _make_api_app():
|
||||
app = FastAPI()
|
||||
attach_router(app)
|
||||
app.state.args = Namespace(log_error_stack=False)
|
||||
app.exception_handler(RequestValidationError)(validation_exception_handler)
|
||||
|
||||
handler = MagicMock(spec=AnthropicServingMessages)
|
||||
handler.create_messages.side_effect = AssertionError(
|
||||
"invalid requests must not reach the serving handler"
|
||||
)
|
||||
app.state.anthropic_serving_messages = handler
|
||||
return app, handler
|
||||
|
||||
def test_cache_salt_openapi_requires_non_empty_string(self):
|
||||
app, _ = self._make_api_app()
|
||||
field_schema = app.openapi()["components"]["schemas"][
|
||||
"AnthropicMessagesRequest"
|
||||
]["properties"]["cache_salt"]
|
||||
string_schema = next(
|
||||
option for option in field_schema["anyOf"] if option.get("type") == "string"
|
||||
)
|
||||
|
||||
assert string_schema["minLength"] == 1
|
||||
|
||||
def test_empty_cache_salt_returns_bad_request(self):
|
||||
app, handler = self._make_api_app()
|
||||
with TestClient(app, raise_server_exceptions=False) as client:
|
||||
response = client.post(
|
||||
"/v1/messages",
|
||||
json={
|
||||
"model": "test-model",
|
||||
"max_tokens": 1,
|
||||
"messages": [{"role": "user", "content": "Hello"}],
|
||||
"cache_salt": "",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
handler.create_messages.assert_not_awaited()
|
||||
|
||||
@@ -135,6 +135,7 @@ class AnthropicMessagesRequest(BaseModel):
|
||||
# vLLM-specific fields that are not in Anthropic spec
|
||||
cache_salt: str | None = Field(
|
||||
default=None,
|
||||
min_length=1,
|
||||
description=(
|
||||
"If specified, the prefix cache will be salted with the provided "
|
||||
"string to prevent an attacker to guess prompts in multi-user "
|
||||
|
||||
Reference in New Issue
Block a user