-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathsanitize.go
More file actions
283 lines (241 loc) · 6.38 KB
/
sanitize.go
File metadata and controls
283 lines (241 loc) · 6.38 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package sanitize
import (
"strings"
"sync"
"unicode"
"github.com/microcosm-cc/bluemonday"
)
var policy *bluemonday.Policy
var policyOnce sync.Once
func Sanitize(input string) string {
s := FilterInvisibleCharacters(input)
s = FilterCodeFenceMetadata(s)
s = protectCodeAngles(s)
s = FilterHTMLTags(s)
s = restoreCodeAngles(s)
return s
}
// FilterInvisibleCharacters removes invisible or control characters that should not appear
// in user-facing titles or bodies. This includes:
// - Unicode tag characters: U+E0001, U+E0020–U+E007F
// - BiDi control characters: U+202A–U+202E, U+2066–U+2069
// - Hidden modifier characters: U+200B, U+200C, U+200E, U+200F, U+00AD, U+FEFF, U+180E, U+2060–U+2064
func FilterInvisibleCharacters(input string) string {
if input == "" {
return input
}
// Filter runes
out := make([]rune, 0, len(input))
for _, r := range input {
if !shouldRemoveRune(r) {
out = append(out, r)
}
}
return string(out)
}
func FilterHTMLTags(input string) string {
if input == "" {
return input
}
return getPolicy().Sanitize(input)
}
// FilterCodeFenceMetadata removes hidden or suspicious info strings from fenced code blocks.
func FilterCodeFenceMetadata(input string) string {
if input == "" {
return input
}
lines := strings.Split(input, "\n")
insideFence := false
currentFenceLen := 0
for i, line := range lines {
sanitized, toggled, fenceLen := sanitizeCodeFenceLine(line, insideFence, currentFenceLen)
lines[i] = sanitized
if toggled {
insideFence = !insideFence
if insideFence {
currentFenceLen = fenceLen
} else {
currentFenceLen = 0
}
}
}
return strings.Join(lines, "\n")
}
const maxCodeFenceInfoLength = 48
func sanitizeCodeFenceLine(line string, insideFence bool, expectedFenceLen int) (string, bool, int) {
idx := strings.Index(line, "```")
if idx == -1 {
return line, false, expectedFenceLen
}
if hasNonWhitespace(line[:idx]) {
return line, false, expectedFenceLen
}
fenceEnd := idx
for fenceEnd < len(line) && line[fenceEnd] == '`' {
fenceEnd++
}
fenceLen := fenceEnd - idx
if fenceLen < 3 {
return line, false, expectedFenceLen
}
rest := line[fenceEnd:]
if insideFence {
if expectedFenceLen != 0 && fenceLen != expectedFenceLen {
return line, false, expectedFenceLen
}
return line[:fenceEnd], true, fenceLen
}
trimmed := strings.TrimSpace(rest)
if trimmed == "" {
return line[:fenceEnd], true, fenceLen
}
if strings.IndexFunc(trimmed, unicode.IsSpace) != -1 {
return line[:fenceEnd], true, fenceLen
}
if len(trimmed) > maxCodeFenceInfoLength {
return line[:fenceEnd], true, fenceLen
}
if !isSafeCodeFenceToken(trimmed) {
return line[:fenceEnd], true, fenceLen
}
if len(rest) > 0 && unicode.IsSpace(rune(rest[0])) {
return line[:fenceEnd] + " " + trimmed, true, fenceLen
}
return line[:fenceEnd] + trimmed, true, fenceLen
}
func hasNonWhitespace(segment string) bool {
for _, r := range segment {
if !unicode.IsSpace(r) {
return true
}
}
return false
}
func isSafeCodeFenceToken(token string) bool {
for _, r := range token {
if unicode.IsLetter(r) || unicode.IsDigit(r) {
continue
}
switch r {
case '+', '-', '_', '#', '.':
continue
}
return false
}
return true
}
func getPolicy() *bluemonday.Policy {
policyOnce.Do(func() {
p := bluemonday.StrictPolicy()
p.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",
"a", "img",
)
p.AllowAttrs("href").OnElements("a")
p.AllowURLSchemes("http", "https")
p.RequireParseableURLs(true)
p.RequireNoFollowOnLinks(true)
p.RequireNoReferrerOnLinks(true)
p.AddTargetBlankToFullyQualifiedLinks(true)
p.AllowImages()
p.AllowAttrs("src", "alt", "title").OnElements("img")
policy = p
})
return policy
}
func shouldRemoveRune(r rune) bool {
switch r {
case 0x200B, // ZERO WIDTH SPACE
0x200C, // ZERO WIDTH NON-JOINER
0x200E, // LEFT-TO-RIGHT MARK
0x200F, // RIGHT-TO-LEFT MARK
0x00AD, // SOFT HYPHEN
0xFEFF, // ZERO WIDTH NO-BREAK SPACE
0x180E: // MONGOLIAN VOWEL SEPARATOR
return true
case 0xE0001: // TAG
return true
}
// Ranges
// Unicode tags: U+E0020–U+E007F
if r >= 0xE0020 && r <= 0xE007F {
return true
}
// BiDi controls: U+202A–U+202E
if r >= 0x202A && r <= 0x202E {
return true
}
// BiDi isolates: U+2066–U+2069
if r >= 0x2066 && r <= 0x2069 {
return true
}
// Hidden modifiers: U+2060–U+2064
if r >= 0x2060 && r <= 0x2064 {
return true
}
return false
}
// Placeholders used to shield angle brackets inside code regions from
// the HTML sanitizer. They must not look like HTML tags themselves and
// must be unlikely to appear in real content.
const (
codeLtPlaceholder = "\x00CODELT\x00"
codeGtPlaceholder = "\x00CODEGT\x00"
)
// protectCodeAngles replaces < and > with unique placeholders inside
// fenced code blocks so that bluemonday does not strip them as HTML tags.
// This must run after FilterCodeFenceMetadata (which cleans fence info
// strings) and before FilterHTMLTags.
func protectCodeAngles(input string) string {
if input == "" {
return input
}
lines := strings.Split(input, "\n")
insideFence := false
currentFenceLen := 0
for i, line := range lines {
fenceIdx := strings.Index(line, "```")
if fenceIdx != -1 && !hasNonWhitespace(line[:fenceIdx]) {
fenceEnd := fenceIdx
for fenceEnd < len(line) && line[fenceEnd] == '`' {
fenceEnd++
}
fenceLen := fenceEnd - fenceIdx
if fenceLen >= 3 {
if insideFence {
if currentFenceLen == 0 || fenceLen >= currentFenceLen {
// Valid closing fence (CommonMark: closing fence
// must be at least as long as the opening fence).
insideFence = false
currentFenceLen = 0
continue
}
// Fence length too short — still inside code.
} else {
// Opening fence.
insideFence = true
currentFenceLen = fenceLen
continue
}
}
}
if insideFence {
lines[i] = strings.ReplaceAll(
strings.ReplaceAll(line, "<", codeLtPlaceholder),
">", codeGtPlaceholder,
)
}
}
return strings.Join(lines, "\n")
}
// restoreCodeAngles reverses the placeholder substitution performed by
// protectCodeAngles.
func restoreCodeAngles(input string) string {
s := strings.ReplaceAll(input, codeLtPlaceholder, "<")
s = strings.ReplaceAll(s, codeGtPlaceholder, ">")
return s
}