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