From cd31d919a9f83ccf888f1850112dbcdf577c4a2b Mon Sep 17 00:00:00 2001 From: tgiachi Date: Tue, 10 Nov 2026 11:52:25 +0100 Subject: [PATCH] test(router): pin the edges of DSN parsing - message/global-delivery-status (RFC 6533) counts like delivery-status. - Only the first delivery-status part is read. - A malformed delivery-status part does not count. - Drop the redundant empty-boundary check: the multipart reader already fails on it. --- internal/router/bounce_test.go | 31 +++++++++++++++++++++++++++++++ internal/router/dsn.go | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/internal/router/bounce_test.go b/internal/router/bounce_test.go index a8fd492..1f94419 100644 --- a/internal/router/bounce_test.go +++ b/internal/router/bounce_test.go @@ -119,3 +119,34 @@ func TestAnyFailedRecipientBlockCounts(t *testing.T) { t.Fatalf("counted %d", n) } } + +func TestGlobalDeliveryStatusCounts(t *testing.T) { + // RFC 6533: the internationalized report has the same fields. + msg := strings.Replace(string(dsn("failed", "5.1.1")), "message/delivery-status", "message/global-delivery-status", 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) + } +} + +func TestOnlyTheFirstDeliveryStatusPartIsRead(t *testing.T) { + // A report has one delivery-status part; another one after it is not the MTA speaking. + extra := "--BOUNDARY\r\nContent-Type: message/delivery-status\r\n\r\nReporting-MTA: dns; x\r\n\r\n" + + "Final-Recipient: rfc822; alice@example.com\r\nAction: failed\r\nStatus: 5.1.1\r\n\r\n--BOUNDARY--" + msg := strings.Replace(string(dsn("delayed", "4.4.1")), "--BOUNDARY--", extra, 1) + e := newEnv(t) + e.command("", e.r.bounceAddress(e.list, e.alice), []byte(msg)) + if n := bounceCount(t, e); n != 0 { + t.Fatalf("counted %d", n) + } +} + +func TestAMalformedDeliveryStatusDoesNotCount(t *testing.T) { + msg := strings.Replace(string(dsn("failed", "5.1.1")), "Arrival-Date:", "this line is not a field\r\nArrival-Date:", 1) + e := newEnv(t) + e.command("", e.r.bounceAddress(e.list, e.alice), []byte(msg)) + if n := bounceCount(t, e); n != 0 { + t.Fatalf("counted %d", n) + } +} diff --git a/internal/router/dsn.go b/internal/router/dsn.go index a5e33b5..f121613 100644 --- a/internal/router/dsn.go +++ b/internal/router/dsn.go @@ -26,7 +26,7 @@ func isHardBounce(data []byte) bool { return false } mt, params, err := mime.ParseMediaType(msg.Header.Get("Content-Type")) - if err != nil || mt != "multipart/report" || params["boundary"] == "" { + if err != nil || mt != "multipart/report" { return false } mr := multipart.NewReader(msg.Body, params["boundary"])