fix(router): count only failed recipient-unknown DSNs as hard bounces

- Parse the bounce as an RFC 3464 report: multipart/report with a
  message/delivery-status (or message/global-delivery-status) part.
- Count a hard bounce only for a per-recipient block with Action: failed
  and a status of 5.1.1, 5.1.2, 5.1.3, 5.1.6 or 5.1.10.
- Policy and content rejections (5.7.x), full mailboxes (5.2.x), delays,
  4.x.x and Status lines in plain text or quoted text no longer count.
- Tests with real-looking DSNs for each class, quoted text and multiple
  recipient blocks; the VERP token check is unchanged.
This commit is contained in:
tgiachi 2026-11-10 11:50:01 +01:00
commit fddf14e285
4 changed files with 189 additions and 5 deletions

View file

@ -0,0 +1,121 @@
package router
import (
"fmt"
"strings"
"testing"
)
// dsn builds a delivery status notification as an MTA sends it (RFC 3464): a multipart/report with
// a human part, a message/delivery-status part and the returned headers.
func dsn(action, status string) []byte {
return []byte(strings.ReplaceAll(fmt.Sprintf(`From: MAILER-DAEMON@mx.example.com
To: dev+bounce@example.org
Subject: Undelivered Mail Returned to Sender
MIME-Version: 1.0
Content-Type: multipart/report; report-type=delivery-status;
boundary="BOUNDARY"
--BOUNDARY
Content-Type: text/plain; charset=us-ascii
I'm sorry to have to inform you that your message could not be delivered.
--BOUNDARY
Content-Type: message/delivery-status
Reporting-MTA: dns; mx.example.com
Arrival-Date: Mon, 9 Nov 2026 10:00:00 +0100
Final-Recipient: rfc822; alice@example.com
Original-Recipient: rfc822;alice@example.com
Action: %s
Status: %s
Diagnostic-Code: smtp; 550 something
--BOUNDARY
Content-Type: text/rfc822-headers
From: bob@example.com
Subject: hello
--BOUNDARY--
`, action, status), "\n", "\r\n"))
}
func bounceCount(t *testing.T, e *env) int {
t.Helper()
m, err := e.s.MemberByID(e.alice)
if err != nil {
t.Fatal(err)
}
return m.BounceCount
}
func TestOnlyFailedRecipientUnknownDSNsCount(t *testing.T) {
cases := []struct {
action, status string
counts bool
}{
{"failed", "5.1.1", true},
{"failed", "5.1.2", true},
{"failed", "5.1.3", true},
{"failed", "5.1.6", true},
{"failed", "5.1.10", true},
{"FAILED (permanent)", "5.1.1 (bad destination mailbox address)", true},
{"failed", "5.7.1", false}, // spam or policy rejection of the content
{"failed", "5.2.2", false}, // mailbox full
{"failed", "5.1.7", false}, // bad sender address
{"failed", "5.1.11", false}, // not in the list (prefix of 5.1.1x must not match)
{"failed", "4.1.1", false},
{"delayed", "5.1.1", false},
{"delayed", "4.4.1", false},
{"delivered", "2.0.0", false},
}
for _, c := range cases {
e := newEnv(t)
handled, err := e.command("", e.r.bounceAddress(e.list, e.alice), dsn(c.action, c.status))
if !handled || err != nil {
t.Fatalf("%s %s: %v %v", c.action, c.status, handled, err)
}
if got := bounceCount(t, e) == 1; got != c.counts {
t.Errorf("%s %s: counted=%v, want %v", c.action, c.status, got, c.counts)
}
}
}
func TestStatusLinesOutsideADeliveryStatusPartDoNotCount(t *testing.T) {
quoted := "From: carol@example.com\r\nSubject: Re: your bounce\r\nContent-Type: text/plain\r\n\r\n" +
"> Action: failed\r\n> Status: 5.1.1\r\nAction: failed\r\nStatus: 5.1.1\r\n"
// A report whose status sits in the human part, not in message/delivery-status.
human := strings.Replace(string(dsn("delayed", "4.4.1")), "I'm sorry", "Action: failed\r\nStatus: 5.1.1\r\n\r\nI'm sorry", 1)
// The returned original message quotes a DSN of its own.
returned := strings.Replace(string(dsn("delayed", "4.4.1")), "Subject: hello", "Subject: hello\r\nAction: failed\r\nStatus: 5.1.1", 1)
for name, msg := range map[string]string{
"plain text reply": quoted,
"status in human part": human,
"status in returned": returned,
"not mime at all": "Action: failed\r\nStatus: 5.1.1\r\n",
"broken multipart": "Content-Type: multipart/report; boundary=x\r\n\r\nno parts here\r\nStatus: 5.1.1\r\n",
"empty": "",
} {
e := newEnv(t)
if _, err := e.command("", e.r.bounceAddress(e.list, e.alice), []byte(msg)); err != nil {
t.Fatalf("%s: %v", name, err)
}
if n := bounceCount(t, e); n != 0 {
t.Errorf("%s: counted %d bounces", name, n)
}
}
}
func TestAnyFailedRecipientBlockCounts(t *testing.T) {
// The first recipient block is a delay, the second a hard failure: the report counts once.
msg := strings.Replace(string(dsn("failed", "5.1.1")), "Final-Recipient:",
"Final-Recipient: rfc822; alice@example.com\r\nAction: delayed\r\nStatus: 4.4.1\r\n\r\nFinal-Recipient:", 1)
e := newEnv(t)
e.command("", e.r.bounceAddress(e.list, e.alice), []byte(msg))
if n := bounceCount(t, e); n != 1 {
t.Fatalf("counted %d", n)
}
}

View file

@ -8,7 +8,6 @@ import (
"fmt"
"mime"
"net/mail"
"regexp"
"strconv"
"strings"
"time"
@ -16,8 +15,6 @@ import (
"codeberg.org/squidd/parsimony/internal/store"
)
var statusRe = regexp.MustCompile(`(?mi)^Status:\s*([245])\.\d+\.\d+`)
func (r *Router) secret() ([]byte, error) {
r.secretOnce.Do(func() { r.secretVal, r.secretErr = r.store.Secret() })
return r.secretVal, r.secretErr
@ -164,7 +161,7 @@ func (r *Router) bounce(l store.List, tag string, data []byte) error {
if err != nil {
return err
}
if sm := statusRe.FindSubmatch(data); sm != nil && string(sm[1]) == "5" {
if isHardBounce(data) {
_, err = r.store.RecordHardBounce(m.ID, store.BounceThreshold)
}
return err

66
internal/router/dsn.go Normal file
View file

@ -0,0 +1,66 @@
package router
import (
"bufio"
"bytes"
"io"
"mime"
"mime/multipart"
"net/mail"
"net/textproto"
"strings"
)
// recipientUnknown are the DSN status codes that say the recipient does not exist: 5.1.1 bad
// mailbox, 5.1.2 bad domain, 5.1.3 bad address syntax, 5.1.6 moved, 5.1.10 null MX. It is the
// same set as outbound.recipientUnknown, used for RCPT TO answers; keep the two in sync.
var recipientUnknown = map[string]bool{"5.1.1": true, "5.1.2": true, "5.1.3": true, "5.1.6": true, "5.1.10": true}
// isHardBounce reports whether data is a delivery status notification (RFC 3464) that reports a
// recipient as non-existent: a multipart/report with a message/delivery-status part holding a
// per-recipient block with "Action: failed" and a status in recipientUnknown. Anything else (a
// policy or content rejection, a full mailbox, a delay, a status line in plain text) is not.
func isHardBounce(data []byte) bool {
msg, err := mail.ReadMessage(bytes.NewReader(data))
if err != nil {
return false
}
mt, params, err := mime.ParseMediaType(msg.Header.Get("Content-Type"))
if err != nil || mt != "multipart/report" || params["boundary"] == "" {
return false
}
mr := multipart.NewReader(msg.Body, params["boundary"])
for {
p, err := mr.NextPart()
if err != nil {
return false
}
pt, _, _ := mime.ParseMediaType(p.Header.Get("Content-Type"))
if pt == "message/delivery-status" || pt == "message/global-delivery-status" {
return failedUnknownRecipient(p)
}
}
}
// failedUnknownRecipient reads the fields of a delivery-status body: a per-message block, then one
// block per recipient, separated by blank lines.
func failedUnknownRecipient(r io.Reader) bool {
tr := textproto.NewReader(bufio.NewReader(r))
for {
h, err := tr.ReadMIMEHeader()
if firstWord(h.Get("Action")) == "failed" && recipientUnknown[firstWord(h.Get("Status"))] {
return true
}
if err != nil {
return false
}
}
}
// firstWord returns the lowercase value of a field without its trailing comment.
func firstWord(v string) string {
if f := strings.Fields(v); len(f) > 0 {
return strings.ToLower(f[0])
}
return ""
}

View file

@ -290,7 +290,7 @@ func TestUnknownCommandAndList(t *testing.T) {
}
func bounceMsg(status string) []byte {
return []byte(fmt.Sprintf("Subject: Undelivered Mail\r\nContent-Type: text/plain\r\n\r\nAction: failed\r\nStatus: %s\r\nDiagnostic-Code: smtp; 550 no such user\r\n", status))
return dsn("failed", status)
}
func TestHardBouncesDisableMember(t *testing.T) {