Skip to content

armgit / posta

Public Imported from GitHub
0

Self-hosted email delivery platform that enables applications to send emails via HTTP APIs while handling SMTP delivery, templates, storage, security, and analytics. Fully self-hostable alternative to services like SendGrid or Mailgun.

fix(attachments): encode non-ASCII filenames and text bodies

Mike Kosulinc7146c4be578
diff --git a/internal/services/email/attachment_mime_test.go b/internal/services/email/attachment_mime_test.go
new file mode 100644
index 0000000..a3e42c6
--- /dev/null
+++ b/internal/services/email/attachment_mime_test.go
@@ -0,0 +1,254 @@
+/*
+ * Copyright 2026 Jonas Kaninda
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ */
+
+package email
+
+import (
+ "encoding/base64"
+ "io"
+ "mime"
+ "mime/multipart"
+ "net/mail"
+ "strings"
+ "testing"
+
+ "github.com/goposta/posta/internal/models"
+ "github.com/goposta/posta/internal/services/inbound"
+)
+
+// parseBuilt parses a message produced by buildMessage back into its bodies and
+// attachments, the way a receiving MTA would.
+func parseBuilt(t *testing.T, raw []byte) *inbound.ParsedEmail {
+ t.Helper()
+ p, err := inbound.ParseRawEmail(raw)
+ if err != nil {
+ t.Fatalf("built message does not parse: %v\n---\n%s", err, raw)
+ }
+ return p
+}
+
+// The SMTP relay hands Posta filenames that came from real mail clients, so they
+// are routinely non-ASCII. A MIME header must be 7-bit; raw UTF-8 in one is what
+// makes a downstream MTA reject the part as unreadable.
+func TestBuildMessageEncodesNonASCIIAttachmentFilename(t *testing.T) {
+ const filename = "Отчёт за июль.pdf"
+ payload := []byte("%PDF-1.4\x00\x01binary\xffbytes")
+
+ raw := buildMessage(
+ "Проверка вложения", "", "Файл во вложении",
+ []models.Attachment{{
+ Filename: filename,
+ ContentType: "application/pdf",
+ Content: base64.StdEncoding.EncodeToString(payload),
+ }},
+ nil, "", "", false,
+ )
+
+ for i, c := range raw {
+ if c > 127 {
+ t.Fatalf("non-ASCII byte %#x at offset %d; the message must be 7-bit clean\n---\n%s", c, i, raw)
+ }
+ }
+
+ p := parseBuilt(t, raw)
+ if len(p.Attachments) != 1 {
+ t.Fatalf("got %d attachments, want 1", len(p.Attachments))
+ }
+ if got := p.Attachments[0].Filename; got != filename {
+ t.Errorf("filename = %q, want %q", got, filename)
+ }
+ if got := string(p.Attachments[0].Content); got != string(payload) {
+ t.Errorf("attachment content = %q, want %q", got, payload)
+ }
+}
+
+// A filename is attacker-controlled on the relay path, so quotes and CRLF must
+// not be able to break out of the header.
+func TestBuildMessageFilenameCannotInjectHeaders(t *testing.T) {
+ raw := buildMessage(
+ "subject", "", "body",
+ []models.Attachment{{
+ Filename: "evil\r\nBcc: [email protected]\r\n\r\nx\".pdf",
+ ContentType: "text/plain",
+ Content: base64.StdEncoding.EncodeToString([]byte("data")),
+ }},
+ nil, "", "", false,
+ )
+
+ if strings.Contains(string(raw), "Bcc:") {
+ t.Fatalf("filename injected a header into the message:\n%s", raw)
+ }
+ p := parseBuilt(t, raw)
+ if len(p.Attachments) != 1 {
+ t.Fatalf("got %d attachments, want 1", len(p.Attachments))
+ }
+ if string(p.Attachments[0].Content) != "data" {
+ t.Errorf("attachment content = %q, want %q", p.Attachments[0].Content, "data")
+ }
+}
+
+// Attachment bytes must survive base64 line-wrapping unchanged, at lengths that
+// do and do not land on a line boundary.
+func TestBuildMessageAttachmentBytesRoundTrip(t *testing.T) {
+ for _, size := range []int{1, 57, 76, 1000, 4096} {
+ payload := make([]byte, size)
+ for i := range payload {
+ payload[i] = byte(i % 256)
+ }
+ raw := buildMessage(
+ "subject", "<p>hi</p>", "hi",
+ []models.Attachment{{
+ Filename: "blob.bin",
+ ContentType: "application/octet-stream",
+ Content: base64.StdEncoding.EncodeToString(payload),
+ }},
+ nil, "", "", false,
+ )
+
+ p := parseBuilt(t, raw)
+ if len(p.Attachments) != 1 {
+ t.Fatalf("size %d: got %d attachments, want 1", size, len(p.Attachments))
+ }
+ if string(p.Attachments[0].Content) != string(payload) {
+ t.Errorf("size %d: attachment bytes corrupted", size)
+ }
+ }
+
+ // Every line must stay within the RFC 2045 76-character limit.
+ raw := buildMessage(
+ "subject", "", "hi",
+ []models.Attachment{{
+ Filename: "blob.bin",
+ ContentType: "application/octet-stream",
+ Content: base64.StdEncoding.EncodeToString(make([]byte, 5000)),
+ }},
+ nil, "", "", false,
+ )
+ for _, line := range strings.Split(string(raw), "\r\n") {
+ if len(line) > 76 {
+ t.Fatalf("line exceeds 76 chars (%d): %q", len(line), line)
+ }
+ }
+}
+
+// End-to-end for the relay: a message as a mail client submits it, through the
+// parser the relay uses, back out through the builder. The attachment must come
+// out the far side byte-identical with its name intact.
+func TestRelayRoundTripPreservesAttachment(t *testing.T) {
+ const filename = "Отчёт за июль.pdf"
+ payload := []byte("%PDF-1.4\x00\x01binary\xffbytes")
+
+ encodedName := mime.BEncoding.Encode("UTF-8", filename)
+ // Boundary begins with "--", as Rails' Mail gem emits.
+ submitted := "From: Ирина <[email protected]>\r\n" +
+ "To: [email protected]\r\n" +
+ "Subject: " + mime.BEncoding.Encode("UTF-8", "Проверка вложения") + "\r\n" +
+ "MIME-Version: 1.0\r\n" +
+ "Content-Type: multipart/mixed; boundary=\"--==_mimepart_abc\"\r\n" +
+ "\r\n" +
+ "----==_mimepart_abc\r\n" +
+ "Content-Type: text/plain; charset=UTF-8\r\n" +
+ "Content-Transfer-Encoding: base64\r\n" +
+ "\r\n" +
+ base64.StdEncoding.EncodeToString([]byte("Файл во вложении")) + "\r\n" +
+ "----==_mimepart_abc\r\n" +
+ "Content-Type: application/pdf; name=\"" + encodedName + "\"\r\n" +
+ "Content-Transfer-Encoding: base64\r\n" +
+ "Content-Disposition: attachment; filename=\"" + encodedName + "\"\r\n" +
+ "\r\n" +
+ base64.StdEncoding.EncodeToString(payload) + "\r\n" +
+ "----==_mimepart_abc--\r\n"
+
+ // What smtprelay's Data() does with the submitted message.
+ parsed, err := inbound.ParseRawEmail([]byte(submitted))
+ if err != nil {
+ t.Fatalf("relay failed to parse submission: %v", err)
+ }
+ if len(parsed.Attachments) != 1 {
+ t.Fatalf("relay parsed %d attachments, want 1", len(parsed.Attachments))
+ }
+ atts := make([]models.Attachment, 0, len(parsed.Attachments))
+ for _, a := range parsed.Attachments {
+ atts = append(atts, models.Attachment{
+ Filename: a.Filename,
+ ContentType: a.ContentType,
+ Content: base64.StdEncoding.EncodeToString(a.Content),
+ })
+ }
+
+ relayed := buildMessage(
+ parsed.Subject, parsed.HTMLBody, parsed.TextBody, atts, nil, "", "", false,
+ )
+
+ out := parseBuilt(t, relayed)
+ if len(out.Attachments) != 1 {
+ t.Fatalf("relayed message has %d attachments, want 1", len(out.Attachments))
+ }
+ if got := out.Attachments[0].Filename; got != filename {
+ t.Errorf("filename = %q, want %q", got, filename)
+ }
+ if string(out.Attachments[0].Content) != string(payload) {
+ t.Errorf("attachment bytes corrupted through the relay")
+ }
+ if out.TextBody != "Файл во вложении" {
+ t.Errorf("text body = %q, want %q", out.TextBody, "Файл во вложении")
+ }
+}
+
+// The message must be a real multipart/mixed, not a flat body with the MIME
+// structure inlined as text.
+func TestBuildMessageWithAttachmentIsMultipart(t *testing.T) {
+ raw := buildMessage(
+ "subject", "", "body text",
+ []models.Attachment{{
+ Filename: "a.txt",
+ ContentType: "text/plain",
+ Content: base64.StdEncoding.EncodeToString([]byte("file data")),
+ }},
+ nil, "", "", false,
+ )
+
+ msg, err := mail.ReadMessage(strings.NewReader(string(raw)))
+ if err != nil {
+ t.Fatalf("ReadMessage: %v", err)
+ }
+ mt, params, err := mime.ParseMediaType(msg.Header.Get("Content-Type"))
+ if err != nil {
+ t.Fatalf("ParseMediaType: %v", err)
+ }
+ if mt != "multipart/mixed" {
+ t.Fatalf("Content-Type = %q, want multipart/mixed", mt)
+ }
+
+ mr := multipart.NewReader(msg.Body, params["boundary"])
+ var parts int
+ for {
+ part, err := mr.NextPart()
+ if err == io.EOF {
+ break
+ }
+ if err != nil {
+ t.Fatalf("NextPart: %v", err)
+ }
+ if _, err := io.ReadAll(part); err != nil {
+ t.Fatalf("read part: %v", err)
+ }
+ parts++
+ }
+ if parts != 2 {
+ t.Fatalf("got %d parts, want 2 (body + attachment)", parts)
+ }
+}
diff --git a/internal/services/email/smtp_sender.go b/internal/services/email/smtp_sender.go
index 8623c9c..7bf1f03 100644
--- a/internal/services/email/smtp_sender.go
+++ b/internal/services/email/smtp_sender.go
@@ -23,6 +23,7 @@ import (
"errors"
"fmt"
"mime"
+ "mime/quotedprintable"
"net/mail"
"net/smtp"
"net/textproto"
@@ -226,6 +227,36 @@ func wrapSendError(stage, recipient string, err error) error {
return &SendError{Stage: stage, Recipient: recipient, Code: code, Msg: msg, Err: err}
}
+const (
+ contentTypeTextPlain = "text/plain"
+ contentTypeTextHTML = "text/html"
+)
+
+// writeTextPart writes a text body part. Non-ASCII bodies are quoted-printable
+// encoded: with no Content-Transfer-Encoding the part defaults to 7bit, so raw
+// UTF-8 would be a lie the next hop is free to mangle.
+func writeTextPart(b *strings.Builder, mediaType, body string) {
+ fmt.Fprintf(b, "Content-Type: %s; charset=\"UTF-8\"\r\n", mediaType)
+ if isASCII(body) {
+ b.WriteString("\r\n")
+ b.WriteString(body)
+ return
+ }
+ b.WriteString("Content-Transfer-Encoding: quoted-printable\r\n\r\n")
+ w := quotedprintable.NewWriter(b)
+ _, _ = w.Write([]byte(body))
+ _ = w.Close()
+}
+
+func isASCII(s string) bool {
+ for i := 0; i < len(s); i++ {
+ if s[i] > 127 {
+ return false
+ }
+ }
+ return true
+}
+
func buildMessage(from string, to []string, subject, htmlBody, textBody string, attachments []models.Attachment, headers map[string]string, listUnsubscribeURL, listUnsubscribeMailto string, listUnsubscribePost bool) []byte {
var b strings.Builder
@@ -269,18 +300,14 @@ func buildMessage(from string, to []string, subject, htmlBody, textBody string,
if htmlBody != "" && textBody != "" {
fmt.Fprintf(&b, "Content-Type: multipart/alternative; boundary=\"%s\"\r\n\r\n", altBoundary)
fmt.Fprintf(&b, "--%s\r\n", altBoundary)
- b.WriteString("Content-Type: text/plain; charset=\"UTF-8\"\r\n\r\n")
- b.WriteString(textBody)
+ writeTextPart(&b, contentTypeTextPlain, textBody)
fmt.Fprintf(&b, "\r\n--%s\r\n", altBoundary)
- b.WriteString("Content-Type: text/html; charset=\"UTF-8\"\r\n\r\n")
- b.WriteString(htmlBody)
+ writeTextPart(&b, contentTypeTextHTML, htmlBody)
fmt.Fprintf(&b, "\r\n--%s--\r\n", altBoundary)
} else if htmlBody != "" {
- b.WriteString("Content-Type: text/html; charset=\"UTF-8\"\r\n\r\n")
- b.WriteString(htmlBody)
+ writeTextPart(&b, contentTypeTextHTML, htmlBody)
} else {
- b.WriteString("Content-Type: text/plain; charset=\"UTF-8\"\r\n\r\n")
- b.WriteString(textBody)
+ writeTextPart(&b, contentTypeTextPlain, textBody)
}
// Attachment parts
@@ -290,9 +317,9 @@ func buildMessage(from string, to []string, subject, htmlBody, textBody string,
if contentType == "" {
contentType = "application/octet-stream"
}
- fmt.Fprintf(&b, "Content-Type: %s; name=\"%s\"\r\n", contentType, att.Filename)
+ fmt.Fprintf(&b, "Content-Type: %s\r\n", mime.FormatMediaType(contentType, map[string]string{"name": att.Filename}))
b.WriteString("Content-Transfer-Encoding: base64\r\n")
- fmt.Fprintf(&b, "Content-Disposition: attachment; filename=\"%s\"\r\n\r\n", att.Filename)
+ fmt.Fprintf(&b, "Content-Disposition: %s\r\n\r\n", mime.FormatMediaType("attachment", map[string]string{"filename": att.Filename}))
// Content is already base64-encoded from the API request
content := att.Content
@@ -311,18 +338,14 @@ func buildMessage(from string, to []string, subject, htmlBody, textBody string,
if htmlBody != "" && textBody != "" {
fmt.Fprintf(&b, "Content-Type: multipart/alternative; boundary=\"%s\"\r\n\r\n", altBoundary)
fmt.Fprintf(&b, "--%s\r\n", altBoundary)
- b.WriteString("Content-Type: text/plain; charset=\"UTF-8\"\r\n\r\n")
- b.WriteString(textBody)
+ writeTextPart(&b, contentTypeTextPlain, textBody)
fmt.Fprintf(&b, "\r\n--%s\r\n", altBoundary)
- b.WriteString("Content-Type: text/html; charset=\"UTF-8\"\r\n\r\n")
- b.WriteString(htmlBody)
+ writeTextPart(&b, contentTypeTextHTML, htmlBody)
fmt.Fprintf(&b, "\r\n--%s--\r\n", altBoundary)
} else if htmlBody != "" {
- b.WriteString("Content-Type: text/html; charset=\"UTF-8\"\r\n\r\n")
- b.WriteString(htmlBody)
+ writeTextPart(&b, contentTypeTextHTML, htmlBody)
} else {
- b.WriteString("Content-Type: text/plain; charset=\"UTF-8\"\r\n\r\n")
- b.WriteString(textBody)
+ writeTextPart(&b, contentTypeTextPlain, textBody)
}
}