Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

Return plain text without HTML entities from markdown.PlainText

sap11103d7b5d9eee6e
diff --git a/app/pkg/markdown/markdown_test.go b/app/pkg/markdown/markdown_test.go
index aa9d7ed1a..ce04769b9 100644
--- a/app/pkg/markdown/markdown_test.go
+++ b/app/pkg/markdown/markdown_test.go
@@ -73,7 +73,7 @@ func TestPlainTextMarkdown(t *testing.T) {
for input, expected := range map[string]string{
"**Hello World**": `Hello World`,
- "Hello <b>Beautiful</b> World": `Hello &lt;b&gt;Beautiful&lt;/b&gt; World`,
+ "Hello <b>Beautiful</b> World": `Hello <b>Beautiful</b> World`,
"[My Link](http://example.com/)": `My Link`,
"![My Image](http://example.com/hello.jpg)": ``,
"Go to http://example.com/hello.jpg": `Go to http://example.com/hello.jpg`,
@@ -92,10 +92,28 @@ How are you?`,
How are you?`: `Hello World
How are you?`,
"### Hello World": `Hello World`,
- "Check this out: `HEEEY`": "Check this out: `HEEEY`",
+ "Check this out: `HEEEY`": "Check this out: HEEEY",
"Bad links should be OK [link without actual link]()": "Bad links should be OK link without actual link",
} {
output := markdown.PlainText(input)
Expect(output).Equals(expected)
}
}
+
+func TestPlainTextMarkdown_NoHTMLEntities(t *testing.T) {
+ RegisterT(t)
+
+ for input, expected := range map[string]string{
+ "Tom & Jerry integration": "Tom & Jerry integration",
+ "Please support C++ & C#": "Please support C++ & C#",
+ "Make it 2x faster -> better UX": "Make it 2x faster -> better UX",
+ "Use List<String> for tags": "Use List<String> for tags",
+ "Support `<details>` blocks": "Support <details> blocks",
+ "Use `a && b` or `a < b`": "Use a && b or a < b",
+ "**Tom & Jerry** [<b>link</b>]()": "Tom & Jerry <b>link</b>",
+ "Tom &amp; Jerry": "Tom & Jerry",
+ } {
+ output := markdown.PlainText(input)
+ Expect(output).Equals(expected)
+ }
+}
diff --git a/app/pkg/markdown/text.go b/app/pkg/markdown/text.go
index 2e264f39a..32583f348 100644
--- a/app/pkg/markdown/text.go
+++ b/app/pkg/markdown/text.go
@@ -1,12 +1,12 @@
package markdown
import (
- "fmt"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/ast"
htmlrenderer "github.com/gomarkdown/markdown/html"
mdparser "github.com/gomarkdown/markdown/parser"
"github.com/microcosm-cc/bluemonday"
+ "html"
"io"
"regexp"
"strings"
@@ -25,7 +25,9 @@ var textRenderer = htmlrenderer.NewRenderer(htmlrenderer.RendererOptions{
_, _ = io.WriteString(w, "\n")
return ast.GoToNext, true
case *ast.Code:
- _, _ = io.WriteString(w, fmt.Sprintf("`%s`", node.Literal))
+ // Escaped so that code like `<details>` is kept as text instead of being
+ // stripped as a tag by strictPolicy.
+ htmlrenderer.EscapeHTML(w, node.Literal)
return ast.GoToNext, true
}
return ast.GoToNext, false
@@ -40,6 +42,9 @@ var regexNewlines = regexp.MustCompile(`\n+`)
// PlainText parses given markdown input and return only the text.
//
+// The result is unescaped plain text (e.g. "Tom & Jerry", not "Tom &amp; Jerry"), so
+// callers must escape it for wherever it is written to, as html/template does.
+//
// 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
// renderer survives.
@@ -50,5 +55,6 @@ func PlainText(input string) string {
output := markdown.ToHTML([]byte(input), parser, textRenderer)
sanitizedOutput := strictPolicy.Sanitize(string(output))
sanitizedOutput = regexNewlines.ReplaceAllString(sanitizedOutput, "\n")
- return strings.TrimSpace(sanitizedOutput)
+ // strictPolicy returns HTML-escaped text; decode it once to get the text as typed.
+ return strings.TrimSpace(html.UnescapeString(sanitizedOutput))
}
diff --git a/app/pkg/tpl/text_test.go b/app/pkg/tpl/text_test.go
new file mode 100644
index 000000000..db482e95e
--- /dev/null
+++ b/app/pkg/tpl/text_test.go
@@ -0,0 +1,34 @@
+package tpl_test
+
+import (
+ "encoding/json"
+ "testing"
+
+ . "github.com/getfider/fider/app/pkg/assert"
+ "github.com/getfider/fider/app/pkg/tpl"
+)
+
+func TestTextTemplate_Markdown(t *testing.T) {
+ RegisterT(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")
+}
+
+func TestTextTemplate_Markdown_QuotedAsJSON(t *testing.T) {
+ RegisterT(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\""})
+ 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”")
+}
diff --git a/app/pkg/web/renderer_test.go b/app/pkg/web/renderer_test.go
index 4e73700c0..1d8218594 100644
--- a/app/pkg/web/renderer_test.go
+++ b/app/pkg/web/renderer_test.go
@@ -17,6 +17,7 @@ import (
. "github.com/getfider/fider/app/pkg/assert"
"github.com/getfider/fider/app/pkg/bus"
"github.com/getfider/fider/app/pkg/env"
+ "github.com/getfider/fider/app/pkg/markdown"
"github.com/getfider/fider/app/pkg/web"
)
@@ -108,6 +109,26 @@ func TestRenderer_Home(t *testing.T) {
compareRendererResponse(buf, "/app/pkg/web/testdata/home.html", ctx)
}
+func TestRenderer_DescriptionIsEscapedOnce(t *testing.T) {
+ RegisterT(t)
+
+ bus.AddHandler(func(ctx context.Context, q *query.ListActiveOAuthProviders) error {
+ return nil
+ })
+
+ buf := new(bytes.Buffer)
+ ctx := newGetContext("https://demo.test.fider.io:3000/", nil)
+ renderer := web.NewRenderer()
+ renderer.Render(buf, http.StatusOK, web.Props{
+ Title: "My Page Title",
+ Description: markdown.PlainText("**Tom & Jerry** `<details>`") + ` "><script>alert(1)</script>`,
+ }, ctx)
+
+ expected := `content="Tom &amp; Jerry &lt;details&gt; &#34;&gt;&lt;script&gt;alert(1)&lt;/script&gt;"`
+ Expect(buf.String()).ContainsSubstring(`<meta name="description" ` + expected + ` />`)
+ Expect(buf.String()).ContainsSubstring(`<meta property="og:description" ` + expected + ` />`)
+}
+
func TestRenderer_Home_SSR(t *testing.T) {
RegisterT(t)
diff --git a/app/services/webhook/dummy_props.go b/app/services/webhook/dummy_props.go
index c56b123c9..7105301c8 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 parse __Markdown__ to **HTML** in post!`,
+You can use ` + "`" + `{{ markdown .post_description }}` + "`" + ` to convert __Markdown__ to plain text in post!`,
CreatedAt: time.Date(2021, time.May, 7, 18, 42, 27, 0, time.UTC),
User: &entity.User{
ID: 7,