fix(knowledge): allow xlsx and other file types in URL import (#2447)

Align URL-based file import validation with direct file upload by
delegating to isValidFileType instead of a narrower whitelist.
main
wizardchen 2026-07-31 12:34:01 +08:00 committed by lyingbug
parent 0d58af8d82
commit 48756d13d6
4 changed files with 38 additions and 18 deletions

View File

@ -289,7 +289,7 @@ func isFileURL(rawURL, fileName, fileType string) bool {
u, err := url.Parse(rawURL)
if err == nil {
ext := strings.ToLower(strings.TrimPrefix(path.Ext(u.Path), "."))
if ext != "" && allowedFileURLExtensions[ext] {
if ext != "" && isAllowedFileURLExtension(ext) {
return true
}
}
@ -467,20 +467,6 @@ func (s *knowledgeService) CreateKnowledgeFromURL(ctx context.Context,
return knowledge, nil
}
// allowedFileURLExtensions defines the supported file extensions for file URL import
var allowedFileURLExtensions = map[string]bool{
"txt": true,
"md": true,
"pdf": true,
"docx": true,
"doc": true,
"mp3": true,
"wav": true,
"m4a": true,
"flac": true,
"ogg": true,
}
// maxFileURLSize is the maximum allowed file size for file URL import (10MB)
const maxFileURLSize = 10 * 1024 * 1024
@ -564,9 +550,9 @@ func (s *knowledgeService) createKnowledgeFromFileURL(
// Validate file extension against whitelist (if we can determine it)
if fileType != "" {
if !allowedFileURLExtensions[strings.ToLower(fileType)] {
if !isAllowedFileURLExtension(fileType) {
logger.Errorf(ctx, "Unsupported file type for file URL import: %s", fileType)
return nil, werrors.NewBadRequestError(fmt.Sprintf("不支持的文件类型: %s,仅支持 txt, md, pdf, docx, doc", fileType))
return nil, werrors.NewBadRequestError(fmt.Sprintf("不支持的文件类型: %s", fileType))
}
}

View File

@ -3185,7 +3185,7 @@ func (s *knowledgeService) ProcessDocument(ctx context.Context, t *asynq.Task) e
return fmt.Errorf("failed to download file from URL: %w", err)
}
if resolvedFileType != "" && !allowedFileURLExtensions[strings.ToLower(resolvedFileType)] {
if resolvedFileType != "" && !isAllowedFileURLExtension(resolvedFileType) {
logger.Errorf(ctx, "Unsupported file type resolved from file URL: %s", resolvedFileType)
knowledge.ParseStatus = "failed"
knowledge.ErrorMessage = fmt.Sprintf("unsupported file type: %s", resolvedFileType)

View File

@ -30,6 +30,15 @@ func isValidFileType(filename string) bool {
}
}
// isAllowedFileURLExtension checks whether a file extension is supported for URL-based file import.
func isAllowedFileURLExtension(ext string) bool {
ext = strings.ToLower(strings.TrimPrefix(ext, "."))
if ext == "" {
return false
}
return isValidFileType("file." + ext)
}
// getFileType extracts the file extension from a filename
func getFileType(filename string) string {
ext := strings.Split(filename, ".")

View File

@ -22,3 +22,28 @@ func TestIsValidFileTypeHTML(t *testing.T) {
})
}
}
func TestIsAllowedFileURLExtension(t *testing.T) {
tests := []struct {
name string
ext string
want bool
}{
{name: "xlsx", ext: "xlsx", want: true},
{name: "xls", ext: "xls", want: true},
{name: "csv", ext: "csv", want: true},
{name: "dot prefix", ext: ".xlsx", want: true},
{name: "uppercase", ext: "XLSX", want: true},
{name: "pdf", ext: "pdf", want: true},
{name: "unsupported", ext: "exe", want: false},
{name: "empty", ext: "", want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isAllowedFileURLExtension(tt.ext); got != tt.want {
t.Fatalf("isAllowedFileURLExtension(%q) = %v, want %v", tt.ext, got, tt.want)
}
})
}
}