From f5bb701fa270f5c801f1572e1478b56f292d8dfc Mon Sep 17 00:00:00 2001 From: Omkar Shewale <84273555+omkar-droid@users.noreply.github.com> Date: Mon, 3 Aug 2026 00:33:14 -0500 Subject: [PATCH] [Bugfix][Frontend] Constrain Anthropic cache_salt to non-empty (#50764) Signed-off-by: Omkar Shewale Signed-off-by: Andreas Karatzas Co-authored-by: Andreas Karatzas Co-authored-by: Andreas Karatzas --- .../test_anthropic_messages_conversion.py | 48 +++++++++++++++++++ vllm/entrypoints/anthropic/protocol.py | 1 + 2 files changed, 49 insertions(+) diff --git a/tests/entrypoints/anthropic/test_anthropic_messages_conversion.py b/tests/entrypoints/anthropic/test_anthropic_messages_conversion.py index 9e98865057..db792f8c73 100644 --- a/tests/entrypoints/anthropic/test_anthropic_messages_conversion.py +++ b/tests/entrypoints/anthropic/test_anthropic_messages_conversion.py @@ -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() diff --git a/vllm/entrypoints/anthropic/protocol.py b/vllm/entrypoints/anthropic/protocol.py index a67845bb43..14c3e4aca7 100644 --- a/vllm/entrypoints/anthropic/protocol.py +++ b/vllm/entrypoints/anthropic/protocol.py @@ -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 "