diff --git a/changelog.d/added-18746-mail-reply-smtp-sender.md b/changelog.d/added-18746-mail-reply-smtp-sender.md new file mode 100644 index 0000000000..398630cdb1 --- /dev/null +++ b/changelog.d/added-18746-mail-reply-smtp-sender.md @@ -0,0 +1 @@ +- **Hub: the sender behind the confirmation reply for mail sent to a hive, off by default ([#18746](https://github.com/hivecommons/hive/issues/18746)).** When the DKIM key file, `From`, selector and the new `HIVE_HUB_MAIL_REPLY_HELO` name are all set, the hub now delivers the "received" and "waiting for approval" replies itself: one plain-text message signed with the key (RSA of at least 2048 bits or Ed25519), a null envelope sender (`MAIL FROM:<>`), one connection to the best-preference MX host of the sender's domain with required STARTTLS, timeouts on every step and no retry (any failure is final and the ledger keeps the claim). The ledger limits are applied before anything is sent; a key that cannot be loaded sends nothing and uses up nothing; a header or address with a control character, a non-ASCII address or a MX host in a private range is refused before any connection; the log holds only ids, a reason class and a server's numeric code. A hub without the key file builds no sender and opens no connection. Optionally the hub hands the signed message to a fixed-IP relay (smarthost) instead of the recipient's MX with `HIVE_HUB_MAIL_REPLY_RELAY` (`host` or `host:port`, default port 587; unset keeps direct delivery) and, if the relay needs SMTP AUTH, `HIVE_HUB_MAIL_REPLY_RELAY_AUTH_FILE` (user name and password read from a mounted file, sent only after STARTTLS, never logged): STARTTLS stays required with no fallback to direct delivery, the envelope sender stays null and the relay must not re-sign. The owner still has to publish the DKIM and SPF records, confirm outbound TCP 25 and run the two-provider `dmarc=pass` test before turning it on. See [Replies to the sender](src/docs/meetings.md#replies-to-the-sender-off-by-default). diff --git a/src/docs/env-vars.md b/src/docs/env-vars.md index e28acd1b66..5561808548 100644 --- a/src/docs/env-vars.md +++ b/src/docs/env-vars.md @@ -98,9 +98,12 @@ behavior. [Maintenance limitations](#keeping-this-reference-current) still apply | `HIVE_HUB_MAIL_MAX_BODY_BYTES` | No | `50331648` | Largest delivery `POST /api/mail/inbound` accepts from a `hive-mx` that sends routing headers (48 MiB, this route only; [#18564](https://github.com/hivecommons/hive/issues/18564)). Without routing headers the cap stays 8 MiB. | | `HIVE_HUB_MAIL_CHUNK_BYTES` | No | `786432` | Piece size when the hub relays a large mail-in delivery to the spoke (768 KiB, under the 1 MiB ingress default); clamped to 64 KiB to 8 MiB. | | `HIVE_HUB_MAIL_SPOOL_DIR` | No | system temp directory | Where the hub keeps a delivery on disk while it relays it. | -| `HIVE_HUB_MAIL_REPLY_DKIM_KEY_FILE` | No | none (replies off) | Path of the mounted DKIM private key the hub signs confirmation replies with ([#18746](https://github.com/hivecommons/hive/issues/18746)). Replies are off while it is unset, missing or empty; the hub only checks that the file exists and never reads or logs it. The sender that uses it is not part of this release, so setting it alone sends nothing. See [Replies to the sender](meetings.md#replies-to-the-sender-off-by-default). | +| `HIVE_HUB_MAIL_REPLY_DKIM_KEY_FILE` | No | none (replies off) | Path of the mounted DKIM private key the hub signs confirmation replies with ([#18746](https://github.com/hivecommons/hive/issues/18746)): a PEM file, PKCS#8 or PKCS#1, RSA of at least 2048 bits (2048 recommended) or Ed25519. Replies are off while it is unset, missing or empty. The hub reads it only to sign a reply, checks on every delivery that it can be loaded (an unusable key sends nothing and uses up nothing), and never logs it. See [Replies to the sender](meetings.md#replies-to-the-sender-off-by-default). | | `HIVE_HUB_MAIL_REPLY_FROM` | With the key | none | The one fixed `From` address replies are signed as, for example `hive@hive.`. Its domain must carry the DKIM record. Mail from this address or its domain is never answered. | | `HIVE_HUB_MAIL_REPLY_DKIM_SELECTOR` | With the key | none | DKIM selector of the key: the record is `._domainkey.`. | +| `HIVE_HUB_MAIL_REPLY_HELO` | With the key | none | Fully qualified host name the hub introduces itself with when it delivers a reply (SMTP `EHLO`). It must be the name that carries the `v=spf1 a -all` record and that the sending IP's reverse DNS returns. Without it (or with a name that is not a fully qualified host) no reply is sent. | +| `HIVE_HUB_MAIL_REPLY_RELAY` | No | none (direct MX delivery) | Optional smarthost for confirmation replies, `` or `:` (port `587` when omitted), so the mail leaves from one stable IP with reverse DNS and SPF. The hub still DKIM-signs the message, uses the null envelope sender and requires STARTTLS with a certificate that verifies for this host name; it never falls back to direct delivery. A value that is not a host name with an optional port builds no sender. See [Replies to the sender](meetings.md#replies-to-the-sender-off-by-default). | +| `HIVE_HUB_MAIL_REPLY_RELAY_AUTH_FILE` | No | none (no AUTH) | Path of a mounted file with the relay's SMTP AUTH credentials: user name on the first line, password on the second (at most 1 KiB). Needs `HIVE_HUB_MAIL_REPLY_RELAY`. Sent as AUTH PLAIN after STARTTLS only; read for each send, never logged. Credentials that cannot be read send nothing and use up nothing. | | `HIVE_HUB_MAIL_REPLY_LEDGER_FILE` | No | `/data/saas/mail-reply-ledger.jsonl` | Reply ledger: ids, salted hashes and counts only, kept 8 days (no address, subject, Message-ID or body). It makes a retry or a restart unable to produce a second reply. | | `HIVE_HUB_MAIL_REPLY_SENDER_PER_HOUR` | No | `10` | Most replies to one sender address in an hour. Past a limit the reply is dropped and counted; the mail is unaffected. | | `HIVE_HUB_MAIL_REPLY_SENDER_PER_DAY` | No | `30` | Most replies to one sender address in 24 hours. | diff --git a/src/docs/inbox-voice-sources.md b/src/docs/inbox-voice-sources.md index 44074d8368..eb90ccd029 100644 --- a/src/docs/inbox-voice-sources.md +++ b/src/docs/inbox-voice-sources.md @@ -563,8 +563,10 @@ A hive never sends mail. What a sender hears back comes from two places: Why no confirmation mail: an automatic reply to a `From` address that was not authenticated goes to whoever the forger named, which is how a mail server ends up on blocklists. A reply only to authenticated senders is -designed in [#18746](https://github.com/hivecommons/hive/issues/18746) and -is not built. +designed in [#18746](https://github.com/hivecommons/hive/issues/18746). It +is built but off until the project's DNS records exist and an operator turns +it on ([Replies to the sender](meetings.md#replies-to-the-sender-off-by-default)), +so today no confirmation mail is sent. The entries below are the reasons, by the code the Recent mail list and a problem report use. The limits are the defaults. diff --git a/src/docs/meetings.md b/src/docs/meetings.md index 1e20e188a0..e8f5478b38 100644 --- a/src/docs/meetings.md +++ b/src/docs/meetings.md @@ -240,7 +240,7 @@ What `hive-mx` does with a message: - **Sender authentication:** after DATA it checks SPF, DKIM and DMARC and records whether SPF or DKIM passed for a domain aligned with the `From` address (`adkim`/`aspf` honoured). Only an aligned sender can ever be trusted by a hive; the receiver itself decides nothing. - **Calendar invites:** only `text/calendar` and `application/ics` parts are read. It keeps UID, SEQUENCE, METHOD (REQUEST or CANCEL), RECURRENCE-ID, the summary (200 characters), organizer and attendees, start and end converted to UTC (Outlook's Windows time zone names such as "Eastern Standard Time" are mapped to IANA zones), RRULE and EXDATE, and the join details: the link from `X-GOOGLE-CONFERENCE`, `X-MICROSOFT-SKYPETEAMSMEETINGURL`, `LOCATION` or a link to `meet.google.com`, `teams.microsoft.com`, `teams.live.com` or `*.zoom.us` found in the description, the platform, and the meeting id and passcode where the platform uses one (Zoom `pwd`, the Teams "Meeting ID" and "Passcode" lines). Nothing else from the description is kept. - **Mail-in files:** a message with no invite may carry Markdown (`.md`), office document (`.pdf`, `.docx`, `.pptx`, `.xlsx`), audio (`.m4a`, `.mp3`, `.wav`, `.ogg`, `.webm`), video (`.mp4`, `.mov`, `.mkv`) and image (`.png`, `.jpg`, `.gif`, `.webp`) attachments. The type is decided by the file's content, not its name, and must agree with the extension; a Word, PowerPoint or Excel file must be a readable container of that kind with no macro project. At most 10 files, 25 MB per file and 30 MB in all ([#18564](https://github.com/hivecommons/hive/issues/18564), [#18699](https://github.com/hivecommons/hive/issues/18699)). The subject becomes the title. Nothing in the message is executed or followed, and a document is stored, never opened. -- **Refusals say why:** a message that leaves nothing to deliver is refused after DATA with a reply the sender's provider returns in its bounce, naming each attachment's extension and what was wrong with it, for example `550 5.6.0 nothing to deliver: a .key attachment is not an accepted type. Accepted types: md, pdf, docx, pptx, xlsx, ...` or `552 5.3.4 nothing to deliver: a .mp4 attachment is over the 25 MB per-file limit (30 MB of attachments per message)`. An attachment left out of a message that is otherwise delivered is listed in the delivery (`files.skipped`: extension and reason) so the hive can show it. Every refusal that is about the message or the address ends with three more lines: how to try again, a link to the reason's entry in [Mailing your hive: what the replies mean](inbox-voice-sources.md#mailing-your-hive-what-the-replies-mean), and where to report a problem ([#18744](https://github.com/hivecommons/hive/issues/18744)). The lines are the same for every sender. This bounce is the only message a sender gets: neither the receiver nor a hive sends mail, so nothing is ever sent to an address that may be forged (a reply to authenticated senders is designed in [#18746](https://github.com/hivecommons/hive/issues/18746)). +- **Refusals say why:** a message that leaves nothing to deliver is refused after DATA with a reply the sender's provider returns in its bounce, naming each attachment's extension and what was wrong with it, for example `550 5.6.0 nothing to deliver: a .key attachment is not an accepted type. Accepted types: md, pdf, docx, pptx, xlsx, ...` or `552 5.3.4 nothing to deliver: a .mp4 attachment is over the 25 MB per-file limit (30 MB of attachments per message)`. An attachment left out of a message that is otherwise delivered is listed in the delivery (`files.skipped`: extension and reason) so the hive can show it. Every refusal that is about the message or the address ends with three more lines: how to try again, a link to the reason's entry in [Mailing your hive: what the replies mean](inbox-voice-sources.md#mailing-your-hive-what-the-replies-mean), and where to report a problem ([#18744](https://github.com/hivecommons/hive/issues/18744)). The lines are the same for every sender. This bounce is the only message a sender gets unless the hub's confirmation reply is turned on, and the receiver never sends mail: nothing is ever sent to an address that may be forged, and a confirmation reply goes only to a sender whose mail passed DMARC alignment ([Replies to the sender](#replies-to-the-sender-off-by-default), [#18746](https://github.com/hivecommons/hive/issues/18746)). - **Text body:** the message's body becomes a Markdown note. `text/plain` is kept as sent; an HTML-only body is reduced to its text, headings, lists and link targets, with no scripts, styles, images or other remote content. A message with no attachment becomes one Markdown item (`message.md`); a message with attachments keeps its body as the note on each of its items. The same allowlist, DMARC and approval rules apply as to attachments, and the body is untrusted data, never instructions. At most 256 KiB of it is kept. - **Proof mails:** a message whose subject carries a proof code (`hive-proof-` and 12 letters or digits) is reported as a proof of the sender address. @@ -305,15 +305,16 @@ nothing for the sender. [#18746](https://github.com/hivecommons/hive/issues/18746) designs a short reply from the hive to whoever mailed it ("received", or "waiting for the owner's approval"). Hive has never sent mail, and `hive-mx` still does not: -the reply is composed and decided by the hub (option A of the design: a -hub-side sender with DKIM, one fixed `From`, the hive's own address as -`Reply-To`). This release ships the decision, the ledger and the two -templates; the SMTP and DKIM sender is a later step. With no sender built -in, a hub sends nothing whatever it is configured with. +the reply is composed, decided and sent by the hub (option A of the design: +a hub-side sender with DKIM, one fixed `From`, the hive's own address as +`Reply-To`). It is off unless `HIVE_HUB_MAIL_REPLY_DKIM_KEY_FILE` names a file that -exists and is not empty (the variables are in -[env-vars.md](env-vars.md)). Removing the file stops replies at once. +exists and is not empty, and the `From` address, DKIM selector and HELO name +are set (the variables are in [env-vars.md](env-vars.md)). Removing the key +file stops replies at once; a key file that cannot be loaded sends nothing +and does not use up the mail's one reply. A hub with none of this set builds +no sender and opens no connection. What decides a reply, all of it enforced in the hub: @@ -352,6 +353,73 @@ outcome and salted hashes of the message id, sender, sender domain and hive: no address, subject or body. The hub logs a sent reply with the delivery id, the hive id and the outcome only, never a key or any part of a mail. +#### How a reply is sent + +The sender (`src/pkg/hub/mail_reply_smtp.go`) runs only after the decision +and the ledger above allowed the reply, so the limits are applied before +anything is sent. + +- **Plain text, null envelope sender.** One `text/plain` message, 7-bit, + with the headers of the design, and `MAIL FROM:<>`. A failed reply + therefore never bounces into a hive. +- **DKIM.** The message is signed (relaxed/relaxed, `rsa-sha256` or + `ed25519-sha256`) as the `From` domain with the configured selector, over + `From`, `To`, `Subject`, `Date`, `Message-ID`, `Reply-To`, `In-Reply-To`, + `References`, `Auto-Submitted`, `X-Auto-Response-Suppress`, `Precedence` + and the MIME headers. The signature expires after 72 hours. +- **Direct delivery to one MX host.** The hub looks up the sender domain's + MX records and uses only the best-preference host: one connection, one + message, no second host, no retry, no redirect. A domain with no MX record + or a null MX gets no reply. The hub connects to a public IPv4 address of + that host (IPv6 only if it has no IPv4 address), on port 25. Addresses in + private, loopback, link-local, shared (100.64.0.0/10), documentation + (IPv6) and similar ranges are refused, because the sender domain is + chosen by whoever sent the mail. +- **STARTTLS is required**, with a certificate that verifies for the MX host + name (TLS 1.2 or later). A server without it, or with a certificate that + does not verify, gets nothing. +- **Optional relay (smarthost).** With `HIVE_HUB_MAIL_REPLY_RELAY` set to + `` or `:` (port 587 when none is given), the + one connection goes to that host instead of the sender's MX, so the mail + leaves from one stable IP that carries the reverse DNS and SPF records. It + is off while the variable is unset, and then everything above is unchanged. + Nothing else changes with a relay: the message is DKIM-signed by the hub + before the hand-off (the relay must not re-sign it), the envelope sender is + still `MAIL FROM:<>`, STARTTLS is still required with a certificate that + verifies for the relay's host name (a relay without it, or whose handshake + fails, gets nothing, and the hub never falls back to direct delivery), and + any failure is still final. The relay is chosen by the operator, so unlike + an MX host it may have a private address. If the relay needs SMTP AUTH, set + `HIVE_HUB_MAIL_REPLY_RELAY_AUTH_FILE` to a mounted file with the user name + on the first line and the password on the second; AUTH PLAIN is sent only + after STARTTLS, the credentials are read from the file for each send and + never come from an inline setting, and they are never logged. A relay + setting that is not a host name with an optional port, or a credentials + file named without a relay, builds no sender (the log names the variable, + not its value); credentials that cannot be read send nothing and use up + nothing, like an unusable key. The hub has no YAML block for this sender, + so the relay is configured through these environment variables only. A + relay runbook is the owner's side of this option. +- **Bounded.** 10 seconds for each DNS lookup, 15 for the connection, 45 + for the whole SMTP conversation, 90 for the whole reply. At most 8 replies + are in flight; past that a reply is dropped and counted. +- **Any failure is final.** A refused recipient, a DNS or TLS failure, a + timeout or a server error ends the reply: it is not retried and the + ledger keeps the claim, so the same message is never answered twice. +- **Refused before sending.** A header value with a control character or a + line break, an address with a display name or outside ASCII, a + `Message-ID` that is not a plain token, a body that is not 7-bit text, or + a `From` other than the configured one is refused before any DNS lookup + or connection. The sender's subject only reaches the `Subject` header, + after the template removed control and invisible characters, and is sent + as an RFC 2047 encoded word when it has letters outside ASCII. +- **Nothing private in the log.** The hub logs a failed reply with the + delivery id, the hive id, a reason class (for example `mx_lookup`, + `no_mx`, `timeout`, `starttls`, `recipient_refused`, `connect`, + `signing_key_unusable`, and with a relay `relay_address_lookup`, + `relay_auth_unavailable`, `relay_auth`, `relay_credentials_unusable`) and, when the server refused, its numeric code. + Never a key, an address, a domain, a subject, a body or a server's text. + #### DNS records and network the hosted hub needs For option A with one fixed `From` (shape 1), `` being the @@ -361,7 +429,7 @@ project's organizational domain and `` the DKIM selector: |---|---|---| | `._domainkey.hive.` | TXT | `v=DKIM1; k=rsa; p=` (2048-bit; the private key stays in the hub's Secret) | | `hive.` | TXT | `v=spf1 ip4: -all` (recommended: DKIM alone satisfies DMARC, since the envelope sender is null) | -| `` | TXT | `v=spf1 a -all` (recommended: covers the HELO identity used with a null envelope sender) | +| `` | TXT | `v=spf1 a -all` (recommended: covers the HELO identity used with a null envelope sender); this is the name to set in `HIVE_HUB_MAIL_REPLY_HELO` | | reverse DNS of `` | PTR | ``, set at the cloud provider, not in the zone | No existing record changes: the organizational DMARC policy (`p=reject`, @@ -369,7 +437,9 @@ strict alignment) already covers `hive.`, and the DKIM signing domain equals the `From` domain, so it aligns. The hub's network also needs outbound TCP 25 (many clouds block it by default); if it cannot be opened the fallback is a transactional mail provider (option C of the -design). Before turning replies on for everyone, send to a test mailbox at +design), or a relay with a fixed IP (the optional relay above), in which +case the records name the relay's IP and host name and the hub itself needs +no outbound port 25. Before turning replies on for everyone, send to a test mailbox at two large providers and check `Authentication-Results` shows `dmarc=pass`. ### Who can invite Betty diff --git a/src/go.mod b/src/go.mod index 369c499bda..58ac4f6e60 100644 --- a/src/go.mod +++ b/src/go.mod @@ -12,6 +12,7 @@ require ( github.com/emersion/go-ical v0.0.0-20261112170022-46c5b53a1723 github.com/emersion/go-message v0.18.2 github.com/emersion/go-msgauth v0.7.0 + github.com/emersion/go-sasl v0.0.0-20241020182733-b788ff22d5a6 github.com/emersion/go-smtp v0.26.0 github.com/fsnotify/fsnotify v1.10.1 github.com/golang-jwt/jwt/v5 v5.3.1 @@ -43,7 +44,6 @@ require ( require ( github.com/ai-openshell/sdk-go v0.1.8 // indirect github.com/antithesishq/antithesis-sdk-go v0.9.0-default-no-op // indirect - github.com/emersion/go-sasl v0.0.0-20241020182733-b788ff22d5a6 // indirect github.com/google/go-tpm v0.9.8 // indirect github.com/klauspost/compress v1.20.4 // indirect github.com/minio/highwayhash v1.0.4 // indirect diff --git a/src/pkg/hub/mail_reply.go b/src/pkg/hub/mail_reply.go index 3cd30beeb6..a9f046cb78 100644 --- a/src/pkg/hub/mail_reply.go +++ b/src/pkg/hub/mail_reply.go @@ -19,10 +19,10 @@ import ( // // This file is the decision: whether a reply may be sent for a delivery the // hub just relayed, and what outcome it states. It sends nothing itself: a -// mailReplySender does, and this release ships none, so a hub never sends -// mail. Everything is off unless the DKIM signing key file named by -// HIVE_HUB_MAIL_REPLY_DKIM_KEY_FILE exists; without the key (or the From -// address, or a sender) no delivery is even parsed for a reply. +// mailReplySender does (mail_reply_smtp.go). Everything is off unless the +// DKIM signing key file named by HIVE_HUB_MAIL_REPLY_DKIM_KEY_FILE exists; +// without the key (or the From address, the selector or the HELO name) no +// delivery is even parsed for a reply, and nothing is ever sent. // // The safety rules of the issue are enforced here, not left to a sender: // only an authenticated (DMARC-aligned) sender is answered; never automatic, @@ -72,6 +72,7 @@ const ( skipUnreadable = "delivery_unreadable" skipBusy = "busy" skipSendFailed = "send_failed" + skipKeyUnusable = "signing_key_unusable" mailReplyStatSent = "sent" mailReplyLogPrefix = "mail reply: " ) @@ -97,14 +98,22 @@ type mailReplyConfig struct { From string Selector string KeyFile string - Limits mailReplyLimits + // Helo is the host name the sender introduces itself with. + Helo string + // Relay is the optional smarthost, "host" or "host:port" (default port + // mailReplyRelayDefaultPort). Empty means direct MX delivery. + Relay string + // RelayAuthFile is the optional file holding the relay's SMTP AUTH + // credentials: the user name on the first line, the password on the + // second. Only meaningful with Relay. + RelayAuthFile string + Limits mailReplyLimits } -// mailReplySenderFactory builds the real sender from the configuration. It -// is nil in this release: no outbound path exists yet (#18746 step 3 builds -// the decision, the ledger and the templates; the sender follows once the -// DNS records and outbound TCP 25 are confirmed). -var mailReplySenderFactory func(cfg mailReplyConfig) (mailReplySender, error) +// mailReplySenderFactory builds the sender from the configuration. Tests +// replace it; it is only called once the gate in newMailReplierFromEnv has +// passed, so a hub without the key never builds a sender. +var mailReplySenderFactory = newSMTPReplySender // mailReplier decides and sends replies; a nil one does nothing. type mailReplier struct { @@ -128,6 +137,10 @@ func newMailReplierFromEnv(logger *slog.Logger, hosts func() map[string]string) From: strings.ToLower(strings.TrimSpace(os.Getenv(envMailReplyFrom))), Selector: strings.TrimSpace(os.Getenv(envMailReplySelector)), KeyFile: strings.TrimSpace(os.Getenv(envMailReplyKeyFile)), + Helo: strings.TrimSpace(os.Getenv(envMailReplyHelo)), + + Relay: strings.TrimSpace(os.Getenv(envMailReplyRelay)), + RelayAuthFile: strings.TrimSpace(os.Getenv(envMailReplyRelayAuthFile)), Limits: mailReplyLimits{ SenderPerHour: int(mailEnvInt64(envMailReplySenderH, defaultReplySenderPerHour)), SenderPerDay: int(mailEnvInt64(envMailReplySenderD, defaultReplySenderPerDay)), @@ -174,7 +187,7 @@ func newMailReplier(cfg mailReplyConfig, sender mailReplySender, ledgerPath stri } // enabled is true only while the signing key file exists and is not empty, -// and a sender is set. The key is never read here. +// and a sender is set. The key is not read here; the sender reads it to sign. func (r *mailReplier) enabled() bool { if r == nil || r.sender == nil || r.ledger == nil || r.cfg.KeyFile == "" { return false @@ -362,6 +375,13 @@ func (r *mailReplier) handle(ctx context.Context, c mailReplyCandidate) string { if !r.enabled() { return "disabled" } + // A key the sender cannot load sends nothing and claims nothing, so the + // reply can still go once the key is fixed. + if rd, ok := r.sender.(mailReplyReadier); ok && rd.Ready() != nil { + r.count(skipKeyUnusable) + r.logger.Warn(mailReplyLogPrefix+"signing key unusable; no reply is sent", "delivery", c.DeliveryID) + return skipKeyUnusable + } claim, reason := r.ledger.reserve(r.now(), c.HiveID, c.MessageID, c.To, c.Domain, c.DeliveryID, c.Outcome, r.cfg.Limits) if reason != "" { r.count(reason) @@ -372,7 +392,8 @@ func (r *mailReplier) handle(ctx context.Context, c mailReplyCandidate) string { if err := r.sender.Send(ctx, msg); err != nil { r.ledger.finish(claim, resultFailed) r.count(skipSendFailed) - r.logger.Warn(mailReplyLogPrefix+"send failed", "delivery", c.DeliveryID, "hive_id", c.HiveID) + reasonClass, code := mailReplyFailure(err) + r.logger.Warn(mailReplyLogPrefix+"send failed", "delivery", c.DeliveryID, "hive_id", c.HiveID, "reason", reasonClass, "smtp_code", code) return skipSendFailed } r.ledger.finish(claim, resultSent) diff --git a/src/pkg/hub/mail_reply_relay_test.go b/src/pkg/hub/mail_reply_relay_test.go new file mode 100644 index 0000000000..ba623d3519 --- /dev/null +++ b/src/pkg/hub/mail_reply_relay_test.go @@ -0,0 +1,495 @@ +package hub + +import ( + "bytes" + "context" + "crypto/tls" + "crypto/x509" + "errors" + "fmt" + "log/slog" + "net" + "os" + "path/filepath" + "strings" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/emersion/go-sasl" + "github.com/emersion/go-smtp" +) + +// Tests of the optional smarthost (relay) mode of the reply sender (#18746): +// the same fake DNS and fake SMTP server as the direct-delivery tests, a relay +// that speaks submission with optional AUTH, no network. + +const ( + testRelayUser = "relay-user-7f3a" + testRelayPass = "relay-pass-Zx9q-secret" +) + +// relayBackend is a fakeMXBackend that can also offer and require AUTH PLAIN. +type relayBackend struct { + f *fakeMX + offerAuth bool + requireAuth bool + + mu sync.Mutex + authSeen []string // "user:pass" of every AUTH attempt, as the server saw it + authOK int +} + +func (b *relayBackend) NewSession(c *smtp.Conn) (smtp.Session, error) { + inner, err := (&fakeMXBackend{f: b.f}).NewSession(c) + if err != nil { + return nil, err + } + return &relaySession{Session: inner, b: b}, nil +} + +type relaySession struct { + smtp.Session + b *relayBackend + authed bool +} + +func (s *relaySession) AuthMechanisms() []string { + if !s.b.offerAuth { + return nil + } + return []string{sasl.Plain} +} + +func (s *relaySession) Auth(mech string) (sasl.Server, error) { + return sasl.NewPlainServer(func(_, user, pass string) error { + s.b.mu.Lock() + defer s.b.mu.Unlock() + s.b.authSeen = append(s.b.authSeen, user+":"+pass) + if user != testRelayUser || pass != testRelayPass { + return errors.New("bad credentials") + } + s.authed = true + s.b.authOK++ + return nil + }), nil +} + +func (s *relaySession) Mail(from string, o *smtp.MailOptions) error { + if s.b.requireAuth && !s.authed { + return &smtp.SMTPError{Code: 530, EnhancedCode: smtp.EnhancedCode{5, 7, 0}, Message: "authentication required"} + } + return s.Session.Mail(from, o) +} + +func (b *relayBackend) seen() (attempts []string, ok int) { + b.mu.Lock() + defer b.mu.Unlock() + return append([]string(nil), b.authSeen...), b.authOK +} + +type relayEnv struct { + *smtpEnv + backend *relayBackend + authFile string + address atomic.Value // last dialed address, host:port as the sender asked + dialed atomic.Int64 +} + +// newRelayEnv is a sender configured with a relay at the fake MX host name (the +// test certificate is for that name). withTLS makes the relay offer STARTTLS; +// auth makes it offer and require AUTH PLAIN and writes the credentials file. +func newRelayEnv(t *testing.T, withTLS, auth bool) *relayEnv { + t.Helper() + base := newSMTPEnv(t, "ed25519", true) + f := &fakeMX{} + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + b := &relayBackend{f: f, offerAuth: auth, requireAuth: auth} + srv := smtp.NewServer(b) + srv.Domain = testSMTPMXHost + srv.ReadTimeout, srv.WriteTimeout = testSMTPWait, testSMTPWait + srv.MaxMessageBytes = 1 << 20 + // Let the server offer AUTH even without TLS, so a sender that sent + // credentials before STARTTLS would be caught. + srv.AllowInsecureAuth = true + pool := x509.NewCertPool() + if withTLS { + cert, p := testServerCert(t) + pool = p + srv.TLSConfig = &tls.Config{Certificates: []tls.Certificate{cert}, MinVersion: tls.VersionTLS12} + } + go func() { _ = srv.Serve(ln) }() + t.Cleanup(func() { _ = srv.Close() }) + + e := &relayEnv{smtpEnv: base, backend: b} + e.mx = f + host, port, err := parseMailReplyRelay(testSMTPMXHost) + if err != nil { + t.Fatal(err) + } + e.sender.relayHost, e.sender.relayPort = host, port + e.sender.dial = func(ctx context.Context, network, address string) (net.Conn, error) { + e.dialed.Add(1) + e.address.Store(address) + d := net.Dialer{Timeout: testSMTPWait} + return d.DialContext(ctx, network, ln.Addr().String()) + } + e.sender.tlsConfig = func(name string) *tls.Config { + return &tls.Config{ServerName: name, MinVersion: tls.VersionTLS12, RootCAs: pool} + } + if auth { + e.authFile = filepath.Join(t.TempDir(), "relay-auth") + writeRelayAuth(t, e.authFile, testRelayUser+"\n"+testRelayPass+"\n") + e.sender.relayAuthFile = e.authFile + } + return e +} + +func writeRelayAuth(t *testing.T, path, content string) { + t.Helper() + if err := os.WriteFile(path, []byte(content), 0o600); err != nil { + t.Fatal(err) + } +} + +// Relay unset: nothing changes. The MX of the recipient's domain is looked up +// and its address dialed on port 25, as before; no relay setting is read. +func TestRelayUnsetKeepsDirectDelivery(t *testing.T) { + e := newSMTPEnv(t, "ed25519", true) + if e.sender.relayHost != "" || e.sender.relayAuthFile != "" { + t.Fatalf("relay configured by default: %+v", e.sender) + } + if err := e.sender.Send(context.Background(), testReplyMessage("Standup notes")); err != nil { + t.Fatal(err) + } + if got, _ := e.dialed.Load().(string); got != net.JoinHostPort(testSMTPMXIP, "25") { + t.Fatalf("dialed %q", got) + } + if len(e.dns.mxCalls) != 1 || e.dns.mxCalls[0] != "example.org" { + t.Fatalf("MX lookups %v", e.dns.mxCalls) + } + + // The factory with the relay settings empty builds the direct sender. + s, err := newSMTPReplySender(mailReplyConfig{From: testReplyFrom, Selector: testSMTPSelector, KeyFile: e.keyPath, Helo: testSMTPHelo}) + if err != nil { + t.Fatal(err) + } + if rs := s.(*smtpReplySender); rs.relayHost != "" || rs.relayPort != "" || rs.relayAuthFile != "" { + t.Fatalf("relay set from an empty configuration: %+v", rs) + } +} + +// Relay set: the one connection goes to the relay on the default submission +// port, the recipient's MX is never looked up, the envelope sender is still +// null, STARTTLS ran, and the message is the one DKIM signature the hub made. +func TestRelaySetRoutesToRelay(t *testing.T) { + e := newRelayEnv(t, true, false) + if e.sender.relayPort != mailReplyRelayDefaultPort || mailReplyRelayDefaultPort != "587" { + t.Fatalf("default relay port %q", e.sender.relayPort) + } + if err := e.sender.Send(context.Background(), testReplyMessage("Standup notes")); err != nil { + t.Fatal(err) + } + sessions, from, rcpts, msgs, tlsOn := e.mx.snapshot() + if sessions != 1 || len(msgs) != 1 || len(from) != 1 { + t.Fatalf("sessions=%d messages=%d mail-from=%d", sessions, len(msgs), len(from)) + } + if from[0] != "" { + t.Fatalf("envelope sender %q, want the null sender", from[0]) + } + if len(rcpts) != 1 || rcpts[0] != testReplySender { + t.Fatalf("recipients %v", rcpts) + } + if len(tlsOn) != 1 || !tlsOn[0] { + t.Fatal("the message was not sent over STARTTLS") + } + if got, _ := e.address.Load().(string); got != net.JoinHostPort(testSMTPMXIP, "587") { + t.Fatalf("dialed %q, want the relay on port 587", got) + } + if len(e.dns.mxCalls) != 0 { + t.Fatalf("the recipient's MX was looked up: %v", e.dns.mxCalls) + } + if n := strings.Count(string(msgs[0]), "DKIM-Signature:"); n != 1 { + t.Fatalf("%d DKIM signatures, want the hub's one", n) + } +} + +// A relay is operator-chosen, so a private address is fine (unlike a MX host +// named by a sender), and an explicit port is honored. +func TestRelayAllowsPrivateAddressAndExplicitPort(t *testing.T) { + e := newRelayEnv(t, true, false) + e.dns.ips[testSMTPMXHost] = []net.IPAddr{{IP: net.ParseIP("10.20.30.40")}} + host, port, err := parseMailReplyRelay("Relay-Test.Example.Org.:2525") + if err != nil || host != "relay-test.example.org" || port != "2525" { + t.Fatalf("parse: %q %q %v", host, port, err) + } + e.sender.relayPort = "2525" + if err := e.sender.Send(context.Background(), testReplyMessage("Standup notes")); err != nil { + t.Fatal(err) + } + if got, _ := e.address.Load().(string); got != "10.20.30.40:2525" { + t.Fatalf("dialed %q", got) + } +} + +func TestParseMailReplyRelay(t *testing.T) { + for _, tc := range []struct{ in, host, port string }{ + {"", "", ""}, + {" ", "", ""}, + {"smtp.relay.example", "smtp.relay.example", "587"}, + {"smtp.relay.example:465", "smtp.relay.example", "465"}, + } { + h, p, err := parseMailReplyRelay(tc.in) + if err != nil || h != tc.host || p != tc.port { + t.Errorf("%q: %q %q %v", tc.in, h, p, err) + } + } + for _, bad := range []string{"bad_host!", "smtp.relay.example:", "smtp.relay.example:0", "smtp.relay.example:65536", + "smtp.relay.example:25x", "smtp.relay.example:+25", "[::1]:25", "a:b:c", ":25", "smtp.relay.example/path"} { + _, _, err := parseMailReplyRelay(bad) + if err == nil { + t.Errorf("%q accepted", bad) + continue + } + if strings.Contains(err.Error(), bad) && bad != "" { + t.Errorf("the error repeats the value %q: %v", bad, err) + } + } +} + +// STARTTLS refused fails closed: a relay that does not offer it gets no +// message, no AUTH and no fallback to direct delivery. +func TestRelayWithoutSTARTTLSFailsClosed(t *testing.T) { + e := newRelayEnv(t, false, true) + err := e.sender.Send(context.Background(), testReplyMessage("Standup notes")) + if reason, _ := sendErrReason(t, err); reason != sendFailNoTLS { + t.Fatalf("reason %q, want %q", reason, sendFailNoTLS) + } + sessions, from, _, msgs, _ := e.mx.snapshot() + if len(msgs) != 0 || len(from) != 0 || sessions != 1 { + t.Fatalf("sessions=%d mail-from=%d messages=%d", sessions, len(from), len(msgs)) + } + if attempts, _ := e.backend.seen(); len(attempts) != 0 { + t.Fatal("AUTH credentials were sent without STARTTLS") + } + if e.dialed.Load() != 1 || len(e.dns.mxCalls) != 0 { + t.Fatalf("%d connections, MX lookups %v: fell back to direct delivery", e.dialed.Load(), e.dns.mxCalls) + } +} + +// A relay whose certificate is not trusted also fails closed, before AUTH. +func TestRelayUntrustedCertificateFailsClosed(t *testing.T) { + e := newRelayEnv(t, true, true) + e.sender.tlsConfig = func(name string) *tls.Config { + return &tls.Config{ServerName: name, MinVersion: tls.VersionTLS12, RootCAs: x509.NewCertPool()} + } + err := e.sender.Send(context.Background(), testReplyMessage("Standup notes")) + if reason, _ := sendErrReason(t, err); reason != sendFailTLS { + t.Fatalf("reason %q, want %q", reason, sendFailTLS) + } + _, _, _, msgs, _ := e.mx.snapshot() + if attempts, _ := e.backend.seen(); len(msgs) != 0 || len(attempts) != 0 { + t.Fatal("something was sent over a connection whose TLS failed") + } +} + +// AUTH from the file: the credentials are read from the file, used after +// STARTTLS, and the message is accepted only because they were right. +func TestRelayAuthFromFile(t *testing.T) { + e := newRelayEnv(t, true, true) + if err := e.sender.Ready(); err != nil { + t.Fatalf("ready: %v", err) + } + if err := e.sender.Send(context.Background(), testReplyMessage("Standup notes")); err != nil { + t.Fatal(err) + } + attempts, ok := e.backend.seen() + if ok != 1 || len(attempts) != 1 || attempts[0] != testRelayUser+":"+testRelayPass { + t.Fatalf("server saw %v (ok=%d)", attempts, ok) + } + _, from, _, msgs, tlsOn := e.mx.snapshot() + if len(msgs) != 1 || len(from) != 1 || from[0] != "" || len(tlsOn) != 1 || !tlsOn[0] { + t.Fatalf("messages=%d from=%v tls=%v", len(msgs), from, tlsOn) + } + + // CRLF line ends and no final newline read the same. + writeRelayAuth(t, e.authFile, testRelayUser+"\r\n"+testRelayPass) + if err := e.sender.Send(context.Background(), testReplyMessage("Second")); err != nil { + t.Fatalf("crlf file: %v", err) + } +} + +func TestRelayAuthFailures(t *testing.T) { + t.Run("wrong password", func(t *testing.T) { + e := newRelayEnv(t, true, true) + writeRelayAuth(t, e.authFile, testRelayUser+"\nnot-the-password\n") + err := e.sender.Send(context.Background(), testReplyMessage("Standup notes")) + if reason, _ := sendErrReason(t, err); reason != sendFailAuthDenied { + t.Fatalf("reason %q", reason) + } + if _, _, _, msgs, _ := e.mx.snapshot(); len(msgs) != 0 { + t.Fatal("a message was sent after a refused AUTH") + } + }) + t.Run("relay does not offer AUTH", func(t *testing.T) { + e := newRelayEnv(t, true, true) + e.backend.offerAuth, e.backend.requireAuth = false, false + err := e.sender.Send(context.Background(), testReplyMessage("Standup notes")) + if reason, _ := sendErrReason(t, err); reason != sendFailNoAuth { + t.Fatalf("reason %q", reason) + } + if _, _, _, msgs, _ := e.mx.snapshot(); len(msgs) != 0 { + t.Fatal("a message was sent to a relay that cannot authenticate") + } + }) + t.Run("credentials file unusable", func(t *testing.T) { + for name, content := range map[string]string{ + "empty": "", + "one line": testRelayUser + "\n", + "three lines": "a\nb\nc\n", + "empty user": "\n" + testRelayPass + "\n", + "empty pass": testRelayUser + "\n\n", + "nul": "a\x00b\nc\n", + "over the cap": strings.Repeat("x", mailReplyAuthMaxBytes) + "\npass\n", + "blank padding": "\n\n", + } { + e := newRelayEnv(t, true, true) + writeRelayAuth(t, e.authFile, content) + if err := e.sender.Ready(); err == nil { + t.Errorf("%s: ready", name) + } + err := e.sender.Send(context.Background(), testReplyMessage("Standup notes")) + if reason, _ := sendErrReason(t, err); reason != sendFailAuthFile { + t.Errorf("%s: reason %q", name, reason) + } + if e.dialed.Load() != 0 { + t.Errorf("%s: connected with unusable credentials", name) + } + } + e := newRelayEnv(t, true, true) + if err := os.Remove(e.authFile); err != nil { + t.Fatal(err) + } + if err := e.sender.Ready(); err == nil || e.dialed.Load() != 0 { + t.Fatal("a missing credentials file was not refused") + } + }) +} + +// No secret reaches an error, a log line or a failed-send report: not the +// password, not the user name, not the credentials file's path. +func TestRelayNoSecretInErrorsOrLogs(t *testing.T) { + e := newRelayEnv(t, true, true) + writeRelayAuth(t, e.authFile, testRelayUser+"\n"+testRelayPass+"-wrong\n") + + logs := &bytes.Buffer{} + logger := slog.New(slog.NewTextHandler(logs, &slog.HandlerOptions{Level: slog.LevelDebug})) + ledger := filepath.Join(t.TempDir(), "ledger.jsonl") + cfg := mailReplyConfig{From: testReplyFrom, Selector: testSMTPSelector, KeyFile: e.keyPath, Helo: testSMTPHelo, + Relay: testSMTPMXHost, RelayAuthFile: e.authFile, + Limits: mailReplyLimits{SenderPerHour: 10, SenderPerDay: 30, DomainPerDay: 100, HivePerDay: 200}} + r, err := newMailReplier(cfg, e.sender, ledger, logger, + func() map[string]string { return map[string]string{testReplyHost: testReplyHive} }, time.Now) + if err != nil { + t.Fatal(err) + } + w := &replyWithSender{smtpEnv: e.smtpEnv, r: r, logs: logs, ledger: ledger} + if got := w.r.handle(context.Background(), w.candidate("m1@mail.example.org", testReplySender)); got != skipSendFailed { + t.Fatalf("got %q", got) + } + + sendErr := e.sender.Send(context.Background(), testReplyMessage("Standup notes")) + if sendErr == nil { + t.Fatal("a wrong password was accepted") + } + reports := []string{logs.String(), sendErr.Error(), fmt.Sprintf("%+v", sendErr), fmt.Sprintf("%#v", sendErr)} + for _, bad := range []string{"", "x", "a:b", "smtp.relay.example:99999"} { + _, _, perr := parseMailReplyRelay(bad) + if perr != nil { + reports = append(reports, perr.Error()) + } + } + _, _, loadErr := loadMailReplyRelayAuth(e.authFile + ".missing") + reports = append(reports, loadErr.Error(), fmt.Sprintf("%+v", loadErr)) + if !strings.Contains(logs.String(), sendFailAuthDenied) { + t.Fatalf("the log lacks the reason class: %s", logs.String()) + } + for _, report := range reports { + for _, secret := range []string{testRelayPass, testRelayUser, "-wrong", e.authFile, filepath.Base(e.authFile), "AUTH PLAIN", "Standup", "alice"} { + if strings.Contains(report, secret) { + t.Fatalf("%q appears in %q", secret, report) + } + } + } +} + +// The environment: a relay setting that cannot be used builds no sender (it +// never falls back to direct delivery), a credentials file needs a relay, and +// the valid combination builds a relay sender. +func TestRelayEnvironment(t *testing.T) { + dir := t.TempDir() + e := newSMTPEnv(t, "ed25519", true) + auth := filepath.Join(dir, "relay-auth") + writeRelayAuth(t, auth, testRelayUser+"\n"+testRelayPass+"\n") + t.Setenv(envMailReplyLedger, filepath.Join(dir, "l.jsonl")) + t.Setenv(envMailReplyKeyFile, e.keyPath) + t.Setenv(envMailReplyFrom, testReplyFrom) + t.Setenv(envMailReplySelector, testSMTPSelector) + t.Setenv(envMailReplyHelo, testSMTPHelo) + t.Setenv(envMailReplyRelay, "") + t.Setenv(envMailReplyRelayAuthFile, "") + + logs := &bytes.Buffer{} + logger := slog.New(slog.NewTextHandler(logs, nil)) + + r := newMailReplierFromEnv(logger, nil) + if r == nil { + t.Fatal("no replier without a relay") + } + if s := r.sender.(*smtpReplySender); s.relayHost != "" || s.relayAuthFile != "" { + t.Fatalf("relay set by default: %+v", s) + } + + t.Setenv(envMailReplyRelay, "smtp.relay.example") + r = newMailReplierFromEnv(logger, nil) + if r == nil { + t.Fatal("no replier with a relay") + } + s := r.sender.(*smtpReplySender) + if s.relayHost != "smtp.relay.example" || s.relayPort != "587" || s.relayAuthFile != "" { + t.Fatalf("relay sender: %+v", s) + } + + t.Setenv(envMailReplyRelay, "smtp.relay.example:2525") + t.Setenv(envMailReplyRelayAuthFile, auth) + r = newMailReplierFromEnv(logger, nil) + if r == nil { + t.Fatal("no replier with a relay and credentials") + } + if s := r.sender.(*smtpReplySender); s.relayPort != "2525" || s.relayAuthFile != auth { + t.Fatalf("relay sender: %+v", s) + } + + // Unusable: no sender, and the log does not hold the value or the path. + logs.Reset() + t.Setenv(envMailReplyRelay, "bad_host!:99999") + if newMailReplierFromEnv(logger, nil) != nil { + t.Fatal("a replier with an unusable relay setting") + } + t.Setenv(envMailReplyRelay, "") + if newMailReplierFromEnv(logger, nil) != nil { + t.Fatal("a replier with credentials but no relay") + } + for _, leak := range []string{"bad_host!", "99999", auth, testRelayPass, testRelayUser} { + if strings.Contains(logs.String(), leak) { + t.Fatalf("the log contains %q: %s", leak, logs.String()) + } + } + if !strings.Contains(logs.String(), envMailReplyRelay) { + t.Fatalf("the log does not name the variable: %s", logs.String()) + } +} diff --git a/src/pkg/hub/mail_reply_smtp.go b/src/pkg/hub/mail_reply_smtp.go new file mode 100644 index 0000000000..15b1bd87b5 --- /dev/null +++ b/src/pkg/hub/mail_reply_smtp.go @@ -0,0 +1,683 @@ +package hub + +import ( + "bytes" + "context" + "crypto" + "crypto/ed25519" + "crypto/rand" + "crypto/rsa" + "crypto/tls" + "crypto/x509" + "encoding/hex" + "encoding/pem" + "errors" + "io" + "mime" + "net" + "net/netip" + "net/smtp" + "net/textproto" + "os" + "regexp" + "sort" + "strconv" + "strings" + "time" + + "github.com/emersion/go-msgauth/dkim" +) + +// The real sender behind mailReplySender (hivecommons/hive#18746, option A): +// one plain-text message, DKIM-signed with the mounted key, delivered by a +// direct MX lookup with STARTTLS. It is reached only through the replier's +// gate (HIVE_HUB_MAIL_REPLY_DKIM_KEY_FILE present, From, selector and HELO +// name set), after the decision and the ledger allowed the reply. +// +// Hard rules, each tested: +// - the envelope sender is always null (MAIL FROM:<>), so a failed reply +// never bounces into a hive; +// - one MX host (the best preference), one connection, one message: no +// second MX, no retry, no redirect; any failure is final; +// - every step is bounded by a timeout; +// - a header value with a control character, a non-ASCII address or a +// Message-ID that is not a plain token is refused before any connection; +// - the key is read for the signature only and never logged, and neither +// are the body, the subject or any address: a failure is reported as a +// reason class and, for a server refusal, its numeric code. +// +// Optional smarthost (the relay option of #18746): with +// HIVE_HUB_MAIL_REPLY_RELAY set, the one connection goes to that host and +// port instead of the recipient's MX, so mail leaves from one stable IP. The +// rules above do not change: the message is already DKIM-signed here, the +// relay never re-signs it, the envelope sender stays null, STARTTLS is +// required (a relay that does not offer it, or whose handshake fails, gets +// nothing: no plain-text fallback, no direct delivery instead), and optional +// SMTP AUTH uses credentials read from a file, never from the environment, +// and never logged. A relay setting that cannot be used builds no sender. +const ( + // envMailReplyHelo is the host name the hub introduces itself with. It is + // the name carrying the HELO SPF record and the reverse DNS of the + // sending IP, so it must be set; there is no default. + envMailReplyHelo = "HIVE_HUB_MAIL_REPLY_HELO" + + // envMailReplyRelay is the optional smarthost, "host" or "host:port". + // Unset means direct MX delivery, as before. + envMailReplyRelay = "HIVE_HUB_MAIL_REPLY_RELAY" + // envMailReplyRelayAuthFile names the file with the relay's SMTP AUTH + // credentials. Unset means no AUTH. + envMailReplyRelayAuthFile = "HIVE_HUB_MAIL_REPLY_RELAY_AUTH_FILE" + + // mailReplyRelayDefaultPort is the relay port when none is given: the + // mail submission port, where STARTTLS and AUTH are expected. + mailReplyRelayDefaultPort = "587" + // mailReplyRelayMaxPort is the highest valid TCP port. + mailReplyRelayMaxPort = 65535 + // mailReplyAuthMaxBytes bounds the credentials file read. + mailReplyAuthMaxBytes = 1 << 10 + + // mailSMTPPort is where mail is delivered to an MX host. + mailSMTPPort = "25" + + // Timeouts. They add up to less than mailReplyTimeout. + mailReplyDNSTimeout = 10 * time.Second + mailReplyDialTimeout = 15 * time.Second + mailReplySessionTimeout = 45 * time.Second + + // mailReplyKeyMaxBytes bounds the key file read. + mailReplyKeyMaxBytes = 16 << 10 + // mailReplyRSAMinBits is the smallest RSA key the sender signs with. + mailReplyRSAMinBits = 2048 + // mailReplySignatureTTL is how long a signature stays valid. + mailReplySignatureTTL = 72 * time.Hour + // mailReplyMsgIDBytes is the random part of the reply's own Message-ID. + mailReplyMsgIDBytes = 16 + // mailReplyMaxLine is the longest body or header line (RFC 5322). + mailReplyMaxLine = 998 + // mailReplyMaxBody bounds a reply body; the templates are far smaller. + mailReplyMaxBody = 8 << 10 + // mailReplyMaxHeaderValue bounds one header value before encoding. + mailReplyMaxHeaderValue = 1 << 10 + // mailReplyCharset is the charset of an encoded Subject. + mailReplyCharset = "utf-8" + // asciiMax is the largest ASCII byte. + asciiMax = 0x7f + // asciiPrintableMin and asciiDEL bound the printable header bytes. + asciiPrintableMin = 0x20 + asciiDEL = 0x7f +) + +// Reasons a send failed. They are classes, never text from a server or a +// mail, so they are safe to log. +const ( + sendFailKey = "signing_key_unusable" + sendFailMessage = "message_refused" + sendFailInternal = "internal" + sendFailMXLookup = "mx_lookup" + sendFailNoMX = "no_mx" + sendFailMXAddr = "mx_address_lookup" + sendFailBlocked = "mx_address_not_public" + sendFailDial = "connect" + sendFailGreeting = "greeting" + sendFailHello = "hello" + sendFailNoTLS = "starttls_unavailable" + sendFailTLS = "starttls" + sendFailMailFrom = "mail_from_refused" + sendFailRcpt = "recipient_refused" + sendFailData = "data_refused" + sendFailTimeout = "timeout" + + sendFailRelayAddr = "relay_address_lookup" + sendFailAuthFile = "relay_credentials_unusable" + sendFailNoAuth = "relay_auth_unavailable" + sendFailAuthDenied = "relay_auth" +) + +// mailReplySendError is a failed send: a reason class and, when the server +// answered with a refusal, its numeric code. It wraps nothing: an underlying +// error can quote an address or a server's text. +type mailReplySendError struct { + Reason string + Code int +} + +func (e *mailReplySendError) Error() string { return "mail reply not sent: " + e.Reason } + +// mailReplyFailure returns the reason and code to log for a send error. +func mailReplyFailure(err error) (reason string, code int) { + var se *mailReplySendError + if errors.As(err, &se) { + return se.Reason, se.Code + } + return sendFailInternal, 0 +} + +// mailReplyReadier is implemented by a sender that can tell, before the +// ledger is touched, that it cannot send (the key file is unusable). The +// reply is then not attempted and the Message-ID stays unclaimed. +type mailReplyReadier interface { + Ready() error +} + +// mailReplyResolver is the DNS the sender uses; *net.Resolver satisfies it +// and tests pass a fake. +type mailReplyResolver interface { + LookupMX(ctx context.Context, name string) ([]*net.MX, error) + LookupIPAddr(ctx context.Context, host string) ([]net.IPAddr, error) +} + +// mailReplyHeaderKeyRE is the shape of a header name the sender writes. +var mailReplyHeaderKeyRE = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9-]*$`) + +// mailReplySignedHeaders are the header fields the signature covers. +var mailReplySignedHeaders = []string{ + "From", "To", "Subject", "Date", "Message-ID", "Reply-To", "In-Reply-To", "References", + "Auto-Submitted", "X-Auto-Response-Suppress", "Precedence", + "MIME-Version", "Content-Type", "Content-Transfer-Encoding", +} + +// mailReplyBlockedPrefixes are address ranges that are never a mail host on +// the public internet and that IsPrivate and its siblings do not cover. +var mailReplyBlockedPrefixes = []netip.Prefix{ + netip.MustParsePrefix("0.0.0.0/8"), + netip.MustParsePrefix("100.64.0.0/10"), + netip.MustParsePrefix("192.0.0.0/24"), + netip.MustParsePrefix("198.18.0.0/15"), + netip.MustParsePrefix("240.0.0.0/4"), + netip.MustParsePrefix("64:ff9b::/96"), + netip.MustParsePrefix("100::/64"), + netip.MustParsePrefix("2001:db8::/32"), +} + +// mailReplyPublicIP is true for an address a mail host may have on the +// public internet. The sender domain is attacker-chosen (any domain whose +// mail passes DMARC), so its MX must never lead the hub to its own network. +func mailReplyPublicIP(ip net.IP) bool { + a, ok := netip.AddrFromSlice(ip) + if !ok { + return false + } + a = a.Unmap() + if !a.IsGlobalUnicast() || a.IsPrivate() || a.IsLoopback() || a.IsLinkLocalUnicast() || a.IsMulticast() || a.IsUnspecified() { + return false + } + for _, p := range mailReplyBlockedPrefixes { + if p.Contains(a) { + return false + } + } + return true +} + +// smtpReplySender is the real mailReplySender. +type smtpReplySender struct { + from, selector, keyFile, helo string + + // relayHost and relayPort are the smarthost; relayHost is empty for + // direct MX delivery. relayAuthFile is the credentials file, if any. + relayHost, relayPort, relayAuthFile string + + resolver mailReplyResolver + // dial opens the one connection. The default refuses nothing itself: + // the address was already checked by mailReplyPublicIP. + dial func(ctx context.Context, network, address string) (net.Conn, error) + // tlsConfig is the client TLS configuration for an MX host name. + tlsConfig func(serverName string) *tls.Config + now func() time.Time + // sessionTimeout bounds the whole SMTP conversation. + sessionTimeout time.Duration +} + +// newSMTPReplySender is the factory the hub installs. It needs the HELO name +// (a valid host name with a dot) besides what the gate already checked. +func newSMTPReplySender(cfg mailReplyConfig) (mailReplySender, error) { + helo := strings.ToLower(strings.TrimSpace(cfg.Helo)) + if !mailHostRE.MatchString(helo) || !strings.Contains(helo, ".") { + return nil, errors.New(envMailReplyHelo + " is missing or not a host name") + } + relayHost, relayPort, err := parseMailReplyRelay(cfg.Relay) + if err != nil { + return nil, err + } + authFile := strings.TrimSpace(cfg.RelayAuthFile) + if authFile != "" && relayHost == "" { + return nil, errors.New(envMailReplyRelayAuthFile + " is set without " + envMailReplyRelay) + } + d := &net.Dialer{Timeout: mailReplyDialTimeout} + return &smtpReplySender{ + from: strings.ToLower(cfg.From), selector: cfg.Selector, keyFile: cfg.KeyFile, helo: helo, + relayHost: relayHost, relayPort: relayPort, relayAuthFile: authFile, + resolver: net.DefaultResolver, + dial: d.DialContext, + tlsConfig: func(serverName string) *tls.Config { + return &tls.Config{ServerName: serverName, MinVersion: tls.VersionTLS12} + }, + now: time.Now, + sessionTimeout: mailReplySessionTimeout, + }, nil +} + +// parseMailReplyRelay splits the relay setting into a host name and a port. +// An empty setting is no relay. A value that is not a host name with an +// optional numeric port is an error that names the variable only, never the +// value: it fails closed instead of falling back to direct delivery. +func parseMailReplyRelay(v string) (host, port string, err error) { + v = strings.TrimSpace(v) + if v == "" { + return "", "", nil + } + invalid := errors.New(envMailReplyRelay + " is not a host name with an optional port") + host, port = v, mailReplyRelayDefaultPort + if strings.Contains(v, ":") { + h, p, serr := net.SplitHostPort(v) + if serr != nil { + return "", "", invalid + } + n, perr := strconv.Atoi(p) + if perr != nil || n < 1 || n > mailReplyRelayMaxPort || strconv.Itoa(n) != p { + return "", "", invalid + } + host, port = h, p + } + host = strings.ToLower(strings.TrimSuffix(host, ".")) + if !mailHostRE.MatchString(host) { + return "", "", invalid + } + return host, port, nil +} + +// Ready reports whether the signing key can be loaded now and, with a relay +// that needs AUTH, whether its credentials can be. +func (s *smtpReplySender) Ready() error { + if _, err := loadMailReplyKey(s.keyFile); err != nil { + return err + } + if s.relayAuthFile != "" { + if _, _, err := loadMailReplyRelayAuth(s.relayAuthFile); err != nil { + return err + } + } + return nil +} + +// Send composes, signs and delivers m. Every failure is final. +func (s *smtpReplySender) Send(ctx context.Context, m mailReplyMessage) error { + raw, err := s.compose(m) + if err != nil { + return err + } + if s.relayHost != "" { + return s.deliverViaRelay(ctx, m.To, raw) + } + mx, err := s.chooseMX(ctx, domainOfAddr(m.To)) + if err != nil { + return err + } + return s.deliver(ctx, mx, m.To, raw) +} + +// compose builds the message and signs it. Nothing is connected to before it +// succeeds, so a bad key or a bad header sends nothing. +func (s *smtpReplySender) compose(m mailReplyMessage) ([]byte, error) { + key, err := loadMailReplyKey(s.keyFile) + if err != nil { + return nil, err + } + refuse := &mailReplySendError{Reason: sendFailMessage} + if !strings.EqualFold(m.From, s.from) || !validMailAddr(m.From) || !validMailAddr(m.To) || !validMailAddr(m.ReplyTo) || + !isASCIIString(m.From) || !isASCIIString(m.To) || !isASCIIString(m.ReplyTo) || !mailMsgIDRE.MatchString(m.MessageID) { + return nil, refuse + } + domain := domainOfAddr(m.From) + if domain == "" { + return nil, refuse + } + idBytes := make([]byte, mailReplyMsgIDBytes) + if _, err := rand.Read(idBytes); err != nil { + return nil, &mailReplySendError{Reason: sendFailInternal} + } + var b bytes.Buffer + line := func(k, v string) { b.WriteString(k + ": " + v + "\r\n") } + line("Date", s.now().UTC().Format(time.RFC1123Z)) + line("Message-ID", "<"+hex.EncodeToString(idBytes)+"@"+domain+">") + for _, h := range m.Headers() { + v := h.Value + if !mailReplyHeaderKeyRE.MatchString(h.Key) || !safeMailHeaderValue(v) { + return nil, refuse + } + if h.Key == "Subject" { + // The only header that may carry the sender's own words. + v = mime.QEncoding.Encode(mailReplyCharset, v) + } else if !isASCIIString(v) { + return nil, refuse + } + line(h.Key, v) + } + line("Content-Transfer-Encoding", "7bit") + b.WriteString("\r\n") + body, ok := normalizeMailBody(m.Text) + if !ok { + return nil, refuse + } + b.WriteString(body) + + var signed bytes.Buffer + err = dkim.Sign(&signed, bytes.NewReader(b.Bytes()), &dkim.SignOptions{ + Domain: domain, + Selector: s.selector, + Signer: key, + Hash: crypto.SHA256, + HeaderCanonicalization: dkim.CanonicalizationRelaxed, + BodyCanonicalization: dkim.CanonicalizationRelaxed, + HeaderKeys: mailReplySignedHeaders, + Expiration: s.now().Add(mailReplySignatureTTL), + }) + if err != nil { + return nil, &mailReplySendError{Reason: sendFailKey} + } + return signed.Bytes(), nil +} + +// chooseMX returns the host of the best-preference MX record of domain. +// Only that one host is ever used: a failure there is final, and a null MX +// (RFC 7505) or a domain without MX records gets no reply. +func (s *smtpReplySender) chooseMX(ctx context.Context, domain string) (string, error) { + if !mailHostRE.MatchString(domain) { + return "", &mailReplySendError{Reason: sendFailMessage} + } + lctx, cancel := context.WithTimeout(ctx, mailReplyDNSTimeout) + defer cancel() + recs, err := s.resolver.LookupMX(lctx, domain) + if len(recs) == 0 { + if err != nil { + return "", classifyMailReplyErr(ctx, sendFailMXLookup, err) + } + return "", &mailReplySendError{Reason: sendFailNoMX} + } + best := make([]*net.MX, 0, len(recs)) + for _, r := range recs { + if r != nil { + best = append(best, r) + } + } + if len(best) == 0 { + return "", &mailReplySendError{Reason: sendFailNoMX} + } + sort.SliceStable(best, func(i, j int) bool { return best[i].Pref < best[j].Pref }) + host := strings.ToLower(strings.TrimSuffix(best[0].Host, ".")) + if host == "" || !mailHostRE.MatchString(host) { + return "", &mailReplySendError{Reason: sendFailNoMX} + } + return host, nil +} + +// pickMailAddr chooses the one address to connect to: the first public IPv4 +// address (the SPF record names an IPv4 address), else the first public IPv6. +func pickMailAddr(addrs []net.IPAddr) (net.IP, bool) { + var v6 net.IP + for _, a := range addrs { + if !mailReplyPublicIP(a.IP) { + continue + } + if a.IP.To4() != nil { + return a.IP, true + } + if v6 == nil { + v6 = a.IP + } + } + return v6, v6 != nil +} + +// deliver makes the one connection and sends raw to one recipient. +func (s *smtpReplySender) deliver(ctx context.Context, mx, to string, raw []byte) error { + lctx, cancel := context.WithTimeout(ctx, mailReplyDNSTimeout) + addrs, err := s.resolver.LookupIPAddr(lctx, mx) + cancel() + if len(addrs) == 0 { + return classifyMailReplyErr(ctx, sendFailMXAddr, err) + } + ip, ok := pickMailAddr(addrs) + if !ok { + return &mailReplySendError{Reason: sendFailBlocked} + } + return s.converse(ctx, net.JoinHostPort(ip.String(), mailSMTPPort), mx, to, raw, nil) +} + +// deliverViaRelay hands raw to the configured smarthost: one connection, one +// message, STARTTLS required, AUTH when credentials are configured. The relay +// is operator-chosen (unlike a sender's MX), so a private address is allowed; +// its certificate is still checked against its configured name. +func (s *smtpReplySender) deliverViaRelay(ctx context.Context, to string, raw []byte) error { + var auth *mailReplyRelayCreds + if s.relayAuthFile != "" { + user, pass, err := loadMailReplyRelayAuth(s.relayAuthFile) + if err != nil { + return err + } + defer clear(pass) + auth = &mailReplyRelayCreds{user: user, pass: pass} + } + lctx, cancel := context.WithTimeout(ctx, mailReplyDNSTimeout) + addrs, err := s.resolver.LookupIPAddr(lctx, s.relayHost) + cancel() + if len(addrs) == 0 { + return classifyMailReplyErr(ctx, sendFailRelayAddr, err) + } + ip := addrs[0].IP + for _, a := range addrs { + if a.IP.To4() != nil { + ip = a.IP + break + } + } + return s.converse(ctx, net.JoinHostPort(ip.String(), s.relayPort), s.relayHost, to, raw, auth) +} + +// mailReplyRelayCreds are the SMTP AUTH credentials, held only for one send. +type mailReplyRelayCreds struct { + user string + pass []byte +} + +// converse makes the one connection to address and sends raw to one +// recipient. serverName is the name the TLS certificate is checked against. +func (s *smtpReplySender) converse(ctx context.Context, address, serverName, to string, raw []byte, auth *mailReplyRelayCreds) error { + conn, err := s.dial(ctx, "tcp", address) + if err != nil { + return classifyMailReplyErr(ctx, sendFailDial, err) + } + defer conn.Close() + // One deadline for the whole conversation, and a cancelled context + // closes the connection under whatever read or write is waiting. + _ = conn.SetDeadline(time.Now().Add(s.sessionTimeout)) + stop := context.AfterFunc(ctx, func() { _ = conn.Close() }) + defer stop() + + c, err := smtp.NewClient(conn, serverName) + if err != nil { + return classifyMailReplyErr(ctx, sendFailGreeting, err) + } + defer c.Close() + if err := c.Hello(s.helo); err != nil { + return classifyMailReplyErr(ctx, sendFailHello, err) + } + if ok, _ := c.Extension("STARTTLS"); !ok { + return &mailReplySendError{Reason: sendFailNoTLS} + } + if err := c.StartTLS(s.tlsConfig(serverName)); err != nil { + return classifyMailReplyErr(ctx, sendFailTLS, err) + } + if auth != nil { + // Only after STARTTLS: the credentials never cross a plain-text link. + if ok, _ := c.Extension("AUTH"); !ok { + return &mailReplySendError{Reason: sendFailNoAuth} + } + if err := c.Auth(smtp.PlainAuth("", auth.user, string(auth.pass), serverName)); err != nil { + return classifyMailReplyErr(ctx, sendFailAuthDenied, err) + } + } + // The null envelope sender: MAIL FROM:<>. + if err := c.Mail(""); err != nil { + return classifyMailReplyErr(ctx, sendFailMailFrom, err) + } + if err := c.Rcpt(to); err != nil { + return classifyMailReplyErr(ctx, sendFailRcpt, err) + } + w, err := c.Data() + if err != nil { + return classifyMailReplyErr(ctx, sendFailData, err) + } + if _, err := w.Write(raw); err != nil { + _ = w.Close() + return classifyMailReplyErr(ctx, sendFailData, err) + } + if err := w.Close(); err != nil { + return classifyMailReplyErr(ctx, sendFailData, err) + } + _ = c.Quit() + return nil +} + +// classifyMailReplyErr turns an error from the network or the server into a +// send error without keeping its text. +func classifyMailReplyErr(ctx context.Context, reason string, err error) error { + e := &mailReplySendError{Reason: reason} + var te *textproto.Error + var ne net.Error + switch { + case ctx.Err() != nil, errors.Is(err, os.ErrDeadlineExceeded), errors.As(err, &ne) && ne.Timeout(): + e.Reason = sendFailTimeout + case errors.As(err, &te): + e.Code = te.Code + } + return e +} + +// loadMailReplyKey reads the DKIM private key (PKCS#8 or PKCS#1 PEM, RSA of +// at least 2048 bits or Ed25519). Every failure is the same error: nothing +// of the file or of the parser's message is passed on. +func loadMailReplyKey(path string) (crypto.Signer, error) { + unusable := &mailReplySendError{Reason: sendFailKey} + if path == "" { + return nil, unusable + } + f, err := os.Open(path) + if err != nil { + return nil, unusable + } + defer f.Close() + st, err := f.Stat() + if err != nil || !st.Mode().IsRegular() || st.Size() == 0 || st.Size() > mailReplyKeyMaxBytes { + return nil, unusable + } + data, err := io.ReadAll(io.LimitReader(f, mailReplyKeyMaxBytes+1)) + if err != nil || len(data) > mailReplyKeyMaxBytes { + return nil, unusable + } + defer clear(data) + block, _ := pem.Decode(data) + if block == nil { + return nil, unusable + } + defer clear(block.Bytes) + var parsed any + switch block.Type { + case "PRIVATE KEY": + parsed, err = x509.ParsePKCS8PrivateKey(block.Bytes) + case "RSA PRIVATE KEY": + parsed, err = x509.ParsePKCS1PrivateKey(block.Bytes) + default: + return nil, unusable + } + if err != nil { + return nil, unusable + } + switch k := parsed.(type) { + case *rsa.PrivateKey: + if k.N.BitLen() < mailReplyRSAMinBits || k.Validate() != nil { + return nil, unusable + } + return k, nil + case ed25519.PrivateKey: + return k, nil + } + return nil, unusable +} + +// loadMailReplyRelayAuth reads the relay's SMTP AUTH credentials: the user +// name on the first line, the password on the second. Like the signing key, +// every failure is the same error and nothing of the file is passed on. +func loadMailReplyRelayAuth(path string) (user string, pass []byte, err error) { + unusable := &mailReplySendError{Reason: sendFailAuthFile} + f, err := os.Open(path) + if err != nil { + return "", nil, unusable + } + defer f.Close() + st, err := f.Stat() + if err != nil || !st.Mode().IsRegular() || st.Size() == 0 || st.Size() > mailReplyAuthMaxBytes { + return "", nil, unusable + } + data, err := io.ReadAll(io.LimitReader(f, mailReplyAuthMaxBytes+1)) + defer clear(data) + if err != nil || len(data) > mailReplyAuthMaxBytes { + return "", nil, unusable + } + lines := bytes.Split(bytes.TrimRight(data, "\r\n"), []byte("\n")) + if len(lines) != 2 { + return "", nil, unusable + } + u := bytes.TrimRight(lines[0], "\r") + p := bytes.TrimRight(lines[1], "\r") + if len(u) == 0 || len(p) == 0 || bytes.ContainsAny(u, "\x00") || bytes.ContainsAny(p, "\x00") { + return "", nil, unusable + } + return string(u), append([]byte(nil), p...), nil +} + +// safeMailHeaderValue is true for a value that can go in a header as it is: +// no CR, LF, NUL or other control character (tab excepted), not too long. +func safeMailHeaderValue(v string) bool { + if len(v) > mailReplyMaxHeaderValue { + return false + } + for i := 0; i < len(v); i++ { + c := v[i] + if (c < asciiPrintableMin && c != '\t') || c == asciiDEL { + return false + } + } + return true +} + +func isASCIIString(s string) bool { + for i := 0; i < len(s); i++ { + if s[i] > asciiMax { + return false + } + } + return true +} + +// normalizeMailBody returns the body with CRLF line ends. It refuses +// anything but 7-bit text: a NUL, a bare CR, a byte above 0x7f, a line over +// 998 bytes or a body over the cap. +func normalizeMailBody(s string) (string, bool) { + if len(s) > mailReplyMaxBody || !isASCIIString(s) { + return "", false + } + s = strings.ReplaceAll(s, "\r\n", "\n") + if strings.ContainsAny(s, "\r\x00") { + return "", false + } + for _, l := range strings.Split(s, "\n") { + if len(l) > mailReplyMaxLine { + return "", false + } + } + if !strings.HasSuffix(s, "\n") { + s += "\n" + } + return strings.ReplaceAll(s, "\n", "\r\n"), true +} diff --git a/src/pkg/hub/mail_reply_smtp_test.go b/src/pkg/hub/mail_reply_smtp_test.go new file mode 100644 index 0000000000..e7dbb2e995 --- /dev/null +++ b/src/pkg/hub/mail_reply_smtp_test.go @@ -0,0 +1,1002 @@ +package hub + +import ( + "bytes" + "context" + "crypto" + "crypto/ecdsa" + "crypto/ed25519" + "crypto/elliptic" + "crypto/rand" + "crypto/rsa" + "crypto/tls" + "crypto/x509" + "crypto/x509/pkix" + "encoding/base64" + "encoding/pem" + "errors" + "io" + "log/slog" + "math/big" + "mime" + "net" + "net/mail" + "os" + "path/filepath" + "strings" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/emersion/go-msgauth/dkim" + "github.com/emersion/go-smtp" +) + +// Tests of the real reply sender (#18746): a fake DNS and a fake SMTP server +// on the loopback interface, no network. Test keys are made in memory, never +// written anywhere but the test's own temp directory. + +const ( + testSMTPHelo = "hub.hivecommons.dev" + testSMTPMXHost = "mx.example.org" + testSMTPMXIP = "203.0.113.25" + testSMTPSelector = "s1" + testSMTPDomain = "hive.hivecommons.dev" + testSMTPWait = 10 * time.Second + smtpTestRSABits = 2048 + smtpTestCertValid = time.Hour +) + +var ( + smtpRSAKeyOnce sync.Once + smtpRSAKey *rsa.PrivateKey +) + +func sharedTestRSAKey(t *testing.T) *rsa.PrivateKey { + t.Helper() + smtpRSAKeyOnce.Do(func() { + k, err := rsa.GenerateKey(rand.Reader, smtpTestRSABits) + if err == nil { + smtpRSAKey = k + } + }) + if smtpRSAKey == nil { + t.Fatal("test key generation failed") + } + return smtpRSAKey +} + +// fakeMailDNS answers MX, address and DKIM key lookups from maps. +type fakeMailDNS struct { + mu sync.Mutex + mx map[string][]*net.MX + mxErr error + ips map[string][]net.IPAddr + ipErr error + txt map[string][]string + mxCalls []string + ipCalls []string +} + +func (f *fakeMailDNS) LookupMX(_ context.Context, name string) ([]*net.MX, error) { + f.mu.Lock() + defer f.mu.Unlock() + f.mxCalls = append(f.mxCalls, name) + return f.mx[name], f.mxErr +} + +func (f *fakeMailDNS) LookupIPAddr(_ context.Context, host string) ([]net.IPAddr, error) { + f.mu.Lock() + defer f.mu.Unlock() + f.ipCalls = append(f.ipCalls, host) + return f.ips[host], f.ipErr +} + +func (f *fakeMailDNS) lookupTXT(name string) ([]string, error) { + f.mu.Lock() + defer f.mu.Unlock() + if v, ok := f.txt[strings.TrimSuffix(name, ".")]; ok { + return v, nil + } + return nil, errors.New("no such record") +} + +// fakeMX is an SMTP server that records what it is sent. +type fakeMX struct { + mu sync.Mutex + ln net.Listener + sessions int + mailFrom []string + rcpts []string + msgs [][]byte + tlsOn []bool + rcptErr error + mailErr error +} + +func (f *fakeMX) snapshot() (sessions int, from, rcpts []string, msgs [][]byte, tlsOn []bool) { + f.mu.Lock() + defer f.mu.Unlock() + return f.sessions, append([]string(nil), f.mailFrom...), append([]string(nil), f.rcpts...), + append([][]byte(nil), f.msgs...), append([]bool(nil), f.tlsOn...) +} + +type fakeMXBackend struct{ f *fakeMX } + +func (b *fakeMXBackend) NewSession(c *smtp.Conn) (smtp.Session, error) { + // go-smtp makes a second session once STARTTLS has run, so only the + // plain-text one counts: that is one per connection. + if _, on := c.TLSConnectionState(); !on { + b.f.mu.Lock() + b.f.sessions++ + b.f.mu.Unlock() + } + return &fakeMXSession{f: b.f, conn: c}, nil +} + +type fakeMXSession struct { + f *fakeMX + conn *smtp.Conn +} + +func (s *fakeMXSession) Reset() {} +func (s *fakeMXSession) Logout() error { return nil } + +func (s *fakeMXSession) Mail(from string, _ *smtp.MailOptions) error { + s.f.mu.Lock() + defer s.f.mu.Unlock() + if s.f.mailErr != nil { + return s.f.mailErr + } + _, on := s.conn.TLSConnectionState() + s.f.tlsOn = append(s.f.tlsOn, on) + s.f.mailFrom = append(s.f.mailFrom, from) + return nil +} + +func (s *fakeMXSession) Rcpt(to string, _ *smtp.RcptOptions) error { + s.f.mu.Lock() + defer s.f.mu.Unlock() + if s.f.rcptErr != nil { + return s.f.rcptErr + } + s.f.rcpts = append(s.f.rcpts, to) + return nil +} + +func (s *fakeMXSession) Data(r io.Reader) error { + b, err := io.ReadAll(r) + if err != nil { + return err + } + s.f.mu.Lock() + s.f.msgs = append(s.f.msgs, b) + s.f.mu.Unlock() + return nil +} + +// testServerCert is a self-signed certificate for the MX host name and the +// pool that trusts it. +func testServerCert(t *testing.T) (tls.Certificate, *x509.CertPool) { + t.Helper() + priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + t.Fatal(err) + } + tmpl := &x509.Certificate{ + SerialNumber: big.NewInt(1), + Subject: pkix.Name{CommonName: testSMTPMXHost}, + DNSNames: []string{testSMTPMXHost}, + NotBefore: time.Now().Add(-time.Minute), + NotAfter: time.Now().Add(smtpTestCertValid), + KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, + BasicConstraintsValid: true, + IsCA: true, + } + der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &priv.PublicKey, priv) + if err != nil { + t.Fatal(err) + } + cert, err := x509.ParseCertificate(der) + if err != nil { + t.Fatal(err) + } + pool := x509.NewCertPool() + pool.AddCert(cert) + return tls.Certificate{Certificate: [][]byte{der}, PrivateKey: priv}, pool +} + +type smtpEnv struct { + dns *fakeMailDNS + mx *fakeMX + sender *smtpReplySender + keyPath string + dials *atomic.Int64 + dialed *atomic.Value +} + +// newSMTPEnv is a sender with a signing key of the given algorithm (rsa or +// ed25519), a fake DNS that publishes its public key and names one MX host, +// and a fake SMTP server (with STARTTLS when withTLS). +func newSMTPEnv(t *testing.T, algo string, withTLS bool) *smtpEnv { + t.Helper() + var signer crypto.Signer + var txt string + switch algo { + case "ed25519": + pub, priv, err := ed25519.GenerateKey(rand.Reader) + if err != nil { + t.Fatal(err) + } + signer = priv + txt = "v=DKIM1; k=ed25519; p=" + base64.StdEncoding.EncodeToString(pub) + default: + k := sharedTestRSAKey(t) + signer = k + der, err := x509.MarshalPKIXPublicKey(&k.PublicKey) + if err != nil { + t.Fatal(err) + } + txt = "v=DKIM1; k=rsa; p=" + base64.StdEncoding.EncodeToString(der) + } + keyDER, err := x509.MarshalPKCS8PrivateKey(signer) + if err != nil { + t.Fatal(err) + } + keyPath := filepath.Join(t.TempDir(), "dkim.pem") + if err := os.WriteFile(keyPath, pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: keyDER}), 0o600); err != nil { + t.Fatal(err) + } + + dns := &fakeMailDNS{ + mx: map[string][]*net.MX{"example.org": {{Host: testSMTPMXHost + ".", Pref: 10}}}, + ips: map[string][]net.IPAddr{testSMTPMXHost: {{IP: net.ParseIP(testSMTPMXIP)}}}, + txt: map[string][]string{testSMTPSelector + "._domainkey." + testSMTPDomain: {txt}}, + } + + mx := &fakeMX{} + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + mx.ln = ln + srv := smtp.NewServer(&fakeMXBackend{f: mx}) + srv.Domain = testSMTPMXHost + srv.ReadTimeout, srv.WriteTimeout = testSMTPWait, testSMTPWait + srv.MaxMessageBytes = 1 << 20 + pool := x509.NewCertPool() + if withTLS { + cert, p := testServerCert(t) + pool = p + srv.TLSConfig = &tls.Config{Certificates: []tls.Certificate{cert}, MinVersion: tls.VersionTLS12} + } + go func() { _ = srv.Serve(ln) }() + t.Cleanup(func() { _ = srv.Close() }) + + e := &smtpEnv{dns: dns, mx: mx, keyPath: keyPath, dials: &atomic.Int64{}, dialed: &atomic.Value{}} + e.sender = &smtpReplySender{ + from: testReplyFrom, selector: testSMTPSelector, keyFile: keyPath, helo: testSMTPHelo, + resolver: dns, + dial: func(ctx context.Context, network, address string) (net.Conn, error) { + e.dials.Add(1) + e.dialed.Store(address) + d := net.Dialer{Timeout: testSMTPWait} + return d.DialContext(ctx, network, ln.Addr().String()) + }, + tlsConfig: func(name string) *tls.Config { + return &tls.Config{ServerName: name, MinVersion: tls.VersionTLS12, RootCAs: pool} + }, + now: time.Now, + sessionTimeout: testSMTPWait, + } + return e +} + +func testReplyMessage(title string) mailReplyMessage { + return renderMailReply(mailReplyReceived, testReplyHost, testReplyFrom, testReplySender, title, testReplyMsgID) +} + +func sendErrReason(t *testing.T, err error) (string, int) { + t.Helper() + var se *mailReplySendError + if !errors.As(err, &se) { + t.Fatalf("not a send error: %v", err) + } + return se.Reason, se.Code +} + +func (e *smtpEnv) sessions() int { + n, _, _, _, _ := e.mx.snapshot() + return n +} + +// The accepted case, for both key types: one connection to the MX host on +// port 25, STARTTLS, a null envelope sender, one recipient, a plain-text +// message that every rule-3 header is on, and a DKIM signature that verifies +// against the published test key. +func TestSMTPReplySenderDeliversSignedPlainText(t *testing.T) { + for _, algo := range []string{"rsa", "ed25519"} { + t.Run(algo, func(t *testing.T) { + e := newSMTPEnv(t, algo, true) + if err := e.sender.Send(context.Background(), testReplyMessage("Standup notes")); err != nil { + t.Fatal(err) + } + sessions, from, rcpts, msgs, tlsOn := e.mx.snapshot() + if sessions != 1 || len(msgs) != 1 || len(from) != 1 { + t.Fatalf("sessions=%d messages=%d mail-from=%d", sessions, len(msgs), len(from)) + } + if from[0] != "" { + t.Fatalf("envelope sender %q, want the null sender", from[0]) + } + if len(rcpts) != 1 || rcpts[0] != testReplySender { + t.Fatalf("recipients %v", rcpts) + } + if len(tlsOn) != 1 || !tlsOn[0] { + t.Fatal("the message was not sent over STARTTLS") + } + if got, _ := e.dialed.Load().(string); got != net.JoinHostPort(testSMTPMXIP, "25") { + t.Fatalf("dialed %q, want the MX host's public address on port 25", got) + } + + vs, err := dkim.VerifyWithOptions(bytes.NewReader(msgs[0]), &dkim.VerifyOptions{LookupTXT: e.dns.lookupTXT}) + if err != nil || len(vs) != 1 { + t.Fatalf("verify: %v (%d signatures)", err, len(vs)) + } + if vs[0].Err != nil || vs[0].Domain != testSMTPDomain { + t.Fatalf("signature: domain %q, error %v", vs[0].Domain, vs[0].Err) + } + signed := strings.ToLower(strings.Join(vs[0].HeaderKeys, ",")) + for _, h := range []string{"from", "to", "subject", "date", "message-id", "in-reply-to", "auto-submitted"} { + if !strings.Contains(signed, h) { + t.Errorf("header %q is not signed (%s)", h, signed) + } + } + + // A changed body no longer verifies. + tampered := bytes.Replace(msgs[0], []byte("reached"), []byte("reachEd"), 1) + vs, err = dkim.VerifyWithOptions(bytes.NewReader(tampered), &dkim.VerifyOptions{LookupTXT: e.dns.lookupTXT}) + if err == nil && len(vs) == 1 && vs[0].Err == nil { + t.Fatal("a modified message still verified") + } + + parsed, err := mail.ReadMessage(bytes.NewReader(msgs[0])) + if err != nil { + t.Fatal(err) + } + want := map[string]string{ + "Auto-Submitted": "auto-replied", + "X-Auto-Response-Suppress": "All", + "Precedence": "auto_reply", + "Reply-To": "hive@" + testReplyHost, + "To": testReplySender, + "Subject": "Re: Standup notes", + "In-Reply-To": "<" + testReplyMsgID + ">", + "References": "<" + testReplyMsgID + ">", + "Content-Type": `text/plain; charset="utf-8"`, + } + for k, v := range want { + if got := parsed.Header.Get(k); got != v { + t.Errorf("%s: %q, want %q", k, got, v) + } + } + if f := parsed.Header.Get("From"); !strings.Contains(f, "<"+testReplyFrom+">") { + t.Errorf("From: %q", f) + } + if parsed.Header.Get("Date") == "" || !strings.HasSuffix(parsed.Header.Get("Message-Id"), "@"+testSMTPDomain+">") { + t.Errorf("Date %q, Message-ID %q", parsed.Header.Get("Date"), parsed.Header.Get("Message-Id")) + } + body, _ := io.ReadAll(parsed.Body) + if !strings.HasPrefix(string(body), "Your mail reached the hive.\r\n") || strings.Contains(strings.ToLower(string(body)), " testSMTPWait { + t.Fatal("the timeout was not honoured") + } + }) + t.Run("context cancelled", func(t *testing.T) { + e := newSMTPEnv(t, "ed25519", true) + e.sender.dial = dialSilent + ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond) + defer cancel() + start := time.Now() + reason, _ := sendErrReason(t, e.sender.Send(ctx, testReplyMessage("Standup notes"))) + if reason != sendFailTimeout || time.Since(start) > testSMTPWait { + t.Fatalf("reason %q after %v", reason, time.Since(start)) + } + }) +} + +// A refused recipient (or sender) is a final failure with the server's code +// and no message data sent; a server that offers no STARTTLS, or whose +// certificate does not verify, gets nothing. +func TestSMTPReplySenderServerRefusals(t *testing.T) { + msg := testReplyMessage("Standup notes") + t.Run("recipient rejected", func(t *testing.T) { + e := newSMTPEnv(t, "ed25519", true) + e.mx.rcptErr = &smtp.SMTPError{Code: 550, EnhancedCode: smtp.EnhancedCode{5, 1, 1}, Message: "no such user alice@example.org"} + err := e.sender.Send(context.Background(), msg) + reason, code := sendErrReason(t, err) + if reason != sendFailRcpt || code != 550 { + t.Fatalf("reason %q code %d", reason, code) + } + if strings.Contains(err.Error(), "alice") { + t.Fatalf("the error carries the server's text: %v", err) + } + sessions, _, _, msgs, _ := e.mx.snapshot() + if sessions != 1 || len(msgs) != 0 { + t.Fatalf("%d sessions, %d messages", sessions, len(msgs)) + } + }) + t.Run("sender rejected", func(t *testing.T) { + e := newSMTPEnv(t, "ed25519", true) + e.mx.mailErr = &smtp.SMTPError{Code: 554, EnhancedCode: smtp.EnhancedCode{5, 7, 1}, Message: "null sender refused"} + reason, code := sendErrReason(t, e.sender.Send(context.Background(), msg)) + if reason != sendFailMailFrom || code != 554 { + t.Fatalf("reason %q code %d", reason, code) + } + }) + t.Run("no STARTTLS", func(t *testing.T) { + e := newSMTPEnv(t, "ed25519", false) + reason, _ := sendErrReason(t, e.sender.Send(context.Background(), msg)) + sessions, from, _, msgs, _ := e.mx.snapshot() + if reason != sendFailNoTLS || sessions != 1 || len(from) != 0 || len(msgs) != 0 { + t.Fatalf("reason %q, %d sessions, %d senders, %d messages", reason, sessions, len(from), len(msgs)) + } + }) + t.Run("certificate not trusted", func(t *testing.T) { + e := newSMTPEnv(t, "ed25519", true) + e.sender.tlsConfig = func(name string) *tls.Config { return &tls.Config{ServerName: name, MinVersion: tls.VersionTLS12} } + reason, _ := sendErrReason(t, e.sender.Send(context.Background(), msg)) + _, from, _, msgs, _ := e.mx.snapshot() + if reason != sendFailTLS || len(from) != 0 || len(msgs) != 0 { + t.Fatalf("reason %q, %d senders, %d messages", reason, len(from), len(msgs)) + } + }) +} + +type replyWithSender struct { + *smtpEnv + r *mailReplier + logs *bytes.Buffer + ledger string + now time.Time +} + +func newReplierWithSMTP(t *testing.T, lim mailReplyLimits) *replyWithSender { + t.Helper() + e := newSMTPEnv(t, "ed25519", true) + w := &replyWithSender{smtpEnv: e, logs: &bytes.Buffer{}, ledger: filepath.Join(t.TempDir(), "ledger.jsonl"), now: time.Date(2026, 11, 18, 12, 0, 0, 0, time.UTC)} + cfg := mailReplyConfig{From: testReplyFrom, Selector: testSMTPSelector, KeyFile: e.keyPath, Helo: testSMTPHelo, Limits: lim} + logger := slog.New(slog.NewTextHandler(w.logs, &slog.HandlerOptions{Level: slog.LevelDebug})) + r, err := newMailReplier(cfg, e.sender, w.ledger, logger, + func() map[string]string { return map[string]string{testReplyHost: testReplyHive} }, func() time.Time { return w.now }) + if err != nil { + t.Fatal(err) + } + w.r = r + return w +} + +func (w *replyWithSender) candidate(msgID, sender string) mailReplyCandidate { + in := goodInput() + in.MessageID = msgID + in.Auth.From = sender + c, reason := w.r.decideMailReply(in, goodAnswer(), testReplyHive, testReplyHost) + if reason != "" { + panic("test candidate refused: " + reason) + } + return c +} + +// The ledger's limits are applied before anything is sent: past a limit the +// server sees no connection at all. +func TestSMTPReplyLimitsApplyBeforeSending(t *testing.T) { + w := newReplierWithSMTP(t, mailReplyLimits{SenderPerHour: 1, SenderPerDay: 30, DomainPerDay: 100, HivePerDay: 200}) + if got := w.r.handle(context.Background(), w.candidate("m1@mail.example.org", testReplySender)); got != mailReplyStatSent { + t.Fatalf("first reply: %q", got) + } + if n := w.sessions(); n != 1 { + t.Fatalf("%d sessions after the first reply", n) + } + if got := w.r.handle(context.Background(), w.candidate("m2@mail.example.org", testReplySender)); got != skipSenderHour { + t.Fatalf("second reply for the same sender: %q", got) + } + if n := w.dials.Load(); n != 1 { + t.Fatalf("%d connections: the reply past the limit was still dialed", n) + } + // The same message again is a duplicate, not a second reply. + if got := w.r.handle(context.Background(), w.candidate("m1@mail.example.org", testReplySender)); got != skipDuplicate || w.dials.Load() != 1 { + t.Fatalf("duplicate: %q, %d connections", got, w.dials.Load()) + } +} + +// A failed send is final: it is not retried, a second delivery of the same +// message does not send, and the log holds ids and a reason class only. +func TestSMTPReplyFailureIsFinalAndLogsNothingPrivate(t *testing.T) { + w := newReplierWithSMTP(t, mailReplyLimits{SenderPerHour: 10, SenderPerDay: 30, DomainPerDay: 100, HivePerDay: 200}) + w.mx.rcptErr = &smtp.SMTPError{Code: 550, EnhancedCode: smtp.EnhancedCode{5, 1, 1}, Message: "no such user alice@example.org"} + c := w.candidate("m1@mail.example.org", testReplySender) + if got := w.r.handle(context.Background(), c); got != skipSendFailed { + t.Fatalf("got %q", got) + } + w.mx.rcptErr = nil + if got := w.r.handle(context.Background(), c); got != skipDuplicate { + t.Fatalf("a failed reply was tried again: %q", got) + } + if n := w.sessions(); n != 1 { + t.Fatalf("%d sessions, want exactly one", n) + } + logs := w.logs.String() + if !strings.Contains(logs, "recipient_refused") || !strings.Contains(logs, "smtp_code=550") || !strings.Contains(logs, testDeliveryID) { + t.Fatalf("log lacks the reason class, code or delivery id: %s", logs) + } + key, err := os.ReadFile(w.keyPath) + if err != nil { + t.Fatal(err) + } + body := strings.Split(strings.TrimSpace(string(key)), "\n")[1] + for _, secret := range []string{"alice", "example.org", "Standup", testReplyMsgID, "m1@", "PRIVATE KEY", body[:24], "Your mail reached"} { + if strings.Contains(logs, secret) { + t.Fatalf("the log contains %q: %s", secret, logs) + } + } +} + +// A key that cannot be loaded sends nothing and claims nothing: once the key +// is fixed the same message is still answered, once. +func TestSMTPReplyUnusableKeyClaimsNothing(t *testing.T) { + w := newReplierWithSMTP(t, mailReplyLimits{SenderPerHour: 10, SenderPerDay: 30, DomainPerDay: 100, HivePerDay: 200}) + good, err := os.ReadFile(w.keyPath) + if err != nil { + t.Fatal(err) + } + if err := os.WriteFile(w.keyPath, []byte("not a key"), 0o600); err != nil { + t.Fatal(err) + } + c := w.candidate("m1@mail.example.org", testReplySender) + if got := w.r.handle(context.Background(), c); got != skipKeyUnusable { + t.Fatalf("unusable key: %q", got) + } + if w.dials.Load() != 0 { + t.Fatal("connected with an unusable key") + } + // Removed altogether: the replier is simply off. + if err := os.Remove(w.keyPath); err != nil { + t.Fatal(err) + } + if got := w.r.handle(context.Background(), c); got != "disabled" || w.dials.Load() != 0 { + t.Fatalf("missing key: %q, %d connections", got, w.dials.Load()) + } + if err := os.WriteFile(w.keyPath, good, 0o600); err != nil { + t.Fatal(err) + } + if got := w.r.handle(context.Background(), c); got != mailReplyStatSent { + t.Fatalf("after the key is fixed: %q", got) + } + if got := w.r.handle(context.Background(), c); got != skipDuplicate || w.sessions() != 1 { + t.Fatalf("second try: %q, %d sessions", got, w.sessions()) + } +} + +// Off by default: no key, nothing is built and nothing is dialed; a key +// without the HELO name builds nothing; the real sender is what a fully +// configured hub gets; and a configured hub whose key file is gone still +// sends nothing. +func TestSMTPReplyOffUnlessConfigured(t *testing.T) { + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) + dir := t.TempDir() + key := filepath.Join(dir, "absent.pem") + t.Setenv(envMailReplyLedger, filepath.Join(dir, "l.jsonl")) + for _, k := range []string{envMailReplyKeyFile, envMailReplyFrom, envMailReplySelector, envMailReplyHelo} { + t.Setenv(k, "") + } + old := mailReplySenderFactory + t.Cleanup(func() { mailReplySenderFactory = old }) + built := 0 + mailReplySenderFactory = func(cfg mailReplyConfig) (mailReplySender, error) { + built++ + return old(cfg) + } + if newMailReplierFromEnv(logger, nil) != nil || built != 0 { + t.Fatal("a replier, or a sender, without any configuration") + } + // The other settings alone do not turn it on either. + t.Setenv(envMailReplyFrom, testReplyFrom) + t.Setenv(envMailReplySelector, testSMTPSelector) + t.Setenv(envMailReplyHelo, testSMTPHelo) + if newMailReplierFromEnv(logger, nil) != nil || built != 0 { + t.Fatal("a replier without the key setting") + } + t.Setenv(envMailReplyKeyFile, key) + t.Setenv(envMailReplyHelo, "") + if newMailReplierFromEnv(logger, nil) != nil { + t.Fatal("a replier without a HELO name") + } + t.Setenv(envMailReplyHelo, "not a host name") + if newMailReplierFromEnv(logger, nil) != nil { + t.Fatal("a replier with an invalid HELO name") + } + t.Setenv(envMailReplyHelo, "localhost") + if newMailReplierFromEnv(logger, nil) != nil { + t.Fatal("a replier with a HELO name that is not a fully qualified host") + } + t.Setenv(envMailReplyHelo, testSMTPHelo) + r := newMailReplierFromEnv(logger, nil) + if r == nil { + t.Fatal("no replier with a complete configuration") + } + s, ok := r.sender.(*smtpReplySender) + if !ok { + t.Fatalf("sender is %T", r.sender) + } + if s.helo != testSMTPHelo || s.selector != testSMTPSelector || s.from != testReplyFrom || s.keyFile != key { + t.Fatalf("sender configuration: %+v", s) + } + // The key file does not exist: still off, and the network is never used. + var dials atomic.Int64 + s.dial = func(context.Context, string, string) (net.Conn, error) { + dials.Add(1) + return nil, errors.New("must not dial") + } + if r.enabled() { + t.Fatal("enabled without the key file") + } + c := mailReplyCandidate{HiveID: testReplyHive, Host: testReplyHost, DeliveryID: testDeliveryID, MessageID: testReplyMsgID, To: testReplySender, Domain: "example.org", Outcome: mailReplyReceived} + if got := r.handle(context.Background(), c); got != "disabled" || dials.Load() != 0 { + t.Fatalf("handle: %q, %d dials", got, dials.Load()) + } + r.dispatch(mailReplyJob{HiveID: testReplyHive, Host: testReplyHost, Spool: bytes.NewReader([]byte("{}")), Size: 2, Answer: []byte(`{"items":["x"]}`)}) + if dials.Load() != 0 { + t.Fatal("dispatch dialed while the key file is missing") + } + // A nil replier, as on every hub without the key, never reaches a sender. + var none *mailReplier + none.dispatch(mailReplyJob{}) +} + +func TestMailReplyFailureClass(t *testing.T) { + if r, c := mailReplyFailure(&mailReplySendError{Reason: sendFailRcpt, Code: 550}); r != sendFailRcpt || c != 550 { + t.Fatalf("%q %d", r, c) + } + if r, c := mailReplyFailure(errors.New("boom 10.0.0.1")); r != sendFailInternal || c != 0 { + t.Fatalf("%q %d", r, c) + } + if r, _ := mailReplyFailure(&mailReplySendError{Reason: sendFailTimeout}); r != sendFailTimeout { + t.Fatal(r) + } +} diff --git a/src/pkg/hub/mail_reply_test.go b/src/pkg/hub/mail_reply_test.go index a5dbdbebe9..638ba14639 100644 --- a/src/pkg/hub/mail_reply_test.go +++ b/src/pkg/hub/mail_reply_test.go @@ -293,11 +293,16 @@ func TestMailReplyEnvConfig(t *testing.T) { } t.Setenv(envMailReplyFrom, testReplyFrom) t.Setenv(envMailReplySelector, "s1") + t.Setenv(envMailReplyHelo, "") + old := mailReplySenderFactory + t.Cleanup(func() { mailReplySenderFactory = old }) + if newMailReplierFromEnv(logger, nil) != nil { + t.Fatal("a replier without a HELO name") + } + mailReplySenderFactory = nil if newMailReplierFromEnv(logger, nil) != nil { t.Fatal("a replier with no sender built in") } - old := mailReplySenderFactory - t.Cleanup(func() { mailReplySenderFactory = old }) mailReplySenderFactory = func(mailReplyConfig) (mailReplySender, error) { return &fakeReplySender{}, nil } r := newMailReplierFromEnv(logger, nil) if r == nil || !r.enabled() || r.cfg.Limits.SenderPerHour != defaultReplySenderPerHour || r.cfg.Limits.HivePerDay != defaultReplyHivePerDay {