From 4800024b98f70c9b3022c27b41e63a5dc9556ed7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20P=C3=A9rez=20de=20Algaba?= <124347725+jperezdealgaba@users.noreply.github.com> Date: Tue, 22 Sep 2026 07:50:23 +0200 Subject: [PATCH] [Security] Reject min_tokens that exceeds the filled max_tokens default (#57731) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Juan Pérez de Algaba --- .../token_in_token_out/test_serving_tokens.py | 23 +++++++++++++ tests/test_request_input_bounds.py | 34 +++++++++++++++++++ vllm/v1/engine/input_processor.py | 7 ++++ 3 files changed, 64 insertions(+) diff --git a/tests/entrypoints/scale_out/token_in_token_out/test_serving_tokens.py b/tests/entrypoints/scale_out/token_in_token_out/test_serving_tokens.py index aa90618a90..fe41e651be 100644 --- a/tests/entrypoints/scale_out/token_in_token_out/test_serving_tokens.py +++ b/tests/entrypoints/scale_out/token_in_token_out/test_serving_tokens.py @@ -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, diff --git a/tests/test_request_input_bounds.py b/tests/test_request_input_bounds.py index 990f10cfeb..3c3999f23e 100644 --- a/tests/test_request_input_bounds.py +++ b/tests/test_request_input_bounds.py @@ -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 --------- diff --git a/vllm/v1/engine/input_processor.py b/vllm/v1/engine/input_processor.py index 582e1abac6..e12b8b90ac 100644 --- a/vllm/v1/engine/input_processor.py +++ b/vllm/v1/engine/input_processor.py @@ -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,