High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.
JavaScript campaigncampaign-managementemail-marketingemail-subscriptionlistmonkmailing-listnewsletternewsletter-managementnewsletter-softwareself-hostedsms-gatewaysmtptransactional-emails
Mandate ARN filtering for SES bounces and shared secret for Azure.
Kailash Nadh0c155bd2663d
diff --git a/cmd/init.go b/cmd/init.goindex 55026d83..3c3530bc 100644--- a/cmd/init.go+++ b/cmd/init.go@@ -556,9 +556,9 @@ func initConstConfig(ko *koanf.Koanf) *Config {c.Privacy.DomainAllowlist = ko.Strings("privacy.domain_allowlist")c.BounceWebhooksEnabled = ko.Bool("bounce.webhooks_enabled")- c.BounceSESEnabled = ko.Bool("bounce.ses_enabled")+ c.BounceSESEnabled = ko.Bool("bounce.ses.enabled")c.BounceAzureEnabled = ko.Bool("bounce.azure.enabled")- c.BounceSendgridEnabled = ko.Bool("bounce.sendgrid_enabled")+ c.BounceSendgridEnabled = ko.Bool("bounce.sendgrid.enabled")c.BouncePostmarkEnabled = ko.Bool("bounce.postmark.enabled")c.BounceForwardemailEnabled = ko.Bool("bounce.forwardemail.enabled")c.BounceLettermintEnabled = ko.Bool("bounce.lettermint.enabled")@@ -895,13 +895,24 @@ func initNotifs(fs stuffbin.FileSystem, i *i18n.I18n, em *email.Emailer, u *UrlC// for incoming bounce events.func initBounceManager(cb func(models.Bounce) error, stmt *sqlx.Stmt, lo *log.Logger, ko *koanf.Koanf) *bounce.Manager {opt := bounce.Opt{- WebhooksEnabled: ko.Bool("bounce.webhooks_enabled"),- SESEnabled: ko.Bool("bounce.ses_enabled"),+ WebhooksEnabled: ko.Bool("bounce.webhooks_enabled"),+ SES: struct {+ Enabled bool+ TopicARNs []string+ }{+ ko.Bool("bounce.ses.enabled"),+ ko.Strings("bounce.ses.topic_arns"),+ },AzureEnabled: ko.Bool("bounce.azure.enabled"),AzureSharedSecret: ko.String("bounce.azure.shared_secret"),AzureSharedSecretHeader: ko.String("bounce.azure.shared_secret_header"),- SendgridEnabled: ko.Bool("bounce.sendgrid_enabled"),- SendgridKey: ko.String("bounce.sendgrid_key"),+ Sendgrid: struct {+ Enabled bool+ Key string+ }{+ ko.Bool("bounce.sendgrid.enabled"),+ ko.String("bounce.sendgrid.key"),+ },Postmark: struct {Enabled boolUsername stringdiff --git a/cmd/settings.go b/cmd/settings.goindex 89c012a3..b77b63a3 100644--- a/cmd/settings.go+++ b/cmd/settings.go@@ -81,7 +81,7 @@ func maskSettings(s *models.Settings) {}s.UploadS3AwsSecretAccessKey = strings.Repeat(pwdMask, utf8.RuneCountInString(s.UploadS3AwsSecretAccessKey))- s.SendgridKey = strings.Repeat(pwdMask, utf8.RuneCountInString(s.SendgridKey))+ s.BounceSendgrid.Key = strings.Repeat(pwdMask, utf8.RuneCountInString(s.BounceSendgrid.Key))s.BounceAzure.SharedSecret = strings.Repeat(pwdMask, utf8.RuneCountInString(s.BounceAzure.SharedSecret))s.BouncePostmark.Password = strings.Repeat(pwdMask, utf8.RuneCountInString(s.BouncePostmark.Password))s.BounceForwardEmail.Key = strings.Repeat(pwdMask, utf8.RuneCountInString(s.BounceForwardEmail.Key))@@ -309,8 +309,8 @@ func (a *App) UpdateSettings(c *echo.Context) error {if set.UploadS3AwsSecretAccessKey == "" {set.UploadS3AwsSecretAccessKey = cur.UploadS3AwsSecretAccessKey}- if set.SendgridKey == "" {- set.SendgridKey = cur.SendgridKey+ if set.BounceSendgrid.Key == "" {+ set.BounceSendgrid.Key = cur.BounceSendgrid.Key}if set.BounceAzure.SharedSecret == "" {set.BounceAzure.SharedSecret = cur.BounceAzure.SharedSecretdiff --git a/docs/docs/content/bounces.md b/docs/docs/content/bounces.mdindex 53c81261..57b82c30 100644--- a/docs/docs/content/bounces.md+++ b/docs/docs/content/bounces.md@@ -78,6 +78,9 @@ If using SES as your SMTP provider, automatic bounce processing is the recommend2. In the AWS console, go to [Simple Notification Service](https://console.aws.amazon.com/sns/) and create a new topic with the following settings:- Type: `Standard`- Name: `ses-bounces` (or any other name)++ Copy the topic ARN into "Allowed SNS topic ARNs" in listmonk bounce settings (one ARN per line) and save before creating the subscription.+3. Create a new subscription to that topic with the following settings:- Protocol: `HTTPS`- Endpoint: `https://listmonk.yoursite.com/webhooks/service/ses`@@ -104,7 +107,7 @@ If you use Azure Communication Services Email, listmonk can receive delivery rep- Enable bounce processing: `Enabled`- Enable bounce webhooks: `Enabled`- Enable Azure ACS: `Enabled`- - Optional: set `Azure Event Grid Shared Secret`.+ - Set `Azure Event Grid Shared Secret`.- Optional: set `Azure Shared Secret Header Name` if you want listmonk to read the secret from a header (defaults to `X-Listmonk-Webhook-Secret`).2. In listmonk settings, go to "SMTP" and use the `Azure ACS` quick preset to fill SMTP defaults.3. In Azure, create an Event Grid subscription for your ACS Email events with:diff --git a/docs/swagger/collections.yaml b/docs/swagger/collections.yamlindex 28ec56dd..c9be7179 100644--- a/docs/swagger/collections.yaml+++ b/docs/swagger/collections.yaml@@ -2444,28 +2444,54 @@ components:type: integerbounce.action:type: string- bounce.ses_enabled:- type: boolean- bounce.azure.enabled:- type: boolean- bounce.azure.shared_secret:- type: string- bounce.azure.shared_secret_header:- type: string- bounce.sendgrid_enabled:- type: boolean- bounce.sendgrid_key:- type: string- bounce.forwardemail_enabled:- type: boolean- bounce.forwardemail_key:- type: string- bounce.postmark_enabled:- type: boolean- bounce.postmark_username:- type: string- bounce.postmark_password:- type: string+ bounce.ses:+ type: object+ properties:+ enabled:+ type: boolean+ topic_arns:+ type: array+ items:+ type: string+ bounce.azure:+ type: object+ properties:+ enabled:+ type: boolean+ shared_secret:+ type: string+ shared_secret_header:+ type: string+ bounce.sendgrid:+ type: object+ properties:+ enabled:+ type: boolean+ key:+ type: string+ bounce.forwardemail:+ type: object+ properties:+ enabled:+ type: boolean+ key:+ type: string+ bounce.postmark:+ type: object+ properties:+ enabled:+ type: boolean+ username:+ type: string+ password:+ type: string+ bounce.lettermint:+ type: object+ properties:+ enabled:+ type: boolean+ key:+ type: stringbounce.mailboxes:type: arrayitems:diff --git a/i18n/en.json b/i18n/en.jsonindex 761f6cc1..ab6f4287 100644--- a/i18n/en.json+++ b/i18n/en.json@@ -283,6 +283,7 @@"globals.messages.noIDs": "No IDs given.","globals.messages.notFound": "{name} not found","globals.messages.numSelected": "{num} selected",+ "globals.messages.onePerLine": "One per line.","globals.messages.passwordChange": "Enter a value to change","globals.messages.passwordChangeFull": "Clear and re-enter the full password in '{name}'.","globals.messages.permissionDenied": "Permission denied: {name}",@@ -473,6 +474,7 @@"settings.appearance.customCSS": "Custom CSS","settings.appearance.customJS": "Custom JavaScript","settings.appearance.publicHelp": "Custom CSS and JavaScript to apply to the public pages.",+ "settings.bounces.sesTopicARNs": "Allowed SNS topic ARNs","settings.bounces.azureSharedSecret": "Azure Event Grid Shared Secret","settings.bounces.azureSharedSecretHeader": "Azure Shared Secret Header Name","settings.bounces.azureSharedSecretHeaderHelp": "Optional HTTP header name to read the Azure shared secret from. If empty, listmonk uses X-Listmonk-Webhook-Secret.",diff --git a/internal/bounce/bounce.go b/internal/bounce/bounce.goindex 30b81f56..d405e4c6 100644--- a/internal/bounce/bounce.go+++ b/internal/bounce/bounce.go@@ -19,17 +19,22 @@ type Mailbox interface {// Opt represents bounce processing options.type Opt struct {- MailboxEnabled bool `json:"mailbox_enabled"`- MailboxType string `json:"mailbox_type"`- Mailbox mailbox.Opt `json:"mailbox"`- WebhooksEnabled bool `json:"webhooks_enabled"`- SESEnabled bool `json:"ses_enabled"`- AzureEnabled bool `json:"azure_enabled"`- AzureSharedSecret string `json:"azure_shared_secret"`- AzureSharedSecretHeader string `json:"azure_shared_secret_header"`- SendgridEnabled bool `json:"sendgrid_enabled"`- SendgridKey string `json:"sendgrid_key"`- Postmark struct {+ MailboxEnabled bool `json:"mailbox_enabled"`+ MailboxType string `json:"mailbox_type"`+ Mailbox mailbox.Opt `json:"mailbox"`+ WebhooksEnabled bool `json:"webhooks_enabled"`+ SES struct {+ Enabled bool+ TopicARNs []string+ }+ AzureEnabled bool `json:"azure_enabled"`+ AzureSharedSecret string `json:"azure_shared_secret"`+ AzureSharedSecretHeader string `json:"azure_shared_secret_header"`+ Sendgrid struct {+ Enabled bool+ Key string+ }+ Postmark struct {Enabled boolUsername stringPassword string@@ -87,15 +92,15 @@ func New(opt Opt, q *Queries, lo *log.Logger) (*Manager, error) {}if opt.WebhooksEnabled {- if opt.SESEnabled {- m.SES = webhooks.NewSES()+ if opt.SES.Enabled {+ m.SES = webhooks.NewSES(opt.SES.TopicARNs)}if opt.AzureEnabled {m.Azure = webhooks.NewAzure(opt.AzureSharedSecret, opt.AzureSharedSecretHeader)}- if opt.SendgridEnabled {- sg, err := webhooks.NewSendgrid(opt.SendgridKey)+ if opt.Sendgrid.Enabled {+ sg, err := webhooks.NewSendgrid(opt.Sendgrid.Key)if err != nil {lo.Printf("error initializing sendgrid webhooks: %v", err)} else {diff --git a/internal/bounce/webhooks/azure.go b/internal/bounce/webhooks/azure.goindex c6e024f0..7b32eb7a 100644--- a/internal/bounce/webhooks/azure.go+++ b/internal/bounce/webhooks/azure.go@@ -144,9 +144,8 @@ func (a *Azure) verifyAuth(req *http.Request) error {querySecretParam = "code")- // If no local credential is configured, allow webhook payloads.if a.sharedSecret == "" {- return nil+ return errors.New("missing azure event grid shared secret")}if req == nil {diff --git a/internal/bounce/webhooks/ses.go b/internal/bounce/webhooks/ses.goindex ce12de6d..d45ea693 100644--- a/internal/bounce/webhooks/ses.go+++ b/internal/bounce/webhooks/ses.go@@ -66,15 +66,25 @@ type sesMail struct {// SES handles SES/SNS webhook notifications including confirming SNS topic subscription// requests and bounce notifications.type SES struct {- mu sync.RWMutex- certs map[string]*x509.Certificate+ mu sync.RWMutex+ certs map[string]*x509.Certificate+ topicARNs map[string]struct{}}// NewSES returns a new SES instance.-func NewSES() *SES {- return &SES{- certs: make(map[string]*x509.Certificate),+func NewSES(topicARNs []string) *SES {+ s := &SES{+ certs: make(map[string]*x509.Certificate),+ topicARNs: make(map[string]struct{}, len(topicARNs)),}++ for _, arn := range topicARNs {+ if arn = strings.TrimSpace(arn); arn != "" {+ s.topicARNs[arn] = struct{}{}+ }+ }++ return s}// ProcessSubscription processes an SNS topic subscribe / unsubscribe notification@@ -197,8 +207,12 @@ func (s *SES) buildSignature(n sesNotif) []byte {return b.Bytes()}-// verifyNotif verifies the signature on a notification payload.+// verifyNotif verifies the topic and signature on a notification payload.func (s *SES) verifyNotif(n sesNotif) error {+ if _, ok := s.topicARNs[n.TopicArn]; !ok {+ return errors.New("unrecognized SNS topic ARN")+ }+// Get the message signing certificate.cert, err := s.getCert(n.SigningCertURL)if err != nil {diff --git a/internal/migrations/v7.0.0.go b/internal/migrations/v7.0.0.goindex f81555fb..5bc5006c 100644--- a/internal/migrations/v7.0.0.go+++ b/internal/migrations/v7.0.0.go@@ -17,6 +17,26 @@ func V7_0_0(db *sqlx.DB, fs stuffbin.FileSystem, ko *koanf.Koanf, lo *log.LoggerUPDATE settings SET value = JSONB_BUILD_ARRAY(value)WHERE key = 'app.from_email' AND JSONB_TYPEOF(value) = 'string';+ -- Migrate SES settings to an object.+ INSERT INTO settings (key, value)+ SELECT 'bounce.ses', JSONB_BUILD_OBJECT(+ 'enabled', COALESCE((SELECT (value#>>'{}')::BOOLEAN FROM settings WHERE key = 'bounce.ses_enabled'), false),+ 'topic_arns', '[]'::JSONB+ )+ ON CONFLICT (key) DO NOTHING;++ DELETE FROM settings WHERE key = 'bounce.ses_enabled';++ -- Migrate SendGrid settings to an object.+ INSERT INTO settings (key, value)+ SELECT 'bounce.sendgrid', JSONB_BUILD_OBJECT(+ 'enabled', COALESCE((SELECT (value#>>'{}')::BOOLEAN FROM settings WHERE key = 'bounce.sendgrid_enabled'), false),+ 'key', COALESCE((SELECT value#>>'{}' FROM settings WHERE key = 'bounce.sendgrid_key'), '')+ )+ ON CONFLICT (key) DO NOTHING;++ DELETE FROM settings WHERE key IN ('bounce.sendgrid_enabled', 'bounce.sendgrid_key');+-- Login session duration.INSERT INTO settings (key, value) VALUES ('security.session_duration', '"7d"')ON CONFLICT (key) DO NOTHING;diff --git a/models/settings.go b/models/settings.goindex 417e2870..0ca96f2c 100644--- a/models/settings.go+++ b/models/settings.go@@ -134,10 +134,15 @@ type Settings struct {Count int `json:"count"`Action string `json:"action"`} `json:"bounce.actions"`- SESEnabled bool `json:"bounce.ses_enabled"`- SendgridEnabled bool `json:"bounce.sendgrid_enabled"`- SendgridKey string `json:"bounce.sendgrid_key"`- BounceAzure struct {+ BounceSES struct {+ Enabled bool `json:"enabled"`+ TopicARNs []string `json:"topic_arns"`+ } `json:"bounce.ses"`+ BounceSendgrid struct {+ Enabled bool `json:"enabled"`+ Key string `json:"key"`+ } `json:"bounce.sendgrid"`+ BounceAzure struct {Enabled bool `json:"enabled"`SharedSecret string `json:"shared_secret"`SharedSecretHeader string `json:"shared_secret_header"`diff --git a/schema.sql b/schema.sqlindex 876dc044..0cb301b3 100644--- a/schema.sql+++ b/schema.sql@@ -288,10 +288,9 @@ INSERT INTO settings (key, value) VALUES('bounce.enabled', 'false'),('bounce.webhooks_enabled', 'false'),('bounce.actions', '{"soft": {"count": 2, "action": "none"}, "hard": {"count": 1, "action": "blocklist"}, "complaint" : {"count": 1, "action": "blocklist"}}'),- ('bounce.ses_enabled', 'false'),+ ('bounce.ses', '{"enabled": false, "topic_arns": []}'),('bounce.azure', '{"enabled": false, "shared_secret": "", "shared_secret_header": ""}'),- ('bounce.sendgrid_enabled', 'false'),- ('bounce.sendgrid_key', '""'),+ ('bounce.sendgrid', '{"enabled": false, "key": ""}'),('bounce.postmark', '{"enabled": false, "username": "", "password": ""}'),('bounce.forwardemail', '{"enabled": false, "key": ""}'),('bounce.lettermint', '{"enabled": false, "key": ""}'),diff --git a/static/admin/assets/js/views/settings.js b/static/admin/assets/js/views/settings.jsindex 3a1b6e0b..f363cdf6 100644--- a/static/admin/assets/js/views/settings.js+++ b/static/admin/assets/js/views/settings.js@@ -378,7 +378,6 @@ function component(settings, userRoles, listRoles) {// Standalone secrets.const secrets = [['upload.s3.aws_secret_access_key', form, 's3'],- ['bounce.sendgrid_key', form, 'sendgrid'],];secrets.forEach(([key, obj, name]) => {if (isDummy(obj[key])) {@@ -389,6 +388,7 @@ function component(settings, userRoles, listRoles) {});const nested = [+ [form['bounce.sendgrid'], 'key', 'sendgrid'],[form['bounce.azure'], 'shared_secret', 'azure shared secret'],[form['security.captcha'].hcaptcha, 'secret', 'captcha'],[form['security.oidc'], 'client_secret', 'oidc'],diff --git a/static/admin/partials/settings/bounces.html b/static/admin/partials/settings/bounces.htmlindex 65601b69..2565bf00 100644--- a/static/admin/partials/settings/bounces.html+++ b/static/admin/partials/settings/bounces.html@@ -129,9 +129,16 @@<fieldset class="card" data-keywords="bounce ses amazon sns aws webhook"><legend>{{ $.L.T "settings.bounces.enableSES" }}</legend><label data-field>- <input type="checkbox" role="switch" x-model="form['bounce.ses_enabled']" />+ <input type="checkbox" role="switch" x-model="form['bounce.ses'].enabled" />{{ $.L.T "globals.buttons.enable" }}</label>+ <template x-if="form['bounce.ses'].enabled">+ <label data-field>+ {{ $.L.T "settings.bounces.sesTopicARNs" }}+ <textarea :value="(form['bounce.ses'].topic_arns || []).join('\n')" @input="form['bounce.ses'].topic_arns = $event.target.value.split('\n')"></textarea>+ <small data-hint>{{ $.L.T "globals.messages.onePerLine" }}</small>+ </label>+ </template></fieldset><fieldset class="card" data-keywords="bounce azure webhook shared secret header">@@ -159,13 +166,13 @@<fieldset class="card" data-keywords="bounce sendgrid webhook key"><legend>{{ $.L.T "settings.bounces.enableSendgrid" }}</legend><label data-field>- <input type="checkbox" role="switch" x-model="form['bounce.sendgrid_enabled']" />+ <input type="checkbox" role="switch" x-model="form['bounce.sendgrid'].enabled" />{{ $.L.T "globals.buttons.enable" }}</label>- <template x-if="form['bounce.sendgrid_enabled']">+ <template x-if="form['bounce.sendgrid'].enabled"><label data-field>{{ $.L.T "settings.bounces.sendgridKey" }}- <input type="password" x-model="form['bounce.sendgrid_key']" />+ <input type="password" x-model="form['bounce.sendgrid'].key" /><small data-hint>{{ $.L.T "globals.messages.passwordChange" }}</small></label></template>diff --git a/tests/specs/settings.spec.js b/tests/specs/settings.spec.jsindex 8f21d2a2..bd17c3d4 100644--- a/tests/specs/settings.spec.js+++ b/tests/specs/settings.spec.js@@ -306,7 +306,7 @@ test.describe('Settings', () => {await page.getByRole('group', { name: 'SES', exact: true }).getByRole('switch').check();await save(page);expect(await settings(page)).toMatchObject({- 'bounce.enabled': true, 'bounce.webhooks_enabled': true, 'bounce.ses_enabled': true,+ 'bounce.enabled': true, 'bounce.webhooks_enabled': true, 'bounce.ses': { enabled: true, topic_arns: [] },'bounce.actions': { hard: { count: 3, action: 'unsubscribe' } },'bounce.mailboxes': [{ enabled: false }],});