refactor(model): streamline model filtering and SSRF validation

- Simplified model filtering logic by removing the deduplication function and directly filtering models based on type.
- Updated SSRF validation to preserve backend-managed fields when parameters are not provided by the frontend.
- Clarified comments and improved code readability in the ModelEditorDialog and ModelSettings components.
This commit is contained in:
wizardchen
2026-03-23 20:09:48 +08:00
committed by lyingbug
parent 68dd6aa778
commit 15ebfe0b4c
3 changed files with 32 additions and 58 deletions
@@ -230,8 +230,8 @@
</p>
</div>
<!-- Chat/VLLM: supports vision toggle -->
<div v-if="modelType === 'chat' || modelType === 'vllm'" class="form-item">
<!-- Chat: supports vision toggle (VLLM models are inherently multimodal) -->
<div v-if="modelType === 'chat'" class="form-item">
<label class="form-label">{{ $t('model.editor.supportsVisionLabel') }}</label>
<div style="display: flex; align-items: center; gap: 8px;">
<t-switch v-model="formData.supportsVision" />
+16 -46
View File
@@ -249,37 +249,29 @@ const loading = ref(true)
// 模型列表数据
const allModels = ref<ModelConfig[]>([])
// 根据类型过滤并去重模型
// 根据类型过滤模型
const chatModels = computed(() =>
deduplicateModels(
allModels.value
.filter(m => m.type === 'KnowledgeQA')
.map(convertToLegacyFormat)
)
allModels.value
.filter(m => m.type === 'KnowledgeQA')
.map(convertToLegacyFormat)
)
const embeddingModels = computed(() =>
deduplicateModels(
allModels.value
.filter(m => m.type === 'Embedding')
.map(convertToLegacyFormat)
)
allModels.value
.filter(m => m.type === 'Embedding')
.map(convertToLegacyFormat)
)
const rerankModels = computed(() =>
deduplicateModels(
allModels.value
.filter(m => m.type === 'Rerank')
.map(convertToLegacyFormat)
)
allModels.value
.filter(m => m.type === 'Rerank')
.map(convertToLegacyFormat)
)
const vllmModels = computed(() =>
deduplicateModels(
allModels.value
.filter(m => m.type === 'VLLM')
.map(convertToLegacyFormat)
)
allModels.value
.filter(m => m.type === 'VLLM')
.map(convertToLegacyFormat)
)
// 将后端模型格式转换为旧的前端格式
@@ -298,30 +290,6 @@ function convertToLegacyFormat(model: ModelConfig) {
}
}
// 去重函数:比较除id外的所有字段,相同的只保留第一个
function deduplicateModels(models: any[]) {
const seen = new Map<string, any>()
return models.filter(model => {
// 创建一个不包含id的签名用于比较
const signature = JSON.stringify({
name: model.name,
source: model.source,
modelName: model.modelName,
baseUrl: model.baseUrl,
apiKey: model.apiKey,
dimension: model.dimension
})
if (seen.has(signature)) {
return false
}
seen.set(signature, model)
return true
})
}
// 加载模型列表
const loadModels = async () => {
loading.value = true
@@ -410,7 +378,9 @@ const handleModelSave = async (modelData: any) => {
truncate_prompt_tokens: 0
}
} : {}),
...((currentModelType.value === 'chat' || currentModelType.value === 'vllm') ? {
...(currentModelType.value === 'vllm' ? {
supports_vision: true
} : currentModelType.value === 'chat' ? {
supports_vision: modelData.supportsVision ?? false
} : {})
}
+14 -10
View File
@@ -301,18 +301,22 @@ func (h *ModelHandler) UpdateModel(c *gin.Context) {
model.Name = req.Name
}
model.Description = req.Description
// Check if any Parameters field is set (can't use struct comparison due to map field)
if req.Parameters.BaseURL != "" || req.Parameters.APIKey != "" || req.Parameters.Provider != "" {
// SSRF validation for updated model BaseURL
if req.Parameters.BaseURL != "" {
if err := secutils.ValidateURLForSSRF(req.Parameters.BaseURL); err != nil {
logger.Warnf(ctx, "SSRF validation failed for model BaseURL: %v", err)
c.Error(errors.NewBadRequestError(fmt.Sprintf("Base URL 未通过安全校验: %v", err)))
return
}
// SSRF validation for updated model BaseURL
if req.Parameters.BaseURL != "" {
if err := secutils.ValidateURLForSSRF(req.Parameters.BaseURL); err != nil {
logger.Warnf(ctx, "SSRF validation failed for model BaseURL: %v", err)
c.Error(errors.NewBadRequestError(fmt.Sprintf("Base URL 未通过安全校验: %v", err)))
return
}
model.Parameters = req.Parameters
}
// Preserve backend-managed fields not sent by frontend
req.Parameters.ParameterSize = model.Parameters.ParameterSize
if req.Parameters.ExtraConfig == nil {
req.Parameters.ExtraConfig = model.Parameters.ExtraConfig
}
model.Parameters = req.Parameters
model.Source = req.Source
model.Type = req.Type