Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
123 changes: 123 additions & 0 deletions internal/mailer/mailer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package mailer
import (
"context"
"errors"
"net/mail"
"net/url"
"regexp"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -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 <noreply@example.com>", "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)
}
}
})
}
}
51 changes: 51 additions & 0 deletions internal/mailer/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<id@>" - 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}

Expand Down Expand Up @@ -273,6 +319,11 @@ 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(), 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)
Expand Down
Loading