[Security] Fix chat template resource-exhaustion DoS (GHSA-4hhp-h66f-… (#50300)
Signed-off-by: jperezde <jperezde@redhat.com> Signed-off-by: Juan Pérez de Algaba <jperezde@redhat.com> Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
co-authored by
Cursor
parent
fc2c801ad9
commit
cff08b461e
+47
-5
@@ -2,6 +2,7 @@
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import copy
|
||||
import inspect
|
||||
import itertools
|
||||
@@ -20,6 +21,7 @@ import jinja2.sandbox
|
||||
import torch
|
||||
from typing_extensions import override
|
||||
|
||||
from vllm import envs
|
||||
from vllm.entrypoints.chat_utils import (
|
||||
PROMPT_EMBEDS_PLACEHOLDER_TOKEN,
|
||||
ChatTemplateResolutionError,
|
||||
@@ -969,6 +971,44 @@ class HfRenderer(BaseRenderer[HfTokenizer]):
|
||||
self.tokenizer, config.model_config.renderer_num_workers + 1
|
||||
)
|
||||
|
||||
def _replace_executor(self) -> None:
|
||||
"""Replace the inner render pool after a stuck worker.
|
||||
|
||||
Called after a render timeout so that the stuck thread (which CPython
|
||||
cannot interrupt) does not permanently block all subsequent renders.
|
||||
The executor *object* is kept so ``make_async`` wrappers bound at
|
||||
init (tokenize, decode, pooling, derender) keep submitting work.
|
||||
The old inner pool is abandoned with wait=False — its thread will
|
||||
eventually finish or be reaped on process exit.
|
||||
"""
|
||||
self._executor.replace_inner()
|
||||
logger.warning(
|
||||
"Chat template render timed out — executor pool replaced "
|
||||
"(%d worker(s)). The stuck thread may continue consuming "
|
||||
"CPU until the process exits.",
|
||||
self.model_config.renderer_num_workers,
|
||||
)
|
||||
|
||||
async def _render_with_timeout(self, coro):
|
||||
"""Run a template-rendering coroutine with a timeout.
|
||||
|
||||
On timeout, replaces the executor so subsequent renders are not
|
||||
permanently blocked by the stuck thread.
|
||||
"""
|
||||
timeout = envs.VLLM_CHAT_TEMPLATE_RENDER_TIMEOUT
|
||||
if timeout <= 0:
|
||||
return await coro
|
||||
try:
|
||||
return await asyncio.wait_for(coro, timeout=timeout)
|
||||
except asyncio.TimeoutError:
|
||||
self._replace_executor()
|
||||
raise TimeoutError(
|
||||
f"Chat template rendering timed out after "
|
||||
f"{timeout}s. This may indicate a malicious or "
|
||||
f"excessively complex template. Adjust "
|
||||
f"VLLM_CHAT_TEMPLATE_RENDER_TIMEOUT to override."
|
||||
) from None
|
||||
|
||||
def _can_produce_offsets(self) -> bool:
|
||||
# Only fast tokenizers expose offset_mapping.
|
||||
return self.tokenizer is not None and self.tokenizer.is_fast
|
||||
@@ -1124,11 +1164,13 @@ class HfRenderer(BaseRenderer[HfTokenizer]):
|
||||
logger.warning_once(_TOKENIZE_OVERRIDE_WARNING)
|
||||
chat_template_kwargs["tokenize"] = True
|
||||
|
||||
prompt_raw = await self._apply_chat_template_async(
|
||||
model_config,
|
||||
tokenizer,
|
||||
conversation,
|
||||
**chat_template_kwargs,
|
||||
prompt_raw = await self._render_with_timeout(
|
||||
self._apply_chat_template_async(
|
||||
model_config,
|
||||
tokenizer,
|
||||
conversation,
|
||||
**chat_template_kwargs,
|
||||
)
|
||||
)
|
||||
|
||||
# NOTE: use_unified_vision_chunk is currently specific to Kimi-K2.5
|
||||
|
||||
Reference in New Issue
Block a user