refactor: Sanitize log inputs in various handlers to enhance security
This commit is contained in:
+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"))
|
||||
|
||||
Reference in New Issue
Block a user