mirror of
https://github.com/warmbly/warmbly.git
synced 2026-11-17 08:18:51 +00:00
Merge pull request #1077 from warmbly/feat/hosted-smtp-auth-policy
feat: enforce hosted SMTP authentication policy across mailbox connections and platform relays
This commit is contained in:
@@ -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 |
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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",
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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},
|
||||
|
||||
Reference in New Issue
Block a user