Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

fix: prevent XSS in markdown rendering and ATOM feed output

Matt Robertsd28a838db7c9
diff --git a/app/handlers/feed.go b/app/handlers/feed.go
index 3285be990..3e96cdfbc 100644
--- a/app/handlers/feed.go
+++ b/app/handlers/feed.go
@@ -3,7 +3,6 @@ package handlers
import (
"encoding/xml"
"fmt"
- "html"
"net/http"
"strconv"
"strings"
@@ -325,7 +324,7 @@ func CommentFeed() web.HandlerFunc {
}
return formatTime(*comment.EditedAt)
}(),
- Content: &Content{Type: "html", Body: string(markdown.Full(html.UnescapeString(comment.Content), true))},
+ Content: &Content{Type: "html", Body: string(markdown.Full(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.go
index 1c30082f1..f902cf0fe 100644
--- a/app/handlers/feed_test.go
+++ b/app/handlers/feed_test.go
@@ -5,6 +5,7 @@ import (
"github.com/getfider/fider/app/pkg/env"
"net/http"
"os"
+ "strings"
"testing"
"time"
@@ -183,6 +184,59 @@ func TestFeedDisabled(t *testing.T) {
Expect(code).Equals(http.StatusNotFound)
}
+func TestCommentFeedHandler_HTMLEscaped(t *testing.T) {
+ RegisterT(t)
+
+ post := &entity.Post{
+ ID: 1,
+ Number: 1,
+ Title: "The Post",
+ Slug: "the-post",
+ Description: "Description of the post",
+ CreatedAt: time.Date(2023, 1, 1, 10, 0, 0, 0, time.UTC),
+ User: &entity.User{ID: 1, Name: "Jon Snow"},
+ Status: enum.PostOpen,
+ }
+
+ xssComment := &entity.Comment{
+ ID: 1,
+ Content: `<script>alert("XSS")</script>`,
+ 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{xssComment}
+ 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()
+ // The <script> tag must NOT appear unescaped in the feed output
+ Expect(strings.Contains(body, "<script>")).IsFalse()
+ Expect(strings.Contains(body, "alert(")).IsFalse()
+}
+
func TestPrivacyEnabled(t *testing.T) {
RegisterT(t)
diff --git a/public/services/markdown.spec.ts b/public/services/markdown.spec.ts
index c5d9b0169..ac8e3df59 100644
--- a/public/services/markdown.spec.ts
+++ b/public/services/markdown.spec.ts
@@ -25,11 +25,11 @@ const testCases = [
expectedSimple: "<p>Hello World</p>",
expectedPlainText: "Hello World",
},
- // {
- // input: "Hello <b>Beautiful</b> World",
- // expectedFull: "<p>Hello &lt;b&gt;Beautiful&lt;/b&gt; World</p>",
- // expectedPlainText: "Hello &lt;b&gt;Beautiful&lt;/b&gt; World",
- // },
+ {
+ input: "Hello <b>Beautiful</b> World",
+ expectedFull: "<p>Hello &lt;b&gt;Beautiful&lt;/b&gt; World</p>",
+ expectedPlainText: "Hello Beautiful World",
+ },
{
input: `[Uh oh...]("onerror="alert('XSS'))`,
expectedFull: '<p><a class="text-link" href="" rel="noopener nofollow" target="_blank">Uh oh...</a></p>',
@@ -86,3 +86,30 @@ testCases.forEach((x) => {
expect(result).toEqual(x.expectedPlainText)
})
})
+
+describe("XSS prevention", () => {
+ const xssInputs = [
+ '<script>alert(1)</script>',
+ '<img src=x onerror=alert(1)>',
+ '<svg onload=alert(1)>',
+ '<a href="javascript:alert(1)">click</a>',
+ ]
+
+ xssInputs.forEach((input) => {
+ test(`full mode neutralizes: ${input}`, () => {
+ const result = markdown.full(input)
+ expect(result).not.toMatch(/<script/i)
+ expect(result).not.toMatch(/onerror/i)
+ expect(result).not.toMatch(/onload/i)
+ expect(result).not.toMatch(/javascript:/i)
+ })
+
+ test(`plainText mode neutralizes: ${input}`, () => {
+ const result = markdown.plainText(input)
+ expect(result).not.toMatch(/<script/i)
+ expect(result).not.toMatch(/onerror/i)
+ expect(result).not.toMatch(/onload/i)
+ expect(result).not.toMatch(/javascript:/i)
+ })
+ })
+})
diff --git a/public/services/markdown.ts b/public/services/markdown.ts
index bfb6bd181..9120ffd18 100644
--- a/public/services/markdown.ts
+++ b/public/services/markdown.ts
@@ -72,12 +72,13 @@ plainTextRenderer.html = (html) => html
plainTextRenderer.del = (text) => text
const entities: { [key: string]: string } = {
- // "<": "&lt;",
- // ">": "&gt;",
+ "<": "&lt;",
+ ">": "&gt;",
}
const encodeHTML = (s: string) => s.replace(/[<>]/g, (tag) => entities[tag] || tag)
-const sanitize = (input: string) => (DOMPurify.isSupported ? DOMPurify.sanitize(input) : input)
+const stripTags = (input: string) => input.replace(/<[^>]*>/g, "")
+const sanitize = (input: string) => (DOMPurify.isSupported ? DOMPurify.sanitize(input) : stripTags(input))
// Helper function to decode HTML entities back to readable characters
const decodeHtmlEntities = (text: string): string => {
return text.replace(/&[#\w]+;/g, (entity) => {