Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix: quote Content-Disposition filename and add empty-title fallback

wizardchene0c1b77b70b8
diff --git a/internal/application/service/knowledge.go b/internal/application/service/knowledge.go
index 42961c8b6..1738f5391 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 6c10754a5..a475c4b3b 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 {