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 <noreply@anthropic.com>
This commit is contained in:
wizardchen
2026-03-16 18:01:07 +08:00
parent 63caf564ee
commit e0c1b77b70
2 changed files with 19 additions and 7 deletions
+4 -2
View File
@@ -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 {