Skip to content

fix(mailer): set Date and Message-ID on SMTP messages - #176

Merged
shockalotti merged 2 commits into
Calnode:mainfrom
tgrahammer:fix/rfc5322-email-headers
Oct 29, 2026
Merged

shockalotti merged 2 commits into
Calnode:mainfrom
tgrahammer:fix/rfc5322-email-headers

Conversation

@tgrahammer

@tgrahammer tgrahammer commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

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 unique-id@sender-domain. Only the SMTP path is affected; the Resend transport sets both headers itself.

Message-ID domain. email_from is not validated anywhere — PatchEmailSettings does not check it and the settings form does not require it — so the domain is not sliced off the raw value. messageIDDomain parses the address 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. This covers an unset email_from (which would have produced <id@>) and the full Calnode <noreply@example.com> form (which would have produced <id@example.com>, closing the identifier early).

Tests. TestBuildRaw_dateHeader validates the header against mail.ParseDate rather than the layout that produced it, plus the numeric zone. TestBuildRaw_messageIDHeader covers shape, a non-empty local part, the expected domain and uniqueness. TestBuildRaw_messageIDDomain tables every From/host combination that reaches buildRaw unvalidated and asserts no header injection.

CHANGELOG has an [Unreleased] → Fixed entry for the change.

Validated with make build, go test ./..., gofmt and go vet.

@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA. ✅
Posted by the CLA Assistant Lite bot.

@tgrahammer

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Oct 28, 2026

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Minor suggestions only — the header generation is correct, but the Message-ID domain has an edge case and neither new header is covered by a test.

Reviewed changes

Reviewed the single commit adding RFC 5322 Date and Message-ID headers to the SMTP transport.

  • internal/mailer/smtp.go — buildRaw now writes Date via time.RFC1123Z (numeric zone) and Message-ID: <uuid@from-domain> before the existing From/To/Subject headers. Only the SMTP path is touched; Resend continues to set its own headers.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash | 𝕏

Comment thread internal/mailer/smtp.go Resolved
Comment thread internal/mailer/smtp.go Resolved

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found. The follow-up commit addresses both prior threads cleanly and adds solid coverage.

Reviewed changes

Reviewed the incremental delta since the previous pullfrog review, commit c622ca7.

  • Message-ID domain hardened — messageIDDomain now recovers the domain by parsing s.from with mail.ParseAddress (handling display names and empty/unparseable values), falling back to the SMTP host and then localhost.
  • Header-injection guard added — messageIDDomainOK restricts the right-hand side to [a-z0-9.-], blocking CR/LF and angle brackets from operator-supplied values.
  • Tests added — TestBuildRaw_dateHeader, TestBuildRaw_messageIDHeader, and TestBuildRaw_messageIDDomain cover the date parse round-trip, numeric-zone form, Message-ID structure/uniqueness, and each From/host fallback including a CRLF-host case.
  • CHANGELOG updated — documents the new headers and the domain fallback behavior.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash | 𝕏

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 <unique-id@sender-domain>,
with the domain taken from the From address. Only the SMTP path is
affected.

Validated with go test ./... and make build.
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 <id@> for an empty setting, and <id@example.com>
for "Calnode <noreply@example.com>" — 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.
@tgrahammer
tgrahammer force-pushed the fix/rfc5322-email-headers branch from c622ca7 to e334f89 Compare October 28, 2026 08:01
shockalotti added a commit to MohammadHadi2031/calnode that referenced this pull request Oct 29, 2026
The append-only guard added in Calnode#161 compares the pull request's ledger with the
tip of the base branch. Signatures are committed to main by the CLA bot, so any
signature added after a PR was opened is absent from its head by construction.
Comparing with today's main calls that "removed" and fails the PR.

That is what happened to Calnode#176, Calnode#179, Calnode#158 and Calnode#159 at once: MohammadHadi2031
signed, the ledger on main grew from 11 to 12 entries, and every PR forked
before that point was reported as revoking his signature. Nothing was removed.

Compare against git merge-base HEAD origin/<base> instead, which only sees
entries the PR's own branch actually had. A shallow clone cannot compute a merge
base, so warn and skip rather than fail closed on an infrastructure limitation.
Checkout now fetches full history for this job; the repository is 8MB.

Verified against the real Calnode#176: base=main tip reports
"MohammadHadi2031 (id 17288524)" as removed and exits 1; base=merge-base
reports nothing and exits 0. A ledger with one signature genuinely deleted
still fails, naming tgrahammer (id 3223680).
@shockalotti

Copy link
Copy Markdown
Contributor

Merging this. The single red check is our bug, not your code.

check is failing on the "CLA ledger is append-only" step, a guard I added that compares the PR's signature ledger against the tip of main instead of the fork point. Signatures are committed to main after a PR opens, so it reports later signatures as revoked. Fixed in #190. Re-running this PR's old job will not clear it, and it is not worth a rebase on your side.

Everything else is green here, including arm64-smoke and both security scans.

The code reads well. Two things I checked deliberately: RFC1123Z gives a numeric zone offset, which is what RFC 5322 wants (time.RFC1123 would emit an alphabetic abbreviation, which moved to obsolete syntax), and messageIDDomainOK keeps CR and LF out of a header built from email_from, which the settings endpoint does not validate. That second one is the kind of thing that turns a deliverability fix into a header injection, so the whitelist is the right call rather than a nicety.

@shockalotti
shockalotti merged commit e130825 into Calnode:main Oct 29, 2026
10 of 11 checks passed
@tgrahammer
tgrahammer deleted the fix/rfc5322-email-headers branch October 29, 2026 06:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants