Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

Keep webhook markdown output HTML-escaped

sap11103d683ad7b7db
diff --git a/app/pkg/markdown/text.go b/app/pkg/markdown/text.go
index 32583f348..084ec9d3f 100644
--- a/app/pkg/markdown/text.go
+++ b/app/pkg/markdown/text.go
@@ -44,6 +44,7 @@ var regexNewlines = regexp.MustCompile(`\n+`)
//
// The result is unescaped plain text (e.g. "Tom & Jerry", not "Tom & Jerry"), so
// callers must escape it for wherever it is written to, as html/template does.
+// It is not HTML-safe and must never be wrapped in template.HTML.
//
// Unlike Full, this needs no destination gating or scheme allow list: strictPolicy strips
// every tag and attribute, so neither an unsafe link destination nor markup forged by the
diff --git a/app/pkg/tpl/text.go b/app/pkg/tpl/text.go
index 864e2ce52..206493ddc 100644
--- a/app/pkg/tpl/text.go
+++ b/app/pkg/tpl/text.go
@@ -1,6 +1,7 @@
package tpl
import (
+ "html"
"strings"
"text/template"
@@ -10,7 +11,9 @@ import (
func GetTextTemplate(name string, rawText string) (*template.Template, error) {
tpl, err := template.New(name).Funcs(templateFunctions).Funcs(template.FuncMap{
"markdown": func(input string) string {
- return markdown.PlainText(input)
+ // Kept HTML-escaped: webhook destinations such as Slack read "<...>" as
+ // control sequences, and HTML destinations must not receive raw tags.
+ return html.EscapeString(markdown.PlainText(input))
},
}).Parse(rawText)
if err != nil {
diff --git a/app/pkg/tpl/text_test.go b/app/pkg/tpl/text_test.go
index db482e95e..1f889348d 100644
--- a/app/pkg/tpl/text_test.go
+++ b/app/pkg/tpl/text_test.go
@@ -14,9 +14,18 @@ func TestTextTemplate_Markdown(t *testing.T) {
tmpl, err := tpl.GetTextTemplate("test", "{{ markdown .description }}")
Expect(err).IsNil()
- result, err := tpl.Execute(tmpl, map[string]any{"description": "**Tom & Jerry** integration"})
- Expect(err).IsNil()
- Expect(result).Equals("Tom & Jerry integration")
+ for input, expected := range map[string]string{
+ "**Tom & Jerry** integration": "Tom &amp; Jerry integration",
+ "Support `<details>` blocks": "Support &lt;details&gt; blocks",
+ "Hey <!channel> look": "Hey &lt;!channel&gt; look",
+ "Hey &lt;!channel&gt; look": "Hey &lt;!channel&gt; look",
+ "Hey `<!channel>` look": "Hey &lt;!channel&gt; look",
+ "Hi <img src=x onerror=alert(1)>": "Hi &lt;img src=x onerror=alert(1)&gt;",
+ } {
+ result, err := tpl.Execute(tmpl, map[string]any{"description": input})
+ Expect(err).IsNil()
+ Expect(result).Equals(expected)
+ }
}
func TestTextTemplate_Markdown_QuotedAsJSON(t *testing.T) {
@@ -25,10 +34,10 @@ func TestTextTemplate_Markdown_QuotedAsJSON(t *testing.T) {
tmpl, err := tpl.GetTextTemplate("test", `{"m": {{ quote (markdown .description) }}}`)
Expect(err).IsNil()
- result, err := tpl.Execute(tmpl, map[string]any{"description": "Tom & Jerry\n\nUse `List<String>` > \"Map\""})
+ result, err := tpl.Execute(tmpl, map[string]any{"description": "Tom & Jerry\n\nUse `List<String>` > <!channel>"})
Expect(err).IsNil()
var payload map[string]string
Expect(json.Unmarshal([]byte(result), &payload)).IsNil()
- Expect(payload["m"]).Equals("Tom & Jerry\nUse List<String> > “Map”")
+ Expect(payload["m"]).Equals("Tom &amp; Jerry\nUse List&lt;String&gt; &gt; &lt;!channel&gt;")
}
diff --git a/app/services/webhook/dummy_props.go b/app/services/webhook/dummy_props.go
index 7105301c8..978f816a6 100644
--- a/app/services/webhook/dummy_props.go
+++ b/app/services/webhook/dummy_props.go
@@ -21,7 +21,7 @@ var dummyPost = &entity.Post{
This is an example of a post, but don't worry, nothing was created on your Fider instance:
It was just a test of a webhook trigger.
-You can use ` + "`" + `{{ markdown .post_description }}` + "`" + ` to convert __Markdown__ to plain text in post!`,
+You can use ` + "`" + `{{ markdown .post_description }}` + "`" + ` to convert __Markdown__ to HTML-escaped plain text in post!`,
CreatedAt: time.Date(2021, time.May, 7, 18, 42, 27, 0, time.UTC),
User: &entity.User{
ID: 7,