feat(mail): add MailSender port and explicit provider selection
This commit is contained in:
1 parent
e7d0d92ef9
commit
6153887eb5
26 files changed
+1253
-29
No files matched your search
@@ -41,9 +41,24 @@ type Config struct {
|
||||
SeaweedFSRegion string
|
||||
SeaweedFSTimeout time.Duration
|
||||
KafkaBrokers []string
|
||||
LogLevel string
|
||||
ReadinessTimeout time.Duration
|
||||
ShutdownTimeout time.Duration
|
||||
// EmailProvider selects the only active mail transport. Settings of the
|
||||
// other transports are not loaded.
|
||||
EmailProvider EmailProvider
|
||||
// EmailFrom is the sender mailbox for every outgoing message.
|
||||
EmailFrom string
|
||||
EmailTimeout time.Duration
|
||||
// AppPublicURL is the public origin without a trailing slash, for links
|
||||
// in mail.
|
||||
AppPublicURL string
|
||||
ResendAPIKey string
|
||||
SMTPHost string
|
||||
SMTPPort int
|
||||
SMTPUsername string
|
||||
SMTPPassword string
|
||||
SMTPTLSMode SMTPTLSMode
|
||||
LogLevel string
|
||||
ReadinessTimeout time.Duration
|
||||
ShutdownTimeout time.Duration
|
||||
}
|
||||
|
||||
type LookupEnv func(string) (string, bool)
|
||||
@@ -75,6 +90,9 @@ func LoadFromEnv(lookup LookupEnv) (Config, error) {
|
||||
if err := config.loadRedis(lookup); err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
if err := config.loadMail(lookup); err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
var err error
|
||||
config.DatabaseMigrateOnStart, err = boolOrDefault(lookup, "DATABASE_MIGRATE_ON_START", defaultDatabaseMigrateOnStart)
|
||||
@@ -113,6 +131,9 @@ func (config Config) Validate() error {
|
||||
if err := config.ValidateKafka(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := config.ValidateMail(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validateAddress("HTTP_ADDR", config.HTTPAddr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -118,10 +118,21 @@ func testEnvironment(values map[string]string) LookupEnv {
|
||||
if key == "SEAWEEDFS_SECRET_KEY" {
|
||||
return "test-secret", true
|
||||
}
|
||||
if value, ok := mailFixture[key]; ok {
|
||||
return value, true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
|
||||
// mailFixture is the smallest valid mail configuration. Mail tests override
|
||||
// or remove entries to cover each transport.
|
||||
var mailFixture = map[string]string{
|
||||
"EMAIL_PROVIDER": "disabled",
|
||||
"EMAIL_FROM": "Lincodex <no-reply@example.com>",
|
||||
"APP_PUBLIC_URL": "http://localhost:8080",
|
||||
}
|
||||
|
||||
func TestLoadFromEnvSeaweedFS(t *testing.T) {
|
||||
settings, err := LoadFromEnv(testEnvironment(nil))
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,240 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/mail"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"lincodex/backend/internal/domain"
|
||||
)
|
||||
|
||||
// EmailProvider selects exactly one mail transport. There is no implicit
|
||||
// fallback between transports.
|
||||
type EmailProvider string
|
||||
|
||||
const (
|
||||
EmailProviderResend EmailProvider = "resend"
|
||||
EmailProviderSMTP EmailProvider = "smtp"
|
||||
EmailProviderDisabled EmailProvider = "disabled"
|
||||
)
|
||||
|
||||
// SMTPTLSMode selects how the SMTP transport protects the connection.
|
||||
// Plaintext SMTP is not supported.
|
||||
type SMTPTLSMode string
|
||||
|
||||
const (
|
||||
// SMTPTLSModeStartTLS upgrades the connection with STARTTLS and fails
|
||||
// when the server does not offer it (submission, usually port 587).
|
||||
SMTPTLSModeStartTLS SMTPTLSMode = "starttls"
|
||||
// SMTPTLSModeImplicit opens TLS before the SMTP dialog (usually port 465).
|
||||
SMTPTLSModeImplicit SMTPTLSMode = "tls"
|
||||
)
|
||||
|
||||
const (
|
||||
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
|
||||
// delivery worker indefinitely.
|
||||
minEmailTimeout = time.Second
|
||||
maxEmailTimeout = time.Minute
|
||||
)
|
||||
|
||||
// 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.
|
||||
//
|
||||
// EMAIL_PROVIDER has no default. The working stand uses resend, but a default
|
||||
// would either pick a transport that this build may not provide or silently
|
||||
// turn mail off; an explicit choice makes both visible at deployment time.
|
||||
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", ""))
|
||||
// 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", "")), "/")
|
||||
|
||||
var err error
|
||||
config.EmailTimeout, err = durationOrDefault(lookup, "EMAIL_TIMEOUT", defaultEmailTimeout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch config.EmailProvider {
|
||||
case EmailProviderResend:
|
||||
config.ResendAPIKey = valueOrDefault(lookup, "RESEND_API_KEY", "")
|
||||
case EmailProviderSMTP:
|
||||
return config.loadSMTP(lookup)
|
||||
case EmailProviderDisabled:
|
||||
// No transport settings are read.
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (config *Config) loadSMTP(lookup LookupEnv) error {
|
||||
config.SMTPHost = strings.TrimSpace(valueOrDefault(lookup, "SMTP_HOST", ""))
|
||||
config.SMTPUsername = strings.TrimSpace(valueOrDefault(lookup, "SMTP_USERNAME", ""))
|
||||
config.SMTPPassword = valueOrDefault(lookup, "SMTP_PASSWORD", "")
|
||||
config.SMTPTLSMode = SMTPTLSMode(strings.ToLower(strings.TrimSpace(valueOrDefault(lookup, "SMTP_TLS_MODE", ""))))
|
||||
|
||||
// An unset or empty port stays zero, which validation reports as missing.
|
||||
port := strings.TrimSpace(valueOrDefault(lookup, "SMTP_PORT", ""))
|
||||
if port == "" {
|
||||
return nil
|
||||
}
|
||||
number, err := strconv.Atoi(port)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Config.loadSMTP - SMTP_PORT must be an integer between 1 and %d", maxPortNumber)
|
||||
}
|
||||
config.SMTPPort = number
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateMail checks the common mail settings and the selected transport.
|
||||
// Errors name the setting and never include secret values.
|
||||
func (config Config) ValidateMail() error {
|
||||
// The value is not echoed: a misplaced secret must not reach the logs.
|
||||
switch config.EmailProvider {
|
||||
case EmailProviderResend, EmailProviderSMTP, EmailProviderDisabled:
|
||||
case "":
|
||||
return errors.New("Config.ValidateMail - EMAIL_PROVIDER must be set to resend, smtp, or disabled")
|
||||
default:
|
||||
return errors.New("Config.ValidateMail - EMAIL_PROVIDER must be one of resend, smtp, or disabled")
|
||||
}
|
||||
if err := validateEmailFrom(config.EmailFrom); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validateAppPublicURL(config.AppPublicURL); err != nil {
|
||||
return err
|
||||
}
|
||||
if config.EmailTimeout < minEmailTimeout || config.EmailTimeout > maxEmailTimeout {
|
||||
return fmt.Errorf("Config.ValidateMail - EMAIL_TIMEOUT must be between %s and %s", minEmailTimeout, maxEmailTimeout)
|
||||
}
|
||||
|
||||
switch config.EmailProvider {
|
||||
case EmailProviderResend:
|
||||
return validateResendAPIKey(config.ResendAPIKey)
|
||||
case EmailProviderSMTP:
|
||||
return config.validateSMTP()
|
||||
case EmailProviderDisabled:
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateEmailFrom accepts one RFC 5322 mailbox, with or without a display
|
||||
// name. The address itself follows the account address rules of
|
||||
// domain.NewEmailAddress: ASCII only, because the mailer does not support
|
||||
// SMTPUTF8, an unquoted local part, and a DNS domain with at least two labels.
|
||||
func validateEmailFrom(raw string) error {
|
||||
if raw == "" {
|
||||
return errors.New("config.validateEmailFrom - EMAIL_FROM must be set")
|
||||
}
|
||||
// Control characters such as CR and LF could inject header lines.
|
||||
if !isPrintableASCII(raw) {
|
||||
return errors.New("config.validateEmailFrom - EMAIL_FROM must contain printable ASCII characters only")
|
||||
}
|
||||
mailbox, err := mail.ParseAddress(raw)
|
||||
if err != nil {
|
||||
return errors.New("config.validateEmailFrom - EMAIL_FROM must be one mailbox such as Lincodex <no-reply@example.com>")
|
||||
}
|
||||
if _, err := domain.NewEmailAddress(mailbox.Address); err != nil {
|
||||
return errors.New("config.validateEmailFrom - EMAIL_FROM address must be an ASCII addr-spec with a DNS domain of at least two labels")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateAppPublicURL accepts the public origin that links in mail point
|
||||
// to. Plain HTTP is accepted only for a loopback host in local runs.
|
||||
func validateAppPublicURL(raw string) error {
|
||||
if raw == "" {
|
||||
return errors.New("config.validateAppPublicURL - APP_PUBLIC_URL must be set")
|
||||
}
|
||||
origin, err := url.Parse(raw)
|
||||
if err != nil || origin.Hostname() == "" || !validHostOrIP(origin.Hostname()) {
|
||||
return errors.New("config.validateAppPublicURL - APP_PUBLIC_URL must be an absolute URL with a valid host")
|
||||
}
|
||||
switch {
|
||||
case origin.Scheme == "https":
|
||||
case origin.Scheme == "http" && isLoopbackHost(origin.Hostname()):
|
||||
default:
|
||||
return errors.New("config.validateAppPublicURL - APP_PUBLIC_URL must use https; http is allowed only for localhost, 127.0.0.1, or [::1]")
|
||||
}
|
||||
|
||||
if !isBareOrigin(origin, raw) {
|
||||
return errors.New("config.validateAppPublicURL - APP_PUBLIC_URL must be an origin without credentials, a path, query, or fragment")
|
||||
}
|
||||
if !validEndpointPort(origin) {
|
||||
return errors.New("config.validateAppPublicURL - APP_PUBLIC_URL must have a valid nonzero port")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// isBareOrigin reports a URL with nothing after the authority. raw is checked
|
||||
// for "#" because url.Parse drops an empty fragment.
|
||||
func isBareOrigin(origin *url.URL, raw string) bool {
|
||||
hasCredentials := origin.User != nil
|
||||
hasPath := origin.Path != "" || origin.RawPath != ""
|
||||
hasQuery := origin.RawQuery != "" || origin.ForceQuery
|
||||
hasFragment := origin.Fragment != "" || strings.Contains(raw, "#")
|
||||
return !hasCredentials && !hasPath && !hasQuery && !hasFragment
|
||||
}
|
||||
|
||||
// validateResendAPIKey checks only what the Authorization header needs. Resend
|
||||
// shows keys as "re_..." in examples but does not document a stable format,
|
||||
// so the prefix is not enforced; the provider itself rejects a wrong key.
|
||||
func validateResendAPIKey(key string) error {
|
||||
if key == "" {
|
||||
return errors.New("config.validateResendAPIKey - RESEND_API_KEY must be set when EMAIL_PROVIDER is resend")
|
||||
}
|
||||
if !isPrintableASCII(key) || strings.Contains(key, " ") {
|
||||
return errors.New("config.validateResendAPIKey - RESEND_API_KEY must not contain whitespace or non-ASCII characters")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (config Config) validateSMTP() error {
|
||||
if config.SMTPHost == "" {
|
||||
return errors.New("Config.validateSMTP - SMTP_HOST must be set when EMAIL_PROVIDER is smtp")
|
||||
}
|
||||
if !validHostOrIP(config.SMTPHost) {
|
||||
return errors.New("Config.validateSMTP - SMTP_HOST must be a hostname or IP address without a port")
|
||||
}
|
||||
if config.SMTPPort < 1 || config.SMTPPort > maxPortNumber {
|
||||
return fmt.Errorf("Config.validateSMTP - SMTP_PORT must be set to a port between 1 and %d when EMAIL_PROVIDER is smtp", maxPortNumber)
|
||||
}
|
||||
// An anonymous relay is not supported: both credentials are required.
|
||||
if config.SMTPUsername == "" {
|
||||
return errors.New("Config.validateSMTP - SMTP_USERNAME must be set when EMAIL_PROVIDER is smtp")
|
||||
}
|
||||
if config.SMTPPassword == "" {
|
||||
return errors.New("Config.validateSMTP - SMTP_PASSWORD must be set when EMAIL_PROVIDER is smtp")
|
||||
}
|
||||
switch config.SMTPTLSMode {
|
||||
case SMTPTLSModeStartTLS, SMTPTLSModeImplicit:
|
||||
return nil
|
||||
default:
|
||||
return errors.New("Config.validateSMTP - SMTP_TLS_MODE must be starttls or tls; plaintext SMTP is not supported")
|
||||
}
|
||||
}
|
||||
|
||||
func validHostOrIP(host string) bool {
|
||||
return net.ParseIP(host) != nil || validHostname(host)
|
||||
}
|
||||
|
||||
func isLoopbackHost(host string) bool {
|
||||
return strings.EqualFold(host, "localhost") || host == "127.0.0.1" || host == "::1"
|
||||
}
|
||||
|
||||
func isPrintableASCII(value string) bool {
|
||||
for index := range len(value) {
|
||||
if value[index] < ' ' || value[index] > '~' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
testResendAPIKey = "re_test-key-value"
|
||||
testSMTPPassword = "smtp password value"
|
||||
secretMarker = "hidden-secret-marker"
|
||||
testSMTPHost = "smtp.example.com"
|
||||
testPublicOrigin = "https://lincodex.example"
|
||||
testSenderMailbox = "Lincodex <no-reply@example.com>"
|
||||
)
|
||||
|
||||
// mailEnvironment overrides mail settings on top of the shared test
|
||||
// environment and removes the keys named in unset.
|
||||
func mailEnvironment(values map[string]string, unset ...string) LookupEnv {
|
||||
base := testEnvironment(values)
|
||||
return func(key string) (string, bool) {
|
||||
for _, removed := range unset {
|
||||
if key == removed {
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
return base(key)
|
||||
}
|
||||
}
|
||||
|
||||
func smtpValues(overrides map[string]string) map[string]string {
|
||||
values := map[string]string{
|
||||
"EMAIL_PROVIDER": "smtp",
|
||||
"SMTP_HOST": testSMTPHost,
|
||||
"SMTP_PORT": "587",
|
||||
"SMTP_USERNAME": "mailer",
|
||||
"SMTP_PASSWORD": testSMTPPassword,
|
||||
"SMTP_TLS_MODE": "starttls",
|
||||
}
|
||||
for key, value := range overrides {
|
||||
values[key] = value
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
func TestLoadFromEnvMailDisabledIgnoresTransportSettings(t *testing.T) {
|
||||
settings, err := LoadFromEnv(mailEnvironment(map[string]string{
|
||||
"EMAIL_PROVIDER": " Disabled ",
|
||||
"EMAIL_FROM": " " + testSenderMailbox + " ",
|
||||
"APP_PUBLIC_URL": testPublicOrigin + "/",
|
||||
"RESEND_API_KEY": testResendAPIKey,
|
||||
"SMTP_HOST": testSMTPHost,
|
||||
"SMTP_PORT": "not-a-port",
|
||||
"SMTP_PASSWORD": testSMTPPassword,
|
||||
"SMTP_TLS_MODE": "plaintext",
|
||||
}))
|
||||
if err != nil {
|
||||
t.Fatalf("LoadFromEnv() error = %v", err)
|
||||
}
|
||||
if settings.EmailProvider != EmailProviderDisabled {
|
||||
t.Fatalf("EmailProvider = %q, want disabled", settings.EmailProvider)
|
||||
}
|
||||
if settings.EmailFrom != testSenderMailbox || settings.AppPublicURL != testPublicOrigin {
|
||||
t.Fatalf("EmailFrom = %q, AppPublicURL = %q", settings.EmailFrom, settings.AppPublicURL)
|
||||
}
|
||||
if settings.EmailTimeout != 10*time.Second {
|
||||
t.Fatalf("EmailTimeout = %s, want the 10s default", settings.EmailTimeout)
|
||||
}
|
||||
if settings.ResendAPIKey != "" || settings.SMTPHost != "" || settings.SMTPPort != 0 || settings.SMTPPassword != "" || settings.SMTPTLSMode != "" {
|
||||
t.Fatal("settings of an inactive transport were loaded")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFromEnvMailResendLoadsOnlyResendSettings(t *testing.T) {
|
||||
settings, err := LoadFromEnv(mailEnvironment(map[string]string{
|
||||
"EMAIL_PROVIDER": "RESEND",
|
||||
"EMAIL_TIMEOUT": "15s",
|
||||
"RESEND_API_KEY": testResendAPIKey,
|
||||
"SMTP_HOST": testSMTPHost,
|
||||
"SMTP_PASSWORD": testSMTPPassword,
|
||||
}))
|
||||
if err != nil {
|
||||
t.Fatalf("LoadFromEnv() error = %v", err)
|
||||
}
|
||||
if settings.EmailProvider != EmailProviderResend || settings.ResendAPIKey != testResendAPIKey || settings.EmailTimeout != 15*time.Second {
|
||||
t.Fatalf("Resend settings were not loaded: provider=%q timeout=%s", settings.EmailProvider, settings.EmailTimeout)
|
||||
}
|
||||
if settings.SMTPHost != "" || settings.SMTPPassword != "" {
|
||||
t.Fatal("SMTP settings were loaded for EMAIL_PROVIDER=resend")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFromEnvMailSMTPLoadsOnlySMTPSettings(t *testing.T) {
|
||||
settings, err := LoadFromEnv(mailEnvironment(smtpValues(map[string]string{
|
||||
"SMTP_PORT": " 465 ",
|
||||
"SMTP_TLS_MODE": " TLS ",
|
||||
"RESEND_API_KEY": testResendAPIKey,
|
||||
})))
|
||||
if err != nil {
|
||||
t.Fatalf("LoadFromEnv() error = %v", err)
|
||||
}
|
||||
if settings.SMTPHost != testSMTPHost || settings.SMTPPort != 465 || settings.SMTPUsername != "mailer" || settings.SMTPPassword != testSMTPPassword || settings.SMTPTLSMode != SMTPTLSModeImplicit {
|
||||
t.Fatal("SMTP settings were not loaded")
|
||||
}
|
||||
if settings.ResendAPIKey != "" {
|
||||
t.Fatal("RESEND_API_KEY was loaded for EMAIL_PROVIDER=smtp")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFromEnvAcceptsMailBoundaries(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
values map[string]string
|
||||
}{
|
||||
{name: "bare sender address", values: map[string]string{"EMAIL_FROM": "no-reply@example.com"}},
|
||||
{name: "quoted display name", values: map[string]string{"EMAIL_FROM": `"Lincodex Team" <no-reply@mail.example.com>`}},
|
||||
{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: "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/"}},
|
||||
{name: "SMTP over STARTTLS", values: smtpValues(map[string]string{"SMTP_TLS_MODE": "STARTTLS"})},
|
||||
{name: "SMTP host as IP address", values: smtpValues(map[string]string{"SMTP_HOST": "10.0.0.25"})},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if _, err := LoadFromEnv(mailEnvironment(test.values)); err != nil {
|
||||
t.Fatalf("LoadFromEnv() error = %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFromEnvRejectsMailSettings(t *testing.T) {
|
||||
resend := func(key string) map[string]string {
|
||||
return map[string]string{"EMAIL_PROVIDER": "resend", "RESEND_API_KEY": key}
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
values map[string]string
|
||||
unset []string
|
||||
want string
|
||||
}{
|
||||
{name: "provider unset", unset: []string{"EMAIL_PROVIDER"}, want: "Config.ValidateMail - EMAIL_PROVIDER must be set to resend, smtp, or disabled"},
|
||||
{name: "provider empty", values: map[string]string{"EMAIL_PROVIDER": " "}, want: "Config.ValidateMail - EMAIL_PROVIDER must be set to resend, smtp, or disabled"},
|
||||
{name: "unknown provider", values: map[string]string{"EMAIL_PROVIDER": "sendgrid"}, want: "Config.ValidateMail - EMAIL_PROVIDER must be one of resend, smtp, or disabled"},
|
||||
{name: "secret pasted as provider", values: map[string]string{"EMAIL_PROVIDER": secretMarker}, want: "Config.ValidateMail - EMAIL_PROVIDER must be one of"},
|
||||
{name: "sender unset", unset: []string{"EMAIL_FROM"}, want: "config.validateEmailFrom - EMAIL_FROM must be set"},
|
||||
{name: "sender empty", values: map[string]string{"EMAIL_FROM": " "}, want: "config.validateEmailFrom - EMAIL_FROM must be set"},
|
||||
{name: "sender without domain", values: map[string]string{"EMAIL_FROM": "no-reply"}, want: "config.validateEmailFrom - EMAIL_FROM must be one mailbox"},
|
||||
{name: "sender list", values: map[string]string{"EMAIL_FROM": "a@example.com, b@example.com"}, want: "config.validateEmailFrom - EMAIL_FROM must be one mailbox"},
|
||||
{name: "sender header injection", values: map[string]string{"EMAIL_FROM": "a@example.com\r\nBcc: b@example.com"}, want: "config.validateEmailFrom - EMAIL_FROM must contain printable ASCII"},
|
||||
{name: "sender non-ASCII name", values: map[string]string{"EMAIL_FROM": "Линкодекс <no-reply@example.com>"}, want: "config.validateEmailFrom - EMAIL_FROM must contain printable ASCII"},
|
||||
{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: "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"},
|
||||
{name: "timeout too short", values: map[string]string{"EMAIL_TIMEOUT": "500ms"}, want: "Config.ValidateMail - EMAIL_TIMEOUT must be between"},
|
||||
{name: "timeout too long", values: map[string]string{"EMAIL_TIMEOUT": "61s"}, want: "Config.ValidateMail - EMAIL_TIMEOUT must be between"},
|
||||
{name: "public URL unset", unset: []string{"APP_PUBLIC_URL"}, want: "config.validateAppPublicURL - APP_PUBLIC_URL must be set"},
|
||||
{name: "public URL without scheme", values: map[string]string{"APP_PUBLIC_URL": "lincodex.example"}, want: "config.validateAppPublicURL - APP_PUBLIC_URL must be an absolute URL"},
|
||||
{name: "public URL with space", values: map[string]string{"APP_PUBLIC_URL": "https://linco dex.example"}, want: "config.validateAppPublicURL - APP_PUBLIC_URL must be an absolute URL"},
|
||||
{name: "public URL other scheme", values: map[string]string{"APP_PUBLIC_URL": "ftp://lincodex.example"}, want: "config.validateAppPublicURL - APP_PUBLIC_URL must use https"},
|
||||
{name: "public URL plain HTTP", values: map[string]string{"APP_PUBLIC_URL": "http://lincodex.example"}, want: "config.validateAppPublicURL - APP_PUBLIC_URL must use https"},
|
||||
{name: "public URL loopback lookalike", values: map[string]string{"APP_PUBLIC_URL": "http://localhost.lincodex.example"}, want: "config.validateAppPublicURL - APP_PUBLIC_URL must use https"},
|
||||
{name: "public URL credentials", values: map[string]string{"APP_PUBLIC_URL": "https://user:" + secretMarker + "@lincodex.example"}, want: "config.validateAppPublicURL - APP_PUBLIC_URL must be an origin"},
|
||||
{name: "public URL path", values: map[string]string{"APP_PUBLIC_URL": "https://lincodex.example/app"}, want: "config.validateAppPublicURL - APP_PUBLIC_URL must be an origin"},
|
||||
{name: "public URL double slash", values: map[string]string{"APP_PUBLIC_URL": "https://lincodex.example//"}, want: "config.validateAppPublicURL - APP_PUBLIC_URL must be an origin"},
|
||||
{name: "public URL query", values: map[string]string{"APP_PUBLIC_URL": "https://lincodex.example?token=" + secretMarker}, want: "config.validateAppPublicURL - APP_PUBLIC_URL must be an origin"},
|
||||
{name: "public URL fragment", values: map[string]string{"APP_PUBLIC_URL": "https://lincodex.example#top"}, want: "config.validateAppPublicURL - APP_PUBLIC_URL must be an origin"},
|
||||
{name: "public URL empty fragment", values: map[string]string{"APP_PUBLIC_URL": "https://lincodex.example#"}, want: "config.validateAppPublicURL - APP_PUBLIC_URL must be an origin"},
|
||||
{name: "public URL zero port", values: map[string]string{"APP_PUBLIC_URL": "https://lincodex.example:0"}, want: "config.validateAppPublicURL - APP_PUBLIC_URL must have a valid nonzero port"},
|
||||
{name: "public URL port out of range", values: map[string]string{"APP_PUBLIC_URL": "https://lincodex.example:70000"}, want: "config.validateAppPublicURL - APP_PUBLIC_URL must have a valid nonzero port"},
|
||||
{name: "Resend key unset", values: map[string]string{"EMAIL_PROVIDER": "resend"}, want: "config.validateResendAPIKey - RESEND_API_KEY must be set when EMAIL_PROVIDER is resend"},
|
||||
{name: "Resend key empty", values: resend(""), want: "config.validateResendAPIKey - RESEND_API_KEY must be set"},
|
||||
{name: "Resend key with space", values: resend("re_" + secretMarker + " x"), want: "config.validateResendAPIKey - RESEND_API_KEY must not contain whitespace"},
|
||||
{name: "Resend key with newline", values: resend("re_" + secretMarker + "\n"), want: "config.validateResendAPIKey - RESEND_API_KEY must not contain whitespace"},
|
||||
{name: "SMTP host unset", values: smtpValues(nil), unset: []string{"SMTP_HOST"}, want: "Config.validateSMTP - SMTP_HOST must be set when EMAIL_PROVIDER is smtp"},
|
||||
{name: "SMTP host with port", values: smtpValues(map[string]string{"SMTP_HOST": "smtp.example.com:587"}), want: "Config.validateSMTP - SMTP_HOST must be a hostname or IP address"},
|
||||
{name: "SMTP port unset", values: smtpValues(nil), unset: []string{"SMTP_PORT"}, want: "Config.validateSMTP - SMTP_PORT must be set to a port between 1 and 65535"},
|
||||
{name: "SMTP port not a number", values: smtpValues(map[string]string{"SMTP_PORT": "submission"}), want: "Config.loadSMTP - SMTP_PORT must be an integer"},
|
||||
{name: "SMTP port zero", values: smtpValues(map[string]string{"SMTP_PORT": "0"}), want: "Config.validateSMTP - SMTP_PORT must be set to a port"},
|
||||
{name: "SMTP port out of range", values: smtpValues(map[string]string{"SMTP_PORT": "70000"}), want: "Config.validateSMTP - SMTP_PORT must be set to a port"},
|
||||
{name: "SMTP username unset", values: smtpValues(nil), unset: []string{"SMTP_USERNAME"}, want: "Config.validateSMTP - SMTP_USERNAME must be set when EMAIL_PROVIDER is smtp"},
|
||||
{name: "SMTP password unset", values: smtpValues(nil), unset: []string{"SMTP_PASSWORD"}, want: "Config.validateSMTP - SMTP_PASSWORD must be set when EMAIL_PROVIDER is smtp"},
|
||||
{name: "SMTP TLS mode unset", values: smtpValues(nil), unset: []string{"SMTP_TLS_MODE"}, want: "Config.validateSMTP - SMTP_TLS_MODE must be starttls or tls"},
|
||||
{name: "SMTP plaintext", values: smtpValues(map[string]string{"SMTP_TLS_MODE": "none"}), want: "Config.validateSMTP - SMTP_TLS_MODE must be starttls or tls; plaintext SMTP is not supported"},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
_, err := LoadFromEnv(mailEnvironment(test.values, test.unset...))
|
||||
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} {
|
||||
if strings.Contains(err.Error(), secret) {
|
||||
t.Fatalf("configuration error discloses a configured value: %v", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user