Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(agent): cap generated document names at 180 characters and 255 bytes (#20909)

Zhengxu (Joshua) Jine8837e95670a
diff --git a/internal/agent/component/docs_generator.go b/internal/agent/component/docs_generator.go
index 25d13414d7..638eb92238 100644
--- a/internal/agent/component/docs_generator.go
+++ b/internal/agent/component/docs_generator.go
@@ -470,11 +470,13 @@ func formatExtension(format string) string {
// sanitizeFilename applies the plan §2.11.5 helper: strip forbidden
// chars, collapse whitespace, cap the base at 180 chars, and append the
-// conventional extension when missing. Returns "file.<ext>" when the
-// resulting base is empty.
+// conventional extension when missing. The whole name is then cut to the
+// 255-byte filename limit on a rune boundary, keeping the extension.
+// Returns "file.<ext>" when the resulting base is empty.
func sanitizeFilename(raw, ext string) string {
const forbidden = `\/:*?"<>|`
const maxBase = 180
+ const maxBytes = 255
trimmed := strings.TrimSpace(raw)
// Strip control characters first; they're never valid in filenames.
var b strings.Builder
@@ -488,16 +490,16 @@ func sanitizeFilename(raw, ext string) string {
b.WriteRune(r)
}
base := strings.Join(strings.Fields(b.String()), "_")
- if len(base) > maxBase {
- base = base[:maxBase]
+ if runes := []rune(base); len(runes) > maxBase {
+ base = string(runes[:maxBase])
}
if base == "" {
return "file" + ext
}
if ext != "" && !strings.HasSuffix(strings.ToLower(base), strings.ToLower(ext)) {
- return base + ext
+ base += ext
}
- return base
+ return utility.TruncateFilename(base, maxBytes)
}
// renderTXT / renderMarkdown / renderHTML live in
diff --git a/internal/agent/component/docs_generator_test.go b/internal/agent/component/docs_generator_test.go
index a460cca662..965b235a68 100644
--- a/internal/agent/component/docs_generator_test.go
+++ b/internal/agent/component/docs_generator_test.go
@@ -20,6 +20,7 @@ import (
"encoding/json"
"strings"
"testing"
+ "unicode/utf8"
"ragflow/internal/agent/canvas"
"ragflow/internal/storage"
@@ -248,3 +249,34 @@ func TestResolveDocsGeneratorContent_ThinkAndTrim(t *testing.T) {
t.Errorf("content = %q, want %q", got, "plain")
}
}
+
+// TestSanitizeFilename_CapsLength pins the two caps on generated names: 180
+// characters for the base, then 255 bytes for the whole name. Both cuts land
+// on a rune boundary and keep the extension; cutting bytes mid-rune left
+// invalid UTF-8 in the filename and download URL.
+func TestSanitizeFilename_CapsLength(t *testing.T) {
+ cases := []struct {
+ name string
+ raw string
+ ext string
+ want string
+ }{
+ {"ascii over 180 chars", strings.Repeat("x", 200), ".pdf", strings.Repeat("x", 180) + ".pdf"},
+ {"cjk at 255 bytes", strings.Repeat("报", 83), ".pdf", strings.Repeat("报", 83) + ".pdf"},
+ {"cjk over 255 bytes", strings.Repeat("报", 84), ".pdf", strings.Repeat("报", 83) + ".pdf"},
+ {"cjk over both caps", strings.Repeat("报", 200), ".pdf", strings.Repeat("报", 83) + ".pdf"},
+ // The leading "a" puts the 255-byte boundary inside a 3-byte character.
+ {"mixed cut on a character", "a" + strings.Repeat("报", 200), ".pdf", "a" + strings.Repeat("报", 83) + ".pdf"},
+ {"longer extension", strings.Repeat("报", 100), ".docx", strings.Repeat("报", 83) + ".docx"},
+ {"extension already present", strings.Repeat("报", 100) + ".PDF", ".pdf", strings.Repeat("报", 83) + ".PDF"},
+ }
+ for _, c := range cases {
+ got := sanitizeFilename(c.raw, c.ext)
+ if got != c.want {
+ t.Errorf("%s: sanitizeFilename = %q (%d bytes), want %q (%d bytes)", c.name, got, len(got), c.want, len(c.want))
+ }
+ if len(got) > 255 || !utf8.ValidString(got) || !strings.HasSuffix(strings.ToLower(got), c.ext) {
+ t.Errorf("%s: %q is longer than 255 bytes, not valid UTF-8 or lacks %s", c.name, got, c.ext)
+ }
+ }
+}
diff --git a/internal/service/file/file_url.go b/internal/service/file/file_url.go
index 50ee05699f..89461a2062 100644
--- a/internal/service/file/file_url.go
+++ b/internal/service/file/file_url.go
@@ -8,7 +8,6 @@ import (
"ragflow/internal/storage"
"ragflow/internal/utility"
"strings"
- "unicode/utf8"
)
// maxUploadFilenameBytes is the longest name checkUploadInfoHealth accepts.
@@ -67,7 +66,7 @@ func normalizeRemoteUploadFilename(rawURL, contentType string, data []byte) stri
}
// The appended ".pdf" can push a name that sanitizeFilename already
// shortened back over the limit.
- return truncateFilename(filename, maxUploadFilenameBytes)
+ return utility.TruncateFilename(filename, maxUploadFilenameBytes)
}
func sanitizeFilename(name string) string {
@@ -93,23 +92,5 @@ func sanitizeFilename(name string) string {
if stem := strings.SplitN(strings.ToUpper(name), ".", 2)[0]; reservedDeviceNames[stem] {
return "download"
}
- return truncateFilename(name, maxUploadFilenameBytes)
-}
-
-// truncateFilename shortens name to at most maxBytes bytes. It keeps the
-// extension when it fits and cuts on a rune boundary, so the result stays
-// valid UTF-8 and keeps the file type the extension implies.
-func truncateFilename(name string, maxBytes int) string {
- if len(name) <= maxBytes {
- return name
- }
- ext := filepath.Ext(name)
- if len(ext) >= maxBytes {
- ext = ""
- }
- end := maxBytes - len(ext)
- for end > 0 && !utf8.RuneStart(name[end]) {
- end--
- }
- return name[:end] + ext
+ return utility.TruncateFilename(name, maxUploadFilenameBytes)
}
diff --git a/internal/utility/file.go b/internal/utility/file.go
index c6cd3902d9..7d22d62ae4 100644
--- a/internal/utility/file.go
+++ b/internal/utility/file.go
@@ -23,6 +23,7 @@ import (
"path/filepath"
"regexp"
"strings"
+ "unicode/utf8"
)
type FileType string
@@ -213,6 +214,24 @@ func GetFileExtension(filename string) string {
return strings.ToLower(ext)
}
+// TruncateFilename shortens name to at most maxBytes bytes. It keeps the
+// extension when it fits and cuts on a rune boundary, so the result stays
+// valid UTF-8 and keeps the file type the extension implies.
+func TruncateFilename(name string, maxBytes int) string {
+ if len(name) <= maxBytes {
+ return name
+ }
+ ext := filepath.Ext(name)
+ if len(ext) >= maxBytes {
+ ext = ""
+ }
+ end := maxBytes - len(ext)
+ for end > 0 && !utf8.RuneStart(name[end]) {
+ end--
+ }
+ return name[:end] + ext
+}
+
// CONTENT_TYPE_MAP maps file extensions to MIME content types
var CONTENT_TYPE_MAP = map[string]string{
// Office