refactor: Sanitize log inputs in mcp_service, duckduckgo, auth, faq, message, and model handlers to enhance security

This commit is contained in:
wizardchen
2025-11-26 00:57:33 +08:00
parent 9fc6c949e5
commit 8e3c2d8547
6 changed files with 19 additions and 14 deletions
+7 -6
View File
@@ -9,6 +9,7 @@ import (
"github.com/Tencent/WeKnora/internal/mcp"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
secutils "github.com/Tencent/WeKnora/internal/utils"
)
// mcpServiceService implements MCPServiceService interface
@@ -175,24 +176,24 @@ func (s *mcpServiceService) UpdateMCPService(ctx context.Context, service *types
if service.AuthConfig != nil {
configChanged = true
}
name := secutils.SanitizeForLog(existing.Name)
// Close existing client connection if:
// 1. Service is now disabled (need to close connection)
// 2. Critical configuration changed (need to reconnect with new config)
if !existing.Enabled {
s.mcpManager.CloseClient(service.ID)
logger.GetLogger(ctx).Infof("MCP service disabled, connection closed: %s (ID: %s)", existing.Name, service.ID)
logger.GetLogger(ctx).Infof("MCP service disabled, connection closed: %s (ID: %s)", name, service.ID)
} else if configChanged {
s.mcpManager.CloseClient(service.ID)
logger.GetLogger(ctx).Infof("MCP service config changed, connection closed for reconnect: %s (ID: %s)", existing.Name, service.ID)
logger.GetLogger(ctx).Infof("MCP service config changed, connection closed for reconnect: %s (ID: %s)", name, service.ID)
} else if oldEnabled != existing.Enabled && existing.Enabled {
// Service was just enabled (was disabled, now enabled)
// Close any existing connection to ensure clean state
s.mcpManager.CloseClient(service.ID)
logger.GetLogger(ctx).Infof("MCP service enabled, existing connection closed for clean state: %s (ID: %s)", existing.Name, service.ID)
logger.GetLogger(ctx).Infof("MCP service enabled, existing connection closed for clean state: %s (ID: %s)", name, service.ID)
}
logger.GetLogger(ctx).Infof("MCP service updated: %s (ID: %s), enabled: %v", existing.Name, service.ID, existing.Enabled)
logger.GetLogger(ctx).Infof("MCP service updated: %s (ID: %s), enabled: %v", name, service.ID, existing.Enabled)
return nil
}
@@ -215,7 +216,7 @@ func (s *mcpServiceService) DeleteMCPService(ctx context.Context, tenantID uint6
return fmt.Errorf("failed to delete MCP service: %w", err)
}
logger.GetLogger(ctx).Infof("MCP service deleted: %s (ID: %s)", existing.Name, id)
logger.GetLogger(ctx).Infof("MCP service deleted: %s (ID: %s)", secutils.SanitizeForLog(existing.Name), id)
return nil
}
@@ -15,6 +15,7 @@ import (
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
secutils "github.com/Tencent/WeKnora/internal/utils"
)
// DuckDuckGoProvider implements web search using DuckDuckGo (HTML first, API fallback)
@@ -74,7 +75,7 @@ func (p *DuckDuckGoProvider) searchHTML(ctx context.Context, query string, maxRe
// print curl of request
curlCommand := fmt.Sprintf("curl -X GET '%s' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'", req.URL.String())
logger.Infof(ctx, "Curl of request: %s", curlCommand)
logger.Infof(ctx, "Curl of request: %s", secutils.SanitizeForLog(curlCommand))
resp, err := p.client.Do(req)
if err != nil {
+3 -1
View File
@@ -10,6 +10,7 @@ import (
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
secutils "github.com/Tencent/WeKnora/internal/utils"
)
// AuthHandler implements HTTP request handlers for user authentication
@@ -58,7 +59,8 @@ func (h *AuthHandler) Register(c *gin.Context) {
c.Error(appErr)
return
}
req.Username = secutils.SanitizeForLog(req.Username)
req.Email = secutils.SanitizeForLog(req.Email)
// Call service to register user
user, err := h.userService.Register(ctx, &req)
if err != nil {
+1 -1
View File
@@ -174,7 +174,7 @@ func (h *FAQHandler) SearchFAQ(c *gin.Context) {
c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
return
}
req.QueryText = secutils.SanitizeForLog(req.QueryText)
entries, err := h.knowledgeService.SearchFAQEntries(ctx, secutils.SanitizeForLog(c.Param("id")), &req)
if err != nil {
logger.ErrorWithFields(ctx, err, nil)
+5 -4
View File
@@ -10,6 +10,7 @@ import (
"github.com/Tencent/WeKnora/internal/errors"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/types/interfaces"
secutils "github.com/Tencent/WeKnora/internal/utils"
)
// MessageHandler handles HTTP requests related to messages within chat sessions
@@ -39,8 +40,8 @@ func (h *MessageHandler) LoadMessages(c *gin.Context) {
// Get path parameters and query parameters
sessionID := c.Param("session_id")
limit := c.DefaultQuery("limit", "20")
beforeTimeStr := c.DefaultQuery("before_time", "")
limit := secutils.SanitizeForLog(c.DefaultQuery("limit", "20"))
beforeTimeStr := secutils.SanitizeForLog(c.DefaultQuery("before_time", ""))
logger.Infof(ctx, "Loading messages params, session ID: %s, limit: %s, before time: %s",
sessionID, limit, beforeTimeStr)
@@ -115,8 +116,8 @@ func (h *MessageHandler) DeleteMessage(c *gin.Context) {
logger.Info(ctx, "Start deleting message")
// Get path parameters for session and message identification
sessionID := c.Param("session_id")
messageID := c.Param("id")
sessionID := secutils.SanitizeForLog(c.Param("session_id"))
messageID := secutils.SanitizeForLog(c.Param("id"))
logger.Infof(ctx, "Deleting message, session ID: %s, message ID: %s", sessionID, messageID)
+1 -1
View File
@@ -84,7 +84,7 @@ func (h *ModelHandler) CreateModel(c *gin.Context) {
c.Error(errors.NewBadRequestError(err.Error()))
return
}
req.Name = secutils.SanitizeForLog(req.Name)
tenantID := c.GetUint64(types.TenantIDContextKey.String())
if tenantID == 0 {
logger.Error(ctx, "Tenant ID is empty")