fix: resolve multipart email parsing issue (#336)
* fix: resolve multipart email parsing issue * resolve multipart email parsing issue 解决邮件正文被附件覆盖的bug * Update email.go 修改xss过滤
This commit is contained in:
parent
c347f30c0c
commit
b0913ee804
@ -54,23 +54,23 @@ type Attachment struct {
|
|||||||
|
|
||||||
// Email is the type used for email messages
|
// Email is the type used for email messages
|
||||||
type Email struct {
|
type Email struct {
|
||||||
ReplyTo []*User
|
ReplyTo []*User
|
||||||
From *User
|
From *User
|
||||||
To []*User
|
To []*User
|
||||||
Bcc []*User
|
Bcc []*User
|
||||||
Cc []*User
|
Cc []*User
|
||||||
Subject string
|
Subject string
|
||||||
Text []byte // Plaintext message (optional)
|
Text []byte // Plaintext message (optional)
|
||||||
HTML []byte // Html message (optional)
|
HTML []byte // Html message (optional)
|
||||||
Sender *User // override From as SMTP envelope sender (optional)
|
Sender *User // override From as SMTP envelope sender (optional)
|
||||||
Headers textproto.MIMEHeader
|
Headers textproto.MIMEHeader
|
||||||
Attachments []*Attachment
|
Attachments []*Attachment
|
||||||
ReadReceipt []string
|
ReadReceipt []string
|
||||||
Date string
|
Date string
|
||||||
Status int // 0未发送,1已发送,2发送失败,3删除,5广告邮件
|
Status int // 0未发送,1已发送,2发送失败,3删除,5广告邮件
|
||||||
MessageId int64
|
MessageId int64
|
||||||
MsgID string // RFC-compliant Message-ID, persisted in DB
|
MsgID string // RFC-compliant Message-ID, persisted in DB
|
||||||
Size int
|
Size int
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenerateMsgID creates an RFC-compliant Message-ID unique enough to avoid spam filters.
|
// GenerateMsgID creates an RFC-compliant Message-ID unique enough to avoid spam filters.
|
||||||
@ -289,16 +289,45 @@ func formatContent(entity *message.Entity, ret *Email) error {
|
|||||||
log.Errorf("email read error! %+v", err)
|
log.Errorf("email read error! %+v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
disp := strings.ToLower(entity.Header.Get("Content-Disposition"))
|
||||||
|
isAttachment := strings.Contains(disp, "attachment")
|
||||||
switch contentType {
|
switch contentType {
|
||||||
case "multipart/alternative":
|
case "multipart/alternative":
|
||||||
case "multipart/mixed":
|
case "multipart/mixed":
|
||||||
case "text/plain":
|
case "text/plain":
|
||||||
testContent, _ := io.ReadAll(entity.Body)
|
body, _ := io.ReadAll(entity.Body)
|
||||||
ret.Text = []byte(strictPolicy.Sanitize(string(testContent)))
|
|
||||||
|
if isAttachment {
|
||||||
|
fileName := getFileName(entity, p)
|
||||||
|
|
||||||
|
ret.Attachments = append(ret.Attachments, &Attachment{
|
||||||
|
Filename: sanitizeText(fileName),
|
||||||
|
ContentType: sanitizeText(strings.TrimSpace(contentType)),
|
||||||
|
Content: body,
|
||||||
|
ContentID: strings.TrimPrefix(strings.TrimSuffix(entity.Header.Get("Content-Id"), ">"), "<"),
|
||||||
|
})
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ret.Text = []byte(strictPolicy.Sanitize(string(body)))
|
||||||
case "text/html":
|
case "text/html":
|
||||||
htmlContent, _ := io.ReadAll(entity.Body)
|
body, _ := io.ReadAll(entity.Body)
|
||||||
ret.HTML = []byte(relaxedPolicy.Sanitize(string(htmlContent)))
|
|
||||||
|
if isAttachment {
|
||||||
|
fileName := getFileName(entity, p)
|
||||||
|
|
||||||
|
ret.Attachments = append(ret.Attachments, &Attachment{
|
||||||
|
Filename: sanitizeText(fileName),
|
||||||
|
ContentType: sanitizeText(strings.TrimSpace(contentType)),
|
||||||
|
Content: body,
|
||||||
|
ContentID: strings.TrimPrefix(strings.TrimSuffix(entity.Header.Get("Content-Id"), ">"), "<"),
|
||||||
|
})
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ret.HTML = []byte(relaxedPolicy.Sanitize(string(body)))
|
||||||
case "multipart/related":
|
case "multipart/related":
|
||||||
entity.Walk(func(path []int, entity *message.Entity, err error) error {
|
entity.Walk(func(path []int, entity *message.Entity, err error) error {
|
||||||
if t, _, _ := entity.Header.ContentType(); t == "multipart/related" {
|
if t, _, _ := entity.Header.ContentType(); t == "multipart/related" {
|
||||||
@ -308,18 +337,7 @@ func formatContent(entity *message.Entity, ret *Email) error {
|
|||||||
})
|
})
|
||||||
default:
|
default:
|
||||||
c, _ := io.ReadAll(entity.Body)
|
c, _ := io.ReadAll(entity.Body)
|
||||||
fileName := p["name"]
|
fileName := getFileName(entity, p)
|
||||||
if fileName == "" {
|
|
||||||
contentDisposition := entity.Header.Get("Content-Disposition")
|
|
||||||
filenameRegex := regexp.MustCompile(`filename\s*=\s*"?([^";]+)"?`)
|
|
||||||
matches := filenameRegex.FindStringSubmatch(contentDisposition)
|
|
||||||
if len(matches) >= 2 {
|
|
||||||
fileName = strings.TrimSpace(matches[1])
|
|
||||||
fileName = strings.Trim(fileName, `"`)
|
|
||||||
} else {
|
|
||||||
fileName = "no_name_file"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ret.Attachments = append(ret.Attachments, &Attachment{
|
ret.Attachments = append(ret.Attachments, &Attachment{
|
||||||
Filename: sanitizeText(fileName),
|
Filename: sanitizeText(fileName),
|
||||||
@ -331,6 +349,24 @@ func formatContent(entity *message.Entity, ret *Email) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
func getFileName(entity *message.Entity, p map[string]string) string {
|
||||||
|
fileName := p["name"]
|
||||||
|
|
||||||
|
if fileName != "" {
|
||||||
|
return fileName
|
||||||
|
}
|
||||||
|
|
||||||
|
contentDisposition := entity.Header.Get("Content-Disposition")
|
||||||
|
|
||||||
|
filenameRegex := regexp.MustCompile(`filename\s*=\s*"?([^";]+)"?`)
|
||||||
|
matches := filenameRegex.FindStringSubmatch(contentDisposition)
|
||||||
|
|
||||||
|
if len(matches) >= 2 {
|
||||||
|
return strings.Trim(strings.TrimSpace(matches[1]), `"`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return "no_name_file"
|
||||||
|
}
|
||||||
|
|
||||||
func BuilderUser(str string) *User {
|
func BuilderUser(str string) *User {
|
||||||
return buildUser(str)
|
return buildUser(str)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user