2017-03-08 18:41:05 +00:00
|
|
|
/* eslint-env browser, jquery */
|
2016-08-14 10:32:22 +00:00
|
|
|
// allow some attributes
|
2018-11-10 19:24:41 +00:00
|
|
|
|
|
|
|
var filterXSS = require('xss')
|
|
|
|
|
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']
|
2018-02-26 12:54:57 +00:00
|
|
|
// allow summary tag
|
2017-03-08 18:41:05 +00:00
|
|
|
whiteList['summary'] = []
|
2018-02-25 13:48:50 +00:00
|
|
|
// allow ruby tag
|
|
|
|
whiteList['ruby'] = []
|
2018-02-26 12:55:10 +00:00
|
|
|
// allow rp tag for ruby
|
|
|
|
whiteList['rp'] = []
|
2018-02-25 13:48:50 +00:00
|
|
|
// allow rt tag for ruby
|
|
|
|
whiteList['rt'] = []
|
|
|
|
// allow figure tag
|
|
|
|
whiteList['figure'] = []
|
|
|
|
// allow figcaption tag
|
|
|
|
whiteList['figcaption'] = []
|
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 = {
|
2018-11-10 19:24:41 +00:00
|
|
|
preventXSS: preventXSS,
|
|
|
|
escapeAttrValue: filterXSS.escapeAttrValue
|
2016-10-08 12:02:30 +00:00
|
|
|
}
|