-
-
Notifications
You must be signed in to change notification settings - Fork 328
/
sanitize.go
191 lines (149 loc) · 6.57 KB
/
sanitize.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package text
import (
"html"
"regexp"
"strings"
"github.com/microcosm-cc/bluemonday"
)
// Regular HTML policy is an adapted version of the default
// bluemonday UGC policy, with some tweaks of our own.
// See: https://github.com/microcosm-cc/bluemonday#usage
var regular *bluemonday.Policy = func() *bluemonday.Policy {
p := bluemonday.NewPolicy()
// AllowStandardAttributes will enable "id", "title" and
// the language specific attributes "dir" and "lang" on
// all elements that are allowed
p.AllowStandardAttributes()
/*
LAYOUT AND FORMATTING
*/
// "aside" is permitted and takes no attributes.
// See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/aside
p.AllowElements("article", "aside")
// "details" is permitted, including the "open" attribute
// which can either be blank or the value "open".
// See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/details
p.AllowAttrs("open").Matching(regexp.MustCompile(`(?i)^(|open)$`)).OnElements("details")
// "section" is permitted and takes no attributes.
// See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/section
p.AllowElements("section")
// "summary" is permitted and takes no attributes.
// See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/summary
p.AllowElements("summary")
// "h1" through "h6" are permitted and take no attributes.
p.AllowElements("h1", "h2", "h3", "h4", "h5", "h6")
// "hgroup" is permitted and takes no attributes.
// See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/hgroup
p.AllowElements("hgroup")
// "blockquote" is permitted, including the "cite"
// attribute which must be a standard URL.
p.AllowAttrs("cite").OnElements("blockquote")
// "br" "div" "hr" "p" "span" "wbr" are permitted and take no attributes
p.AllowElements("br", "div", "hr", "p", "span", "wbr")
// The following are all inline phrasing elements:
p.AllowElements("abbr", "acronym", "cite", "code", "dfn", "em",
"figcaption", "mark", "s", "samp", "strong", "sub", "sup", "var")
// "q" is permitted and "cite" is a URL and handled by URL policies
p.AllowAttrs("cite").OnElements("q")
// "time" is permitted
p.AllowAttrs("datetime").Matching(bluemonday.ISO8601).OnElements("time")
// Block and inline elements that impart no
// semantic meaning but style the document.
// Underlines, italics, bold, strikethrough etc.
p.AllowElements("b", "i", "pre", "small", "strike", "tt", "u")
// "del" "ins" are permitted
p.AllowAttrs("cite").Matching(bluemonday.Paragraph).OnElements("del", "ins")
p.AllowAttrs("datetime").Matching(bluemonday.ISO8601).OnElements("del", "ins")
// Enable ordered, unordered, and definition lists.
p.AllowLists()
// Class needed on span for mentions, which look like this when assembled:
// `<span class="h-card"><a href="https://example.org/users/targetAccount" class="u-url mention">@<span>someusername</span></a></span>`
p.AllowAttrs("class").OnElements("span")
/*
LANGUAGE FORMATTING
*/
// "bdi" "bdo" are permitted on "dir".
// See: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/dir
p.AllowAttrs("dir").Matching(bluemonday.Direction).OnElements("bdi", "bdo")
// "rp" "rt" "ruby" are permitted. See:
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/rp
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/rt
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ruby
p.AllowElements("rp", "rt", "ruby")
/*
CODE BLOCKS
*/
// Permit language tags for code elements.
p.AllowAttrs("class").Matching(regexp.MustCompile("^language-[a-zA-Z0-9]+$")).OnElements("code")
// Don't sanitize HTML inside code blocks.
p.SkipElementsContent("code", "pre")
/*
LINKS AND LINK SAFETY.
*/
// Permit hyperlinks.
p.AllowAttrs("class", "href", "rel").OnElements("a")
// URLs must be parseable by net/url.Parse().
p.RequireParseableURLs(true)
// Most common URL schemes only.
p.AllowURLSchemes("mailto", "http", "https")
// Force rel="noreferrer".
// See: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/noreferrer
p.RequireNoReferrerOnLinks(true)
// Add rel="nofollow" on all fully qualified (not relative) links.
// See: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel#nofollow
p.RequireNoFollowOnFullyQualifiedLinks(true)
// Force crossorigin="anonymous"
// See: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin#anonymous
p.RequireCrossOriginAnonymous(true)
// Force target="_blank".
// See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#target
p.AddTargetBlankToFullyQualifiedLinks(true)
return p
}()
// '[C]an be thought of as equivalent to stripping all HTML
// elements and their attributes as it has nothing on its allowlist.
// An example usage scenario would be blog post titles where HTML
// tags are not expected at all and if they are then the elements
// and the content of the elements should be stripped. This is a
// very strict policy.'
//
// Source: https://github.com/microcosm-cc/bluemonday#usage
var strict *bluemonday.Policy = bluemonday.StrictPolicy()
// removeHTML strictly removes *all* recognized
// HTML elements from the given string.
func removeHTML(in string) string {
return strict.Sanitize(in)
}
// SanitizeToHTML sanitizes only risky html elements
// from the given string, allowing safe ones through.
func SanitizeToHTML(in string) string {
return regular.Sanitize(in)
}
// SanitizeToPlaintext runs text through basic sanitization.
// This removes any html elements that were in the string,
// and returns clean plaintext.
func SanitizeToPlaintext(in string) string {
// Unescape first to catch any tricky critters.
content := html.UnescapeString(in)
// Remove all detected HTML.
content = removeHTML(content)
// Unescape again to return plaintext.
content = html.UnescapeString(content)
return strings.TrimSpace(content)
}