Skip to content

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API

fix(api): harden webhook email fallback

Acho Arnold Ewin9fa5b7653c7c
diff --git a/api/pkg/emails/event_payload_formatter.go b/api/pkg/emails/event_payload_formatter.go
index 130aaf6f..0a863854 100644
--- a/api/pkg/emails/event_payload_formatter.go
+++ b/api/pkg/emails/event_payload_formatter.go
@@ -23,7 +23,7 @@ func formatEventPayload(payload string) (string, template.HTML) {
content = highlightEventPayloadJSON(formattedPayload)
}
- // Every payload token is escaped before this trusted wrapper is constructed.
+ // #nosec G203 -- every dynamic payload token is escaped before the static wrapper is added.
richPayload := template.HTML(`<pre style="` + eventPayloadCodeBlockStyle + `">` + content + `</pre>`)
return formattedPayload, richPayload
}
diff --git a/api/pkg/emails/hermes_notification_email_factory.go b/api/pkg/emails/hermes_notification_email_factory.go
index 73f129de..2229a502 100644
--- a/api/pkg/emails/hermes_notification_email_factory.go
+++ b/api/pkg/emails/hermes_notification_email_factory.go
@@ -123,12 +123,14 @@ func (factory *hermesNotificationEmailFactory) WebhookSendFailed(user *entities.
return nil, stacktrace.Propagate(err, "cannot generate html email")
}
- text, err := factory.generator.GeneratePlainText(email)
+ eventPayloadEntryIndex := len(email.Body.Dictionary) - 1
+ text, err := generateWebhookSendFailedPlainText(func(value string) (string, error) {
+ email.Body.Dictionary[eventPayloadEntryIndex].Value = value
+ return factory.generator.GeneratePlainText(email)
+ }, formattedPayload)
if err != nil {
return nil, stacktrace.Propagate(err, "cannot generate text email")
}
- // Hermes/html2text collapses dictionary whitespace, so restore the payload after plain-text generation.
- text = replaceWebhookSendFailedEventPayloadPlaceholder(text, formattedPayload)
return &Email{
ToEmail: user.Email,
@@ -138,6 +140,21 @@ func (factory *hermesNotificationEmailFactory) WebhookSendFailed(user *entities.
}, nil
}
+func generateWebhookSendFailedPlainText(generate func(string) (string, error), formattedPayload string) (string, error) {
+ text, err := generate(webhookSendFailedEventPayloadPlaceholder)
+ if err != nil {
+ return "", err
+ }
+
+ if strings.Contains(text, webhookSendFailedEventPayloadPlaceholder) {
+ // Hermes/html2text collapses dictionary whitespace, so restore the payload after plain-text generation.
+ return replaceWebhookSendFailedEventPayloadPlaceholder(text, formattedPayload), nil
+ }
+
+ // Preserve the real payload if a Hermes change stops carrying the placeholder through unchanged.
+ return generate(formattedPayload)
+}
+
func replaceWebhookSendFailedEventPayloadPlaceholder(text string, formattedPayload string) string {
before, after, found := strings.Cut(text, webhookSendFailedEventPayloadPlaceholder)
if !found {
diff --git a/api/pkg/emails/hermes_notification_email_factory_test.go b/api/pkg/emails/hermes_notification_email_factory_test.go
index 37c9333a..47234a5e 100644
--- a/api/pkg/emails/hermes_notification_email_factory_test.go
+++ b/api/pkg/emails/hermes_notification_email_factory_test.go
@@ -50,6 +50,7 @@ func TestWebhookSendFailedFormatsOnlyEventPayload(t *testing.T) {
assert.Contains(t, email.Text, `"retry": false`)
assert.NotContains(t, email.Text, "<pre")
assert.NotContains(t, email.Text, webhookSendFailedEventPayloadPlaceholder)
+ assert.NotContains(t, email.HTML, webhookSendFailedEventPayloadPlaceholder)
}
func TestWebhookSendFailedPreservesNonJSONEventPayload(t *testing.T) {
@@ -76,6 +77,24 @@ func TestWebhookSendFailedPreservesNonJSONEventPayload(t *testing.T) {
assert.Contains(t, email.Text, "line one\n line two")
}
+func TestGenerateWebhookSendFailedPlainTextFallsBackToFormattedPayload(t *testing.T) {
+ formattedPayload := "{\n \"message\": \"hello\"\n}"
+ values := make([]string, 0, 2)
+ generate := func(value string) (string, error) {
+ values = append(values, value)
+ if len(values) == 1 {
+ return "plain text without the placeholder", nil
+ }
+ return value, nil
+ }
+
+ text, err := generateWebhookSendFailedPlainText(generate, formattedPayload)
+
+ require.NoError(t, err)
+ assert.Equal(t, formattedPayload, text)
+ assert.Equal(t, []string{webhookSendFailedEventPayloadPlaceholder, formattedPayload}, values)
+}
+
func TestReplaceWebhookSendFailedEventPayloadPlaceholder(t *testing.T) {
tests := []struct {
name string