forked from maxwells/sanitize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhitelist.go
273 lines (228 loc) · 6.62 KB
/
whitelist.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
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
package sanitize
import (
"bytes"
"encoding/json"
"errors"
"io"
"strings"
"golang.org/x/net/html"
)
type Whitelist struct {
StripWhitespace bool `json:"stripWhitespace"`
StripComments bool `json:"stripComments"`
Elements map[string][]string `json:"elements"`
}
func (w *Whitelist) AddElement(elementTag string, attributes []string) {
w.Elements[elementTag] = attributes
}
func (w *Whitelist) HasElement(elementTag string) bool {
_, ok := w.Elements[elementTag]
return ok
}
func (w *Whitelist) GetAttributesForElement(elementTag string) []string {
val, _ := w.Elements[elementTag]
return val
}
func (w *Whitelist) HasAttributeForElement(elementTag string, attributeName string) bool {
val, ok := w.Elements[elementTag]
if !ok {
return false
}
for _, attribute := range val {
if attribute == attributeName {
return true
}
}
return false
}
func (w *Whitelist) ToJSON() (string, error) {
v, err := json.Marshal(w)
return string(v), err
}
// Remove all attributes on the provided node
// that are not contained within this whitelist
func (w *Whitelist) sanitizeAttributes(n *html.Node) {
attributes := make([]html.Attribute, len(n.Attr))
i := 0
for _, attribute := range n.Attr {
if w.HasAttributeForElement(n.Data, attribute.Key) {
attributes[i] = attribute
i += 1
}
}
n.Attr = attributes[0:i]
}
// Remove the comment if this whitelist is configured
// with the StripComments configuration
func (w *Whitelist) handleComment(n *html.Node) {
if w.StripComments {
if n.Parent != nil {
n.Parent.RemoveChild(n)
}
}
}
// Strip whitespace if this whitelist is configured
// with the StripWhitespace configuration
func (w *Whitelist) handleText(n *html.Node) {
if w.StripWhitespace {
n.Data = strings.TrimSpace(n.Data)
}
}
// Helper function to process a specific node.
// Defers logic around how to handle ElementNodes to
// the caller.
//
// Returns the return value of handleElement:
// a boolean describing whether the children
// of the node should be further sanitized (ie. not skipped).
func (w *Whitelist) sanitizeNode(n *html.Node, handleElement func(*html.Node) bool) error {
switch n.Type {
case html.ErrorNode:
return errors.New("Unable to parse HTML")
case html.TextNode:
w.handleText(n)
case html.DocumentNode:
case html.ElementNode:
if !handleElement(n) {
return nil
}
w.sanitizeAttributes(n)
case html.CommentNode:
w.handleComment(n)
case html.DoctypeNode:
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
err := w.sanitizeNode(c, handleElement)
if err != nil {
return err
}
}
return nil
}
// sanitizeRemove traverses pre-order over the nodes,
// removing any element nodes that are not whitelisted
// and and removing any attributes that are not whitelisted
// from a given element node
func (w *Whitelist) sanitizeRemove(n *html.Node) error {
return w.sanitizeNode(n, func(n *html.Node) bool {
if !w.HasElement(n.Data) {
if n.Parent != nil {
nextSibling := n.NextSibling
n.Parent.RemoveChild(n)
// reset next sibling to support continuation
// of linked-list style traversal of parent node's children
n.NextSibling = nextSibling
}
return false
}
return true
})
}
// remove non whitelisted elements entirely from a full HTML document
func (w *Whitelist) SanitizeRemove(reader io.Reader) (string, error) {
var buffer bytes.Buffer
doc, err := html.Parse(reader)
if err != nil {
return buffer.String(), err
}
err = w.sanitizeRemove(doc)
if err != nil {
return buffer.String(), err
}
err = html.Render(&buffer, doc)
return buffer.String(), err
}
// remove non whitelisted elements in provided document fragment
//
// given the go.net/html library creates a document root with a head
// and body by default around the provided fragment, simply unwrap
// those portions along before performing the sanitizeRemove function
// on the remaining children
func (w *Whitelist) SanitizeRemoveFragment(reader io.Reader) (string, error) {
var buffer bytes.Buffer
doc, err := html.Parse(reader)
if err != nil {
return buffer.String(), err
}
err = renderForEachChild(doc.FirstChild.FirstChild, &buffer, w.sanitizeRemove)
if err != nil {
return buffer.String(), err
}
err = renderForEachChild(doc.FirstChild.LastChild, &buffer, w.sanitizeRemove)
return buffer.String(), err
}
// sanitizeUnwrap traverses pre-order over the nodes, reattaching
// the whitelisted children of any element nodes that are not
// whitelisted to the parent of the unwhitelisted node
func (w *Whitelist) sanitizeUnwrap(n *html.Node) error {
return w.sanitizeNode(n, func(n *html.Node) bool {
if w.HasElement(n.Data) || n.Parent == nil {
return true
}
insertBefore := n.NextSibling
firstChild := n.FirstChild
for c := n.FirstChild; c != nil; {
nodeToUnwrap := c
c = c.NextSibling
n.RemoveChild(nodeToUnwrap)
n.Parent.InsertBefore(nodeToUnwrap, insertBefore)
}
n.Parent.RemoveChild(n)
// reset next sibling to support continuation
// of linked-list style traversal of parent node's children
n.NextSibling = firstChild
return false
})
}
// unwrap non whitelisted elements from a full HTML document
func (w *Whitelist) SanitizeUnwrap(reader io.Reader) (string, error) {
var buffer bytes.Buffer
doc, err := html.Parse(reader)
if err != nil {
return buffer.String(), err
}
err = w.sanitizeUnwrap(doc)
if err != nil {
return buffer.String(), err
}
err = html.Render(&buffer, doc)
return buffer.String(), err
}
// unwrap non whitelisted elements in provided document fragment
//
// given the go.net/html library creates a document root with a head
// and body by default around the provided fragment, simply unwrap
// those portions along before performing the sanitizeUnwrap function
// on the remaining children
func (w *Whitelist) SanitizeUnwrapFragment(reader io.Reader) (string, error) {
var buffer bytes.Buffer
doc, err := html.Parse(reader)
if err != nil {
return buffer.String(), err
}
err = renderForEachChild(doc.FirstChild.FirstChild, &buffer, w.sanitizeUnwrap)
if err != nil {
return buffer.String(), err
}
err = renderForEachChild(doc.FirstChild.LastChild, &buffer, w.sanitizeUnwrap)
return buffer.String(), err
}
// render for every child of the node provided,
// render that node into the provided buffer
// after performing the provided function on it
func renderForEachChild(n *html.Node, buffer *bytes.Buffer, fn func(*html.Node) error) error {
for c := n.FirstChild; c != nil; c = c.NextSibling {
err := fn(c)
if err != nil {
return err
}
if c.Parent == n {
// this node wasn't removed
err = html.Render(buffer, c)
if err != nil {
return err
}
}
}
return nil
}