-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
269 lines (244 loc) · 5.56 KB
/
index.js
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
function cockblock(html, options) {
return sanitizeHtml(html, options || cockblock.defaults);
}
function $wrap(value) {
cockblock.$ = cockblock.$ || window.$;
if (!cockblock.$) {
throw new Error("cockblock.$ must be set to cheerio to jquery");
}
return cockblock.$(value);
}
cockblock.url = function(url, options) {
return sanitizeResource(url, options || cockblock.defaults);
};
// Exposed for testing
// TODO: expose these under cockblock.utils instead
cockblock._sanitizeAttributes = sanitizeAttributes;
cockblock._getAttributeName = getAttributeName;
cockblock.defaults = {
elements: [
"a",
"aside",
"b",
"blockquote",
"br",
"caption",
"code",
"del",
"dd",
"dfn",
"div",
"dl",
"dt",
"em",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"hr",
"i",
"img",
"ins",
"kbd",
"li",
"ol",
"p",
"pre",
"q",
"samp",
"span",
"strike",
"strong",
"sub",
"sup",
"table",
"tbody",
"td",
"tfoot",
"th",
"thead",
"tr",
"tt",
"ul",
"var"
],
attributes: {
"a": ["href"],
"img": ["src"],
"div": ["itemscope", "itemtype"],
"all": [
"abbr",
"accept",
"accept-charset",
"accesskey",
"action",
"align",
"alt",
"axis",
"border",
"cellpadding",
"cellspacing",
"char",
"charoff",
"charset",
"checked",
"cite",
"clear",
"cols",
"colspan",
"color",
"compact",
"coords",
// "data-[a-z0-9-]+",
"datetime",
"dir",
"disabled",
"enctype",
"for",
"frame",
"headers",
"height",
"hreflang",
"hspace",
"ismap",
"label",
"lang",
"longdesc",
"maxlength",
"media",
"method",
"multiple",
"name",
"nohref",
"noshade",
"nowrap",
"prompt",
"readonly",
"rel",
"rev",
"rows",
"rowspan",
"rules",
"scope",
"selected",
"shape",
"size",
"span",
"start",
"summary",
"tabindex",
"target",
"title",
"type",
"usemap",
"valign",
"value",
"vspace",
"width",
"itemprop"
]
},
// Default protocol support includes http(s), mailto, and relative.
// TODO: Support protocol resolution too? //example.com
protocols: /^(http|https|mailto|#|\/)/i
};
var CONTAINED = {};
CONTAINED.thead = CONTAINED.tbody = CONTAINED.tfoot = /^table$/i;
CONTAINED.tr = /^(table|thead|tbody|tfoot)$/i;
CONTAINED.th = CONTAINED.td = /^tr$/i;
CONTAINED.li = /^(ul|ol)$/i;
// src: img, iframe
// href: a
var RESOURCEFUL = /^(src|href)$/;
function sanitizeHtml(html, options) {
var $wrapper = $wrap("<body>");
$wrapper.html(html);
sanitizeChildren($wrapper, initializeOptions(options));
return $wrapper.html();
}
function initializeOptions(options) {
var opts = {};
opts.protocols = options.protocols;
opts.elements = arrayToRegExp(options.elements);
opts.attributes = {};
for (var tagName in options.attributes) {
var attributes = options.attributes[tagName];
if (tagName != "all") attributes = attributes.concat(options.attributes.all);
opts.attributes[tagName] = arrayToRegExp(attributes);
}
return opts;
}
function arrayToRegExp(array) {
return new RegExp("^(" + array.join("|") + ")$", "i");
}
function sanitizeElement($el, options) {
if (options.elements.test(getTagName($el)) && isContained($el)) {
sanitizeAttributes($el, options);
sanitizeChildren($el, options);
return $el;
} else {
$el.remove();
}
}
function sanitizeChildren($el, options) {
$el.children().each(function() {
sanitizeElement($wrap(this), options);
});
}
// List and table items must be contained or they can break out.
function isContained($el) {
var requiredParent = CONTAINED[getTagNameLower($el)];
return !requiredParent || requiredParent.test(getTagName($el.parent()));
}
function sanitizeAttributes($el, options) {
var tagName = getTagNameLower($el);
var attribute, attributes = getAttributes($el);
var whitelist = options.attributes[tagName] || options.attributes.all;
for (var index in attributes) {
if (attributes.hasOwnProperty(index)) {
if ((attribute = getAttributeName(attributes, index))) {
if (whitelist.test(attribute)) {
sanitizeAttribute($el, attribute, options);
} else {
$el.removeAttr(attribute);
}
}
}
}
}
function sanitizeAttribute($el, attribute, options) {
if (RESOURCEFUL.test(attribute)) {
$el.attr(attribute, sanitizeResource($el.attr(attribute), options));
}
}
function sanitizeResource(value, options) {
return (value && options.protocols.test(value)) ? value : '';
}
// Conformity helpers since cheerio couldn't go the easy route and just
// use the same variable names browsers do.
function getTagName($el) {
return $el[0].tagName || $el[0].name;
}
function getTagNameLower($el) {
return getTagName($el).toLowerCase();
}
function getAttributes($el) {
return $el[0].attributes || $el[0].attribs;
}
// In the browser the attributes object looks like:
// {"0": {"name": "class"}, "1": ...}
//
// In node / cheerio the attributes are keyed by name instead.
//
// - in IE9 it's possible for attribute to be undefined (issue #1)
function getAttributeName(attributes, index) {
if (Number(index) == index) {
var attribute = attributes[String(index)];
return attribute && attribute.name;
} else {
return index;
}
}
module.exports = cockblock;