|
1 | 1 | package sanitize |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "sync" |
| 5 | + |
4 | 6 | "github.com/microcosm-cc/bluemonday" |
5 | 7 | ) |
6 | 8 |
|
7 | 9 | var policy *bluemonday.Policy |
| 10 | +var policyOnce sync.Once |
8 | 11 |
|
9 | 12 | func Sanitize(input string) string { |
10 | 13 | return FilterHTMLTags(FilterInvisibleCharacters(input)) |
@@ -41,14 +44,30 @@ func FilterHTMLTags(input string) string { |
41 | 44 | } |
42 | 45 |
|
43 | 46 | func policyInit() { |
44 | | - if policy != nil { |
45 | | - return |
46 | | - } |
47 | | - policy = bluemonday.StrictPolicy() |
48 | | - policy.AllowElements("b", "blockquote", "br", "code", "em", "h1", "h2", "h3", "h4", "h5", "h6", "hr", "i", "li", "ol", "p", "pre", "strong", "sub", "sup", "table", "tbody", "td", "th", "thead", "tr", "ul") |
49 | | - policy.AllowAttrs("img", "a") |
50 | | - policy.AllowURLSchemes("https") |
51 | | - policy.AllowImages() |
| 47 | + policyOnce.Do(func() { |
| 48 | + p := bluemonday.StrictPolicy() |
| 49 | + |
| 50 | + p.AllowElements( |
| 51 | + "b", "blockquote", "br", "code", "em", |
| 52 | + "h1", "h2", "h3", "h4", "h5", "h6", |
| 53 | + "hr", "i", "li", "ol", "p", "pre", |
| 54 | + "strong", "sub", "sup", "table", "tbody", |
| 55 | + "td", "th", "thead", "tr", "ul", |
| 56 | + "a", "img", |
| 57 | + ) |
| 58 | + |
| 59 | + p.AllowAttrs("href").OnElements("a") |
| 60 | + p.AllowURLSchemes("https") |
| 61 | + p.RequireParseableURLs(true) |
| 62 | + p.RequireNoFollowOnLinks(true) |
| 63 | + p.RequireNoReferrerOnLinks(true) |
| 64 | + p.AddTargetBlankToFullyQualifiedLinks(true) |
| 65 | + |
| 66 | + p.AllowImages() |
| 67 | + p.AllowAttrs("src", "alt", "title").OnElements("img") |
| 68 | + |
| 69 | + policy = p |
| 70 | + }) |
52 | 71 | } |
53 | 72 |
|
54 | 73 | func shouldRemoveRune(r rune) bool { |
|
0 commit comments