[Security] Reject min_tokens that exceeds the filled max_tokens default (#57731)

Signed-off-by: Juan Pérez de Algaba <jperezde@redhat.com>
This commit is contained in:
Juan Pérez de Algaba
2026-09-22 05:50:23 +00:00
committed by GitHub
parent d50723df04
commit 4800024b98
3 changed files with 64 additions and 0 deletions
@@ -112,6 +112,29 @@ async def test_generate_endpoint(client):
assert data["choices"][0].get("sampling_mask") is None
@pytest.mark.asyncio
async def test_generate_rejects_min_tokens_above_filled_max_tokens(client):
"""Explicit null max_tokens must not skip the min_tokens bound."""
payload = {
"model": MODEL_NAME,
"token_ids": [1, 2, 3],
"sampling_params": {"max_tokens": None, "min_tokens": 2147483648},
"stream": False,
}
resp = await client.post(GEN_ENDPOINT, json=payload)
assert resp.status_code == 400
assert "min_tokens" in resp.json()["error"]["message"]
followup = {
"model": MODEL_NAME,
"token_ids": [1, 2, 3],
"sampling_params": {"max_tokens": 4},
"stream": False,
}
resp = await client.post(GEN_ENDPOINT, json=followup)
resp.raise_for_status()
@pytest.mark.asyncio
@pytest.mark.skipif(
envs.VLLM_USE_RUST_FRONTEND,
+34
View File
@@ -61,6 +61,40 @@ def test_routed_experts_offset_validated_before_engine_submission(offset):
)
def _process_inputs_with_max_model_len(
params: SamplingParams, max_model_len: int = 2048
):
processor = Mock(spec=InputProcessor)
processor.tokenizer = None
processor.generation_config_fields = {}
processor.renderer = Mock()
processor.renderer.get_eos_token_id.return_value = None
processor.vllm_config = Mock()
processor.model_config = Mock()
processor.model_config.max_model_len = max_model_len
prompt = {"type": "token", "prompt_token_ids": [1, 2, 3]}
return InputProcessor.process_inputs(
processor, "req", prompt, params, ("generate",)
)
def test_unset_max_tokens_rejects_oversized_min_tokens():
"""Reject min_tokens that only becomes invalid after max_tokens is filled."""
params = SamplingParams(max_tokens=None, min_tokens=2147483648)
with pytest.raises(
VLLMValidationError,
match="min_tokens must be less than or equal to max_tokens",
):
_process_inputs_with_max_model_len(params)
def test_unset_max_tokens_accepts_min_tokens_within_filled_max():
params = SamplingParams(max_tokens=None, min_tokens=10)
request = _process_inputs_with_max_model_len(params)
assert request.sampling_params.max_tokens == 2045
assert request.sampling_params.min_tokens == 10
# --- Stop strings: public requests cap the number of stop strings ---------
+7
View File
@@ -397,6 +397,13 @@ class InputProcessor:
sampling_params.max_tokens = (
self.model_config.max_model_len - prompt_len
)
# min_tokens is not checked while max_tokens is unset.
if sampling_params.min_tokens > sampling_params.max_tokens:
raise VLLMValidationError(
f"min_tokens must be less than or equal to "
f"max_tokens={sampling_params.max_tokens}, got "
f"{sampling_params.min_tokens}."
)
sampling_params.update_from_generation_config(
self.generation_config_fields,