|
@@ -13,6 +13,7 @@ import (
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
+ "github.com/microcosm-cc/bluemonday"
|
|
|
"github.com/shopspring/decimal"
|
|
|
xhtml "golang.org/x/net/html"
|
|
|
"html"
|
|
@@ -2306,8 +2307,8 @@ func isValidSrc(src string) bool {
|
|
|
return validSchemes.MatchString(src)
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-func ContentXssFilter(content string) (err error) {
|
|
|
+
|
|
|
+func ContentXssCheck(content string) (err error) {
|
|
|
|
|
|
node, err := xhtml.Parse(strings.NewReader(content))
|
|
|
if err != nil {
|
|
@@ -2358,3 +2359,213 @@ func ContentXssFilter(content string) (err error) {
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+func ContentXssFilter(content string) (cleanContent string) {
|
|
|
+ p := customXssPolicy()
|
|
|
+
|
|
|
+
|
|
|
+ cleanContent = p.Sanitize(
|
|
|
+ content,
|
|
|
+ )
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func customXssPolicy() (p *bluemonday.Policy) {
|
|
|
+ p = bluemonday.NewPolicy()
|
|
|
+ p.AllowStandardAttributes()
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ p.AllowStandardURLs()
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ p.AllowElements("article", "aside")
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ p.AllowAttrs(
|
|
|
+ "open",
|
|
|
+ ).Matching(regexp.MustCompile(`(?i)^(|open)$`)).OnElements("details")
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ p.AllowElements("figure")
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ p.AllowElements("section")
|
|
|
+
|
|
|
+
|
|
|
+ p.AllowElements("summary")
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ p.AllowElements("h1", "h2", "h3", "h4", "h5", "h6")
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ p.AllowElements("hgroup")
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ p.AllowAttrs("cite").OnElements("blockquote")
|
|
|
+
|
|
|
+
|
|
|
+ p.AllowElements("br", "div", "hr", "p", "span", "wbr")
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ p.AllowAttrs("href").OnElements("a")
|
|
|
+
|
|
|
+
|
|
|
+ p.AllowAttrs("name").Matching(
|
|
|
+ regexp.MustCompile(`^([\p{L}\p{N}_-]+)$`),
|
|
|
+ ).OnElements("map")
|
|
|
+ p.AllowAttrs("alt").Matching(bluemonday.Paragraph).OnElements("area")
|
|
|
+ p.AllowAttrs("coords").Matching(
|
|
|
+ regexp.MustCompile(`^([0-9]+,)+[0-9]+$`),
|
|
|
+ ).OnElements("area")
|
|
|
+ p.AllowAttrs("href").OnElements("area")
|
|
|
+ p.AllowAttrs("rel").Matching(bluemonday.SpaceSeparatedTokens).OnElements("area")
|
|
|
+ p.AllowAttrs("shape").Matching(
|
|
|
+ regexp.MustCompile(`(?i)^(default|circle|rect|poly)$`),
|
|
|
+ ).OnElements("area")
|
|
|
+ p.AllowAttrs("usemap").Matching(
|
|
|
+ regexp.MustCompile(`(?i)^#[\p{L}\p{N}_-]+$`),
|
|
|
+ ).OnElements("img")
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ p.AllowElements("abbr", "acronym", "cite", "code", "dfn", "em",
|
|
|
+ "figcaption", "mark", "s", "samp", "strong", "sub", "sup", "var")
|
|
|
+
|
|
|
+
|
|
|
+ p.AllowAttrs("cite").OnElements("q")
|
|
|
+
|
|
|
+
|
|
|
+ p.AllowAttrs("datetime").Matching(bluemonday.ISO8601).OnElements("time")
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ p.AllowElements("b", "i", "pre", "small", "strike", "tt", "u")
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ p.AllowAttrs("dir").Matching(bluemonday.Direction).OnElements("bdi", "bdo")
|
|
|
+
|
|
|
+
|
|
|
+ p.AllowElements("rp", "rt", "ruby")
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ p.AllowAttrs("cite").Matching(bluemonday.Paragraph).OnElements("del", "ins")
|
|
|
+ p.AllowAttrs("datetime").Matching(bluemonday.ISO8601).OnElements("del", "ins")
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ p.AllowLists()
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ p.AllowTables()
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ p.AllowAttrs(
|
|
|
+ "value",
|
|
|
+ "min",
|
|
|
+ "max",
|
|
|
+ "low",
|
|
|
+ "high",
|
|
|
+ "optimum",
|
|
|
+ ).Matching(bluemonday.Number).OnElements("meter")
|
|
|
+
|
|
|
+
|
|
|
+ p.AllowAttrs("value", "max").Matching(bluemonday.Number).OnElements("progress")
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ p.AllowImages()
|
|
|
+
|
|
|
+
|
|
|
+ p.AllowElements("iframe")
|
|
|
+ p.AllowAttrs("width").Matching(bluemonday.Number).OnElements("iframe")
|
|
|
+ p.AllowAttrs("height").Matching(bluemonday.Number).OnElements("iframe")
|
|
|
+ p.AllowAttrs("src").OnElements("iframe")
|
|
|
+ p.AllowAttrs("frameborder").Matching(bluemonday.Number).OnElements("iframe")
|
|
|
+ p.AllowAttrs("allow").Matching(regexp.MustCompile(`[a-z; -]*`)).OnElements("iframe")
|
|
|
+ p.AllowAttrs("allowfullscreen").OnElements("iframe")
|
|
|
+ return
|
|
|
+}
|