From bcef5d160fdd334f8d2732530e9b7630335e266e Mon Sep 17 00:00:00 2001 From: Matthew Meszaros Date: Wed, 28 Oct 2026 09:49:27 +0100 Subject: [PATCH] feat: enforce hosted SMTP authentication policy across mailbox negotiation and platform relays while preserving self-host compatibility --- .../docs/development/configuration.mdx | 2 +- docs/content/docs/guides/mailboxes.mdx | 4 +- internal/client/smtpimap/smtp/auth.go | 5 ++- internal/client/smtpimap/smtp/auth_test.go | 21 ++++++++++ internal/client/smtpimap/smtp/server_test.go | 1 + internal/config/providers.go | 3 ++ internal/email/smtp.go | 4 +- internal/errx/email.go | 2 +- internal/notify/smtp.go | 5 ++- internal/notify/smtp_test.go | 42 +++++++++++++++++++ internal/pkg/mailcause/catalogue.go | 2 +- internal/pkg/mailcause/classify.go | 2 +- internal/pkg/mailcause/mailcause_test.go | 2 + 13 files changed, 85 insertions(+), 10 deletions(-) create mode 100644 internal/notify/smtp_test.go diff --git a/docs/content/docs/development/configuration.mdx b/docs/content/docs/development/configuration.mdx index d59dfd232..c97cad871 100644 --- a/docs/content/docs/development/configuration.mdx +++ b/docs/content/docs/development/configuration.mdx @@ -268,7 +268,7 @@ Platform mail is the product's own outbound: registration codes, password resets | `SMTP_PORT` | Relay port. Follows `SMTP_SECURITY` when unset | derived | yes | | `SMTP_USERNAME`, `SMTP_PASSWORD` | Relay credentials. Never sent over an unencrypted connection | unset | yes | | `SMTP_SECURITY` | `starttls` (587), `tls` (465) or `none` (25) | `starttls` | yes | -| `SMTP_AUTH` | `auto`, `plain`, `login`, `cram-md5` or `none` | `auto` | yes | +| `SMTP_AUTH` | `auto`, `plain`, `login`, `cram-md5` or `none`. Hosted production permits LOGIN and PLAIN over TLS; `cram-md5` is available only on self-hosted or development instances | `auto` | yes | | `SMTP_EHLO_NAME` | EHLO name presented to the relay | the sender domain | yes | | `SMTP_TLS_INSECURE_SKIP_VERIFY` | Skips certificate verification. Only for a relay with a private certificate authority. Honoured on a self-hosted or development instance and ignored on hosted production | `false` | yes | | `EMAIL_BRAND_NAME` | The product name in transactional subjects and the header | `Warmbly` | yes | diff --git a/docs/content/docs/guides/mailboxes.mdx b/docs/content/docs/guides/mailboxes.mdx index db0a189b5..6cb44a7b6 100644 --- a/docs/content/docs/guides/mailboxes.mdx +++ b/docs/content/docs/guides/mailboxes.mdx @@ -151,7 +151,9 @@ The same holds for a server that answers but refuses what Warmbly asked. The dra With two-factor authentication on, generate an app password in your provider's security settings and use that. -Warmbly signs in with whichever method your server offers, preferring LOGIN, then PLAIN, and using CRAM-MD5 only when the server offers neither. Every one of them is sent over the established TLS connection, never before it: a server that offers to sign in without encryption is refused with an error that says so and asks for the server's SSL/TLS port (usually 465) or a port that offers STARTTLS (usually 587), except a relay on the worker's own machine with the **None** mode. A server that offers none of the three methods is refused with the methods it does offer, so you can tell whether another host or port is needed. Servers that accept only one of these, which includes Microsoft 365 relays and most appliance relays, work without any setting to change. +Warmbly signs in using LOGIN or PLAIN over an established TLS connection, preferring LOGIN when both are offered. Hosted production requires one of these password methods or a supported OAuth connection. Self-hosted and development instances also support CRAM-MD5 when the server offers neither LOGIN nor PLAIN. A server with no supported method is refused with the methods it offers, so you can check whether another submission host or port is needed. + +Password authentication requires encryption: use the server's SSL/TLS port (usually 465) or a port that offers STARTTLS (usually 587). The **None** mode is available only for a relay on the worker's own machine on a self-hosted instance. ### Servers with their own certificate diff --git a/internal/client/smtpimap/smtp/auth.go b/internal/client/smtpimap/smtp/auth.go index d16a56eaa..a9f8f9c49 100644 --- a/internal/client/smtpimap/smtp/auth.go +++ b/internal/client/smtpimap/smtp/auth.go @@ -6,6 +6,7 @@ import ( "net/smtp" "strings" + "github.com/warmbly/warmbly/internal/config" "github.com/warmbly/warmbly/internal/models" ) @@ -29,7 +30,7 @@ var ErrSMTPNoSupportedAuth = errors.New("smtp: the server advertises no supporte // Over that link LOGIN and PLAIN come first: TLS already protects the // password, and a server that speaks only one of the two speaks LOGIN // (Exchange Online, most appliance relays), which is why it is negotiated -// rather than sent blind. CRAM-MD5 is used only when neither is offered. +// rather than sent blind. Self-hosts may use CRAM-MD5 when neither is offered. // // A server that advertises no AUTH at all gets a nil Auth and no error: it // wants no authentication, which is the local development sink. @@ -57,7 +58,7 @@ func chooseAuth(advertised string, encrypted bool, username, password, host stri return NewLoginAuth(username, password, host), nil case offered["PLAIN"]: return smtp.PlainAuth("", username, password, host), nil - case offered["CRAM-MD5"]: + case offered["CRAM-MD5"] && config.SMTPCRAMMD5Allowed(): return smtp.CRAMMD5Auth(username, password), nil } return nil, fmt.Errorf("%w: %q", ErrSMTPNoSupportedAuth, advertised) diff --git a/internal/client/smtpimap/smtp/auth_test.go b/internal/client/smtpimap/smtp/auth_test.go index 49f93eac7..6426bbfc8 100644 --- a/internal/client/smtpimap/smtp/auth_test.go +++ b/internal/client/smtpimap/smtp/auth_test.go @@ -9,6 +9,7 @@ import ( ) func TestChooseAuthPicksByLinkAndAdvertisement(t *testing.T) { + t.Setenv("DEPLOYMENT_MODE", "self_hosted") for _, tc := range []struct { name string advertised string @@ -54,6 +55,26 @@ func TestChooseAuthPicksByLinkAndAdvertisement(t *testing.T) { } } +func TestHostedSMTPAuthUsesLoginOrPlain(t *testing.T) { + t.Setenv("DEPLOYMENT_MODE", "cloud") + t.Setenv("APP_ENV", "prod") + for _, advertised := range []string{"CRAM-MD5", "cram-md5"} { + if auth, err := chooseAuth(advertised, true, "user", "password", "mail.example.com"); auth != nil || !errors.Is(err, ErrSMTPNoSupportedAuth) { + t.Fatalf("%q: auth=%v err=%v", advertised, auth, err) + } + } + for _, advertised := range []string{"CRAM-MD5 LOGIN", "CRAM-MD5 PLAIN"} { + auth, err := chooseAuth(advertised, true, "user", "password", "mail.example.com") + if err != nil { + t.Fatal(err) + } + mechanism, _, err := auth.Start(&smtp.ServerInfo{Name: "mail.example.com", TLS: true}) + if err != nil || (mechanism != "LOGIN" && mechanism != "PLAIN") { + t.Fatalf("%q: mechanism=%q err=%v", advertised, mechanism, err) + } + } +} + func TestLoginAuthRefusesCleartextToARemoteHost(t *testing.T) { auth := NewLoginAuth("user", "pw", "mail.example.com") if _, _, err := auth.Start(&smtp.ServerInfo{Name: "mail.example.com"}); !errors.Is(err, ErrSMTPCleartextAuth) { diff --git a/internal/client/smtpimap/smtp/server_test.go b/internal/client/smtpimap/smtp/server_test.go index 6c435d787..cdda223a1 100644 --- a/internal/client/smtpimap/smtp/server_test.go +++ b/internal/client/smtpimap/smtp/server_test.go @@ -190,6 +190,7 @@ func (s *fakeServer) decode(b64 string) { // the client sent AUTH PLAIN blind, the server refused, and the refusal was // reported to the mailbox's owner as a wrong password. func TestNegotiateAuthPrefersWhatTheServerAdvertises(t *testing.T) { + t.Setenv("DEPLOYMENT_MODE", "self_hosted") for _, tc := range []struct { advertised string wantCmd string diff --git a/internal/config/providers.go b/internal/config/providers.go index ce4376d95..29258ce54 100644 --- a/internal/config/providers.go +++ b/internal/config/providers.go @@ -148,6 +148,9 @@ func ExplicitDevelopment() bool { // HostedProduction reports a hosted (not self-host) instance outside development. func HostedProduction() bool { return !SelfHosted() && !DevEnv() } +// SMTPCRAMMD5Allowed preserves legacy mail-server compatibility outside hosted production. +func SMTPCRAMMD5Allowed() bool { return !HostedProduction() } + // UnsafeWebhookURLsAllowed reports whether outbound HTTP to customer URLs may use // http:// and private addresses. Only a self-hosted or development instance may opt in. func UnsafeWebhookURLsAllowed() bool { diff --git a/internal/email/smtp.go b/internal/email/smtp.go index 4ae15cd6f..15d61b1c8 100644 --- a/internal/email/smtp.go +++ b/internal/email/smtp.go @@ -188,9 +188,9 @@ func ehloName(user string) string { // noSupportedAuthDetail names the sign-in methods the server offered when none is one Warmbly speaks. func noSupportedAuthDetail(c *smtp.Client) string { - detail := "the server offers no sign-in method Warmbly supports (Warmbly signs in with LOGIN, PLAIN or CRAM-MD5)" + detail := "the server offers no sign-in method allowed by this instance (use LOGIN or PLAIN over TLS)" if _, offered := c.Extension("AUTH"); offered != "" { - detail = "the server offers no sign-in method Warmbly supports: it offers " + probeDetail(errors.New(offered)) + ", and Warmbly signs in with LOGIN, PLAIN or CRAM-MD5" + detail = "the server offers no sign-in method allowed by this instance: it offers " + probeDetail(errors.New(offered)) + "; use LOGIN or PLAIN over TLS" } return detail + ". Use the provider's documented SMTP submission host, which usually offers one of them" } diff --git a/internal/errx/email.go b/internal/errx/email.go index 6d882fccf..b538af6ea 100644 --- a/internal/errx/email.go +++ b/internal/errx/email.go @@ -350,7 +350,7 @@ var ( ErrMailAuthUnsupported = MError( MailErrorCritical, MailErrorCodeAuthUnsupported, - "This mail server offers no sign-in method Warmbly supports (Warmbly signs in with LOGIN, PLAIN or CRAM-MD5). Use the provider's documented SMTP submission host, which usually offers one of them, or check the server's documentation for an app password.", + "This mail server offers no sign-in method allowed by this instance. Use the provider's documented SMTP submission host with LOGIN or PLAIN over TLS, or check the server's documentation for an app password.", MailErrorResolveMethodReload, ) ErrMailDomainAuthRejected = MError( diff --git a/internal/notify/smtp.go b/internal/notify/smtp.go index c2099e1f7..433e2a9e6 100644 --- a/internal/notify/smtp.go +++ b/internal/notify/smtp.go @@ -196,11 +196,14 @@ func (s *smtpEmailNotificationService) authMechanism(client *smtp.Client) (smtp. case config.SMTPAuthLogin: return wsmtp.NewLoginAuth(s.cfg.Username, s.cfg.Password, s.cfg.Host), nil case config.SMTPAuthCRAMMD5: + if !config.SMTPCRAMMD5Allowed() { + return nil, fmt.Errorf("%w: CRAM-MD5 is unavailable on hosted production", wsmtp.ErrSMTPNoSupportedAuth) + } return smtp.CRAMMD5Auth(s.cfg.Username, s.cfg.Password), nil } // auto: the mailbox send path's rule, which picks from what the server - // advertises over the link it is on (LOGIN, then PLAIN, CRAM-MD5 last). + // advertises over the link it is on, subject to the deployment's auth policy. if ok, _ := client.Extension("AUTH"); !ok { return nil, fmt.Errorf("smtp: %s advertises no AUTH extension but SMTP_USERNAME is set", s.cfg.Host) } diff --git a/internal/notify/smtp_test.go b/internal/notify/smtp_test.go new file mode 100644 index 000000000..52dcaac4c --- /dev/null +++ b/internal/notify/smtp_test.go @@ -0,0 +1,42 @@ +package notify + +import ( + "errors" + "net/smtp" + "testing" + + wsmtp "github.com/warmbly/warmbly/internal/client/smtpimap/smtp" + "github.com/warmbly/warmbly/internal/config" +) + +func TestExplicitCRAMMD5FollowsDeploymentPolicy(t *testing.T) { + s := &smtpEmailNotificationService{cfg: &config.SMTPConfig{Auth: config.SMTPAuthCRAMMD5, Username: "user", Password: "password"}} + for _, tc := range []struct { + mode, env string + allowed bool + }{ + {"cloud", "prod", false}, + {"cloud", "staging", false}, + {"self_hosted", "prod", true}, + {"cloud", "dev", true}, + } { + t.Run(tc.mode+"/"+tc.env, func(t *testing.T) { + t.Setenv("DEPLOYMENT_MODE", tc.mode) + t.Setenv("APP_ENV", tc.env) + auth, err := s.authMechanism(nil) + if !tc.allowed { + if auth != nil || !errors.Is(err, wsmtp.ErrSMTPNoSupportedAuth) { + t.Fatalf("auth=%v err=%v", auth, err) + } + return + } + if err != nil { + t.Fatal(err) + } + mechanism, _, err := auth.Start(&smtp.ServerInfo{Name: "mail.example.com", TLS: true}) + if err != nil || mechanism != "CRAM-MD5" { + t.Fatalf("mechanism=%q err=%v", mechanism, err) + } + }) + } +} diff --git a/internal/pkg/mailcause/catalogue.go b/internal/pkg/mailcause/catalogue.go index 5e56a4347..072862a95 100644 --- a/internal/pkg/mailcause/catalogue.go +++ b/internal/pkg/mailcause/catalogue.go @@ -83,7 +83,7 @@ var catalogue = []Cause{ {TooManyLogins, "Too many sign-in attempts", "The provider is limiting sign-ins for this account or from this network. Wait 15 to 60 minutes before retrying, and avoid retrying the same failing password repeatedly.", true}, {AuthUnavailable, "The server does not take a password here", - "The server did not offer a password sign-in method Warmbly supports (LOGIN, PLAIN or CRAM-MD5) on this port. Check the host and port: use the provider's submission server (usually port 587 or 465), not the MX server or port 25.", false}, + "The server did not offer a password sign-in method allowed by this instance on this port. Use the provider's submission server with LOGIN or PLAIN over TLS (usually port 587 or 465), not the MX server or port 25.", false}, {AuthRefused, "The server refused the sign-in", "The server refused this username and password. Check both, and whether the provider needs an app password or a username other than the email address.", true}, {HostNotFound, "The server name does not exist", diff --git a/internal/pkg/mailcause/classify.go b/internal/pkg/mailcause/classify.go index c4f825f25..844253572 100644 --- a/internal/pkg/mailcause/classify.go +++ b/internal/pkg/mailcause/classify.go @@ -122,7 +122,7 @@ func classify(in Input) string { return PrivateHost case hasAny(text, "no such host", "nxdomain", "name or service not known"): return HostNotFound - case hasAny(text, "no supported authentication", "no sign-in method warmbly supports", "auth not supported", "authentication not supported", + case hasAny(text, "no supported authentication", "no sign-in method warmbly supports", "no sign-in method allowed by this instance", "auth not supported", "authentication not supported", "authentication not enabled", "unrecognized authentication type", "does not support auth", "authentication mechanism not supported", "auth not available", "authentication not available"): return AuthUnavailable diff --git a/internal/pkg/mailcause/mailcause_test.go b/internal/pkg/mailcause/mailcause_test.go index 778aa4c56..970678359 100644 --- a/internal/pkg/mailcause/mailcause_test.go +++ b/internal/pkg/mailcause/mailcause_test.go @@ -108,6 +108,8 @@ func TestClassify(t *testing.T) { SMTP: Leg{Reason: models.MailProbeProtocol, Host: "mx.example.org", Port: 25, Detail: "smtp: server doesn't support AUTH; 503 5.5.1 Error: authentication not enabled"}}, AuthUnavailable}, {"no supported sign-in method", Input{IMAP: ok("IMAP"), SMTP: Leg{Reason: models.MailProbeProtocol, Host: "mail.example.org", Port: 587, Detail: "the server offers no sign-in method Warmbly supports: it offers XOAUTH2, and Warmbly signs in with LOGIN, PLAIN or CRAM-MD5"}}, AuthUnavailable}, + {"no allowed sign-in method", Input{IMAP: ok("IMAP"), + SMTP: Leg{Reason: models.MailProbeProtocol, Host: "mail.example.org", Port: 587, Detail: "the server offers no sign-in method allowed by this instance: it offers CRAM-MD5; use LOGIN or PLAIN over TLS"}}, AuthUnavailable}, {"timeout both", Input{IMAP: Leg{Reason: models.MailProbeTimeout, Port: 993}, SMTP: Leg{Reason: models.MailProbeTimeout, Port: 587}}, Timeout}, {"temporary", Input{IMAP: ok("IMAP"), SMTP: Leg{Reason: models.MailProbeTemporary, Detail: "451 4.3.0 Temporary server error"}}, Temporary}, {"auth outranks timeout", Input{MailHost: "other", IMAP: Leg{Reason: models.MailProbeTimeout},