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:
@@ -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,
|
||||
) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user