Open platform to collect and prioritize feedback
Sanitize backend markdown output and filter URL schemes
Matt Roberts8262cb374524
diff --git a/app/handlers/apiv1/invite.go b/app/handlers/apiv1/invite.goindex 5b3d2ab7d..752903c28 100644--- a/app/handlers/apiv1/invite.go+++ b/app/handlers/apiv1/invite.go@@ -26,7 +26,7 @@ func SendSampleInvite() web.HandlerFunc {action.Message = strings.ReplaceAll(action.Message, app.InvitePlaceholder, "*[the link to join will be here]*")to := dto.NewRecipient(c.User().Name, c.User().Email, dto.Props{"subject": action.Subject,- "message": markdown.Full(action.Message, true),+ "message": markdown.Full(c, action.Message, true),})bus.Publish(c, &cmd.SendMail{diff --git a/app/handlers/feed.go b/app/handlers/feed.goindex 3e96cdfbc..59a477193 100644--- a/app/handlers/feed.go+++ b/app/handlers/feed.go@@ -119,7 +119,7 @@ func generatePostContent(c *web.Context, post *entity.Post, options *generatorOp})}- return string(markdown.Full(title+post.Description+footer, true))+ return string(markdown.Full(c, title+post.Description+footer, true))}func appendTags(c *web.Context, categories []*Category, post *entity.Post) ([]*Category, error) {@@ -158,7 +158,7 @@ func GlobalFeed() web.HandlerFunc {feed := &AtomFeed{Title: c.Tenant().Name,- Subtitle: Content{Body: string(markdown.Full(c.Tenant().WelcomeMessage, true)), Type: "html"},+ Subtitle: Content{Body: string(markdown.Full(c, c.Tenant().WelcomeMessage, true)), Type: "html"},Id: web.BaseURL(c),Link: []Link{{Href: fmt.Sprintf("%s/feed/global.atom", web.BaseURL(c)), Type: "application/atom+xml", Rel: "self"},@@ -256,7 +256,7 @@ func CommentFeed() web.HandlerFunc {feed := &AtomFeed{Title: post.Title,- Subtitle: Content{Body: string(markdown.Full(post.Description, true)), Type: "html"},+ Subtitle: Content{Body: string(markdown.Full(c, post.Description, true)), Type: "html"},Author: &Author{Name: authorName},Id: fmt.Sprintf("%s/posts/%d/#comments", web.BaseURL(c), post.Number),Link: []Link{@@ -298,7 +298,7 @@ func CommentFeed() web.HandlerFunc {Link: []Link{{Href: fmt.Sprintf("%s/posts/%d", web.BaseURL(c), post.Number), Type: "text/html", Rel: "alternate"},},- Content: &Content{Type: "html", Body: string(markdown.Full(post.Response.Text, true))},+ Content: &Content{Type: "html", Body: string(markdown.Full(c, post.Response.Text, true))},Categories: []*Category{{Term: i18n.T(c, "enum.poststatus."+post.Status.Name())}},})}@@ -324,7 +324,7 @@ func CommentFeed() web.HandlerFunc {}return formatTime(*comment.EditedAt)}(),- Content: &Content{Type: "html", Body: string(markdown.Full(comment.Content, true))},+ Content: &Content{Type: "html", Body: string(markdown.Full(c, comment.Content, true))},Id: fmt.Sprintf("%s/posts/%d/#comment-%d", web.BaseURL(c), post.Number, comment.ID),Link: []Link{{Href: fmt.Sprintf("%s/posts/%d/#comment-%d", web.BaseURL(c), post.Number, comment.ID), Type: "text/html", Rel: "alternate"}},})diff --git a/app/handlers/feed_test.go b/app/handlers/feed_test.goindex b74035fec..d97af50fd 100644--- a/app/handlers/feed_test.go+++ b/app/handlers/feed_test.go@@ -265,3 +265,59 @@ func TestPrivacyEnabled(t *testing.T) {Expect(code).Equals(http.StatusNotFound)}++func TestCommentFeedHandler_UnsafeSchemesFiltered(t *testing.T) {+ RegisterT(t)++ post := &entity.Post{+ ID: 1,+ Number: 1,+ Title: "The Post",+ Slug: "the-post",+ Description: "[desc](javascript:alert(1))",+ CreatedAt: time.Date(2023, 1, 1, 10, 0, 0, 0, time.UTC),+ User: &entity.User{ID: 1, Name: "Jon Snow"},+ Status: enum.PostOpen,+ }++ // Atom content is declared type="html", so whatever the renderer produces is handed+ // to the reader's HTML renderer. Unsafe destinations must not survive that far.+ unsafeComment := &entity.Comment{+ ID: 1,+ Content: "[click](javascript:alert(1)) ) ```a\"><img\fsrc=x\fonerror=alert(1)>\ncode\n```",+ CreatedAt: time.Date(2023, 1, 2, 10, 0, 0, 0, time.UTC),+ User: &entity.User{ID: 2, Name: "Arya Stark"},+ }++ bus.AddHandler(func(ctx context.Context, q *query.GetPostByNumber) error {+ if q.Number == post.Number {+ q.Result = post+ }+ return nil+ })++ bus.AddHandler(func(ctx context.Context, q *query.GetCommentsByPost) error {+ q.Result = []*entity.Comment{unsafeComment}+ return nil+ })++ bus.AddHandler(func(ctx context.Context, q *query.GetAssignedTags) error {+ q.Result = []*entity.Tag{}+ return nil+ })++ server := mock.NewServer()+ code, response := server.+ OnTenant(mock.DemoTenant).+ AsUser(mock.JonSnow).+ AddParam("path", "1.atom").+ Execute(handlers.CommentFeed())++ Expect(code).Equals(http.StatusOK)++ body := response.Body.String()+ Expect(strings.Contains(body, "javascript:")).IsFalse()+ Expect(strings.Contains(body, "onerror")).IsFalse()+ // The link and comment text itself is still delivered.+ Expect(strings.Contains(body, "click")).IsTrue()+}diff --git a/app/handlers/testdata/comment_feed.atom b/app/handlers/testdata/comment_feed.atomindex 10cca8617..c38772cad 100755--- a/app/handlers/testdata/comment_feed.atom+++ b/app/handlers/testdata/comment_feed.atom@@ -1,2 +1,2 @@<?xml version="1.0" encoding="utf-8"?>-<feed xmlns="http://www.w3.org/2005/Atom"><title>The Post</title><subtitle type="html"><p>Description of the post</p></subtitle><id>http:///posts/1/#comments</id><updated>2023-01-03T10:00:00+00:00</updated><link rel="self" href="http:///feed/posts/1.atom" type="application/atom+xml"></link><link rel="alternate" href="http:///posts/1" type="text/html"></link><author><name>Jon Snow</name></author><entry><title>Original Post by Jon Snow</title><id>http:///posts/1</id><published>2023-01-01T10:00:00+00:00</published><updated>2023-01-01T10:00:00+00:00</updated><link rel="alternate" href="http:///posts/1" type="text/html"></link><author><name>Jon Snow</name></author><content type="html"><h1>The Post</h1>

<p>0 votes, 0 comments</p>

<hr />

<p>Description of the post</p></content><category term="Open"></category></entry><entry><title>Comment by Arya Stark</title><id>http:///posts/1/#comment-1</id><published>2023-01-02T10:00:00+00:00</published><updated>2023-01-02T10:00:00+00:00</updated><link rel="alternate" href="http:///posts/1/#comment-1" type="text/html"></link><author><name>Arya Stark</name></author><content type="html"><p>This is comment 1</p></content></entry><entry><title>Comment by Sansa Stark</title><id>http:///posts/1/#comment-2</id><published>2023-01-03T10:00:00+00:00</published><updated>2023-01-03T10:00:00+00:00</updated><link rel="alternate" href="http:///posts/1/#comment-2" type="text/html"></link><author><name>Sansa Stark</name></author><content type="html"><p>This is comment 2</p></content></entry></feed>\ No newline at end of file+<feed xmlns="http://www.w3.org/2005/Atom"><title>The Post</title><subtitle type="html"><p>Description of the post</p></subtitle><id>http:///posts/1/#comments</id><updated>2023-01-03T10:00:00+00:00</updated><link rel="self" href="http:///feed/posts/1.atom" type="application/atom+xml"></link><link rel="alternate" href="http:///posts/1" type="text/html"></link><author><name>Jon Snow</name></author><entry><title>Original Post by Jon Snow</title><id>http:///posts/1</id><published>2023-01-01T10:00:00+00:00</published><updated>2023-01-01T10:00:00+00:00</updated><link rel="alternate" href="http:///posts/1" type="text/html"></link><author><name>Jon Snow</name></author><content type="html"><h1>The Post</h1>

<p>0 votes, 0 comments</p>

<hr/>

<p>Description of the post</p></content><category term="Open"></category></entry><entry><title>Comment by Arya Stark</title><id>http:///posts/1/#comment-1</id><published>2023-01-02T10:00:00+00:00</published><updated>2023-01-02T10:00:00+00:00</updated><link rel="alternate" href="http:///posts/1/#comment-1" type="text/html"></link><author><name>Arya Stark</name></author><content type="html"><p>This is comment 1</p></content></entry><entry><title>Comment by Sansa Stark</title><id>http:///posts/1/#comment-2</id><published>2023-01-03T10:00:00+00:00</published><updated>2023-01-03T10:00:00+00:00</updated><link rel="alternate" href="http:///posts/1/#comment-2" type="text/html"></link><author><name>Sansa Stark</name></author><content type="html"><p>This is comment 2</p></content></entry></feed>\ No newline at end of filediff --git a/app/handlers/testdata/global_feed.atom b/app/handlers/testdata/global_feed.atomindex 735b2dbad..fba0592fe 100755--- a/app/handlers/testdata/global_feed.atom+++ b/app/handlers/testdata/global_feed.atom@@ -1,2 +1,2 @@<?xml version="1.0" encoding="utf-8"?>-<feed xmlns="http://www.w3.org/2005/Atom"><title>Demonstration</title><subtitle type="html"></subtitle><id>http://</id><updated>2023-01-03T10:00:00+00:00</updated><link rel="self" href="http:///feed/global.atom" type="application/atom+xml"></link><link rel="alternate" href="http://" type="text/html"></link><entry><title>(5 Votes) First Post</title><id>http:///posts/1</id><published>2023-01-01T10:00:00+00:00</published><updated>2023-01-01T10:00:00+00:00</updated><link rel="self" href="http:///feed/posts/1.atom" type="application/atom+xml"></link><link rel="alternate" href="http:///posts/1" type="text/html"></link><author><name>Jon Snow</name></author><content type="html"><p>Description of first post</p>

<hr />

<p>5 votes, 0 comments - view <a href="http:///posts/1" rel="nofollow noreferrer">in the web</a> or <a href="http:///feed/posts/1.atom" rel="nofollow noreferrer">as a feed</a></p></content><category term="Open"></category></entry><entry><title>(2 Votes) Second Post</title><id>http:///posts/2</id><published>2023-01-03T10:00:00+00:00</published><updated>2023-01-03T10:00:00+00:00</updated><link rel="self" href="http:///feed/posts/2.atom" type="application/atom+xml"></link><link rel="alternate" href="http:///posts/2" type="text/html"></link><author><name>Arya Stark</name></author><content type="html"><p>Description of second post</p>

<hr />

<p>2 votes, 0 comments - view <a href="http:///posts/2" rel="nofollow noreferrer">in the web</a> or <a href="http:///feed/posts/2.atom" rel="nofollow noreferrer">as a feed</a></p></content><category term="Open"></category></entry></feed>\ No newline at end of file+<feed xmlns="http://www.w3.org/2005/Atom"><title>Demonstration</title><subtitle type="html"></subtitle><id>http://</id><updated>2023-01-03T10:00:00+00:00</updated><link rel="self" href="http:///feed/global.atom" type="application/atom+xml"></link><link rel="alternate" href="http://" type="text/html"></link><entry><title>(5 Votes) First Post</title><id>http:///posts/1</id><published>2023-01-01T10:00:00+00:00</published><updated>2023-01-01T10:00:00+00:00</updated><link rel="self" href="http:///feed/posts/1.atom" type="application/atom+xml"></link><link rel="alternate" href="http:///posts/1" type="text/html"></link><author><name>Jon Snow</name></author><content type="html"><p>Description of first post</p>

<hr/>

<p>5 votes, 0 comments - view <a href="http:///posts/1" rel="nofollow noreferrer">in the web</a> or <a href="http:///feed/posts/1.atom" rel="nofollow noreferrer">as a feed</a></p></content><category term="Open"></category></entry><entry><title>(2 Votes) Second Post</title><id>http:///posts/2</id><published>2023-01-03T10:00:00+00:00</published><updated>2023-01-03T10:00:00+00:00</updated><link rel="self" href="http:///feed/posts/2.atom" type="application/atom+xml"></link><link rel="alternate" href="http:///posts/2" type="text/html"></link><author><name>Arya Stark</name></author><content type="html"><p>Description of second post</p>

<hr/>

<p>2 votes, 0 comments - view <a href="http:///posts/2" rel="nofollow noreferrer">in the web</a> or <a href="http:///feed/posts/2.atom" rel="nofollow noreferrer">as a feed</a></p></content><category term="Open"></category></entry></feed>\ No newline at end of filediff --git a/app/pkg/markdown/markdown.go b/app/pkg/markdown/markdown.goindex 3463b0213..0b83325f1 100644--- a/app/pkg/markdown/markdown.go+++ b/app/pkg/markdown/markdown.go@@ -1,6 +1,7 @@package markdownimport (+ "context""html/template""io""strings"@@ -31,7 +32,7 @@ var htmlFlags = 0 |htmlrenderer.NofollowLinks |htmlrenderer.NoreferrerLinks-func createRenderer(handleImages bool) *htmlrenderer.Renderer {+func createRenderer(handleImages bool, pol *policy) *htmlrenderer.Renderer {return htmlrenderer.NewRenderer(htmlrenderer.RendererOptions{Flags: htmlFlags,RenderNodeHook: func(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {@@ -44,9 +45,28 @@ func createRenderer(handleImages bool) *htmlrenderer.Renderer {htmlrenderer.EscapeHTML(w, node.Literal)_, _ = io.WriteString(w, "\n")return ast.GoToNext, true+ case *ast.CodeBlock:+ // The renderer interpolates the fenced-code info string straight into+ // class="language-..." without escaping it, and the parser accepts+ // arbitrary bytes there. An info string containing a quote can therefore+ // close the attribute and forge sibling attributes and elements. Reduce+ // the info string to a plain language name before the renderer sees it.+ node.Info = safeCodeBlockInfo(node.Info)+ case *ast.Link:+ // The renderer has no scheme filtering of its own unless the Safelink flag+ // is set, and that flag cannot be used here: it routes through+ // parser.IsSafeURL, which slices the destination before checking its+ // length and so panics on an empty link (see issue #1233). Gate the+ // destination ourselves instead. Writing nothing for the anchor while+ // still walking into the children drops the tags but keeps the link text.+ if !pol.allowsDestination(node.Destination) {+ return ast.GoToNext, true+ }case *ast.Image:- if !handleImages {- // Skip images entirely when handleImages is false+ // Skip images entirely when handleImages is false, or when the+ // destination is not renderable. Note that Safelink would not have+ // covered images in any case: imageEnter never consults it.+ if !handleImages || !pol.allowsDestination(node.Destination) {return ast.SkipChildren, true}}@@ -55,14 +75,25 @@ func createRenderer(handleImages bool) *htmlrenderer.Renderer {})}-// Full turns a markdown into HTML using all rules-func Full(input string, handleImages bool) template.HTML {+// Full turns a markdown into HTML using all rules.+//+// The context carries the tenant, whose AllowedSchemes setting extends the set of link+// destinations that may be rendered. A context without a tenant falls back to the baseline+// schemes, so forgetting to pass one fails closed.+func Full(ctx context.Context, input string, handleImages bool) template.HTML {+ pol := policyFor(ctx)+// Apparently a parser cannot be reused.// https://github.com/gomarkdown/markdown/issues/229parser := mdparser.NewWithExtensions(mdExtns)- renderer := createRenderer(handleImages)+ renderer := createRenderer(handleImages, pol)output := markdown.ToHTML([]byte(input), parser, renderer)- return template.HTML(strings.TrimSpace(string(output)))++ // Sanitize the rendered HTML as well as gating destinations above. The renderer can+ // forge markup on its own: it interpolates the fenced-code info string into+ // class="language-..." without escaping it, so the output is not trustworthy simply+ // because the input was parsed as markdown.+ return template.HTML(strings.TrimSpace(pol.html.Sanitize(string(output))))}diff --git a/app/pkg/markdown/markdown_test.go b/app/pkg/markdown/markdown_test.goindex 0ff764812..aa9d7ed1a 100644--- a/app/pkg/markdown/markdown_test.go+++ b/app/pkg/markdown/markdown_test.go@@ -1,6 +1,7 @@package markdown_testimport (+ "context""html/template""testing"@@ -14,20 +15,20 @@ func TestFullMarkdown(t *testing.T) {for input, expected := range map[string]string{"# Hello World": `<h1>Hello World</h1>`,"Hello <b>Beautiful</b> World": `<p>Hello <b>Beautiful</b> World</p>`,- "": `<p><img src="http://example.com/hello.jpg" alt="" /></p>`,+ "": `<p><img src="http://example.com/hello.jpg" alt=""/></p>`,"Go to http://example.com/hello.jpg": `<p>Go to <a href="http://example.com/hello.jpg" rel="nofollow noreferrer">http://example.com/hello.jpg</a></p>`,`-123-456--789`: `<p>-123<br />--456<br />+-789`: `<p>-123<br/>+-456<br/>-789</p>`,`- **Option 1**- *Option 2*- ~~Option 3~~`: `<ul>-<li><strong>Option 1</strong><br />+<li><strong>Option 1</strong><br/></li>-<li><em>Option 2</em><br />+<li><em>Option 2</em><br/></li><li><del>Option 3</del></li></ul>`,@@ -37,15 +38,15 @@ func TestFullMarkdown(t *testing.T) {– READ_PHONE_STATEThis will allow to send and receive SMS and get the IMEI No. in our app.-Thanks!`: `<p>Please add:<br />-– SEND_SMS<br />-– RECEIVE_SMS<br />-– READ_PHONE_STATE<br />+Thanks!`: `<p>Please add:<br/>+– SEND_SMS<br/>+– RECEIVE_SMS<br/>+– READ_PHONE_STATE<br/>This will allow to send and receive SMS and get the IMEI No. in our app.</p><p>Thanks!</p>`,} {- output := markdown.Full(input, true)+ output := markdown.Full(context.Background(), input, true)Expect(output).Equals(template.HTML(expected))}}@@ -62,7 +63,7 @@ func TestFullMarkdownWithoutImages(t *testing.T) {"Hello **bold** text with ": `<p>Hello <strong>bold</strong> text with </p>`,"Go to http://example.com/hello.jpg": `<p>Go to <a href="http://example.com/hello.jpg" rel="nofollow noreferrer">http://example.com/hello.jpg</a></p>`,} {- output := markdown.Full(input, false)+ output := markdown.Full(context.Background(), input, false)Expect(output).Equals(template.HTML(expected))}}diff --git a/app/pkg/markdown/policy.go b/app/pkg/markdown/policy.gonew file mode 100644index 000000000..ddf6d835f--- /dev/null+++ b/app/pkg/markdown/policy.go@@ -0,0 +1,246 @@+package markdown++import (+ "bytes"+ "context"+ stdhtml "html"+ "regexp"+ "strings"+ "sync"++ "github.com/getfider/fider/app"+ "github.com/getfider/fider/app/models/dto"+ "github.com/getfider/fider/app/models/entity"+ "github.com/getfider/fider/app/pkg/env"+ "github.com/getfider/fider/app/pkg/log"+ "github.com/microcosm-cc/bluemonday"+)++// baselineURLPattern is the set of link and image destinations we accept without any+// tenant configuration. It mirrors the schemes DOMPurify permits in the browser (which is+// what public/services/markdown.ts relies on), plus relative references, with one+// hardening: no ASCII control characters or spaces anywhere in the value, so that+// "java\tscript:alert(1)" cannot slip through the relative-reference branch.+//+// The relative branch is expressed positively - "no ':' before the first '/', '?' or '#'" -+// rather than as a negated scheme match, so a scheme we have not thought about is rejected+// rather than accepted.+const baselineURLPattern = `(?:` ++ `(?i:https?|mailto|ftps?|tel|sms|callto|cid|xmpp):[^\x00-\x20\x7f]*` ++ `|[^:/?#\x00-\x20\x7f]*(?:[/?#][^\x00-\x20\x7f]*)?` ++ `)`++// blockedSchemes are never renderable, whatever the tenant has configured. This mirrors+// the unconditional /^javascript/i block in public/services/markdown.ts, extended to the+// rest of the script-capable and local-resource family.+var blockedSchemes = map[string]struct{}{+ "javascript": {},+ "data": {},+ "vbscript": {},+ "livescript": {},+ "mocha": {},+ "view-source": {},+ "file": {},+ "blob": {},+ "jar": {},+ "about": {},+}++// dangerousProbes keep the tenant allow list purely additive. bluemonday can only+// allow-on-match - it has no deny list - so before folding an admin-supplied pattern into+// the policy we check it cannot accept anything from this set. A tenant administrator+// cannot re-open the hole by configuring "^javascript".+var dangerousProbes = []string{+ "javascript:alert(1)",+ "JaVaScRiPt:alert(1)",+ "data:text/html,<script>alert(1)</script>",+ "DATA:text/html;base64,AAAA",+ "vbscript:msgbox(1)",+ "livescript:x",+ "view-source:https://example.com/",+ "file:///etc/passwd",+}++var schemeRegex = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9+.\-]*:`)++// policy decides which destinations may be rendered, and sanitizes the final HTML.+type policy struct {+ allowURL *regexp.Regexp+ html *bluemonday.Policy+}++// policyCache is keyed by the raw AllowedSchemes string, so it is bounded by the number of+// distinct tenant configurations rather than by the number of tenants - in practice almost+// every entry is the empty string.+var policyCache sync.Map++func policyFor(ctx context.Context) *policy {+ allowedSchemes := allowedSchemesFrom(ctx)+ if cached, ok := policyCache.Load(allowedSchemes); ok {+ return cached.(*policy)+ }+ p := compilePolicy(ctx, allowedSchemes)+ policyCache.Store(allowedSchemes, p)+ return p+}++// allowedSchemesFrom reads the tenant's extra schemes off the context. A missing tenant+// fails closed: baseline schemes only. The ALLOW_ALLOWED_SCHEMES kill switch is honoured+// at read time as well as at write time, because+// app/services/sqlstore/postgres/tenant.go only blanks the column on save.+func allowedSchemesFrom(ctx context.Context) string {+ if ctx == nil || !env.Config.AllowAllowedSchemes {+ return ""+ }+ if tenant, ok := ctx.Value(app.TenantCtxKey).(*entity.Tenant); ok && tenant != nil {+ return tenant.AllowedSchemes+ }+ return ""+}++func compilePolicy(ctx context.Context, allowedSchemes string) *policy {+ alternatives := []string{baselineURLPattern}++ for _, line := range strings.Split(allowedSchemes, "\n") {+ pattern := strings.TrimSpace(line)+ if pattern == "" {+ continue+ }++ // The browser compiles these with the "i" flag (public/hooks/use-fider.ts), so match that.+ compiled, err := regexp.Compile(`(?i:` + pattern + `)`)+ if err != nil {+ log.Warnf(ctx, "Ignoring invalid allowed scheme pattern @{Pattern}", dto.Props{+ "Pattern": pattern,+ "Error": err.Error(),+ })+ continue+ }++ if matchesDangerousProbe(compiled) {+ log.Warnf(ctx, "Ignoring allowed scheme pattern @{Pattern} because it would permit a blocked scheme", dto.Props{+ "Pattern": pattern,+ })+ continue+ }++ // Tenant patterns are prefix patterns such as "^monero:[48]". Keep those prefix+ // semantics, but require the remainder to be free of control characters and spaces.+ alternatives = append(alternatives, `(?i:`+pattern+`)[^\x00-\x20\x7f]*`)+ }++ allowURL, err := regexp.Compile(`\A(?:` + strings.Join(alternatives, "|") + `)\z`)+ if err != nil {+ // Composition of already-compiled alternatives should not fail. Fail closed anyway.+ log.Warnf(ctx, "Falling back to baseline URL policy, could not compose allowed schemes", dto.Props{+ "Error": err.Error(),+ })+ allowURL = regexp.MustCompile(`\A(?:` + baselineURLPattern + `)\z`)+ }++ return &policy{allowURL: allowURL, html: newHTMLPolicy(allowURL)}+}++func matchesDangerousProbe(re *regexp.Regexp) bool {+ for _, probe := range dangerousProbes {+ if re.MatchString(probe) {+ return true+ }+ }+ return false+}++// allowsDestination reports whether a link or image destination may be rendered.+//+// The destination is normalized the way a browser would before deciding: the renderer's+// EscLink entity-decodes the destination on its way into the attribute, so+// "javascript:alert(1)" arrives here encoded but reaches the client decoded, and the+// ASCII whitespace and control characters that clients ignore inside a URL are stripped.+func (p *policy) allowsDestination(raw []byte) bool {+ destination := stdhtml.UnescapeString(string(raw))+ destination = strings.Map(func(r rune) rune {+ if r <= 0x20 || r == 0x7f {+ return -1+ }+ return r+ }, destination)++ if destination == "" {+ return false+ }++ if scheme := schemeRegex.FindString(destination); scheme != "" {+ name := strings.ToLower(strings.TrimSuffix(scheme, ":"))+ if _, blocked := blockedSchemes[name]; blocked {+ return false+ }+ }++ return p.allowURL.MatchString(destination)+}++// codeLangName is the only shape of fenced-code info string we let through to the+// renderer, which builds class="language-..." out of it without escaping.+var codeLangName = regexp.MustCompile(`\A[\w.+#-]+\z`)++// safeCodeBlockInfo reduces a fenced-code info string to a plain language name, or to+// nothing at all when it is not one. It mirrors the renderer's own truncation at the first+// tab or space, so what we validate is what the renderer would emit - but note that the+// renderer does not truncate at the other characters HTML treats as whitespace, such as+// form feed and carriage return, which is what makes forging a second attribute possible.+func safeCodeBlockInfo(info []byte) []byte {+ if end := bytes.IndexAny(info, "\t "); end >= 0 {+ info = info[:end]+ }+ if len(info) == 0 || !codeLangName.Match(info) {+ return nil+ }+ return info+}++var (+ // codeLangClass is the sanitizer-side counterpart of codeLangName.+ codeLangClass = regexp.MustCompile(`\Alanguage-[\w.+#-]+\z`)+ // headingClass covers the Titleblock extension's <h1 class="title">.+ headingClass = regexp.MustCompile(`\A(?:title|special|title special)\z`)+ // linkRel covers the NofollowLinks / NoreferrerLinks renderer flags.+ linkRel = regexp.MustCompile(`\A(?:nofollow|noreferrer|noopener)(?: (?:nofollow|noreferrer|noopener))*\z`)+)++// newHTMLPolicy builds a bluemonday policy that permits exactly what gomarkdown can emit+// for the extensions and flags configured in markdown.go, and nothing else. It is the+// backstop that catches markup forged by the renderer itself rather than supplied as a+// link destination.+//+// Deliberately not built on UGCPolicy: that does not allow rel on anchors (so+// rel="nofollow noreferrer" would be rewritten to rel="nofollow"), it enables+// RequireParseableURLs which round-trips every URL through url.URL.String(), and it+// constrains alt and title with bluemonday.Paragraph, silently dropping alt text+// containing a colon, ampersand or quote.+func newHTMLPolicy(allowURL *regexp.Regexp) *bluemonday.Policy {+ p := bluemonday.NewPolicy()++ p.AllowElements(+ "p", "br", "hr",+ "h1", "h2", "h3", "h4", "h5", "h6",+ "strong", "em", "del", "code", "pre", "blockquote", "tt", "sup", "sub",+ "ul", "ol", "li", "dl", "dt", "dd",+ "table", "thead", "tbody", "tfoot", "tr", "th", "td", "caption",+ )++ p.AllowAttrs("class").Matching(codeLangClass).OnElements("code")+ p.AllowAttrs("class").Matching(headingClass).OnElements("h1", "h2", "h3", "h4", "h5", "h6")++ // allowURL is anchored, so it also rejects any value containing a space or control character.+ p.AllowAttrs("href").Matching(allowURL).OnElements("a")+ p.AllowAttrs("src").Matching(allowURL).OnElements("img")++ p.AllowAttrs("rel").Matching(linkRel).OnElements("a")+ p.AllowAttrs("alt").OnElements("img")+ p.AllowAttrs("title").OnElements("a", "img")++ p.AllowAttrs("align").Matching(bluemonday.CellAlign).OnElements("th", "td")+ p.AllowAttrs("colspan").Matching(bluemonday.Integer).OnElements("th", "td")++ return p+}diff --git a/app/pkg/markdown/scheme_test.go b/app/pkg/markdown/scheme_test.gonew file mode 100644index 000000000..0f58fb467--- /dev/null+++ b/app/pkg/markdown/scheme_test.go@@ -0,0 +1,117 @@+package markdown_test++import (+ "context"+ "strings"+ "testing"++ "github.com/getfider/fider/app"+ "github.com/getfider/fider/app/models/entity"+ . "github.com/getfider/fider/app/pkg/assert"+ "github.com/getfider/fider/app/pkg/env"+ "github.com/getfider/fider/app/pkg/markdown"+)++func tenantCtx(allowedSchemes string) context.Context {+ return context.WithValue(context.Background(), app.TenantCtxKey, &entity.Tenant{AllowedSchemes: allowedSchemes})+}++func TestFullMarkdownBlocksDangerousSchemes(t *testing.T) {+ RegisterT(t)++ // An unrenderable link keeps its text and loses the anchor, which is how DOMPurify+ // behaves in the browser. An unrenderable image disappears entirely, rather than+ // leaving a broken-image icon behind in every notification email.+ for input, expected := range map[string]string{+ "[x](javascript:alert(1))": `<p>x</p>`,+ "[x](JaVaScRiPt:alert(1))": `<p>x</p>`,+ "[x](javascript:alert(1))": `<p>x</p>`,+ "[x](javascript:alert(1))": `<p>x</p>`,+ "[x](java\tscript:alert(1))": `<p>x</p>`,+ "[x](vbscript:msgbox(1))": `<p>x</p>`,+ "[x](data:text/html;base64,PHNjcmlwdD)": `<p>x</p>`,+ "[x](view-source:https://example.com)": `<p>x</p>`,+ "[x](file:///etc/passwd)": `<p>x</p>`,+ ")": `<p></p>`,+ "": `<p></p>`,++ // Safe destinations must keep working.+ "[x](http://example.com/a)": `<p><a href="http://example.com/a" rel="nofollow noreferrer">x</a></p>`,+ "[x](https://example.com/a)": `<p><a href="https://example.com/a" rel="nofollow noreferrer">x</a></p>`,+ "[x](mailto:[email protected])": `<p><a href="mailto:[email protected]" rel="nofollow noreferrer">x</a></p>`,+ "[x](/posts/1)": `<p><a href="/posts/1">x</a></p>`,+ "[x](#anchor)": `<p><a href="#anchor">x</a></p>`,++ // An empty destination is not a link. Rendering it as a bare <a href=""> is what+ // made the Safelink renderer flag unusable here (issue #1233).+ "[link without actual link]()": `<p>link without actual link</p>`,+ "[]()": `<p></p>`,+ } {+ Expect(string(markdown.Full(context.Background(), input, true))).Equals(expected)+ }+}++func TestFullMarkdownTenantAllowedSchemes(t *testing.T) {+ RegisterT(t)++ const moneroLink = "[pay](monero:4AdUndXHHZ6cfufTMvppY6JwXNouMBzSkbLYfpAV5Usx3skxNgYeYTRJ5AmD5H3F)"+ configured := tenantCtx("^monero:[48]\n^bitcoin:(1|3|bc1)")++ // A scheme the tenant allows renders in emails and feeds, exactly as it does in the+ // web UI, so the backend does not silently drop links the admin opted into.+ Expect(string(markdown.Full(configured, moneroLink, true))).ContainsSubstring(`href="monero:4AdUnd`)+ Expect(string(markdown.Full(configured, "[pay](bitcoin:bc1qxyz)", true))).ContainsSubstring(`href="bitcoin:bc1qxyz"`)++ // Without that configuration, or for a scheme outside it, the link is not rendered.+ Expect(string(markdown.Full(context.Background(), moneroLink, true))).Equals(`<p>pay</p>`)+ Expect(string(markdown.Full(configured, "[pay](litecoin:LTC1abc)", true))).Equals(`<p>pay</p>`)++ // The allow list is additive only: a pattern that would re-enable a blocked scheme is+ // discarded rather than honoured.+ Expect(string(markdown.Full(tenantCtx("^javascript:"), "[x](javascript:alert(1))", true))).Equals(`<p>x</p>`)+ Expect(string(markdown.Full(tenantCtx("^data:"), "", true))).Equals(`<p></p>`)++ // An invalid pattern is ignored, leaving the baseline intact rather than panicking.+ invalid := tenantCtx("^mon(ero:")+ Expect(string(markdown.Full(invalid, moneroLink, true))).Equals(`<p>pay</p>`)+ Expect(string(markdown.Full(invalid, "[x](https://example.com/a)", true))).ContainsSubstring(`href="https://example.com/a"`)++ // The instance-wide kill switch wins over tenant configuration.+ env.Config.AllowAllowedSchemes = false+ defer func() { env.Config.AllowAllowedSchemes = true }()+ Expect(string(markdown.Full(configured, moneroLink, true))).Equals(`<p>pay</p>`)+}++func TestFullMarkdownCodeFenceInfoString(t *testing.T) {+ RegisterT(t)++ // The renderer builds class="language-..." out of the fenced-code info string without+ // escaping it, and the parser accepts arbitrary bytes there. Note the form feed: the+ // renderer truncates the class at a tab or space but not at the other characters HTML+ // treats as whitespace, so that variant forges a real second attribute.+ for _, input := range []string{+ "```a\"><img/src=x/onerror=alert(1)>\ncode\n```",+ "```a\"><img\fsrc=x\fonerror=alert(1)>\ncode\n```",+ "```a\"><svg\fonload=alert(1)>\ncode\n```",+ "```a\"></code></pre><script>alert(1)</script>\ncode\n```",+ "```a\"><iframe\fsrc=javascript:alert(1)>\ncode\n```",+ } {+ output := string(markdown.Full(context.Background(), input, true))+ Expect(strings.Contains(output, "<img")).IsFalse()+ Expect(strings.Contains(output, "<svg")).IsFalse()+ Expect(strings.Contains(output, "<iframe")).IsFalse()+ Expect(strings.Contains(output, "<script")).IsFalse()+ Expect(strings.Contains(output, "onerror")).IsFalse()+ Expect(strings.Contains(output, "onload")).IsFalse()+ // The code itself must survive; this is a rendering fix, not a content filter.+ Expect(output).ContainsSubstring("code")+ }++ // A legitimate language is still emitted as a class.+ Expect(string(markdown.Full(context.Background(), "```go\nfmt.Println(1)\n```", true))).+ Equals("<pre><code class=\"language-go\">fmt.Println(1)\n</code></pre>")++ // As is the Titleblock extension's heading class.+ Expect(string(markdown.Full(context.Background(), "% Title\n\nbody", true))).+ ContainsSubstring(`<h1 class="title">Title</h1>`)+}diff --git a/app/pkg/markdown/text.go b/app/pkg/markdown/text.goindex 42264a605..2e264f39a 100644--- a/app/pkg/markdown/text.go+++ b/app/pkg/markdown/text.go@@ -38,7 +38,11 @@ var strictPolicy = bluemonday.StrictPolicy()// The regular expression finds duplicate newlines.var regexNewlines = regexp.MustCompile(`\n+`)-// PlainText parses given markdown input and return only the text+// PlainText parses given markdown input and return only the text.+//+// 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.func PlainText(input string) string {// Apparently a parser cannot be reused.// https://github.com/gomarkdown/markdown/issues/229diff --git a/app/pkg/tpl/funcs.go b/app/pkg/tpl/funcs.goindex 58d832400..03595e3f9 100644--- a/app/pkg/tpl/funcs.go+++ b/app/pkg/tpl/funcs.go@@ -10,7 +10,6 @@ import ("github.com/getfider/fider/app/pkg/crypto""github.com/getfider/fider/app/pkg/i18n"- "github.com/getfider/fider/app/pkg/markdown""github.com/microcosm-cc/bluemonday")@@ -36,7 +35,9 @@ var templateFunctions = map[string]any{return "This is overwritten later on..."},"markdown": func(input string) template.HTML {- return markdown.Full(input, true)+ // Markdown rendering needs the tenant from the context to resolve the+ // allowed URL schemes, so this is overwritten per render in Render.+ return ""},"dict": func(values ...any) map[string]any {if len(values)%2 != 0 {diff --git a/app/pkg/tpl/template.go b/app/pkg/tpl/template.goindex ffddaacf0..0cd5e0293 100644--- a/app/pkg/tpl/template.go+++ b/app/pkg/tpl/template.go@@ -9,6 +9,7 @@ import ("github.com/getfider/fider/app/pkg/env""github.com/getfider/fider/app/pkg/errors""github.com/getfider/fider/app/pkg/i18n"+ "github.com/getfider/fider/app/pkg/markdown")var cache = make(map[string]*template.Template)@@ -35,6 +36,9 @@ func Render(ctx context.Context, tmpl *template.Template, w io.Writer, data any)"translate": func(key string, params ...i18n.Params) string {return i18n.T(ctx, key, params...)},+ "markdown": func(input string) template.HTML {+ return markdown.Full(ctx, input, true)+ },}).Execute(w, data); err != nil {return err}diff --git a/app/tasks/delete_post.go b/app/tasks/delete_post.goindex 0bfe37db5..d1339c230 100644--- a/app/tasks/delete_post.go+++ b/app/tasks/delete_post.go@@ -79,7 +79,7 @@ func NotifyAboutDeletedPost(post *entity.Post, deleteCommentAdded bool) worker.Tprops := dto.Props{"title": post.Title,"siteName": tenant.Name,- "content": markdown.Full(post.Response.Text, true),+ "content": markdown.Full(c, post.Response.Text, true),"change": linkWithText(i18n.T(c, "email.subscription.change"), baseURL, "/settings"),"logo": logoURL,}diff --git a/app/tasks/invites.go b/app/tasks/invites.goindex ec173e55f..7b75eb688 100644--- a/app/tasks/invites.go+++ b/app/tasks/invites.go@@ -32,7 +32,7 @@ func SendInvites(subject, message string, invitations []*actions.UserInvitation)url := fmt.Sprintf("%s/invite/verify?k=%s", web.BaseURL(c), invite.VerificationKey)toMessage := strings.ReplaceAll(message, app.InvitePlaceholder, url)to[i] = dto.NewRecipient("", invite.Email, dto.Props{- "message": markdown.Full(toMessage, true),+ "message": markdown.Full(c, toMessage, true),})}diff --git a/app/tasks/invites_test.go b/app/tasks/invites_test.goindex cb0c9ed93..716bb293e 100644--- a/app/tasks/invites_test.go+++ b/app/tasks/invites_test.go@@ -69,3 +69,30 @@ func TestSendInvites(t *testing.T) {Expect(savedKeys[1].Key).Equals("5678")Expect(savedKeys[1].Request.GetEmail()).Equals("[email protected]")}++func TestSendInvites_UnsafeSchemeInMessage(t *testing.T) {+ RegisterT(t)+ bus.Init(emailmock.Service{})++ bus.AddHandler(func(ctx context.Context, c *cmd.SaveVerificationKey) error {+ return nil+ })++ worker := mock.NewWorker()+ task := tasks.SendInvites("My Subject", "[click](javascript:alert(1)) %invite%", []*actions.UserInvitation{+ {Email: "[email protected]", VerificationKey: "1234"},+ })++ err := worker.+ OnTenant(mock.DemoTenant).+ AsUser(mock.JonSnow).+ WithBaseURL("http://domain.com").+ Execute(task)++ Expect(err).IsNil()+ Expect(emailmock.MessageHistory).HasLen(1)+ // invite_email.html renders this prop raw, so the anchor must already be gone.+ Expect(emailmock.MessageHistory[0].To[0].Props["message"]).Equals(+ template.HTML(`<p>click <a href="http://domain.com/invite/verify?k=1234" rel="nofollow noreferrer">http://domain.com/invite/verify?k=1234</a></p>`),+ )+}diff --git a/app/tasks/new_comment.go b/app/tasks/new_comment.goindex e26afc0c7..489d24342 100644--- a/app/tasks/new_comment.go+++ b/app/tasks/new_comment.go@@ -298,7 +298,7 @@ func sendEmailNotifications(c *worker.Context, post *entity.Post, to []dto.Recip"messageLocaleString": messaleLocaleString,"siteName": tenant.Name,"userName": author.Name,- "content": markdown.Full(comment, false),+ "content": markdown.Full(c, comment, false),"postLink": linkWithText(fmt.Sprintf("#%d", post.Number), baseURL, "/posts/%d/%s", post.Number, post.Slug),"view": linkWithText(i18n.T(c, "email.subscription.view"), baseURL, "/posts/%d/%s", post.Number, post.Slug),"unsubscribe": linkWithText(i18n.T(c, "email.subscription.unsubscribe"), baseURL, "/posts/%d/%s", post.Number, post.Slug),diff --git a/app/tasks/new_post.go b/app/tasks/new_post.goindex 79e4e88a2..6e3aa43e2 100644--- a/app/tasks/new_post.go+++ b/app/tasks/new_post.go@@ -119,7 +119,7 @@ func NotifyAboutNewPost(post *entity.Post) worker.Task {"title": post.Title,"siteName": tenant.Name,"userName": author.Name,- "content": markdown.Full(contentString.SanitizeMentions(), false),+ "content": markdown.Full(c, contentString.SanitizeMentions(), false),"postLink": linkWithText(fmt.Sprintf("#%d", post.Number), baseURL, "/posts/%d/%s", post.Number, post.Slug),"view": linkWithText(i18n.T(c, "email.subscription.view"), baseURL, "/posts/%d/%s", post.Number, post.Slug),"change": linkWithText(i18n.T(c, "email.subscription.change"), baseURL, "/settings"),diff --git a/app/tasks/status_change.go b/app/tasks/status_change.goindex 17c7dc4f6..4b8153e0f 100644--- a/app/tasks/status_change.go+++ b/app/tasks/status_change.go@@ -72,7 +72,7 @@ func NotifyAboutStatusChange(post *entity.Post, prevStatus enum.PostStatus) work"title": post.Title,"postLink": linkWithText(fmt.Sprintf("#%d", post.Number), baseURL, "/posts/%d/%s", post.Number, post.Slug),"siteName": tenant.Name,- "content": markdown.Full(post.Response.Text, true),+ "content": markdown.Full(c, post.Response.Text, true),"status": i18n.T(c, fmt.Sprintf("enum.poststatus.%s", post.Status.Name())),"duplicate": duplicate,"view": linkWithText(i18n.T(c, "email.subscription.view"), baseURL, "/posts/%d/%s", post.Number, post.Slug),diff --git a/views/email/change_status.html b/views/email/change_status.htmlindex 55af6755d..2f76e975f 100644--- a/views/email/change_status.html+++ b/views/email/change_status.html@@ -11,6 +11,7 @@{{ end }}</p><div style="margin:0;">+ {{/* Sanitized HTML from markdown.Full. Do not render any other HTML here raw. */}}{{ .content }}</div><table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin-top:20px;">diff --git a/views/email/delete_post.html b/views/email/delete_post.htmlindex 0e7c316fe..fda258101 100644--- a/views/email/delete_post.html+++ b/views/email/delete_post.html@@ -7,6 +7,7 @@{{ translate "email.delete_post.text" (dict "title" (.title | stripHtml)) | html }}</p><div style="margin:0;">+ {{/* Sanitized HTML from markdown.Full. Do not render any other HTML here raw. */}}{{ .content }}</div><table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin-top:20px;">diff --git a/views/email/invite_email.html b/views/email/invite_email.htmlindex 433723783..490ae81ef 100644--- a/views/email/invite_email.html+++ b/views/email/invite_email.html@@ -2,6 +2,7 @@{{define "body"}}<tr>+ {{/* Sanitized HTML from markdown.Full. Do not render any other HTML here raw. */}}<td style="padding:20px 30px 30px 30px;">{{ .message }}</td></tr>{{end}}\ No newline at end of filediff --git a/views/email/new_comment.html b/views/email/new_comment.htmlindex 1c2aba39d..199d06d45 100644--- a/views/email/new_comment.html+++ b/views/email/new_comment.html@@ -7,6 +7,7 @@{{ translate .messageLocaleString (dict "userName" .userName "title" (.title | stripHtml) "postLink" .postLink) | html }}</p><div style="margin:0;">+ {{/* Sanitized HTML from markdown.Full. Do not render any other HTML here raw. */}}{{ .content }}</div><table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin-top:20px;">diff --git a/views/email/new_post.html b/views/email/new_post.htmlindex 0bca9edb8..f53d46ee9 100644--- a/views/email/new_post.html+++ b/views/email/new_post.html@@ -7,6 +7,7 @@{{ translate "email.new_post.text" (dict "userName" (.userName | stripHtml) "title" (.title | stripHtml) "postLink" .postLink) | html }}</p><div style="margin:0;">+ {{/* Sanitized HTML from markdown.Full. Do not render any other HTML here raw. */}}{{ .content }}</div><table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin-top:20px;">