From e0c1b77b70b862d7045b714f9439bbe4cf3433c3 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Mon, 16 Mar 2026 18:01:07 +0800 Subject: [PATCH] fix: quote Content-Disposition filename and add empty-title fallback - Use mime.FormatMediaType for both attachment and inline Content-Disposition headers in DownloadKnowledgeFile and the preview handler, preventing header injection via a user-controlled knowledge title (e.g. containing ';' or '=') - Extract inline sanitize logic into sanitizeManualDownloadFilename helper and add a blank/whitespace-only title fallback to "untitled" so the download never produces a degenerate ".md" filename Co-Authored-By: Claude Sonnet 4.6 --- internal/application/service/knowledge.go | 20 +++++++++++++++----- internal/handler/knowledge.go | 6 ++++-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/internal/application/service/knowledge.go b/internal/application/service/knowledge.go index 42961c8b..1738f539 100644 --- a/internal/application/service/knowledge.go +++ b/internal/application/service/knowledge.go @@ -2425,11 +2425,7 @@ func (s *knowledgeService) GetKnowledgeFile(ctx context.Context, id string) (io. if meta != nil { content = meta.Content } - // Sanitize title: strip characters that are invalid or dangerous in HTTP headers / filenames. - safeName := strings.NewReplacer( - "\n", "", "\r", "", "/", "-", "\\", "-", "\"", "'", - ).Replace(knowledge.Title) - filename := safeName + ".md" + filename := sanitizeManualDownloadFilename(knowledge.Title) return io.NopCloser(strings.NewReader(content)), filename, nil } @@ -6750,6 +6746,20 @@ func ensureManualFileName(title string) string { return trimmed + manualFileExtension } +// sanitizeManualDownloadFilename converts a knowledge title into a safe .md +// download filename. Characters that are illegal or dangerous in HTTP header +// values and file-system paths are removed or replaced; a blank result falls +// back to "untitled". +func sanitizeManualDownloadFilename(title string) string { + safeName := strings.NewReplacer( + "\n", "", "\r", "", "/", "-", "\\", "-", "\"", "'", + ).Replace(title) + if strings.TrimSpace(safeName) == "" { + safeName = "untitled" + } + return safeName + manualFileExtension +} + func (s *knowledgeService) triggerManualProcessing(ctx context.Context, kb *types.KnowledgeBase, knowledge *types.Knowledge, content string, doSync bool, ) { diff --git a/internal/handler/knowledge.go b/internal/handler/knowledge.go index 6c10754a..a475c4b3 100644 --- a/internal/handler/knowledge.go +++ b/internal/handler/knowledge.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "io" + "mime" "net/http" "strconv" "strings" @@ -664,7 +665,8 @@ func (h *KnowledgeHandler) DownloadKnowledgeFile(c *gin.Context) { // Set response headers for file download c.Header("Content-Description", "File Transfer") c.Header("Content-Transfer-Encoding", "binary") - c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename)) + cd := mime.FormatMediaType("attachment", map[string]string{"filename": filename}) + c.Header("Content-Disposition", cd) c.Header("Content-Type", "application/octet-stream") c.Header("Expires", "0") c.Header("Cache-Control", "must-revalidate") @@ -765,7 +767,7 @@ func (h *KnowledgeHandler) PreviewKnowledgeFile(c *gin.Context) { contentType := mimeTypeByExt(filename) c.Header("Content-Type", contentType) - c.Header("Content-Disposition", fmt.Sprintf("inline; filename=%s", filename)) + c.Header("Content-Disposition", mime.FormatMediaType("inline", map[string]string{"filename": filename})) c.Header("Cache-Control", "private, max-age=3600") c.Stream(func(w io.Writer) bool {