Open platform to collect and prioritize feedback
fix(notifications): escape markdown in names and titles in notification titles
Matt Roberts469a34869e8e
diff --git a/app/pkg/markdown/escape.go b/app/pkg/markdown/escape.gonew file mode 100644index 000000000..8850bb0e0--- /dev/null+++ b/app/pkg/markdown/escape.go@@ -0,0 +1,45 @@+package markdown++import "strings"++// Escape returns input with its markdown syntax neutralised, so that it renders+// as the same literal text when placed inline in a markdown string, e.g. a user+// name inside "**%s**". It targets CommonMark/GFM as rendered by the frontend+// (marked), which is what displays in-app notification titles. This package's+// own renderer honours fewer backslash escapes, so don't use it for content+// rendered by Full or PlainText.+//+// Every ASCII punctuation character is backslash-escaped, which CommonMark and+// GFM always render as the literal character. This covers emphasis, links,+// images, code spans, strikethrough, tables, entities ("&") and Fider mentions+// ("@[...]"), and also stops GFM from auto-linking bare URLs, "www." hosts and+// email addresses, because "://", "www." and "@" no longer appear contiguously.+//+// "<" is the exception: the frontend entity-encodes "<" before parsing, so inline+// HTML and "<...>" autolinks are already shown as plain text, and a "\<" would+// be displayed as "<" rather than "<".+//+// Line breaks are replaced with spaces so the value cannot start a block+// element such as a heading, list or code block.+func Escape(input string) string {+ var sb strings.Builder+ sb.Grow(len(input) * 2)+ for _, r := range input {+ switch {+ case r == '\r' || r == '\n':+ sb.WriteByte(' ')+ case r == '<':+ sb.WriteRune(r)+ case r < 0x80 && isASCIIPunctuation(byte(r)):+ sb.WriteByte('\\')+ sb.WriteRune(r)+ default:+ sb.WriteRune(r)+ }+ }+ return sb.String()+}++func isASCIIPunctuation(c byte) bool {+ return (c >= '!' && c <= '/') || (c >= ':' && c <= '@') || (c >= '[' && c <= '`') || (c >= '{' && c <= '~')+}diff --git a/app/pkg/markdown/escape_test.go b/app/pkg/markdown/escape_test.gonew file mode 100644index 000000000..e4afc4bae--- /dev/null+++ b/app/pkg/markdown/escape_test.go@@ -0,0 +1,28 @@+package markdown_test++import (+ "testing"++ . "github.com/getfider/fider/app/pkg/assert"+ "github.com/getfider/fider/app/pkg/markdown"+)++func TestEscape(t *testing.T) {+ RegisterT(t)++ for input, expected := range map[string]string{+ "Jon Snow": "Jon Snow",+ "Zoë Ünïcode ✓": "Zoë Ünïcode ✓",+ "[Click](https://evil.example)": `\[Click\]\(https\:\/\/evil\.example\)`,+ "**bold** _it_ ~~del~~ `code`": `\*\*bold\*\* \_it\_ \~\~del\~\~ \` + "`" + `code\` + "`",+ "": `\!\[img\]\(x\.png\)`,+ "www.evil.example": `www\.evil\.example`,+ "[email protected]": `jon\@evil\.example`,+ "@[Jon Snow]": `\@\[Jon Snow\]`,+ "Tom & Jerry <3 <b>x</b>": `Tom \& Jerry <3 <b\>x<\/b\>`,+ `a\b | c # d`: `a\\b \| c \# d`,+ "line1\nline2\r\n# heading": `line1 line2 \# heading`,+ } {+ Expect(markdown.Escape(input)).Equals(expected)+ }+}diff --git a/app/tasks/delete_post.go b/app/tasks/delete_post.goindex d1339c230..51fceeb1c 100644--- a/app/tasks/delete_post.go+++ b/app/tasks/delete_post.go@@ -22,7 +22,7 @@ func NotifyAboutDeletedPost(post *entity.Post, deleteCommentAdded bool) worker.Ttenant := c.Tenant()baseURL, logoURL := web.BaseURL(c), web.LogoURL(c)author := c.User()- title := fmt.Sprintf("**%s** deleted **%s**", author.Name, post.Title)+ title := fmt.Sprintf("**%s** deleted **%s**", markdown.Escape(author.Name), markdown.Escape(post.Title))// WebhookwebhookProps := webhook.Props{}diff --git a/app/tasks/new_comment.go b/app/tasks/new_comment.goindex 489d24342..d3386e93c 100644--- a/app/tasks/new_comment.go+++ b/app/tasks/new_comment.go@@ -34,7 +34,7 @@ func NotifyAboutNewComment(comment *entity.Comment, post *entity.Post) worker.Ta}author := c.User()- title := fmt.Sprintf("**%s** left a comment on **%s**", author.Name, post.Title)+ title := fmt.Sprintf("**%s** left a comment on **%s**", markdown.Escape(author.Name), markdown.Escape(post.Title))link := fmt.Sprintf("/posts/%d/%s", post.Number, post.Slug)for _, user := range users {if user.ID != author.ID {@@ -51,7 +51,7 @@ func NotifyAboutNewComment(comment *entity.Comment, post *entity.Post) worker.Ta}// Web notification - mentions- title = fmt.Sprintf("**%s** mentioned you in **%s**", author.Name, post.Title)+ title = fmt.Sprintf("**%s** mentioned you in **%s**", markdown.Escape(author.Name), markdown.Escape(post.Title))if mentions != nil {@@ -188,7 +188,7 @@ func NotifyAboutUpdatedComment(post *entity.Post, comment *entity.Comment) worke})author := c.User()- title := fmt.Sprintf("**%s** mentioned you in **%s**", author.Name, post.Title)+ title := fmt.Sprintf("**%s** mentioned you in **%s**", markdown.Escape(author.Name), markdown.Escape(post.Title))link := fmt.Sprintf("/posts/%d/%s", post.Number, post.Slug)mentionNotificationSent := falseif mentions != nil {diff --git a/app/tasks/new_post.go b/app/tasks/new_post.goindex 6e3aa43e2..65bf91bc6 100644--- a/app/tasks/new_post.go+++ b/app/tasks/new_post.go@@ -33,7 +33,7 @@ func NotifyAboutNewPost(post *entity.Post) worker.Task {}author := c.User()- title := fmt.Sprintf("New post: **%s**", post.Title)+ title := fmt.Sprintf("New post: **%s**", markdown.Escape(post.Title))link := fmt.Sprintf("/posts/%d/%s", post.Number, post.Slug)for _, user := range users {if user.ID != author.ID {@@ -51,7 +51,7 @@ func NotifyAboutNewPost(post *entity.Post) worker.Task {// Web notification - mentionsif len(mentions) > 0 {- title = fmt.Sprintf("**%s** mentioned you in **%s**", author.Name, post.Title)+ title = fmt.Sprintf("**%s** mentioned you in **%s**", markdown.Escape(author.Name), markdown.Escape(post.Title))users, err = getActiveSubscribers(c, post, enum.NotificationChannelWeb, enum.NotificationEventMention)if err != nil {@@ -195,7 +195,7 @@ func NotifyAboutUpdatedPost(post *entity.Post) worker.Task {})author := c.User()- title := fmt.Sprintf("**%s** mentioned you in **%s**", author.Name, post.Title)+ title := fmt.Sprintf("**%s** mentioned you in **%s**", markdown.Escape(author.Name), markdown.Escape(post.Title))link := fmt.Sprintf("/posts/%d/%s", post.Number, post.Slug)mentionNotificationSent := falseif len(mentions) > 0 {diff --git a/app/tasks/notification_title_test.go b/app/tasks/notification_title_test.gonew file mode 100644index 000000000..9e17bd1cb--- /dev/null+++ b/app/tasks/notification_title_test.go@@ -0,0 +1,128 @@+package tasks_test++import (+ "context"+ "testing"+ "time"++ "github.com/getfider/fider/app/models/cmd"+ "github.com/getfider/fider/app/models/entity"+ "github.com/getfider/fider/app/models/enum"+ "github.com/getfider/fider/app/models/query"+ . "github.com/getfider/fider/app/pkg/assert"+ "github.com/getfider/fider/app/pkg/bus"+ "github.com/getfider/fider/app/pkg/mock"+ "github.com/getfider/fider/app/pkg/worker"+ "github.com/getfider/fider/app/services/email/emailmock"+ "github.com/getfider/fider/app/tasks"+)++func TestNotificationTitles_EscapeMarkdownInNamesAndTitles(t *testing.T) {+ RegisterT(t)++ name := "[Your account needs verifying](https://evil.example) <b>x</b> **bold**"+ escapedName := `\[Your account needs verifying\]\(https\:\/\/evil\.example\) <b\>x<\/b\> \*\*bold\*\*`+ title := "_Urgent_ www.evil.example "+ escapedTitle := `\_Urgent\_ www\.evil\.example \!\[i\]\(x\.png\)`++ newPost := func() *entity.Post {+ return &entity.Post{+ ID: 1,+ Number: 1,+ Title: title,+ Slug: "urgent",+ Description: "Hello @[Jon Snow]",+ User: mock.AryaStark,+ Status: enum.PostDuplicate,+ Response: &entity.PostResponse{+ RespondedAt: time.Now(),+ User: mock.JonSnow,+ Original: &entity.OriginalPost{Number: 2, Title: "Original", Slug: "original"},+ },+ }+ }++ testCases := []struct {+ task func(post *entity.Post) worker.Task+ expected []string+ }{+ {+ task: func(post *entity.Post) worker.Task { return tasks.NotifyAboutNewPost(post) },+ expected: []string{+ "New post: **" + escapedTitle + "**",+ "**" + escapedName + "** mentioned you in **" + escapedTitle + "**",+ },+ },+ {+ task: func(post *entity.Post) worker.Task { return tasks.NotifyAboutUpdatedPost(post) },+ expected: []string{+ "**" + escapedName + "** mentioned you in **" + escapedTitle + "**",+ },+ },+ {+ task: func(post *entity.Post) worker.Task {+ return tasks.NotifyAboutNewComment(&entity.Comment{Content: "Hello @[Jon Snow]"}, post)+ },+ expected: []string{+ "**" + escapedName + "** left a comment on **" + escapedTitle + "**",+ "**" + escapedName + "** mentioned you in **" + escapedTitle + "**",+ },+ },+ {+ task: func(post *entity.Post) worker.Task {+ return tasks.NotifyAboutUpdatedComment(post, &entity.Comment{Content: "Hello @[Jon Snow]"})+ },+ expected: []string{+ "**" + escapedName + "** mentioned you in **" + escapedTitle + "**",+ },+ },+ {+ task: func(post *entity.Post) worker.Task { return tasks.NotifyAboutStatusChange(post, enum.PostOpen) },+ expected: []string{+ "**" + escapedName + "** changed status of **" + escapedTitle + "** to **duplicate**",+ },+ },+ {+ task: func(post *entity.Post) worker.Task { return tasks.NotifyAboutDeletedPost(post, true) },+ expected: []string{+ "**" + escapedName + "** deleted **" + escapedTitle + "**",+ },+ },+ }++ for _, testCase := range testCases {+ bus.Init(emailmock.Service{})++ titles := make([]string, 0)+ bus.AddHandler(func(ctx context.Context, c *cmd.AddNewNotification) error {+ titles = append(titles, c.Title)+ return nil+ })+ bus.AddHandler(func(ctx context.Context, c *cmd.AddMentionNotification) error {+ return nil+ })+ bus.AddHandler(func(ctx context.Context, q *query.GetMentionNotifications) error {+ q.Result = []*entity.MentionNotification{}+ return nil+ })+ bus.AddHandler(func(ctx context.Context, q *query.GetActiveSubscribers) error {+ q.Result = []*entity.User{mock.JonSnow}+ return nil+ })+ bus.AddHandler(func(ctx context.Context, c *cmd.TriggerWebhooks) error {+ return nil+ })++ author := *mock.AryaStark+ author.Name = name++ err := mock.NewWorker().+ OnTenant(mock.DemoTenant).+ AsUser(&author).+ WithBaseURL("http://domain.com").+ Execute(testCase.task(newPost()))++ Expect(err).IsNil()+ Expect(titles).Equals(testCase.expected)+ }+}diff --git a/app/tasks/status_change.go b/app/tasks/status_change.goindex 4b8153e0f..7edd998cd 100644--- a/app/tasks/status_change.go+++ b/app/tasks/status_change.go@@ -30,7 +30,7 @@ func NotifyAboutStatusChange(post *entity.Post, prevStatus enum.PostStatus) work}author := c.User()- title := fmt.Sprintf("**%s** changed status of **%s** to **%s**", author.Name, post.Title, post.Status.Name())+ title := fmt.Sprintf("**%s** changed status of **%s** to **%s**", markdown.Escape(author.Name), markdown.Escape(post.Title), post.Status.Name())link := fmt.Sprintf("/posts/%d/%s", post.Number, post.Slug)for _, user := range users {if user.ID != author.ID {diff --git a/public/components/common/Markdown.spec.tsx b/public/components/common/Markdown.spec.tsxindex d84b853ac..9caff8cc5 100644--- a/public/components/common/Markdown.spec.tsx+++ b/public/components/common/Markdown.spec.tsx@@ -1,6 +1,8 @@import React from "react"import { render } from "@testing-library/react"import { Markdown } from "./Markdown"+import { fiderAllowedSchemes } from "@fider/hooks"+fiderAllowedSchemes.get = () => ""describe("<Markdown style='plainText' />", () => {const payloads = ["<img src=x onerror=alert(1)>", "<script>alert(1)</script>", "`<img src=x onerror=alert(1)>`"]@@ -17,3 +19,31 @@ describe("<Markdown style='plainText' />", () => {expect(container.textContent).toEqual("Jane's & Jim's > Matt <b>hi</b>")})})++describe("<Markdown style='full' /> with markdown-escaped values", () => {+ // Notification titles embed user names and post titles escaped by markdown.Escape (Go),+ // e.g. "**" + Escape(name) + "**". They must render as the literal text.+ const cases = [+ {+ escaped: "\\[Your account needs verifying\\]\\(https\\:\\/\\/evil\\.example\\)",+ text: "[Your account needs verifying](https://evil.example)",+ },+ { escaped: "https\\:\\/\\/evil\\.example", text: "https://evil.example" },+ { escaped: "www\\.evil\\.example", text: "www.evil.example" },+ { escaped: "jon\\@evil\\.example", text: "[email protected]" },+ { escaped: "\\@\\[Jon Snow\\]", text: "@[Jon Snow]" },+ { escaped: "\\!\\[i\\]\\(https\\:\\/\\/evil\\.example\\/x\\.png\\)", text: "" },+ { escaped: "\\_Urgent\\_ \\*\\*bold\\*\\* \\~\\~del\\~\\~ \\`code\\`", text: "_Urgent_ **bold** ~~del~~ `code`" },+ { escaped: "<a href\\=\\'https\\:\\/\\/evil\\.example\\'\\>x<\\/a\\>", text: "<a href='https://evil.example'>x</a>" },+ { escaped: "<https\\:\\/\\/evil\\.example\\>", text: "<https://evil.example>" },+ { escaped: "Tom \\& Jerry <3 \\<\\;b\\>\\;", text: "Tom & Jerry <3 <b>" },+ ]++ cases.forEach(({ escaped, text }) => {+ test(`renders literally: ${text}`, () => {+ const { container } = render(<Markdown style="full" text={`**${escaped}** left a comment`} />)+ expect(container.querySelector("a, img, em, del, code, span.mention")).toBeNull()+ expect(container.querySelector("strong")?.textContent).toEqual(text)+ })+ })+})