RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(parser): parse email attachments whose names are RFC 2047 encoded (#21361)
Lukas5c478dee8b7e
diff --git a/internal/parser/parser/email_parser.go b/internal/parser/parser/email_parser.goindex 9aff694217..158b7a26d6 100644--- a/internal/parser/parser/email_parser.go+++ b/internal/parser/parser/email_parser.go@@ -390,16 +390,24 @@ func isAttachmentPart(part *multipart.Part) bool {}// attachmentFilename extracts a filename from the part's-// Content-Disposition or Content-Type headers.+// Content-Disposition or Content-Type headers. Mail clients write a+// non-ASCII name as RFC 2047 encoded-words inside the parameter, as in+// filename="=?utf-8?B?...?=", and the mime package leaves them encoded. Such+// a name has no file extension, so rechunkEmailAttachments skipped the+// attachment; it is decoded here. The filename is decoded before+// filepath.Base drops any directory from it, as part.FileName does, because a+// base64 encoded-word can contain a "/".func attachmentFilename(part *multipart.Part) string {- if fn := part.FileName(); fn != "" {- return fn+ if _, params, err := mime.ParseMediaType(part.Header.Get("Content-Disposition")); err == nil {+ if fn := params["filename"]; fn != "" {+ return filepath.Base(decodeHeaderWord(fn))+ }}ct := part.Header.Get("Content-Type")if ct != "" {_, params, _ := mime.ParseMediaType(ct)if name, ok := params["name"]; ok {- return name+ return decodeHeaderWord(name)}}return "attachment.bin"diff --git a/internal/parser/parser/email_parser_test.go b/internal/parser/parser/email_parser_test.goindex 0d803d3691..42cb1d567d 100644--- a/internal/parser/parser/email_parser_test.go+++ b/internal/parser/parser/email_parser_test.go@@ -110,6 +110,69 @@ func TestEmailParser_EmlJSON(t *testing.T) {}}+// A non-ASCII attachment name arrives as RFC 2047 encoded-words inside the+// filename or name parameter, from Outlook, Gmail and Chinese mail clients+// alike. Left encoded, the name had no extension and the attachment was not+// parsed. 合同草稿.txt in base64 contains a "/".+func TestEmailParser_EmlParsesAttachmentsWithEncodedNames(t *testing.T) {+ b64 := func(charset string, raw []byte) string {+ return "=?" + charset + "?B?" + base64.StdEncoding.EncodeToString(raw) + "?="+ }+ attachment := func(headers []string, text string) string {+ return strings.Join(append(append([]string{"--b"}, headers...), "", text), "\r\n")+ }+ raw := strings.Join([]string{+ "From: [email protected]",+ "Subject: Attachments",+ "MIME-Version: 1.0",+ "Content-Type: multipart/mixed; boundary=b",+ "",+ "--b",+ "Content-Type: text/plain; charset=utf-8",+ "",+ "Body.",+ attachment([]string{+ "Content-Type: text/plain; charset=utf-8",+ `Content-Disposition: attachment; filename="` + b64("utf-8", []byte("Übersicht.txt")) + `"`,+ }, "First attachment text."),+ attachment([]string{+ "Content-Type: text/plain; charset=utf-8",+ `Content-Disposition: attachment; filename="` + b64("gb2312", []byte{0xD4, 0xA4, 0xCB, 0xE3, 0xB1, 0xA8, 0xB8, 0xE6, '.', 't', 'x', 't'}) + `"`,+ }, "Second attachment text."),+ attachment([]string{+ "Content-Type: text/plain; charset=utf-8",+ `Content-Disposition: attachment; filename="` + b64("utf-8", []byte("合同草稿.txt")) + `"`,+ }, "Third attachment text."),+ attachment([]string{+ "Content-Type: text/plain; charset=utf-8",+ `Content-Disposition: attachment; filename="=?iso-8859-1?Q?R=E9sum=E9.txt?="`,+ }, "Fourth attachment text."),+ attachment([]string{+ `Content-Type: text/plain; charset=utf-8; name="` + b64("utf-8", []byte("Notizen.txt")) + `"`,+ "Content-Disposition: attachment",+ }, "Fifth attachment text."),+ "--b--",+ }, "\r\n")++ p := NewEmailParser()+ p.ConfigureFromSetup(map[string]any{"fields": []string{"body", "attachments"}})+ result := p.ParseWithResult(t.Context(), "attachments.eml", []byte(raw))+ if result.Err != nil {+ t.Fatalf("unexpected error: %v", result.Err)+ }+ var texts []string+ for _, item := range result.JSON {+ text, _ := item.ToMap()["text"].(string)+ texts = append(texts, text)+ }+ joined := strings.Join(texts, "\n")+ for _, want := range []string{"First", "Second", "Third", "Fourth", "Fifth"} {+ if !strings.Contains(joined, want+" attachment text.") {+ t.Errorf("%s attachment was not parsed; items: %q", want, texts)+ }+ }+}+func TestEmailParser_InvalidEMLReturnsError(t *testing.T) {p := NewEmailParser()result := p.ParseWithResult(t.Context(), "broken.eml", []byte("not a valid RFC 5322 header"))