From fc31a5c20032b45d314ecef4c9ebde7fd7f90bc8 Mon Sep 17 00:00:00 2001 From: Koni Marti Date: Thu, 24 Oct 2024 21:00:15 +0200 Subject: attach: fix content-transfer-encoding for rfc822 attachments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix the content-transfer-encoding for attachments with message/rfc822 mime types. It's not allowed by RFC2046 to set any other content-transfer-encoding than 7bit, 8bit, or binary for message/rfc822 mime types (see RFC2046, section 5.2.1). We can enforce this by setting a content-transfer-encoding header for attachments to 'binary' explicitly. 'binary' is more lenient than '8bit' with respect to line length and CRLF semantics and thus seems more suitable. Link: https://datatracker.ietf.org/doc/html/rfc2046#section-5.2.1 Link: https://lists.sr.ht/~rjarry/aerc-devel/%3CD48A6YOQOXRG.3KKB6UTGMT8LY@maslowski.xyz%3E Reported-by: Piotr Masłowski Reported-by: inwit Signed-off-by: Koni Marti Acked-by: Robin Jarry --- lib/attachment.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/attachment.go b/lib/attachment.go index 4dd3f41d..63b8f161 100644 --- a/lib/attachment.go +++ b/lib/attachment.go @@ -90,6 +90,8 @@ func (fa *FileAttachment) WriteTo(w *mail.Writer) error { // setting the filename auto sets the content disposition ah.SetFilename(filename) + fixContentTransferEncoding(mimeType, &ah) + aw, err := w.CreateAttachment(ah) if err != nil { return errors.Wrap(err, "CreateAttachment") @@ -127,6 +129,8 @@ func (pa *PartAttachment) WriteTo(w *mail.Writer) error { // setting the filename auto sets the content disposition ah.SetFilename(pa.Name()) + fixContentTransferEncoding(pa.part.MimeType, &ah) + aw, err := w.CreateAttachment(ah) if err != nil { return errors.Wrap(err, "CreateAttachment") @@ -174,3 +178,15 @@ func FindMimeType(filename string, reader *bufio.Reader) (string, map[string]str // so we need to break them apart before passing them to the headers return mime.ParseMediaType(mimeString) } + +// fixContentTransferEncoding checks the mime type of the attachment and +// corrects the content-transfer-encoding if necessary. +// +// It's expressly forbidden by RFC2046 to set any other +// content-transfer-encoding than 7bit, 8bit, or binary for +// message/rfc822 mime types (see RFC2046, section 5.2.1) +func fixContentTransferEncoding(mimeType string, header *mail.AttachmentHeader) { + if strings.ToLower(mimeType) == "message/rfc822" { + header.Add("Content-Transfer-Encoding", "binary") + } +} -- cgit