refactor: Sanitize log inputs in various handlers to enhance security
This commit is contained in:
@@ -3508,9 +3508,6 @@ func (s *knowledgeService) triggerManualProcessing(ctx context.Context,
|
||||
// 检查是否需要启用多模态(对于手动内容通常不需要,但保持一致性)
|
||||
enableMultimodel := kb.VLMConfig.Enabled && kb.StorageConfig.Provider != ""
|
||||
|
||||
logger.GetLogger(ctx).Infof("triggerManualProcessing split manual content size: %d, file name: %s, file type: %s, separators: %v, enable multimodal: %v",
|
||||
len(contentBytes), fileName, fileType, kb.ChunkingConfig.Separators, enableMultimodel)
|
||||
|
||||
var vlmConfig *proto.VLMConfig
|
||||
if enableMultimodel {
|
||||
cfg, cfgErr := s.getVLMProtoConfig(ctx, kb)
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
secutils "github.com/Tencent/WeKnora/internal/utils"
|
||||
)
|
||||
|
||||
// ToInterfaceSlice converts a slice of strings to a slice of empty interfaces.
|
||||
@@ -174,7 +175,7 @@ func PipelineLog(stage, action string, fields map[string]interface{}) string {
|
||||
builder.WriteString(" ")
|
||||
builder.WriteString(key)
|
||||
builder.WriteString("=")
|
||||
builder.WriteString(formatPipelineLogValue(fields[key]))
|
||||
builder.WriteString(secutils.SanitizeForLog(formatPipelineLogValue(fields[key])))
|
||||
}
|
||||
}
|
||||
return builder.String()
|
||||
|
||||
+11
-14
@@ -27,13 +27,12 @@ func (h *ChunkHandler) GetChunkByIDOnly(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
logger.Info(ctx, "Start retrieving chunk by ID only")
|
||||
|
||||
chunkID := c.Param("id")
|
||||
chunkID := secutils.SanitizeForLog(c.Param("id"))
|
||||
if chunkID == "" {
|
||||
logger.Error(ctx, "Chunk ID is empty")
|
||||
c.Error(errors.NewBadRequestError("Chunk ID cannot be empty"))
|
||||
return
|
||||
}
|
||||
safeChunkID := secutils.SanitizeForLog(chunkID)
|
||||
|
||||
// Get tenant ID from context
|
||||
tenantID, exists := c.Get(types.TenantIDContextKey.String())
|
||||
@@ -43,13 +42,13 @@ func (h *ChunkHandler) GetChunkByIDOnly(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Retrieving chunk by ID, chunk ID: %s, tenant ID: %d", safeChunkID, tenantID)
|
||||
logger.Infof(ctx, "Retrieving chunk by ID, chunk ID: %s, tenant ID: %d", chunkID, tenantID)
|
||||
|
||||
// Get chunk by ID
|
||||
chunk, err := h.service.GetChunkByID(ctx, chunkID)
|
||||
if err != nil {
|
||||
if err == service.ErrChunkNotFound {
|
||||
logger.Warnf(ctx, "Chunk not found, chunk ID: %s", safeChunkID)
|
||||
logger.Warnf(ctx, "Chunk not found, chunk ID: %s", chunkID)
|
||||
c.Error(errors.NewNotFoundError("Chunk not found"))
|
||||
return
|
||||
}
|
||||
@@ -63,7 +62,7 @@ func (h *ChunkHandler) GetChunkByIDOnly(c *gin.Context) {
|
||||
logger.Warnf(
|
||||
ctx,
|
||||
"Tenant has no permission to access chunk, chunk ID: %s, req tenant: %d, chunk tenant: %d",
|
||||
safeChunkID, tenantID.(uint64), chunk.TenantID,
|
||||
chunkID, tenantID.(uint64), chunk.TenantID,
|
||||
)
|
||||
c.Error(errors.NewForbiddenError("No permission to access this chunk"))
|
||||
return
|
||||
@@ -85,7 +84,7 @@ func (h *ChunkHandler) ListKnowledgeChunks(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
logger.Info(ctx, "Start retrieving knowledge chunks list")
|
||||
|
||||
knowledgeID := c.Param("knowledge_id")
|
||||
knowledgeID := secutils.SanitizeForLog(c.Param("knowledge_id"))
|
||||
if knowledgeID == "" {
|
||||
logger.Error(ctx, "Knowledge ID is empty")
|
||||
c.Error(errors.NewBadRequestError("Knowledge ID cannot be empty"))
|
||||
@@ -143,20 +142,18 @@ func (h *ChunkHandler) validateAndGetChunk(c *gin.Context) (*types.Chunk, string
|
||||
ctx := c.Request.Context()
|
||||
|
||||
// Validate knowledge ID
|
||||
knowledgeID := c.Param("knowledge_id")
|
||||
knowledgeID := secutils.SanitizeForLog(c.Param("knowledge_id"))
|
||||
if knowledgeID == "" {
|
||||
logger.Error(ctx, "Knowledge ID is empty")
|
||||
return nil, "", errors.NewBadRequestError("Knowledge ID cannot be empty")
|
||||
}
|
||||
safeKnowledgeID := secutils.SanitizeForLog(knowledgeID)
|
||||
|
||||
// Validate chunk ID
|
||||
id := c.Param("id")
|
||||
id := secutils.SanitizeForLog(c.Param("id"))
|
||||
if id == "" {
|
||||
logger.Error(ctx, "Chunk ID is empty")
|
||||
return nil, knowledgeID, errors.NewBadRequestError("Chunk ID cannot be empty")
|
||||
}
|
||||
safeChunkID := secutils.SanitizeForLog(id)
|
||||
|
||||
// Get tenant ID from context
|
||||
tenantID, exists := c.Get(types.TenantIDContextKey.String())
|
||||
@@ -165,13 +162,13 @@ func (h *ChunkHandler) validateAndGetChunk(c *gin.Context) (*types.Chunk, string
|
||||
return nil, knowledgeID, errors.NewUnauthorizedError("Unauthorized")
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Retrieving knowledge chunk information, knowledge ID: %s, chunk ID: %s", safeKnowledgeID, safeChunkID)
|
||||
logger.Infof(ctx, "Retrieving knowledge chunk information, knowledge ID: %s, chunk ID: %s", knowledgeID, id)
|
||||
|
||||
// Get existing chunk
|
||||
chunk, err := h.service.GetChunkByID(ctx, id)
|
||||
if err != nil {
|
||||
if err == service.ErrChunkNotFound {
|
||||
logger.Warnf(ctx, "Chunk not found, knowledge ID: %s, chunk ID: %s", safeKnowledgeID, safeChunkID)
|
||||
logger.Warnf(ctx, "Chunk not found, knowledge ID: %s, chunk ID: %s", knowledgeID, id)
|
||||
return nil, knowledgeID, errors.NewNotFoundError("Chunk not found")
|
||||
}
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
@@ -183,7 +180,7 @@ func (h *ChunkHandler) validateAndGetChunk(c *gin.Context) (*types.Chunk, string
|
||||
logger.Warnf(
|
||||
ctx,
|
||||
"Tenant has no permission to access chunk, knowledge ID: %s, chunk ID: %s, req tenant: %d, chunk tenant: %d",
|
||||
safeKnowledgeID, safeChunkID, tenantID, chunk.TenantID,
|
||||
knowledgeID, id, tenantID, chunk.TenantID,
|
||||
)
|
||||
return nil, knowledgeID, errors.NewForbiddenError("No permission to access this chunk")
|
||||
}
|
||||
@@ -259,7 +256,7 @@ func (h *ChunkHandler) DeleteChunksByKnowledgeID(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
logger.Info(ctx, "Start deleting all chunks under knowledge")
|
||||
|
||||
knowledgeID := c.Param("knowledge_id")
|
||||
knowledgeID := secutils.SanitizeForLog(c.Param("knowledge_id"))
|
||||
if knowledgeID == "" {
|
||||
logger.Error(ctx, "Knowledge ID is empty")
|
||||
c.Error(errors.NewBadRequestError("Knowledge ID cannot be empty"))
|
||||
|
||||
@@ -34,7 +34,7 @@ func (h *FAQHandler) ListEntries(c *gin.Context) {
|
||||
|
||||
tagID := c.Query("tag_id")
|
||||
|
||||
result, err := h.knowledgeService.ListFAQEntries(ctx, c.Param("id"), &page, tagID)
|
||||
result, err := h.knowledgeService.ListFAQEntries(ctx, secutils.SanitizeForLog(c.Param("id")), &page, tagID)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(err)
|
||||
@@ -57,7 +57,7 @@ func (h *FAQHandler) UpsertEntries(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
taskID, err := h.knowledgeService.UpsertFAQEntries(ctx, c.Param("id"), &req)
|
||||
taskID, err := h.knowledgeService.UpsertFAQEntries(ctx, secutils.SanitizeForLog(c.Param("id")), &req)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(err)
|
||||
@@ -82,7 +82,8 @@ func (h *FAQHandler) UpdateEntry(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.knowledgeService.UpdateFAQEntry(ctx, c.Param("id"), c.Param("entry_id"), &req); err != nil {
|
||||
if err := h.knowledgeService.UpdateFAQEntry(ctx,
|
||||
secutils.SanitizeForLog(c.Param("id")), secutils.SanitizeForLog(c.Param("entry_id")), &req); err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(err)
|
||||
return
|
||||
@@ -102,7 +103,7 @@ func (h *FAQHandler) UpdateEntryTagBatch(c *gin.Context) {
|
||||
c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
if err := h.knowledgeService.UpdateFAQEntryTagBatch(ctx, c.Param("id"), req.Updates); err != nil {
|
||||
if err := h.knowledgeService.UpdateFAQEntryTagBatch(ctx, secutils.SanitizeForLog(c.Param("id")), req.Updates); err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(err)
|
||||
return
|
||||
@@ -121,7 +122,7 @@ func (h *FAQHandler) UpdateEntryStatusBatch(c *gin.Context) {
|
||||
c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
if err := h.knowledgeService.UpdateFAQEntryStatusBatch(ctx, c.Param("id"), req.Updates); err != nil {
|
||||
if err := h.knowledgeService.UpdateFAQEntryStatusBatch(ctx, secutils.SanitizeForLog(c.Param("id")), req.Updates); err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(err)
|
||||
return
|
||||
@@ -153,7 +154,7 @@ func (h *FAQHandler) DeleteEntries(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.knowledgeService.DeleteFAQEntries(ctx, c.Param("id"), req.IDs); err != nil {
|
||||
if err := h.knowledgeService.DeleteFAQEntries(ctx, secutils.SanitizeForLog(c.Param("id")), req.IDs); err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(err)
|
||||
return
|
||||
|
||||
@@ -1589,7 +1589,7 @@ func (h *InitializationHandler) TestMultimodalFunction(c *gin.Context) {
|
||||
c.Error(errors.NewBadRequestError("图片文件大小不能超过10MB"))
|
||||
return
|
||||
}
|
||||
logger.Infof(ctx, "Processing image: %s, size: %d bytes", secutils.SanitizeForLog(header.Filename), header.Size)
|
||||
logger.Infof(ctx, "Processing image: %s", secutils.SanitizeForLog(header.Filename))
|
||||
|
||||
// 解析文档分割配置
|
||||
chunkSizeInt32, err := strconv.ParseInt(req.ChunkSize, 10, 32)
|
||||
|
||||
@@ -250,7 +250,7 @@ func (h *KnowledgeHandler) GetKnowledge(c *gin.Context) {
|
||||
logger.Info(ctx, "Start retrieving knowledge")
|
||||
|
||||
// Get knowledge ID from URL path parameter
|
||||
id := c.Param("id")
|
||||
id := secutils.SanitizeForLog(c.Param("id"))
|
||||
if id == "" {
|
||||
logger.Error(ctx, "Knowledge ID is empty")
|
||||
c.Error(errors.NewBadRequestError("Knowledge ID cannot be empty"))
|
||||
@@ -279,7 +279,7 @@ func (h *KnowledgeHandler) ListKnowledge(c *gin.Context) {
|
||||
logger.Info(ctx, "Start retrieving knowledge list")
|
||||
|
||||
// Get knowledge base ID from URL path parameter
|
||||
kbID := c.Param("id")
|
||||
kbID := secutils.SanitizeForLog(c.Param("id"))
|
||||
if kbID == "" {
|
||||
logger.Error(ctx, "Knowledge base ID is empty")
|
||||
c.Error(errors.NewBadRequestError("Knowledge base ID cannot be empty"))
|
||||
@@ -324,7 +324,7 @@ func (h *KnowledgeHandler) DeleteKnowledge(c *gin.Context) {
|
||||
logger.Info(ctx, "Start deleting knowledge")
|
||||
|
||||
// Get knowledge ID from URL path parameter
|
||||
id := c.Param("id")
|
||||
id := secutils.SanitizeForLog(c.Param("id"))
|
||||
if id == "" {
|
||||
logger.Error(ctx, "Knowledge ID is empty")
|
||||
c.Error(errors.NewBadRequestError("Knowledge ID cannot be empty"))
|
||||
@@ -353,7 +353,7 @@ func (h *KnowledgeHandler) DownloadKnowledgeFile(c *gin.Context) {
|
||||
logger.Info(ctx, "Start downloading knowledge file")
|
||||
|
||||
// Get knowledge ID from URL path parameter
|
||||
id := c.Param("id")
|
||||
id := secutils.SanitizeForLog(c.Param("id"))
|
||||
if id == "" {
|
||||
logger.Error(ctx, "Knowledge ID is empty")
|
||||
c.Error(errors.NewBadRequestError("Knowledge ID cannot be empty"))
|
||||
@@ -452,7 +452,7 @@ func (h *KnowledgeHandler) UpdateKnowledge(c *gin.Context) {
|
||||
logger.Info(ctx, "Start Update knowledge")
|
||||
|
||||
// Get knowledge ID from URL path parameter
|
||||
id := c.Param("id")
|
||||
id := secutils.SanitizeForLog(c.Param("id"))
|
||||
if id == "" {
|
||||
logger.Error(ctx, "Knowledge ID is empty")
|
||||
c.Error(errors.NewBadRequestError("Knowledge ID cannot be empty"))
|
||||
@@ -472,7 +472,7 @@ func (h *KnowledgeHandler) UpdateKnowledge(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Knowledge updated successfully, knowledge ID: %s", secutils.SanitizeForLog(knowledge.ID))
|
||||
logger.Infof(ctx, "Knowledge updated successfully, knowledge ID: %s", id)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"message": "Knowledge chunk updated successfully",
|
||||
@@ -484,8 +484,8 @@ func (h *KnowledgeHandler) UpdateManualKnowledge(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
logger.Info(ctx, "Start updating manual knowledge")
|
||||
|
||||
knowledgeID := c.Param("id")
|
||||
if knowledgeID == "" {
|
||||
id := secutils.SanitizeForLog(c.Param("id"))
|
||||
if id == "" {
|
||||
logger.Error(ctx, "Knowledge ID is empty")
|
||||
c.Error(errors.NewBadRequestError("Knowledge ID cannot be empty"))
|
||||
return
|
||||
@@ -498,20 +498,20 @@ func (h *KnowledgeHandler) UpdateManualKnowledge(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
knowledge, err := h.kgService.UpdateManualKnowledge(ctx, knowledgeID, &req)
|
||||
knowledge, err := h.kgService.UpdateManualKnowledge(ctx, id, &req)
|
||||
if err != nil {
|
||||
if appErr, ok := errors.IsAppError(err); ok {
|
||||
c.Error(appErr)
|
||||
return
|
||||
}
|
||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{
|
||||
"knowledge_id": knowledgeID,
|
||||
"knowledge_id": id,
|
||||
})
|
||||
c.Error(errors.NewInternalServerError(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Manual knowledge updated successfully, knowledge ID: %s", secutils.SanitizeForLog(knowledge.ID))
|
||||
logger.Infof(ctx, "Manual knowledge updated successfully, knowledge ID: %s", id)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": knowledge,
|
||||
@@ -547,14 +547,14 @@ func (h *KnowledgeHandler) UpdateImageInfo(c *gin.Context) {
|
||||
logger.Info(ctx, "Start updating image info")
|
||||
|
||||
// Get knowledge ID from URL path parameter
|
||||
id := c.Param("id")
|
||||
id := secutils.SanitizeForLog(c.Param("id"))
|
||||
if id == "" {
|
||||
logger.Error(ctx, "Knowledge ID is empty")
|
||||
c.Error(errors.NewBadRequestError("Knowledge ID cannot be empty"))
|
||||
return
|
||||
}
|
||||
chunkID := c.Param("chunk_id")
|
||||
if id == "" {
|
||||
chunkID := secutils.SanitizeForLog(c.Param("chunk_id"))
|
||||
if chunkID == "" {
|
||||
logger.Error(ctx, "Chunk ID is empty")
|
||||
c.Error(errors.NewBadRequestError("Chunk ID cannot be empty"))
|
||||
return
|
||||
@@ -571,7 +571,7 @@ func (h *KnowledgeHandler) UpdateImageInfo(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Update chunk properties
|
||||
logger.Infof(ctx, "Updating knowledge chunk, knowledge ID: %s, chunk ID: %s", secutils.SanitizeForLog(id), secutils.SanitizeForLog(chunkID))
|
||||
logger.Infof(ctx, "Updating knowledge chunk, knowledge ID: %s, chunk ID: %s", id, chunkID)
|
||||
err := h.kgService.UpdateImageInfo(ctx, id, chunkID, request.ImageInfo)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
@@ -579,7 +579,7 @@ func (h *KnowledgeHandler) UpdateImageInfo(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Knowledge chunk updated successfully, knowledge ID: %s, chunk ID: %s", secutils.SanitizeForLog(id), secutils.SanitizeForLog(chunkID))
|
||||
logger.Infof(ctx, "Knowledge chunk updated successfully, knowledge ID: %s, chunk ID: %s", id, chunkID)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"message": "Knowledge chunk image updated successfully",
|
||||
|
||||
@@ -84,7 +84,7 @@ func (h *MCPServiceHandler) ListMCPServices(c *gin.Context) {
|
||||
// GET /api/mcp-services/:id
|
||||
func (h *MCPServiceHandler) GetMCPService(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
serviceID := c.Param("id")
|
||||
serviceID := secutils.SanitizeForLog(c.Param("id"))
|
||||
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
if tenantID == 0 {
|
||||
@@ -110,7 +110,7 @@ func (h *MCPServiceHandler) GetMCPService(c *gin.Context) {
|
||||
// PUT /api/mcp-services/:id
|
||||
func (h *MCPServiceHandler) UpdateMCPService(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
serviceID := c.Param("id")
|
||||
serviceID := secutils.SanitizeForLog(c.Param("id"))
|
||||
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
if tenantID == 0 {
|
||||
@@ -227,7 +227,7 @@ func (h *MCPServiceHandler) UpdateMCPService(c *gin.Context) {
|
||||
// DELETE /api/mcp-services/:id
|
||||
func (h *MCPServiceHandler) DeleteMCPService(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
serviceID := c.Param("id")
|
||||
serviceID := secutils.SanitizeForLog(c.Param("id"))
|
||||
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
if tenantID == 0 {
|
||||
@@ -253,7 +253,7 @@ func (h *MCPServiceHandler) DeleteMCPService(c *gin.Context) {
|
||||
// POST /api/mcp-services/:id/test
|
||||
func (h *MCPServiceHandler) TestMCPService(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
serviceID := c.Param("id")
|
||||
serviceID := secutils.SanitizeForLog(c.Param("id"))
|
||||
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
if tenantID == 0 {
|
||||
@@ -288,7 +288,7 @@ func (h *MCPServiceHandler) TestMCPService(c *gin.Context) {
|
||||
// GET /api/mcp-services/:id/tools
|
||||
func (h *MCPServiceHandler) GetMCPServiceTools(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
serviceID := c.Param("id")
|
||||
serviceID := secutils.SanitizeForLog(c.Param("id"))
|
||||
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
if tenantID == 0 {
|
||||
@@ -314,7 +314,7 @@ func (h *MCPServiceHandler) GetMCPServiceTools(c *gin.Context) {
|
||||
// GET /api/mcp-services/:id/resources
|
||||
func (h *MCPServiceHandler) GetMCPServiceResources(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
serviceID := c.Param("id")
|
||||
serviceID := secutils.SanitizeForLog(c.Param("id"))
|
||||
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
if tenantID == 0 {
|
||||
|
||||
+15
-16
@@ -131,18 +131,18 @@ func (h *ModelHandler) GetModel(c *gin.Context) {
|
||||
|
||||
logger.Info(ctx, "Start retrieving model")
|
||||
|
||||
id := c.Param("id")
|
||||
id := secutils.SanitizeForLog(c.Param("id"))
|
||||
if id == "" {
|
||||
logger.Error(ctx, "Model ID is empty")
|
||||
c.Error(errors.NewBadRequestError("Model ID cannot be empty"))
|
||||
return
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Retrieving model, ID: %s", secutils.SanitizeForLog(id))
|
||||
logger.Infof(ctx, "Retrieving model, ID: %s", id)
|
||||
model, err := h.service.GetModelByID(ctx, id)
|
||||
if err != nil {
|
||||
if err == service.ErrModelNotFound {
|
||||
logger.Warnf(ctx, "Model not found, ID: %s", secutils.SanitizeForLog(id))
|
||||
logger.Warnf(ctx, "Model not found, ID: %s", id)
|
||||
c.Error(errors.NewNotFoundError("Model not found"))
|
||||
return
|
||||
}
|
||||
@@ -151,12 +151,12 @@ func (h *ModelHandler) GetModel(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Retrieved model successfully, ID: %s, Name: %s", secutils.SanitizeForLog(model.ID), secutils.SanitizeForLog(model.Name))
|
||||
logger.Infof(ctx, "Retrieved model successfully, ID: %s, Name: %s", model.ID, model.Name)
|
||||
|
||||
// Hide sensitive information for builtin models
|
||||
responseModel := hideSensitiveInfo(model)
|
||||
if model.IsBuiltin {
|
||||
logger.Infof(ctx, "Builtin model detected, hiding sensitive information for model: %s", secutils.SanitizeForLog(model.ID))
|
||||
logger.Infof(ctx, "Builtin model detected, hiding sensitive information for model: %s", model.ID)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
@@ -181,7 +181,6 @@ func (h *ModelHandler) ListModels(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Retrieving model list, Tenant ID: %d", tenantID)
|
||||
models, err := h.service.ListModels(ctx)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
@@ -196,7 +195,7 @@ func (h *ModelHandler) ListModels(c *gin.Context) {
|
||||
for i, model := range models {
|
||||
responseModels[i] = hideSensitiveInfo(model)
|
||||
if model.IsBuiltin {
|
||||
logger.Infof(ctx, "Builtin model detected in list, hiding sensitive information for model: %s", secutils.SanitizeForLog(model.ID))
|
||||
logger.Infof(ctx, "Builtin model detected in list, hiding sensitive information for model: %s", model.ID)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,7 +225,7 @@ func (h *ModelHandler) UpdateModel(c *gin.Context) {
|
||||
|
||||
logger.Info(ctx, "Start updating model")
|
||||
|
||||
id := c.Param("id")
|
||||
id := secutils.SanitizeForLog(c.Param("id"))
|
||||
if id == "" {
|
||||
logger.Error(ctx, "Model ID is empty")
|
||||
c.Error(errors.NewBadRequestError("Model ID cannot be empty"))
|
||||
@@ -240,11 +239,11 @@ func (h *ModelHandler) UpdateModel(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Retrieving model information, ID: %s", secutils.SanitizeForLog(id))
|
||||
logger.Infof(ctx, "Retrieving model information, ID: %s", id)
|
||||
model, err := h.service.GetModelByID(ctx, id)
|
||||
if err != nil {
|
||||
if err == service.ErrModelNotFound {
|
||||
logger.Warnf(ctx, "Model not found, ID: %s", secutils.SanitizeForLog(id))
|
||||
logger.Warnf(ctx, "Model not found, ID: %s", id)
|
||||
c.Error(errors.NewNotFoundError("Model not found"))
|
||||
return
|
||||
}
|
||||
@@ -264,14 +263,14 @@ func (h *ModelHandler) UpdateModel(c *gin.Context) {
|
||||
model.Source = req.Source
|
||||
model.Type = req.Type
|
||||
|
||||
logger.Infof(ctx, "Updating model, ID: %s, Name: %s", secutils.SanitizeForLog(id), secutils.SanitizeForLog(model.Name))
|
||||
logger.Infof(ctx, "Updating model, ID: %s, Name: %s", id, model.Name)
|
||||
if err := h.service.UpdateModel(ctx, model); err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(errors.NewInternalServerError(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Model updated successfully, ID: %s", secutils.SanitizeForLog(id))
|
||||
logger.Infof(ctx, "Model updated successfully, ID: %s", id)
|
||||
|
||||
// Hide sensitive information for builtin models (though builtin models cannot be updated)
|
||||
responseModel := hideSensitiveInfo(model)
|
||||
@@ -292,17 +291,17 @@ func (h *ModelHandler) DeleteModel(c *gin.Context) {
|
||||
|
||||
logger.Info(ctx, "Start deleting model")
|
||||
|
||||
id := c.Param("id")
|
||||
id := secutils.SanitizeForLog(c.Param("id"))
|
||||
if id == "" {
|
||||
logger.Error(ctx, "Model ID is empty")
|
||||
c.Error(errors.NewBadRequestError("Model ID cannot be empty"))
|
||||
return
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Deleting model, ID: %s", secutils.SanitizeForLog(id))
|
||||
logger.Infof(ctx, "Deleting model, ID: %s", id)
|
||||
if err := h.service.DeleteModel(ctx, id); err != nil {
|
||||
if err == service.ErrModelNotFound {
|
||||
logger.Warnf(ctx, "Model not found, ID: %s", secutils.SanitizeForLog(id))
|
||||
logger.Warnf(ctx, "Model not found, ID: %s", id)
|
||||
c.Error(errors.NewNotFoundError("Model not found"))
|
||||
return
|
||||
}
|
||||
@@ -311,7 +310,7 @@ func (h *ModelHandler) DeleteModel(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Model deleted successfully, ID: %s", secutils.SanitizeForLog(id))
|
||||
logger.Infof(ctx, "Model deleted successfully, ID: %s", id)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"message": "Model deleted",
|
||||
|
||||
@@ -74,13 +74,12 @@ func (h *Handler) KnowledgeQA(c *gin.Context) {
|
||||
logger.Info(ctx, "Start processing knowledge QA request")
|
||||
|
||||
// Get session ID from URL parameter
|
||||
sessionID := c.Param("session_id")
|
||||
sessionID := secutils.SanitizeForLog(c.Param("session_id"))
|
||||
if sessionID == "" {
|
||||
logger.Error(ctx, "Session ID is empty")
|
||||
c.Error(errors.NewBadRequestError(errors.ErrInvalidSessionID.Error()))
|
||||
return
|
||||
}
|
||||
safeSessionID := secutils.SanitizeForLog(sessionID)
|
||||
|
||||
// Parse request body
|
||||
var request CreateKnowledgeQARequest
|
||||
@@ -105,12 +104,12 @@ func (h *Handler) KnowledgeQA(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Knowledge QA request, session ID: %s, query: %s", safeSessionID, secutils.SanitizeForLog(request.Query))
|
||||
logger.Infof(ctx, "Knowledge QA request, session ID: %s, query: %s", sessionID, secutils.SanitizeForLog(request.Query))
|
||||
|
||||
// Get session to prepare knowledge base IDs
|
||||
session, err := h.sessionService.GetSession(ctx, sessionID)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to get session, session ID: %s, error: %v", safeSessionID, err)
|
||||
logger.Errorf(ctx, "Failed to get session, session ID: %s, error: %v", sessionID, err)
|
||||
c.Error(errors.NewInternalServerError(err.Error()))
|
||||
return
|
||||
}
|
||||
@@ -132,13 +131,12 @@ func (h *Handler) AgentQA(c *gin.Context) {
|
||||
logger.Info(ctx, "Start processing agent QA request")
|
||||
|
||||
// Get session ID from URL parameter
|
||||
sessionID := c.Param("session_id")
|
||||
sessionID := secutils.SanitizeForLog(c.Param("session_id"))
|
||||
if sessionID == "" {
|
||||
logger.Error(ctx, "Session ID is empty")
|
||||
c.Error(errors.NewBadRequestError(errors.ErrInvalidSessionID.Error()))
|
||||
return
|
||||
}
|
||||
safeSessionID := secutils.SanitizeForLog(sessionID)
|
||||
|
||||
// Parse request body
|
||||
var request CreateKnowledgeQARequest
|
||||
@@ -165,13 +163,13 @@ func (h *Handler) AgentQA(c *gin.Context) {
|
||||
// Get session information first
|
||||
session, err := h.sessionService.GetSession(ctx, sessionID)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to get session, session ID: %s, error: %v", secutils.SanitizeForLog(sessionID), err)
|
||||
logger.Errorf(ctx, "Failed to get session, session ID: %s, error: %v", sessionID, err)
|
||||
c.Error(errors.NewNotFoundError("Session not found"))
|
||||
return
|
||||
}
|
||||
sessionJSON, err := json.Marshal(session)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to marshal session, session ID: %s, error: %v", secutils.SanitizeForLog(sessionID), err)
|
||||
logger.Errorf(ctx, "Failed to marshal session, session ID: %s, error: %v", sessionID, err)
|
||||
c.Error(errors.NewInternalServerError(err.Error()))
|
||||
return
|
||||
}
|
||||
@@ -248,14 +246,14 @@ func (h *Handler) AgentQA(c *gin.Context) {
|
||||
|
||||
// If configuration changed, clear context and update session
|
||||
if configChanged {
|
||||
logger.Warnf(ctx, "Configuration changed, clearing context for session: %s", safeSessionID)
|
||||
logger.Warnf(ctx, "Configuration changed, clearing context for session: %s", sessionID)
|
||||
// Clear the LLM context to prevent contamination
|
||||
if err := h.sessionService.ClearContext(ctx, sessionID); err != nil {
|
||||
logger.Errorf(ctx, "Failed to clear context for session %s: %v", safeSessionID, err)
|
||||
logger.Errorf(ctx, "Failed to clear context for session %s: %v", sessionID, err)
|
||||
// Continue anyway - this is not a fatal error
|
||||
}
|
||||
if err := h.sessionService.DeleteWebSearchTempKBState(ctx, sessionID); err != nil {
|
||||
logger.Errorf(ctx, "Failed to delete temp knowledge base for session %s: %v", safeSessionID, err)
|
||||
logger.Errorf(ctx, "Failed to delete temp knowledge base for session %s: %v", sessionID, err)
|
||||
// Continue anyway - this is not a fatal error
|
||||
}
|
||||
session.AgentConfig.KnowledgeBases = request.KnowledgeBaseIDs
|
||||
@@ -264,16 +262,16 @@ func (h *Handler) AgentQA(c *gin.Context) {
|
||||
session.SummaryModelID = summaryModelID
|
||||
// Persist the session changes
|
||||
if err := h.sessionService.UpdateSession(ctx, session); err != nil {
|
||||
logger.Errorf(ctx, "Failed to update session %s: %v", safeSessionID, err)
|
||||
logger.Errorf(ctx, "Failed to update session %s: %v", sessionID, err)
|
||||
c.Error(errors.NewInternalServerError("Failed to update session configuration"))
|
||||
return
|
||||
}
|
||||
logger.Infof(ctx, "Session configuration updated successfully for session: %s", safeSessionID)
|
||||
logger.Infof(ctx, "Session configuration updated successfully for session: %s", sessionID)
|
||||
}
|
||||
|
||||
// If Agent mode is disabled, delegate to KnowledgeQA
|
||||
if !request.AgentEnabled {
|
||||
logger.Infof(ctx, "Agent mode disabled, delegating to KnowledgeQA for session: %s", safeSessionID)
|
||||
logger.Infof(ctx, "Agent mode disabled, delegating to KnowledgeQA for session: %s", sessionID)
|
||||
|
||||
// Use knowledge bases from request or session config
|
||||
knowledgeBaseIDs := request.KnowledgeBaseIDs
|
||||
@@ -334,7 +332,7 @@ func (h *Handler) AgentQA(c *gin.Context) {
|
||||
}
|
||||
assistantMessage = assistantMessagePtr
|
||||
|
||||
logger.Infof(ctx, "Calling agent QA service, session ID: %s", safeSessionID)
|
||||
logger.Infof(ctx, "Calling agent QA service, session ID: %s", sessionID)
|
||||
|
||||
// Write initial agent_query event to StreamManager
|
||||
h.writeAgentQueryEvent(ctx, sessionID, assistantMessage.ID)
|
||||
@@ -347,7 +345,7 @@ func (h *Handler) AgentQA(c *gin.Context) {
|
||||
|
||||
// Start async title generation if session has no title
|
||||
if session.Title == "" {
|
||||
logger.Infof(ctx, "Session has no title, starting async title generation, session ID: %s", safeSessionID)
|
||||
logger.Infof(ctx, "Session has no title, starting async title generation, session ID: %s", sessionID)
|
||||
h.sessionService.GenerateTitleAsync(asyncCtx, session, request.Query, eventBus)
|
||||
}
|
||||
|
||||
@@ -362,11 +360,11 @@ func (h *Handler) AgentQA(c *gin.Context) {
|
||||
logger.ErrorWithFields(asyncCtx,
|
||||
errors.NewInternalServerError(fmt.Sprintf("Agent QA service panicked: %v\n%s", r, string(buf))),
|
||||
map[string]interface{}{
|
||||
"session_id": safeSessionID,
|
||||
"session_id": sessionID,
|
||||
})
|
||||
}
|
||||
h.completeAssistantMessage(asyncCtx, assistantMessage)
|
||||
logger.Infof(asyncCtx, "Agent QA service completed for session: %s", safeSessionID)
|
||||
logger.Infof(asyncCtx, "Agent QA service completed for session: %s", sessionID)
|
||||
}()
|
||||
err := h.sessionService.AgentQA(asyncCtx, session, request.Query, assistantMessage.ID, eventBus)
|
||||
if err != nil {
|
||||
@@ -403,7 +401,6 @@ func (h *Handler) handleKnowledgeQARequest(
|
||||
webSearchEnabled bool, // Whether web search is enabled
|
||||
) {
|
||||
sessionID := session.ID
|
||||
safeSessionID := secutils.SanitizeForLog(sessionID)
|
||||
requestID := getRequestID(c)
|
||||
|
||||
// Create user message
|
||||
@@ -444,7 +441,7 @@ func (h *Handler) handleKnowledgeQARequest(
|
||||
|
||||
// Generate title if needed
|
||||
if generateTitle && session.Title == "" {
|
||||
logger.Infof(ctx, "Session has no title, starting async title generation, session ID: %s", safeSessionID)
|
||||
logger.Infof(ctx, "Session has no title, starting async title generation, session ID: %s", sessionID)
|
||||
h.sessionService.GenerateTitleAsync(asyncCtx, session, query, eventBus)
|
||||
}
|
||||
|
||||
@@ -455,7 +452,7 @@ func (h *Handler) handleKnowledgeQARequest(
|
||||
}
|
||||
assistantMessage.Content += data.Content
|
||||
if data.Done {
|
||||
logger.Infof(asyncCtx, "Knowledge QA service completed for session: %s", safeSessionID)
|
||||
logger.Infof(asyncCtx, "Knowledge QA service completed for session: %s", sessionID)
|
||||
h.completeAssistantMessage(asyncCtx, assistantMessage)
|
||||
// Emit completion event when stream finishes
|
||||
if err := eventBus.Emit(asyncCtx, event.Event{
|
||||
|
||||
@@ -388,7 +388,7 @@ func (h *TenantHandler) updateTenantAgentConfigInternal(c *gin.Context) {
|
||||
// - "web-search-config": returns masked tenant.WebSearchConfig (API key masked)
|
||||
func (h *TenantHandler) GetTenantKV(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
key := c.Param("key")
|
||||
key := secutils.SanitizeForLog(c.Param("key"))
|
||||
|
||||
switch key {
|
||||
case "agent-config":
|
||||
@@ -411,7 +411,7 @@ func (h *TenantHandler) GetTenantKV(c *gin.Context) {
|
||||
// Body is the JSON value to set for the key.
|
||||
func (h *TenantHandler) UpdateTenantKV(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
key := c.Param("key")
|
||||
key := secutils.SanitizeForLog(c.Param("key"))
|
||||
|
||||
switch key {
|
||||
case "agent-config":
|
||||
|
||||
Reference in New Issue
Block a user