[Security] Add timeout guard for regex compilation in structured outp… (#45118)
Signed-off-by: jperezde <jperezde@redhat.com> Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com> Co-authored-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
This commit is contained in:
co-authored by
Harry Mellor
parent
96fa5cdd9e
commit
2b3006076c
@@ -0,0 +1,61 @@
|
|||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||||
|
"""Tests for regex compilation timeout guard.
|
||||||
|
|
||||||
|
Verifies that adversarial regex patterns that would cause exponential
|
||||||
|
DFA state-space explosion are rejected with a timeout rather than
|
||||||
|
hanging indefinitely.
|
||||||
|
|
||||||
|
Addresses advisory GHSA-rwxx-mrjm-wc2m.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import time
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from vllm.v1.structured_output.utils import compile_regex_with_timeout
|
||||||
|
|
||||||
|
|
||||||
|
class TestCompileRegexWithTimeout:
|
||||||
|
"""Unit tests for the compile_regex_with_timeout utility."""
|
||||||
|
|
||||||
|
def test_normal_regex_compiles_successfully(self):
|
||||||
|
result = compile_regex_with_timeout(lambda pat: "compiled", r"[a-z]+")
|
||||||
|
assert result == "compiled"
|
||||||
|
|
||||||
|
def test_timeout_raises_value_error(self):
|
||||||
|
def slow_compile(pattern: str):
|
||||||
|
time.sleep(10)
|
||||||
|
return "never"
|
||||||
|
|
||||||
|
with (
|
||||||
|
patch("vllm.envs.VLLM_REGEX_COMPILATION_TIMEOUT_S", 1),
|
||||||
|
pytest.raises(ValueError, match="timed out"),
|
||||||
|
):
|
||||||
|
compile_regex_with_timeout(slow_compile, r"(a+)+b")
|
||||||
|
|
||||||
|
def test_timeout_disabled_when_zero(self):
|
||||||
|
result = None
|
||||||
|
with patch("vllm.envs.VLLM_REGEX_COMPILATION_TIMEOUT_S", 0):
|
||||||
|
result = compile_regex_with_timeout(lambda pat: "no_timeout", r"(a+)+b")
|
||||||
|
assert result == "no_timeout"
|
||||||
|
|
||||||
|
def test_compilation_error_propagates(self):
|
||||||
|
def failing_compile(pattern: str):
|
||||||
|
raise RuntimeError("compilation failed")
|
||||||
|
|
||||||
|
with pytest.raises(RuntimeError, match="compilation failed"):
|
||||||
|
compile_regex_with_timeout(failing_compile, r"bad")
|
||||||
|
|
||||||
|
def test_pattern_included_in_error_message(self):
|
||||||
|
def slow_compile(pattern: str):
|
||||||
|
time.sleep(10)
|
||||||
|
return "never"
|
||||||
|
|
||||||
|
pattern = r"(a+)+b"
|
||||||
|
with (
|
||||||
|
patch("vllm.envs.VLLM_REGEX_COMPILATION_TIMEOUT_S", 1),
|
||||||
|
pytest.raises(ValueError, match=r"\(a\+\)\+b"),
|
||||||
|
):
|
||||||
|
compile_regex_with_timeout(slow_compile, pattern)
|
||||||
@@ -188,6 +188,7 @@ if TYPE_CHECKING:
|
|||||||
VLLM_FLASHINFER_ALLREDUCE_BACKEND: Literal["auto", "trtllm", "mnnvl"] = "auto"
|
VLLM_FLASHINFER_ALLREDUCE_BACKEND: Literal["auto", "trtllm", "mnnvl"] = "auto"
|
||||||
VLLM_FLASHINFER_WORKSPACE_BUFFER_SIZE: int = 394 * 1024 * 1024
|
VLLM_FLASHINFER_WORKSPACE_BUFFER_SIZE: int = 394 * 1024 * 1024
|
||||||
VLLM_XGRAMMAR_CACHE_MB: int = 0
|
VLLM_XGRAMMAR_CACHE_MB: int = 0
|
||||||
|
VLLM_REGEX_COMPILATION_TIMEOUT_S: int = 5
|
||||||
VLLM_MSGPACK_ZERO_COPY_THRESHOLD: int = 256
|
VLLM_MSGPACK_ZERO_COPY_THRESHOLD: int = 256
|
||||||
VLLM_ALLOW_INSECURE_SERIALIZATION: bool = False
|
VLLM_ALLOW_INSECURE_SERIALIZATION: bool = False
|
||||||
VLLM_DISABLE_REQUEST_ID_RANDOMIZATION: bool = False
|
VLLM_DISABLE_REQUEST_ID_RANDOMIZATION: bool = False
|
||||||
@@ -1447,6 +1448,13 @@ environment_variables: dict[str, Callable[[], Any]] = {
|
|||||||
# of 512 MB should be enough for roughly 1000 JSON schemas.
|
# of 512 MB should be enough for roughly 1000 JSON schemas.
|
||||||
# It can be changed with this variable if needed for some reason.
|
# It can be changed with this variable if needed for some reason.
|
||||||
"VLLM_XGRAMMAR_CACHE_MB": lambda: int(os.getenv("VLLM_XGRAMMAR_CACHE_MB", "512")),
|
"VLLM_XGRAMMAR_CACHE_MB": lambda: int(os.getenv("VLLM_XGRAMMAR_CACHE_MB", "512")),
|
||||||
|
# Maximum time in seconds allowed for regex compilation in structured
|
||||||
|
# output backends (xgrammar, outlines). Prevents ReDoS attacks where
|
||||||
|
# adversarial patterns cause exponential DFA state-space explosion.
|
||||||
|
# Set to 0 to disable the timeout (not recommended in production).
|
||||||
|
"VLLM_REGEX_COMPILATION_TIMEOUT_S": lambda: int(
|
||||||
|
os.getenv("VLLM_REGEX_COMPILATION_TIMEOUT_S", "5")
|
||||||
|
),
|
||||||
# Control the threshold for msgspec to use 'zero copy' for
|
# Control the threshold for msgspec to use 'zero copy' for
|
||||||
# serialization/deserialization of tensors. Tensors below
|
# serialization/deserialization of tensors. Tensors below
|
||||||
# this limit will be encoded into the msgpack buffer, and
|
# this limit will be encoded into the msgpack buffer, and
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ from vllm.v1.structured_output.backend_types import (
|
|||||||
)
|
)
|
||||||
from vllm.v1.structured_output.utils import (
|
from vllm.v1.structured_output.utils import (
|
||||||
OutlinesVocabulary,
|
OutlinesVocabulary,
|
||||||
|
compile_regex_with_timeout,
|
||||||
get_outlines_cache,
|
get_outlines_cache,
|
||||||
get_outlines_vocabulary,
|
get_outlines_vocabulary,
|
||||||
)
|
)
|
||||||
@@ -61,7 +62,10 @@ class OutlinesBackend(StructuredOutputBackend):
|
|||||||
if cache_key in self.cache:
|
if cache_key in self.cache:
|
||||||
return self.cache[cache_key]
|
return self.cache[cache_key]
|
||||||
|
|
||||||
index = oc.Index(regex_string, vocabulary.inner)
|
index = compile_regex_with_timeout(
|
||||||
|
lambda pat: oc.Index(pat, vocabulary.inner),
|
||||||
|
regex_string,
|
||||||
|
)
|
||||||
self.cache[cache_key] = index
|
self.cache[cache_key] = index
|
||||||
|
|
||||||
return index
|
return index
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ from vllm.v1.structured_output.backend_types import (
|
|||||||
)
|
)
|
||||||
from vllm.v1.structured_output.utils import (
|
from vllm.v1.structured_output.utils import (
|
||||||
choice_as_grammar,
|
choice_as_grammar,
|
||||||
|
compile_regex_with_timeout,
|
||||||
convert_lark_to_ebnf,
|
convert_lark_to_ebnf,
|
||||||
grammar_is_likely_lark,
|
grammar_is_likely_lark,
|
||||||
)
|
)
|
||||||
@@ -88,7 +89,10 @@ class XgrammarBackend(StructuredOutputBackend):
|
|||||||
elif request_type == StructuredOutputOptions.GRAMMAR:
|
elif request_type == StructuredOutputOptions.GRAMMAR:
|
||||||
ctx = self.compiler.compile_grammar(grammar_spec)
|
ctx = self.compiler.compile_grammar(grammar_spec)
|
||||||
elif request_type == StructuredOutputOptions.REGEX:
|
elif request_type == StructuredOutputOptions.REGEX:
|
||||||
ctx = self.compiler.compile_regex(grammar_spec)
|
ctx = compile_regex_with_timeout(
|
||||||
|
self.compiler.compile_regex,
|
||||||
|
grammar_spec,
|
||||||
|
)
|
||||||
elif request_type == StructuredOutputOptions.STRUCTURAL_TAG:
|
elif request_type == StructuredOutputOptions.STRUCTURAL_TAG:
|
||||||
s_tag = json.loads(grammar_spec)
|
s_tag = json.loads(grammar_spec)
|
||||||
if "structures" in s_tag:
|
if "structures" in s_tag:
|
||||||
@@ -277,7 +281,10 @@ def validate_xgrammar_grammar(sampling_params: SamplingParams) -> None:
|
|||||||
|
|
||||||
if so_params.regex:
|
if so_params.regex:
|
||||||
try:
|
try:
|
||||||
xgr.Grammar.from_regex(so_params.regex)
|
compile_regex_with_timeout(
|
||||||
|
xgr.Grammar.from_regex,
|
||||||
|
so_params.regex,
|
||||||
|
)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Failed to transform regex into a grammar: {err}"
|
f"Failed to transform regex into a grammar: {err}"
|
||||||
|
|||||||
@@ -6,7 +6,9 @@ import hashlib
|
|||||||
import importlib.metadata
|
import importlib.metadata
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
from typing import TYPE_CHECKING
|
from collections.abc import Callable
|
||||||
|
from concurrent.futures import ThreadPoolExecutor, TimeoutError
|
||||||
|
from typing import TYPE_CHECKING, TypeVar
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import regex as re
|
import regex as re
|
||||||
@@ -38,9 +40,49 @@ else:
|
|||||||
|
|
||||||
logger = init_logger(__name__)
|
logger = init_logger(__name__)
|
||||||
|
|
||||||
|
_T = TypeVar("_T")
|
||||||
|
|
||||||
CACHE = None
|
CACHE = None
|
||||||
|
|
||||||
|
|
||||||
|
def compile_regex_with_timeout(fn: Callable[[str], _T], pattern: str) -> _T:
|
||||||
|
"""Run a regex compilation callable with a timeout.
|
||||||
|
|
||||||
|
Prevents ReDoS attacks where adversarial regex patterns (e.g. nested
|
||||||
|
quantifiers like ``(a+)+b``) cause exponential DFA state-space explosion,
|
||||||
|
hanging the inference worker indefinitely.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
fn: Single-argument callable that takes the pattern and performs
|
||||||
|
the regex compilation.
|
||||||
|
pattern: The regex pattern string, passed to *fn* and included in
|
||||||
|
timeout error messages.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError: If compilation exceeds the configured timeout.
|
||||||
|
"""
|
||||||
|
timeout = envs.VLLM_REGEX_COMPILATION_TIMEOUT_S
|
||||||
|
if timeout <= 0:
|
||||||
|
return fn(pattern)
|
||||||
|
|
||||||
|
executor = ThreadPoolExecutor(max_workers=1)
|
||||||
|
future = executor.submit(fn, pattern)
|
||||||
|
try:
|
||||||
|
result = future.result(timeout=timeout)
|
||||||
|
except TimeoutError:
|
||||||
|
future.cancel()
|
||||||
|
executor.shutdown(wait=False, cancel_futures=True)
|
||||||
|
raise ValueError(
|
||||||
|
f"Regex compilation timed out after {timeout}s. "
|
||||||
|
"The pattern may be too complex or contain constructs that "
|
||||||
|
"cause exponential state-space explosion (e.g. nested "
|
||||||
|
f"quantifiers). Pattern: {pattern[:200]}"
|
||||||
|
) from None
|
||||||
|
else:
|
||||||
|
executor.shutdown(wait=False)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
def apply_grammar_bitmask(
|
def apply_grammar_bitmask(
|
||||||
scheduler_output: SchedulerOutput,
|
scheduler_output: SchedulerOutput,
|
||||||
grammar_output: GrammarOutput,
|
grammar_output: GrammarOutput,
|
||||||
|
|||||||
Reference in New Issue
Block a user