Skip to content

Commit aba63b2

Browse files
committed
Escape complex config in llama-swap
1 parent 9706528 commit aba63b2

1 file changed

Lines changed: 46 additions & 4 deletions

File tree

backend/llama_swap_config.py

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import subprocess
44
import re
55
import json
6+
import shlex
67
from typing import Dict, Any, Set, Optional
78
from backend.logging_config import get_logger
89

@@ -12,6 +13,35 @@
1213
_supported_flags_cache: Dict[str, Set[str]] = {}
1314

1415

16+
def _quote_arg_if_needed(arg: str) -> str:
17+
"""
18+
Quote an argument if it contains spaces, special characters, or is a complex value.
19+
20+
Args:
21+
arg: The argument value to potentially quote
22+
23+
Returns:
24+
The argument, quoted if necessary
25+
"""
26+
if not isinstance(arg, str):
27+
arg = str(arg)
28+
29+
# Always quote if it contains spaces, quotes, or shell special characters
30+
if any(char in arg for char in [' ', '\t', '\n', '|', '&', ';', '(', ')', '<', '>', '*', '?', '[', ']', '{', '}', '$', '`', '\\']):
31+
# Use shlex.quote for proper shell escaping
32+
return shlex.quote(arg)
33+
34+
# Quote regex patterns (common in --override-tensor)
35+
if re.search(r'[.*+?^${}|()\[\]\\]', arg):
36+
return shlex.quote(arg)
37+
38+
# Quote if it starts with a dash (could be confused with a flag)
39+
if arg.startswith('-') and not arg.startswith('--'):
40+
return shlex.quote(arg)
41+
42+
return arg
43+
44+
1545
def _coerce_model_config(config_value: Optional[Any]) -> Dict[str, Any]:
1646
if not config_value:
1747
return {}
@@ -242,7 +272,9 @@ def generate_llama_swap_config(models: Dict[str, Dict[str, Any]], llama_server_p
242272
logger.debug(f"Model {proxy_model_name}: jinja={config.get('jinja')} (type: {type(config.get('jinja'))})")
243273

244274
# Build llama.cpp command arguments
245-
cmd_args = [llama_server_path, "--model", model_path, "--port", "${PORT}"]
275+
# Quote model path if it contains spaces or special characters
276+
quoted_model_path = _quote_arg_if_needed(model_path)
277+
cmd_args = [llama_server_path, "--model", quoted_model_path, "--port", "${PORT}"]
246278

247279
# Default values to skip (these cause errors if flag isn't supported)
248280
default_values = {
@@ -374,7 +406,11 @@ def generate_llama_swap_config(models: Dict[str, Dict[str, Any]], llama_server_p
374406
logger.debug(f"Skipping duplicate --temp flag for {proxy_model_name}")
375407
continue
376408
temp_flag_added = True
377-
cmd_args.extend([flag_options[0], str(value)])
409+
# Quote complex values (grammar, json_schema, yaml, etc.)
410+
value_str = str(value)
411+
if key in ("grammar", "json_schema", "yaml") or flag_options[0] in ("--grammar", "--json-schema", "--yaml"):
412+
value_str = _quote_arg_if_needed(value_str)
413+
cmd_args.extend([flag_options[0], value_str])
378414

379415
# Special handling: MoE offload flags
380416
# Check for direct cpu_moe or n_cpu_moe parameters first (these take precedence)
@@ -442,7 +478,9 @@ def generate_llama_swap_config(models: Dict[str, Dict[str, Any]], llama_server_p
442478
llama_cpp_config = model_data["config"]
443479

444480
# Build llama.cpp command arguments (using full path to llama-server)
445-
cmd_args = [llama_server_path, "--model", model_path, "--port", "${PORT}"]
481+
# Quote model path if it contains spaces or special characters
482+
quoted_model_path = _quote_arg_if_needed(model_path)
483+
cmd_args = [llama_server_path, "--model", quoted_model_path, "--port", "${PORT}"]
446484

447485
# Default values to skip (these cause errors if flag isn't supported)
448486
default_values = {
@@ -569,7 +607,11 @@ def generate_llama_swap_config(models: Dict[str, Dict[str, Any]], llama_server_p
569607
logger.debug(f"Skipping duplicate --temp flag")
570608
continue
571609
temp_flag_added = True
572-
cmd_args.extend([flag_options[0], str(value)])
610+
# Quote complex values (grammar, json_schema, yaml, etc.)
611+
value_str = str(value)
612+
if key in ("grammar", "json_schema", "yaml") or flag_options[0] in ("--grammar", "--json-schema", "--yaml"):
613+
value_str = _quote_arg_if_needed(value_str)
614+
cmd_args.extend([flag_options[0], value_str])
573615

574616
# Special handling: MoE offload flags
575617
# Check for direct cpu_moe or n_cpu_moe parameters first (these take precedence)

0 commit comments

Comments
 (0)