feat(notification): add durable mail delivery state

This commit is contained in:
shira committed 2026-10-06 10:52:47 +03:00
1 parent 391c8a6cec
commit 2db585fbff
41 files changed
+4903 -108

No files matched your search

+4 -1
View File
@@ -81,7 +81,10 @@ type Config struct {
// other transports are not loaded.
EmailProvider EmailProvider
// EmailFrom is the sender mailbox for every outgoing message.
EmailFrom string
EmailFrom string
// EmailAccount labels the provider account that new deliveries are
// pinned to. It is configuration identity, never a credential.
EmailAccount string
EmailTimeout time.Duration
// AppPublicURL is the public origin without a trailing slash, for links
// in mail.
+13
View File
@@ -6,6 +6,7 @@ import (
"net"
"net/mail"
"net/url"
"regexp"
"strconv"
"strings"
"time"
@@ -36,6 +37,9 @@ const (
)
const (
// defaultEmailAccount labels the only provider account of a stand that
// never switched accounts.
defaultEmailAccount = "default"
defaultEmailTimeout = 10 * time.Second
// One attempt waits at least long enough for a TLS handshake and an API
// answer, and at most one minute so that a hung provider cannot hold a
@@ -44,6 +48,10 @@ const (
maxEmailTimeout = time.Minute
)
// emailAccountPattern matches domain.NewMailProviderAccount: a lowercase
// label that cannot hold the mixed-case characters of an API key.
var emailAccountPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9._-]{0,63}$`)
// loadMail reads the common mail settings and only the settings of the
// selected transport, so settings of an inactive transport, including its
// secrets, never reach Config.
@@ -55,6 +63,7 @@ func (config *Config) loadMail(lookup LookupEnv) error {
provider := strings.ToLower(strings.TrimSpace(valueOrDefault(lookup, "EMAIL_PROVIDER", "")))
config.EmailProvider = EmailProvider(provider)
config.EmailFrom = strings.TrimSpace(valueOrDefault(lookup, "EMAIL_FROM", ""))
config.EmailAccount = strings.TrimSpace(valueOrDefault(lookup, "EMAIL_ACCOUNT", defaultEmailAccount))
// One trailing slash is accepted and dropped so that the stored value is
// an origin that links can be appended to.
config.AppPublicURL = strings.TrimSuffix(strings.TrimSpace(valueOrDefault(lookup, "APP_PUBLIC_URL", "")), "/")
@@ -109,6 +118,10 @@ func (config Config) ValidateMail() error {
if err := validateEmailFrom(config.EmailFrom); err != nil {
return err
}
// The value is not echoed: an API key pasted here must not reach logs.
if !emailAccountPattern.MatchString(config.EmailAccount) {
return errors.New("Config.ValidateMail - EMAIL_ACCOUNT must be a label of 1 to 64 lowercase letters, digits, '.', '_', or '-' that starts with a letter or digit")
}
if err := validateAppPublicURL(config.AppPublicURL); err != nil {
return err
}
+11 -1
View File
@@ -67,6 +67,9 @@ func TestLoadFromEnvMailDisabledIgnoresTransportSettings(t *testing.T) {
if settings.EmailTimeout != 10*time.Second {
t.Fatalf("EmailTimeout = %s, want the 10s default", settings.EmailTimeout)
}
if settings.EmailAccount != "default" {
t.Fatalf("EmailAccount = %q, want the default label", settings.EmailAccount)
}
if settings.ResendAPIKey != "" || settings.SMTPHost != "" || settings.SMTPPort != 0 || settings.SMTPPassword != "" || settings.SMTPTLSMode != "" {
t.Fatal("settings of an inactive transport were loaded")
}
@@ -118,6 +121,8 @@ func TestLoadFromEnvAcceptsMailBoundaries(t *testing.T) {
{name: "punycode sender domain", values: map[string]string{"EMAIL_FROM": "no-reply@xn--e1afmkfd.xn--p1ai"}},
{name: "minimum timeout", values: map[string]string{"EMAIL_TIMEOUT": "1s"}},
{name: "maximum timeout", values: map[string]string{"EMAIL_TIMEOUT": "60s"}},
{name: "account label with separators", values: map[string]string{"EMAIL_ACCOUNT": " resend-main.2_eu "}},
{name: "account label of 64 characters", values: map[string]string{"EMAIL_ACCOUNT": strings.Repeat("a", 64)}},
{name: "HTTPS origin with port", values: map[string]string{"APP_PUBLIC_URL": "https://lincodex.example:8443"}},
{name: "HTTP loopback IPv4", values: map[string]string{"APP_PUBLIC_URL": "http://127.0.0.1:8080"}},
{name: "HTTP loopback IPv6", values: map[string]string{"APP_PUBLIC_URL": "http://[::1]:8080/"}},
@@ -156,6 +161,11 @@ func TestLoadFromEnvRejectsMailSettings(t *testing.T) {
{name: "sender single-label domain", values: map[string]string{"EMAIL_FROM": "no-reply@localhost"}, want: "config.validateEmailFrom - EMAIL_FROM address must be"},
{name: "sender IP literal", values: map[string]string{"EMAIL_FROM": "no-reply@[127.0.0.1]"}, want: "config.validateEmailFrom - EMAIL_FROM address must be"},
{name: "sender quoted local part", values: map[string]string{"EMAIL_FROM": `"no reply"@example.com`}, want: "config.validateEmailFrom - EMAIL_FROM address must be"},
{name: "account empty", values: map[string]string{"EMAIL_ACCOUNT": " "}, want: "Config.ValidateMail - EMAIL_ACCOUNT must be a label"},
{name: "account with uppercase", values: map[string]string{"EMAIL_ACCOUNT": "Main"}, want: "Config.ValidateMail - EMAIL_ACCOUNT must be a label"},
{name: "API key pasted as account", values: map[string]string{"EMAIL_ACCOUNT": "re_" + strings.ToUpper(secretMarker)}, want: "Config.ValidateMail - EMAIL_ACCOUNT must be a label"},
{name: "account starting with a dash", values: map[string]string{"EMAIL_ACCOUNT": "-main"}, want: "Config.ValidateMail - EMAIL_ACCOUNT must be a label"},
{name: "account too long", values: map[string]string{"EMAIL_ACCOUNT": strings.Repeat("a", 65)}, want: "Config.ValidateMail - EMAIL_ACCOUNT must be a label"},
{name: "timeout unparsable", values: map[string]string{"EMAIL_TIMEOUT": "soon"}, want: "config.durationOrDefault - parse EMAIL_TIMEOUT"},
{name: "timeout empty", values: map[string]string{"EMAIL_TIMEOUT": ""}, want: "config.durationOrDefault - parse EMAIL_TIMEOUT"},
{name: "timeout zero", values: map[string]string{"EMAIL_TIMEOUT": "0s"}, want: "Config.ValidateMail - EMAIL_TIMEOUT must be between 1s and 1m0s"},
@@ -196,7 +206,7 @@ func TestLoadFromEnvRejectsMailSettings(t *testing.T) {
if err == nil || !strings.HasPrefix(err.Error(), test.want) {
t.Fatalf("LoadFromEnv() error = %v, want an error starting with %q", err, test.want)
}
for _, secret := range []string{secretMarker, testResendAPIKey, testSMTPPassword} {
for _, secret := range []string{secretMarker, strings.ToUpper(secretMarker), testResendAPIKey, testSMTPPassword} {
if strings.Contains(err.Error(), secret) {
t.Fatalf("configuration error discloses a configured value: %v", err)
}