2017-03-08 18:41:05 +00:00
|
|
|
/* eslint-env browser, jquery */
|
|
|
|
/* global filterXSS */
|
2016-08-14 10:32:22 +00:00
|
|
|
// allow some attributes
|
2017-03-08 18:41:05 +00:00
|
|
|
var whiteListAttr = ['id', 'class', 'style']
|
|
|
|
window.whiteListAttr = whiteListAttr
|
2017-03-22 10:26:35 +00:00
|
|
|
// allow link starts with '.', '/' and custom protocol with '://', exclude link starts with javascript://
|
2017-04-11 14:25:14 +00:00
|
|
|
var linkRegex = /^(?!javascript:\/\/)([\w|-]+:\/\/)|^([.|/])+/i
|
2016-08-15 03:00:02 +00:00
|
|
|
// allow data uri, from https://gist.github.com/bgrins/6194623
|
2017-03-08 18:41:05 +00:00
|
|
|
var dataUriRegex = /^\s*data:([a-z]+\/[a-z0-9-+.]+(;[a-z-]+=[a-z0-9-]+)?)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@/?%\s]*)\s*$/i
|
2016-08-14 10:32:22 +00:00
|
|
|
// custom white list
|
2017-03-08 18:41:05 +00:00
|
|
|
var whiteList = filterXSS.whiteList
|
2016-08-14 10:32:22 +00:00
|
|
|
// allow ol specify start number
|
2017-03-08 18:41:05 +00:00
|
|
|
whiteList['ol'] = ['start']
|
2017-02-17 13:56:35 +00:00
|
|
|
// allow li specify value number
|
2017-03-08 18:41:05 +00:00
|
|
|
whiteList['li'] = ['value']
|
2016-08-14 10:32:22 +00:00
|
|
|
// allow style tag
|
2017-03-08 18:41:05 +00:00
|
|
|
whiteList['style'] = []
|
2016-08-14 10:32:22 +00:00
|
|
|
// allow kbd tag
|
2017-03-08 18:41:05 +00:00
|
|
|
whiteList['kbd'] = []
|
2016-08-14 10:32:22 +00:00
|
|
|
// allow ifram tag with some safe attributes
|
2017-11-24 09:10:50 +00:00
|
|
|
whiteList['iframe'] = ['allowfullscreen', 'name', 'referrerpolicy', 'sandbox', 'src', 'width', 'height']
|
2016-10-29 15:43:58 +00:00
|
|
|
// allow summary tag
|
2017-03-08 18:41:05 +00:00
|
|
|
whiteList['summary'] = []
|
2016-02-11 20:33:21 +00:00
|
|
|
|
|
|
|
var filterXSSOptions = {
|
2017-03-08 18:41:05 +00:00
|
|
|
allowCommentTag: true,
|
|
|
|
whiteList: whiteList,
|
|
|
|
escapeHtml: function (html) {
|
2017-03-22 10:26:30 +00:00
|
|
|
// allow html comment in multiple lines
|
2017-09-27 10:20:04 +00:00
|
|
|
return html.replace(/<(?!!--)/g, '<').replace(/-->/g, '__HTML_COMMENT_END__').replace(/>/g, '>').replace(/__HTML_COMMENT_END__/g, '-->')
|
2017-03-08 18:41:05 +00:00
|
|
|
},
|
|
|
|
onIgnoreTag: function (tag, html, options) {
|
2017-03-22 10:26:30 +00:00
|
|
|
// allow comment tag
|
2017-03-08 18:41:05 +00:00
|
|
|
if (tag === '!--') {
|
2016-02-11 20:33:21 +00:00
|
|
|
// do not filter its attributes
|
2017-03-08 18:41:05 +00:00
|
|
|
return html
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onTagAttr: function (tag, name, value, isWhiteAttr) {
|
2017-03-22 10:26:30 +00:00
|
|
|
// allow href and src that match linkRegex
|
2017-03-08 18:41:05 +00:00
|
|
|
if (isWhiteAttr && (name === 'href' || name === 'src') && linkRegex.test(value)) {
|
|
|
|
return name + '="' + filterXSS.escapeAttrValue(value) + '"'
|
|
|
|
}
|
2017-03-22 10:26:30 +00:00
|
|
|
// allow data uri in img src
|
2017-03-08 18:41:05 +00:00
|
|
|
if (isWhiteAttr && (tag === 'img' && name === 'src') && dataUriRegex.test(value)) {
|
|
|
|
return name + '="' + filterXSS.escapeAttrValue(value) + '"'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onIgnoreTagAttr: function (tag, name, value, isWhiteAttr) {
|
2017-03-22 10:26:30 +00:00
|
|
|
// allow attr start with 'data-' or in the whiteListAttr
|
2017-03-08 18:41:05 +00:00
|
|
|
if (name.substr(0, 5) === 'data-' || window.whiteListAttr.indexOf(name) !== -1) {
|
2017-03-22 10:26:30 +00:00
|
|
|
// escape its value using built-in escapeAttrValue function
|
2017-03-08 18:41:05 +00:00
|
|
|
return name + '="' + filterXSS.escapeAttrValue(value) + '"'
|
2016-02-11 20:33:21 +00:00
|
|
|
}
|
2017-03-08 18:41:05 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-11 20:33:21 +00:00
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
function preventXSS (html) {
|
|
|
|
return filterXSS(html, filterXSSOptions)
|
2016-10-08 12:02:30 +00:00
|
|
|
}
|
2017-03-08 18:41:05 +00:00
|
|
|
window.preventXSS = preventXSS
|
2016-10-08 12:02:30 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
preventXSS: preventXSS
|
|
|
|
}
|