fix(security): bound completion prompt list to prevent unbounded engine fan-out (#47845)
Signed-off-by: jperezde <jperezde@redhat.com>
This commit is contained in:
@@ -474,3 +474,139 @@ def test_negative_prompt_token_ids_flat():
|
||||
prompt=[-1],
|
||||
max_tokens=10,
|
||||
)
|
||||
|
||||
|
||||
class TestCompletionPromptListLimit:
|
||||
"""Regression tests for CVE: unbounded prompt list fan-out."""
|
||||
|
||||
def test_scalar_prompt_allowed(self):
|
||||
request = CompletionRequest(
|
||||
model=MODEL_NAME,
|
||||
prompt="hello",
|
||||
max_tokens=1,
|
||||
)
|
||||
assert request.prompt == "hello"
|
||||
|
||||
def test_single_token_list_allowed(self):
|
||||
request = CompletionRequest(
|
||||
model=MODEL_NAME,
|
||||
prompt=[1, 2, 3],
|
||||
max_tokens=1,
|
||||
)
|
||||
assert request.prompt == [1, 2, 3]
|
||||
|
||||
def test_bounded_text_prompt_list_allowed(self, monkeypatch):
|
||||
monkeypatch.setenv("VLLM_MAX_COMPLETION_PROMPTS", "10")
|
||||
from vllm import envs
|
||||
|
||||
if hasattr(envs.__getattr__, "cache_clear"):
|
||||
envs.__getattr__.cache_clear()
|
||||
|
||||
request = CompletionRequest(
|
||||
model=MODEL_NAME,
|
||||
prompt=["a", "b", "c"],
|
||||
max_tokens=1,
|
||||
)
|
||||
assert request.prompt == ["a", "b", "c"]
|
||||
|
||||
def test_bounded_token_id_prompt_list_allowed(self, monkeypatch):
|
||||
monkeypatch.setenv("VLLM_MAX_COMPLETION_PROMPTS", "10")
|
||||
from vllm import envs
|
||||
|
||||
if hasattr(envs.__getattr__, "cache_clear"):
|
||||
envs.__getattr__.cache_clear()
|
||||
|
||||
request = CompletionRequest(
|
||||
model=MODEL_NAME,
|
||||
prompt=[[1], [2], [3]],
|
||||
max_tokens=1,
|
||||
)
|
||||
assert request.prompt == [[1], [2], [3]]
|
||||
|
||||
def test_oversized_text_prompt_list_rejected(self, monkeypatch):
|
||||
monkeypatch.setenv("VLLM_MAX_COMPLETION_PROMPTS", "5")
|
||||
from vllm import envs
|
||||
|
||||
if hasattr(envs.__getattr__, "cache_clear"):
|
||||
envs.__getattr__.cache_clear()
|
||||
|
||||
with pytest.raises(
|
||||
Exception, match="prompt list length 10 exceeds the maximum"
|
||||
):
|
||||
CompletionRequest(
|
||||
model=MODEL_NAME,
|
||||
prompt=["x"] * 10,
|
||||
max_tokens=1,
|
||||
)
|
||||
|
||||
def test_oversized_token_id_prompt_list_rejected(self, monkeypatch):
|
||||
monkeypatch.setenv("VLLM_MAX_COMPLETION_PROMPTS", "5")
|
||||
from vllm import envs
|
||||
|
||||
if hasattr(envs.__getattr__, "cache_clear"):
|
||||
envs.__getattr__.cache_clear()
|
||||
|
||||
with pytest.raises(
|
||||
Exception, match="prompt list length 10 exceeds the maximum"
|
||||
):
|
||||
CompletionRequest(
|
||||
model=MODEL_NAME,
|
||||
prompt=[[1]] * 10,
|
||||
max_tokens=1,
|
||||
)
|
||||
|
||||
def test_exact_limit_allowed(self, monkeypatch):
|
||||
monkeypatch.setenv("VLLM_MAX_COMPLETION_PROMPTS", "5")
|
||||
from vllm import envs
|
||||
|
||||
if hasattr(envs.__getattr__, "cache_clear"):
|
||||
envs.__getattr__.cache_clear()
|
||||
|
||||
request = CompletionRequest(
|
||||
model=MODEL_NAME,
|
||||
prompt=["x"] * 5,
|
||||
max_tokens=1,
|
||||
)
|
||||
assert len(request.prompt) == 5
|
||||
|
||||
def test_one_over_limit_rejected(self, monkeypatch):
|
||||
monkeypatch.setenv("VLLM_MAX_COMPLETION_PROMPTS", "5")
|
||||
from vllm import envs
|
||||
|
||||
if hasattr(envs.__getattr__, "cache_clear"):
|
||||
envs.__getattr__.cache_clear()
|
||||
|
||||
with pytest.raises(Exception, match="prompt list length 6 exceeds the maximum"):
|
||||
CompletionRequest(
|
||||
model=MODEL_NAME,
|
||||
prompt=["x"] * 6,
|
||||
max_tokens=1,
|
||||
)
|
||||
|
||||
def test_oversized_prompt_embeds_list_rejected(self, monkeypatch):
|
||||
monkeypatch.setenv("VLLM_MAX_COMPLETION_PROMPTS", "5")
|
||||
from vllm import envs
|
||||
|
||||
if hasattr(envs.__getattr__, "cache_clear"):
|
||||
envs.__getattr__.cache_clear()
|
||||
|
||||
with pytest.raises(Exception, match="prompt_embeds list length 10 exceeds"):
|
||||
CompletionRequest(
|
||||
model=MODEL_NAME,
|
||||
prompt_embeds=[b"\x00"] * 10,
|
||||
max_tokens=1,
|
||||
)
|
||||
|
||||
def test_bounded_prompt_embeds_list_allowed(self, monkeypatch):
|
||||
monkeypatch.setenv("VLLM_MAX_COMPLETION_PROMPTS", "5")
|
||||
from vllm import envs
|
||||
|
||||
if hasattr(envs.__getattr__, "cache_clear"):
|
||||
envs.__getattr__.cache_clear()
|
||||
|
||||
request = CompletionRequest(
|
||||
model=MODEL_NAME,
|
||||
prompt_embeds=[b"\x00"] * 5,
|
||||
max_tokens=1,
|
||||
)
|
||||
assert len(request.prompt_embeds) == 5
|
||||
|
||||
Reference in New Issue
Block a user