Skip to content

armgit / ARTEX

Public Imported from GitHub
0

ARTEX - AI autonomous penetration testing system (English translation; upstream: Autumn-27/ARTEX, AGPL-3.0)

fix(notify): add Date and Message-ID headers to notification emails

thejesh23ad4c976732c7
diff --git a/notify/email.go b/notify/email.go
index 57be69f..c3e8336 100644
--- a/notify/email.go
+++ b/notify/email.go
@@ -2,6 +2,7 @@ package notify
import (
"context"
+ "crypto/rand"
"crypto/tls"
"encoding/base64"
"errors"
@@ -220,6 +221,10 @@ func buildEmailMessage(from string, to []string, m Message) (string, error) {
var b strings.Builder
fmt.Fprintf(&b, "From: %s\r\n", from)
fmt.Fprintf(&b, "To: %s\r\n", strings.Join(to, ", "))
+ // RFC 5322 requires Date and recommends Message-ID. Not every submission
+ // server adds them, and spam filters penalize mail without them.
+ fmt.Fprintf(&b, "Date: %s\r\n", time.Now().Format(time.RFC1123Z))
+ fmt.Fprintf(&b, "Message-ID: <%s@%s>\r\n", rand.Text(), messageIDDomain(from))
// A non-ASCII subject must be RFC 2047 encoded, otherwise clients display it
// as mojibake.
fmt.Fprintf(&b, "Subject: %s\r\n", mime.QEncoding.Encode("utf-8", htmlTitle(m)))
@@ -237,3 +242,14 @@ func buildEmailMessage(from string, to []string, m Message) (string, error) {
b.WriteString(encoded + "\r\n")
return b.String(), nil
}
+
+// messageIDDomain is the right-hand side of a Message-ID: the sender's domain,
+// or "localhost" when the sender address has none.
+func messageIDDomain(from string) string {
+ if i := strings.LastIndex(from, "@"); i >= 0 {
+ if d := strings.TrimRight(strings.TrimSpace(from[i+1:]), ">"); d != "" {
+ return d
+ }
+ }
+ return "localhost"
+}
diff --git a/notify/email_test.go b/notify/email_test.go
index e26c535..d1b77df 100644
--- a/notify/email_test.go
+++ b/notify/email_test.go
@@ -5,9 +5,11 @@ import (
"context"
"net"
+ "net/mail"
"strings"
"sync"
"testing"
+ "time"
)
// This file adds the protocol-level tests for the email channel. Before it,
@@ -187,6 +189,38 @@ func TestEmailSendDeliversFullMessage(t *testing.T) {
}
}
+// RFC 5322 section 3.6 requires a Date header, and every message should carry
+// a Message-ID. Not every submission server adds them, and spam filters
+// penalize mail without them.
+func TestEmailMessageHasDateAndMessageID(t *testing.T) {
+ ids := map[string]bool{}
+ for range 2 {
+ raw, err := buildEmailMessage("[email protected]", []string{"[email protected]"}, singleMsg())
+ if err != nil {
+ t.Fatal(err)
+ }
+ msg, err := mail.ReadMessage(strings.NewReader(raw))
+ if err != nil {
+ t.Fatalf("message does not parse: %v\n%s", err, raw)
+ }
+ date, err := msg.Header.Date()
+ if err != nil {
+ t.Fatalf("missing or invalid Date header: %v\n%s", err, raw)
+ }
+ if d := time.Since(date); d < -time.Minute || d > time.Minute {
+ t.Errorf("Date %v is not the send time", date)
+ }
+ id := msg.Header.Get("Message-ID")
+ if !strings.HasPrefix(id, "<") || !strings.HasSuffix(id, "@example.com>") {
+ t.Errorf("Message-ID %q, want <[email protected]>", id)
+ }
+ ids[id] = true
+ }
+ if len(ids) != 2 {
+ t.Errorf("two messages share a Message-ID: %v", ids)
+ }
+}
+
func TestEmailSendWithoutAuth(t *testing.T) {
// AUTH must not be sent when no account is configured — some relays reject
// because of it.