Skip to content

📚 The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.

Enable html sanitizer to prevent XSS attack

Smaug67398da8daaebdd8
diff --git a/backend/controllers/nodes.controller.go b/backend/controllers/nodes.controller.go
index 9350f5db..2138cbbe 100644
--- a/backend/controllers/nodes.controller.go
+++ b/backend/controllers/nodes.controller.go
@@ -22,6 +22,7 @@ type NodeController interface {
}
func NewNodeController(app *app.App) NodeController {
+ utils.InitBluemonday()
return &Controller{
app: app,
authorizer: permissions.NewAuthorizer(app.Services.Permissions),
@@ -171,6 +172,7 @@ func (ctr *Controller) CreateNode(c *gin.Context) (int, any) {
if err != nil {
return http.StatusUnauthorized, err
}
+ escapedHTMLContent := utils.EscapeHTML(*node.ContentCompiled)
node = &models.Node{
Id: ctr.app.Snowflake.Generate(),
ParentId: node.ParentId,
@@ -188,7 +190,7 @@ func (ctr *Controller) CreateNode(c *gin.Context) (int, any) {
Display: node.Display,
Order: node.Order,
Content: node.Content,
- ContentCompiled: node.ContentCompiled,
+ ContentCompiled: &escapedHTMLContent,
CreatedTimestamp: time.Now().UnixMilli(),
UpdatedTimestamp: time.Now().UnixMilli(),
}
@@ -244,6 +246,7 @@ func (ctr *Controller) UpdateNode(c *gin.Context) (int, any) {
node.Accessibility = dbNode.Accessibility
node.Access = dbNode.Access
}
+ escapedHTMLContent := utils.EscapeHTML(*node.ContentCompiled)
node = &models.Node{
Id: nodeId,
ParentId: node.ParentId,
@@ -261,7 +264,7 @@ func (ctr *Controller) UpdateNode(c *gin.Context) (int, any) {
Display: node.Display,
Order: node.Order,
Content: node.Content,
- ContentCompiled: node.ContentCompiled,
+ ContentCompiled: &escapedHTMLContent,
Metadata: node.Metadata,
UpdatedTimestamp: time.Now().UnixMilli(),
}
diff --git a/backend/go.mod b/backend/go.mod
index 2a6dc059..953c7266 100644
--- a/backend/go.mod
+++ b/backend/go.mod
@@ -5,11 +5,14 @@ go 1.24.0
require github.com/wneessen/go-mail v0.7.2
require (
+ github.com/aymerick/douceur v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/goccy/go-yaml v1.18.0 // indirect
+ github.com/gorilla/css v1.0.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/kr/text v0.2.0 // indirect
+ github.com/microcosm-cc/bluemonday v1.0.27 // indirect
github.com/philhofer/fwd v1.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/quic-go/qpack v0.5.1 // indirect
diff --git a/backend/go.sum b/backend/go.sum
index 6478438e..2912a979 100644
--- a/backend/go.sum
+++ b/backend/go.sum
@@ -6,6 +6,8 @@ github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
+github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
+github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/bytedance/sonic v1.14.0 h1:/OfKt8HFw0kh2rj8N0F6C/qPGRESq0BbaNZgcNXXzQQ=
github.com/bytedance/sonic v1.14.0/go.mod h1:WoEbx8WTcFJfzCe0hbmyTGrfjt8PzNEBdxlNUO24NhA=
github.com/bytedance/sonic/loader v0.3.0 h1:dskwH8edlzNMctoruo8FPTJDF3vLtDT0sXZwvZJyqeA=
@@ -73,6 +75,8 @@ github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
+github.com/gorilla/css v1.0.1 h1:ntNaBIghp6JmvWnxbZKANoLyuXTPZ4cAMlo6RyhlbO8=
+github.com/gorilla/css v1.0.1/go.mod h1:BvnYkspnSzMmwRK+b8/xgNPLiIuNZr6vbZBTPQ2A3b0=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
@@ -97,6 +101,8 @@ github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
+github.com/microcosm-cc/bluemonday v1.0.27 h1:MpEUotklkwCSLeH+Qdx1VJgNqLlpY2KXwXFM08ygZfk=
+github.com/microcosm-cc/bluemonday v1.0.27/go.mod h1:jFi9vgW+H7c3V0lb6nR74Ib/DIB5OBs92Dimizgw2cA=
github.com/minio/crc64nvme v1.0.2 h1:6uO1UxGAD+kwqWWp7mBFsi5gAse66C4NXO8cmcVculg=
github.com/minio/crc64nvme v1.0.2/go.mod h1:eVfm2fAzLlxMdUGc0EEBGSMmPwmXD5XiNRpnu9J3bvg=
github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34=
diff --git a/backend/utils/escape-html.go b/backend/utils/escape-html.go
new file mode 100644
index 00000000..55544692
--- /dev/null
+++ b/backend/utils/escape-html.go
@@ -0,0 +1,51 @@
+package utils
+
+import (
+ "github.com/microcosm-cc/bluemonday"
+)
+
+var policy *bluemonday.Policy
+
+func InitBluemonday() {
+ policy = bluemonday.UGCPolicy()
+
+ // Autoriser les éléments KaTeX / MathML
+ policy.AllowElements(
+ "math", "maction", "annotation", "annotation-xml",
+ "menclose", "merror",
+ "mfenced", "mfrac", "mi", "mmultiscripts",
+ "mn", "mo", "mover", "mpadded", "mphantom",
+ "mprescripts", "mroot", "mrow", "ms", "semantics",
+ "mspace", "msqrt", "mstyle", "msub", "msup",
+ "msubsup", "mtable", "mtd", "mtext", "mtr",
+ "mtimes", "munder", "munderover",
+
+ "div", "span", "svg", "path", "g", "style", "line", "polygon", "polyline", "rect", "circle", "ellipse", "text",
+ "input", "label", "form", "button", "select", "option", "textarea", "fieldset", "legend",
+ "audio", "video",
+ )
+
+ // Autoriser les attributs nécessaires
+ policy.AllowAttrs("class", "id", "style", "tabindex", "aria-hidden", "display", "xmlns", "encoding", "accent", "width", "height", "type").Globally()
+ policy.AllowAttrs("viewBox", "xmlns", "fill-rule", "d", "x", "y", "cx", "cy", "r", "rx", "ry", "points").OnElements("svg", "path", "line", "polygon", "polyline", "rect", "circle", "ellipse", "text")
+ policy.AllowAttrs("href").OnElements("a")
+ policy.AllowAttrs("checked").OnElements("input")
+ policy.AllowDataAttributes()
+ policy.RequireNoFollowOnLinks(false)
+
+ policy.AllowNoAttrs().OnElements(
+ "math", "maction", "annotation", "annotation-xml",
+ "menclose", "merror",
+ "mfenced", "mfrac", "mi", "mmultiscripts",
+ "mn", "mo", "mover", "mpadded", "mphantom",
+ "mprescripts", "mroot", "mrow", "ms", "semantics",
+ "mspace", "msqrt", "mstyle", "msub", "msup",
+ "msubsup", "mtable", "mtd", "mtext", "mtr",
+ "mtimes", "munder", "munderover",
+ )
+}
+
+// Utilisation
+func EscapeHTML(input string) string {
+ return policy.Sanitize(input)
+}