From 7e679f230349bbc51e3021a2f98c57a983513f0e Mon Sep 17 00:00:00 2001 From: Thomas Grahammer <3223680+tgrahammer@users.noreply.github.com> Date: Wed, 28 Oct 2026 08:32:12 +0100 Subject: [PATCH 1/2] fix(mailer): set Date and Message-ID on SMTP messages Messages sent through the SMTP mailer carried From, To and Subject but no Date or Message-ID header. RFC 5322 requires Date on every message and says each message should carry a Message-ID. Some relays add the missing headers on the way through, but not all of them do, and receiving servers and spam filters score messages without them as suspicious, so confirmations and reminders could land in spam. buildRaw now writes Date (RFC 1123 with numeric zone, the RFC 5322 date-time form) and a Message-ID of the form , with the domain taken from the From address. Only the SMTP path is affected. Validated with go test ./... and make build. --- internal/mailer/smtp.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/mailer/smtp.go b/internal/mailer/smtp.go index 5816ce7c..ec1cca20 100644 --- a/internal/mailer/smtp.go +++ b/internal/mailer/smtp.go @@ -273,6 +273,12 @@ func (s *SMTP) buildRaw(msg Message) ([]byte, error) { } var buf bytes.Buffer + // RFC 5322: Required origination date. + fmt.Fprintf(&buf, "Date: %s\r\n", + time.Now().Format(time.RFC1123Z)) + // RFC 5322: Globally unique message identifier. + fmt.Fprintf(&buf, "Message-ID: <%s@%s>\r\n", + uid.New(), strings.ToLower(from.Address[strings.LastIndex(from.Address, "@")+1:])) fmt.Fprintf(&buf, "From: %s\r\n", from.String()) fmt.Fprintf(&buf, "To: %s\r\n", strings.Join(toFormatted, ", ")) fmt.Fprintf(&buf, "Subject: %s\r\n", subject) From e334f89bd27606471d8ca4bff2f3282ea329aa33 Mon Sep 17 00:00:00 2001 From: Thomas Grahammer <3223680+tgrahammer@users.noreply.github.com> Date: Wed, 28 Oct 2026 08:50:44 +0100 Subject: [PATCH 2/2] fix(mailer): derive the Message-ID domain from a parsed From address MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit email_from is not validated anywhere: PatchEmailSettings does not check it and the settings form does not require it. Slicing the domain off the raw value therefore produced for an empty setting, and for "Calnode " — malformed identifiers, which work against the deliverability the header was added for. messageIDDomain parses the address instead and falls back to the SMTP host, then to localhost. Both operator-supplied candidates must pass a dot-atom check first, so neither can carry a CR/LF into the header line. Cover the Date and Message-ID headers: Date against mail.ParseDate rather than the layout that produced it, Message-ID for shape, uniqueness and each From/host combination that reaches buildRaw unvalidated. --- CHANGELOG.md | 7 ++ internal/mailer/mailer_test.go | 123 +++++++++++++++++++++++++++++++++ internal/mailer/smtp.go | 49 ++++++++++++- 3 files changed, 177 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31c54f3e..f8f99a5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,13 @@ exact tag (`ghcr.io/calnode/calnode:0.1.0`) if you need stability between upgrad ## [Unreleased] ### Fixed +- **Mail sent over SMTP carries `Date` and `Message-ID`.** Messages went out with only + `From`, `To` and `Subject`. RFC 5322 requires an origination date on every message and + expects a message identifier; some relays add what is missing, but not all do, and + receiving servers and spam filters score mail without them as suspicious — so + confirmations and reminders could land in spam. The identifier's domain comes from the + configured sender address, falling back to the SMTP host when that address is unset or + unusable. The Resend transport was unaffected; it sets both headers itself. - **A plain `docker run` no longer loses stored credentials on restart.** The documented first install starts Calnode with no `CALNODE_ENCRYPTION_KEY`. With `BASE_URL` on `http://`, the vault treated that as development mode and generated an ephemeral data key, diff --git a/internal/mailer/mailer_test.go b/internal/mailer/mailer_test.go index 1c1a946a..a94b08be 100644 --- a/internal/mailer/mailer_test.go +++ b/internal/mailer/mailer_test.go @@ -3,7 +3,9 @@ package mailer import ( "context" "errors" + "net/mail" "net/url" + "regexp" "strings" "sync" "testing" @@ -391,3 +393,124 @@ func TestBuildRaw_recipientAddresses(t *testing.T) { }) } } + +// --------------------------------------------------------------------------- +// buildRaw — RFC 5322 Date and Message-ID +// --------------------------------------------------------------------------- + +// headerLine returns the value of the first occurrence of the named header in a raw +// message, or "" when it is absent. +func headerLine(raw, name string) string { + prefix := name + ": " + for _, line := range strings.Split(raw, "\r\n") { + if line == "" { + break // end of the header block + } + if strings.HasPrefix(line, prefix) { + return strings.TrimPrefix(line, prefix) + } + } + return "" +} + +// TestBuildRaw_dateHeader checks the header against the parser receiving servers use +// rather than against the layout string that produced it: RFC 5322 requires a date-time +// a reader can actually parse, and a format change that stops satisfying mail.ParseDate +// would otherwise only show up as mail scoring badly at the far end. +func TestBuildRaw_dateHeader(t *testing.T) { + before := time.Now().Truncate(time.Second) + raw := string(mustBuildRaw(t, smtpForTest(), Message{ + To: []string{"user@example.com"}, Subject: "Hi", Text: "body", + })) + + got := headerLine(raw, "Date") + if got == "" { + t.Fatal("Date: header missing") + } + parsed, err := mail.ParseDate(got) + if err != nil { + t.Fatalf("mail.ParseDate(%q): %v", got, err) + } + if parsed.Before(before) || parsed.After(time.Now().Add(time.Minute)) { + t.Errorf("Date: header = %q; want the time of sending", got) + } + // A numeric zone ("+0000"), not "UTC" or another alphabetic abbreviation: RFC 5322 + // moved those to the obsolete syntax, and time.RFC1123 - one character away from the + // layout used here - produces exactly that. + if !regexp.MustCompile(`[+-]\d{4}$`).MatchString(got) { + t.Errorf("Date: header = %q; want it to end in a numeric zone offset", got) + } +} + +func TestBuildRaw_messageIDHeader(t *testing.T) { + raw := string(mustBuildRaw(t, smtpForTest(), Message{ + To: []string{"user@example.com"}, Subject: "Hi", Text: "body", + })) + + got := headerLine(raw, "Message-ID") + if !strings.HasPrefix(got, "<") || !strings.HasSuffix(got, ">") { + t.Fatalf("Message-ID: header = %q; want it wrapped in angle brackets", got) + } + id := strings.Trim(got, "<>") + local, domain, ok := strings.Cut(id, "@") + if !ok { + t.Fatalf("Message-ID %q has no @; want id@domain", id) + } + if local == "" { + t.Errorf("Message-ID %q has an empty local part", id) + } + if domain != "example.com" { + t.Errorf("Message-ID domain = %q; want the From domain %q", domain, "example.com") + } + + // Two messages must not share an identifier — threading clients collapse messages + // that do. + other := string(mustBuildRaw(t, smtpForTest(), Message{ + To: []string{"user@example.com"}, Subject: "Hi", Text: "body", + })) + if headerLine(other, "Message-ID") == got { + t.Errorf("two messages share the Message-ID %q", got) + } +} + +// TestBuildRaw_messageIDDomain covers the From values that reach buildRaw unvalidated - +// the settings endpoint does not require email_from, let alone check it. Each must still +// produce a well-formed right-hand side, since a malformed Message-ID works against the +// deliverability the header exists for. +func TestBuildRaw_messageIDDomain(t *testing.T) { + for _, tc := range []struct { + name string + from string + host string + want string + }{ + {"plain address", "noreply@example.com", "smtp.example.net", "example.com"}, + {"address with a display name", "Calnode ", "smtp.example.net", "example.com"}, + {"uppercase domain is normalised", "noreply@EXAMPLE.COM", "", "example.com"}, + {"subdomain is kept", "noreply@mail.team.example.com", "", "mail.team.example.com"}, + {"empty From falls back to the host", "", "smtp.example.net", "smtp.example.net"}, + {"unparseable From falls back to the host", "not-an-address", "smtp.example.net", "smtp.example.net"}, + {"no From and no host", "", "", "localhost"}, + {"unusable host is not written through", "", "smtp example net", "localhost"}, + {"CRLF in the host cannot start a header", "", "smtp.example.net\r\nBcc: attacker@evil.example", "localhost"}, + } { + t.Run(tc.name, func(t *testing.T) { + s := &SMTP{from: tc.from, fromName: "Calnode", host: tc.host} + if got := s.messageIDDomain(); got != tc.want { + t.Fatalf("messageIDDomain() = %q; want %q", got, tc.want) + } + + raw := string(mustBuildRaw(t, s, Message{ + To: []string{"user@example.com"}, Subject: "Hi", Text: "body", + })) + if got := headerLine(raw, "Message-ID"); !strings.HasSuffix(got, "@"+tc.want+">") { + t.Errorf("Message-ID: header = %q; want it to end in @%s>", got, tc.want) + } + for _, line := range strings.Split(raw, "\r\n") { + if strings.HasPrefix(line, "Bcc:") { + t.Errorf("header injected through the Message-ID domain: %q", line) + } + } + }) + } +} diff --git a/internal/mailer/smtp.go b/internal/mailer/smtp.go index ec1cca20..b47f0b35 100644 --- a/internal/mailer/smtp.go +++ b/internal/mailer/smtp.go @@ -244,6 +244,52 @@ func (s *SMTP) Send(ctx context.Context, msg Message) error { // caller-supplied, may carry CR/LF, and this error is logged. var ErrInvalidRecipient = errors.New("mailer: invalid recipient address") +// messageIDDomain returns the right-hand side of the Message-ID header. +// +// The sender's own domain is the natural choice, but it cannot simply be sliced off +// s.from: email_from is a free-text setting that the settings endpoint does not validate +// and the settings form does not require, so it reaches here empty, or as something that +// is not an address at all. Slicing an empty value yields "" - a malformed +// Message-ID, which is worse for deliverability than the missing header this change set +// out to fix. So the address is parsed, and the SMTP host stands in when it yields no +// usable domain. +// +// Both candidates are operator-supplied and are written into a header line verbatim, so +// each must pass messageIDDomainOK first; "localhost" is the last resort, valid by +// construction. +func (s *SMTP) messageIDDomain() string { + if a, err := mail.ParseAddress(s.from); err == nil { + if i := strings.LastIndex(a.Address, "@"); i >= 0 { + if d := strings.ToLower(a.Address[i+1:]); messageIDDomainOK(d) { + return d + } + } + } + if d := strings.ToLower(s.host); messageIDDomainOK(d) { + return d + } + return "localhost" +} + +// messageIDDomainOK reports whether d can be written into a Message-ID as it stands: a +// non-empty run of letters, digits, dots and hyphens. That is narrower than RFC 5322's +// dot-atom, deliberately - it keeps out the CR/LF that would end the header line and the +// angle brackets that would end the identifier, without needing to reason about which of +// the remaining atom characters are harmless here. +func messageIDDomainOK(d string) bool { + if d == "" { + return false + } + for _, r := range d { + switch { + case r >= 'a' && r <= 'z', r >= '0' && r <= '9', r == '.', r == '-': + default: + return false + } + } + return true +} + func (s *SMTP) buildRaw(msg Message) ([]byte, error) { from := mail.Address{Name: s.fromName, Address: s.from} @@ -277,8 +323,7 @@ func (s *SMTP) buildRaw(msg Message) ([]byte, error) { fmt.Fprintf(&buf, "Date: %s\r\n", time.Now().Format(time.RFC1123Z)) // RFC 5322: Globally unique message identifier. - fmt.Fprintf(&buf, "Message-ID: <%s@%s>\r\n", - uid.New(), strings.ToLower(from.Address[strings.LastIndex(from.Address, "@")+1:])) + fmt.Fprintf(&buf, "Message-ID: <%s@%s>\r\n", uid.New(), s.messageIDDomain()) fmt.Fprintf(&buf, "From: %s\r\n", from.String()) fmt.Fprintf(&buf, "To: %s\r\n", strings.Join(toFormatted, ", ")) fmt.Fprintf(&buf, "Subject: %s\r\n", subject)