xiaozhi-esp32-server/main/xiaozhi-server/core/utils/llm.py
tuanaiseo d77f12db31 fix(security): dynamic module loading uses unsanitized class name
Multiple factory helpers build import paths from `class_name` and only gate with `os.path.exists(...)`. If `class_name` can be influenced by configuration or user-controlled input, an attacker may load unintended modules or traverse paths, resulting in arbitrary code execution through import side effects.

Affected files: llm.py

Signed-off-by: tuanaiseo <221258316+tuanaiseo@users.noreply.github.com>
2026-04-08 06:32:40 +07:00

49 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import sys
# 添加项目根目录到Python路径
current_dir = os.path.dirname(os.path.abspath(__file__))
project_root = os.path.abspath(os.path.join(current_dir, "..", ".."))
sys.path.insert(0, project_root)
from config.logger import setup_logging
import importlib
import re
logger = setup_logging()
_VALID_CLASS_NAME = re.compile(r'^[a-zA-Z_][a-zA-Z0-9_]*$')
def _build_llm_module_map():
module_map = {}
base_dir = os.path.join(project_root, 'core', 'providers', 'llm')
if not os.path.isdir(base_dir):
return module_map
for name in os.listdir(base_dir):
if not _VALID_CLASS_NAME.fullmatch(name):
continue
provider_file = os.path.join(base_dir, name, f'{name}.py')
if os.path.isfile(provider_file):
module_map[name] = f'core.providers.llm.{name}.{name}'
return module_map
LLM_MODULE_MAP = _build_llm_module_map()
def create_instance(class_name, *args, **kwargs):
# 创建LLM实例
if not isinstance(class_name, str) or not _VALID_CLASS_NAME.fullmatch(class_name):
raise ValueError(f"不支持的LLM类型: {class_name},请检查该配置的type是否设置正确")
lib_name = LLM_MODULE_MAP.get(class_name)
if lib_name:
if lib_name not in sys.modules:
sys.modules[lib_name] = importlib.import_module(lib_name)
return sys.modules[lib_name].LLMProvider(*args, **kwargs)
raise ValueError(f"不支持的LLM类型: {class_name},请检查该配置的type是否设置正确")