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