Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

fix(email): unescape HTML entities in rendered subject

Américo Dias2f7aa7472347
diff --git a/app/services/email/email_test.go b/app/services/email/email_test.go
index 9756d49c9..f1d6f0905 100644
--- a/app/services/email/email_test.go
+++ b/app/services/email/email_test.go
@@ -10,6 +10,20 @@ import (
. "github.com/getfider/fider/app/pkg/assert"
)
+func TestRenderMessage_SubjectUnescapesHTMLEntities(t *testing.T) {
+ RegisterT(t)
+
+ // Reproducer for the issue where characters like '+', '"', '&', '<', '>'
+ // in the rendered subject ended up as their numeric HTML entities
+ // (e.g. "&#43;", "&#34;") because the subject is rendered through
+ // html/template along with the body. SMTP headers are plain text and
+ // must contain the literal characters.
+ message := email.RenderMessage(context.Background(), "echo_test", email.NoReply, dto.Props{
+ "name": `Encontrar+se "quoted" & <tagged>`,
+ })
+ Expect(message.Subject).Equals(`Message to: Encontrar+se "quoted" & <tagged>`)
+}
+
func TestRenderMessage(t *testing.T) {
RegisterT(t)
diff --git a/app/services/email/message.go b/app/services/email/message.go
index 9374c5eb2..a9ed834d3 100644
--- a/app/services/email/message.go
+++ b/app/services/email/message.go
@@ -3,6 +3,7 @@ package email
import (
"bytes"
"context"
+ "html"
"mime"
"strings"
"unicode"
@@ -53,8 +54,15 @@ func RenderMessage(ctx context.Context, templateName string, fromAddress string,
lines := strings.Split(bf.String(), "\n")
body := strings.TrimLeft(strings.Join(lines[2:], "\n"), " ")
+ // The subject is rendered through html/template (because it shares the
+ // template set with the HTML body), which escapes characters like '+',
+ // '"', '&', '<', '>' to numeric entities (e.g. "&#43;"). SMTP headers
+ // are plain text and clients do not decode HTML entities in them, so
+ // undo the escaping for the subject only.
+ subject := html.UnescapeString(strings.TrimLeft(lines[0], "subject: "))
+
return &Message{
- Subject: strings.TrimLeft(lines[0], "subject: "),
+ Subject: subject,
Body: body,
}
}