forked from apostrophecms/sanitize-html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
435 lines (392 loc) · 13.1 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
var htmlparser = require('htmlparser2');
var extend = require('xtend');
var quoteRegexp = require('regexp-quote');
var Result = require('./result');
function each(obj, cb) {
if (obj) Object.keys(obj).forEach(function (key) {
cb(obj[key], key);
});
}
// Avoid false positives with .__proto__, .hasOwnProperty, etc.
function has(obj, key) {
return ({}).hasOwnProperty.call(obj, key);
}
function writeToStream(stream, chunk) {
stream.write(chunk);
}
module.exports = sanitizeHtml;
// Ignore the _recursing flag; it's there for recursive
// invocation as a guard against this exploit:
// https://github.com/fb55/htmlparser2/issues/105
function sanitizeHtml(html, options, _recursing) {
function processChunk(chunk) {
if (options.stream && options.stream.output) {
writeToStream(options.stream.output, chunk);
}
return chunk;
}
var result = new Result(processChunk);
function Frame(tag, attribs) {
var that = this;
this.tag = tag;
this.attribs = attribs || {};
this.tagPosition = result.length;
this.escapeText = true;
this.parent = null;
this.text = ''; // Node inner text
this.updateParentNodeText = function() {
if (stack.length) {
var parentFrame = that.parent;
parentFrame.text += that.text;
}
};
}
if (!options) {
options = sanitizeHtml.defaults;
options.parser = htmlParserDefaults;
} else {
options = extend(sanitizeHtml.defaults, options);
if (options.parser) {
options.parser = extend(htmlParserDefaults, options.parser);
} else {
options.parser = htmlParserDefaults;
}
}
// Tags that contain something other than HTML, or where discarding
// the text when the tag is disallowed makes sense for other reasons.
// If we are not allowing these tags, we should drop their content too.
// For other tags you would drop the tag but keep its content.
var nonTextTagsArray = options.nonTextTags || [ 'script', 'style', 'textarea' ];
var allowedAttributesMap;
var allowedAttributesGlobMap;
if(options.allowedAttributes) {
allowedAttributesMap = {};
allowedAttributesGlobMap = {};
each(options.allowedAttributes, function(attributes, tag) {
allowedAttributesMap[tag] = [];
var globRegex = [];
attributes.forEach(function(name) {
if(name.indexOf('*') >= 0) {
globRegex.push(quoteRegexp(name).replace(/\\\*/g, '.*'));
} else {
allowedAttributesMap[tag].push(name);
}
});
allowedAttributesGlobMap[tag] = new RegExp('^(' + globRegex.join('|') + ')$');
});
}
var allowedClassesMap = {};
each(options.allowedClasses, function(classes, tag) {
// Implicitly allows the class attribute
if(allowedAttributesMap) {
if (!has(allowedAttributesMap, tag)) {
allowedAttributesMap[tag] = [];
}
allowedAttributesMap[tag].push('class');
}
allowedClassesMap[tag] = classes;
});
var transformTagsMap = {};
var transformTagsAll;
each(options.transformTags, function(transform, tag) {
var transFun;
if (typeof transform === 'function') {
transFun = transform;
} else if (typeof transform === "string") {
transFun = sanitizeHtml.simpleTransform(transform);
}
if (tag === '*') {
transformTagsAll = transFun;
} else {
transformTagsMap[tag] = transFun;
}
});
var depth = 0;
var stack = [];
var skipMap = {};
var transformMap = {};
var skipText = false;
var skipTextDepth = 0;
var txt;
var parser = new htmlparser.Parser({
onopentag: function(name, attribs) {
if (skipText) {
skipTextDepth++;
return;
}
var frame = new Frame(name, attribs);
frame.parent = stack.length > 0 ? stack[stack.length - 1] : null;
stack.push(frame);
var skip = false;
var hasText = frame.text ? true : false;
var transformedTag;
if (has(transformTagsMap, name)) {
transformedTag = transformTagsMap[name](name, attribs, stack);
frame.attribs = attribs = transformedTag.attribs;
if (transformedTag.text !== undefined) {
frame.innerText = transformedTag.text;
}
frame.escapeText = transformedTag.escape === false ? false : true;
if (name !== transformedTag.tagName) {
frame.name = name = transformedTag.tagName;
transformMap[depth] = transformedTag.tagName;
}
}
if (transformTagsAll) {
transformedTag = transformTagsAll(name, attribs);
frame.attribs = attribs = transformedTag.attribs;
if (name !== transformedTag.tagName) {
frame.name = name = transformedTag.tagName;
transformMap[depth] = transformedTag.tagName;
}
}
if (options.allowedTags && options.allowedTags.indexOf(name) === -1) {
skip = true;
if (nonTextTagsArray.indexOf(name) !== -1) {
skipText = true;
skipTextDepth = 1;
}
skipMap[depth] = true;
}
depth++;
if (skip) {
// We want the contents but not this tag
return;
}
result.add('<' + name);
if (!allowedAttributesMap || has(allowedAttributesMap, name) || allowedAttributesMap['*']) {
each(attribs, function(value, a) {
if (!allowedAttributesMap ||
(has(allowedAttributesMap, name) && allowedAttributesMap[name].indexOf(a) !== -1 ) ||
(allowedAttributesMap['*'] && allowedAttributesMap['*'].indexOf(a) !== -1 ) ||
(has(allowedAttributesGlobMap, name) && allowedAttributesGlobMap[name].test(a)) ||
(allowedAttributesGlobMap['*'] && allowedAttributesGlobMap['*'].test(a))) {
if ((a === 'href') || (a === 'src')) {
if (naughtyHref(name, value)) {
delete frame.attribs[a];
return;
}
}
if (a === 'class') {
value = filterClasses(value, allowedClassesMap[name]);
if (!value.length) {
delete frame.attribs[a];
return;
}
}
result.add(' ' + a);
if (value.length) {
result.add('="' + escapeHtml(value) + '"');
}
} else {
delete frame.attribs[a];
}
});
}
if (options.selfClosing.indexOf(name) !== -1) {
result.add(" />");
} else {
result.add(">");
if (frame.innerText && !hasText && !options.textFilter) {
result.add(frame.innerText);
frame.resultAdded = true;
}
}
},
ontext: function(text) {
if (skipText) {
return;
}
var lastFrame = stack[stack.length-1];
var tag;
var escapeText = true;
if (lastFrame) {
tag = lastFrame.tag;
// If inner text was set by transform function then let's use it
text = lastFrame.innerText !== undefined ? lastFrame.innerText : text;
escapeText = lastFrame.escapeText === false ? false : true;
}
if ((tag === 'script') || (tag === 'style')) {
// htmlparser2 gives us these as-is. Escaping them ruins the content. Allowing
// script tags is, by definition, game over for XSS protection, so if that's
// your concern, don't allow them. The same is essentially true for style tags
// which have their own collection of XSS vectors.
result.add(text);
} else {
if (lastFrame && lastFrame.innerText && lastFrame.resultAdded) {
return;
}
var escaped = escapeText ? escapeHtml(text) : text;
if (options.textFilter) {
result.add(options.textFilter(escaped));
} else {
result.add(escaped);
}
if (lastFrame && lastFrame.innerText) {
lastFrame.resultAdded = true;
}
}
if (stack.length) {
var frame = stack[stack.length - 1];
if (lastFrame && !lastFrame.innerText) {
frame.text += text;
}
}
},
onclosetag: function(name) {
if (skipText) {
skipTextDepth--;
if (!skipTextDepth) {
skipText = false;
} else {
return;
}
}
var frame = stack.pop();
if (!frame) {
// Do not crash on bad markup
return;
}
skipText = false;
depth--;
if (skipMap[depth]) {
delete skipMap[depth];
frame.updateParentNodeText();
return;
}
if (transformMap[depth]) {
name = transformMap[depth];
delete transformMap[depth];
}
if (options.exclusiveFilter && options.exclusiveFilter(frame)) {
result.remove(0, frame.tagPosition);
result.flush();
return;
}
frame.updateParentNodeText();
if (options.selfClosing.indexOf(name) !== -1) {
// Already output />
result.flush();
return;
}
result.add("</" + name + ">");
result.flush();
}
}, options.parser);
if (options.stream) {
var inputStream = options.stream.input;
if (!(typeof inputStream.read === 'function' && typeof inputStream.on === 'function')) {
throw new Error("For streaming the input must be a readable stream");
}
// If output is defined, pipe to that stream, else use callback
if (options.stream.output) {
// Test if it is an instance of stream using duck typing
var outputStream = options.stream.output;
if (!(typeof outputStream.write === 'function' && typeof outputStream.end === 'function')) {
throw new Error('Stream "output" field must point to a writeable stream');
}
} else if (options.stream.callback) {
if (typeof options.stream.callback !== 'function') {
throw new Error('Stream "callback" field must be a function');
}
}
// Then treat html variable as a stream
inputStream.on('data', function (data) {
parser.write(data);
});
inputStream.on('end', function () {
parser.end();
if (options.stream && options.stream.callback) {
options.stream.callback(result.toString());
} else if (options.stream && options.stream.output) {
options.stream.output.end();
}
});
return undefined;
} else {
parser.write(html);
parser.end();
return result.toString();
}
function escapeHtml(s) {
if (typeof(s) !== 'string') {
s = s + '';
}
return s.replace(/\&/g, '&').replace(/</g, '<').replace(/\>/g, '>').replace(/\"/g, '"');
}
function naughtyHref(name, href) {
// Browsers ignore character codes of 32 (space) and below in a surprising
// number of situations. Start reading here:
// https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet#Embedded_tab
href = href.replace(/[\x00-\x20]+/g, '');
// Clobber any comments in URLs, which the browser might
// interpret inside an XML data island, allowing
// a javascript: URL to be snuck through
href = href.replace(/<\!\-\-.*?\-\-\>/g, '');
// Case insensitive so we don't get faked out by JAVASCRIPT #1
var matches = href.match(/^([a-zA-Z]+)\:/);
if (!matches) {
// Protocol-relative URL: "//some.evil.com/nasty"
if (href.match(/^\/\//)) {
return !options.allowProtocolRelative;
}
// No scheme
return false;
}
var scheme = matches[1].toLowerCase();
if (has(options.allowedSchemesByTag, name)) {
return options.allowedSchemesByTag[name].indexOf(scheme) === -1;
}
return !options.allowedSchemes || options.allowedSchemes.indexOf(scheme) === -1;
}
function filterClasses(classes, allowed) {
if (!allowed) {
// The class attribute is allowed without filtering on this tag
return classes;
}
classes = classes.split(/\s+/);
return classes.filter(function(clss) {
return allowed.indexOf(clss) !== -1;
}).join(' ');
}
}
// Defaults are accessible to you so that you can use them as a starting point
// programmatically if you wish
var htmlParserDefaults = {
decodeEntities: true
};
sanitizeHtml.defaults = {
allowedTags: [ 'h3', 'h4', 'h5', 'h6', 'blockquote', 'p', 'a', 'ul', 'ol',
'nl', 'li', 'b', 'i', 'strong', 'em', 'strike', 'code', 'hr', 'br', 'div',
'table', 'thead', 'caption', 'tbody', 'tr', 'th', 'td', 'pre' ],
allowedAttributes: {
a: [ 'href', 'name', 'target' ],
// We don't currently allow img itself by default, but this
// would make sense if we did
img: [ 'src' ]
},
// Lots of these won't come up by default because we don't allow them
selfClosing: [ 'img', 'br', 'hr', 'area', 'base', 'basefont', 'input', 'link', 'meta' ],
// URL schemes we permit
allowedSchemes: [ 'http', 'https', 'ftp', 'mailto' ],
allowedSchemesByTag: {},
allowProtocolRelative: true
};
sanitizeHtml.simpleTransform = function(newTagName, newAttribs, merge) {
merge = (merge === undefined) ? true : merge;
newAttribs = newAttribs || {};
return function(tagName, attribs) {
var attrib;
if (merge) {
for (attrib in newAttribs) {
attribs[attrib] = newAttribs[attrib];
}
} else {
attribs = newAttribs;
}
return {
tagName: newTagName,
attribs: attribs
};
};
};