增加工具函数,提供一些特殊应用
This commit is contained in:
@@ -84,6 +84,8 @@ var html = xss('<script>alert("xss");</script>', options);
|
||||
console.log(html);
|
||||
```
|
||||
|
||||
## 其他应用
|
||||
|
||||
|
||||
## 测试
|
||||
|
||||
|
||||
3
index.js
3
index.js
@@ -276,3 +276,6 @@ exports = module.exports = function (html, options) {
|
||||
exports.whiteList = defaultWhiteList;
|
||||
exports.onTagAttr = defaultOnTagAttr;
|
||||
exports.onIgnoreTag = defaultOnIgnoreTag;
|
||||
|
||||
// 工具函数
|
||||
exports.utils = require('./utils');
|
||||
|
||||
@@ -113,35 +113,18 @@ describe('test XSS', function () {
|
||||
assert.deepEqual(position, [4, 30, 50]);
|
||||
assert.deepEqual(originalPosition, [4, 24, 38]);
|
||||
|
||||
// 替换检验
|
||||
var hidden = [];
|
||||
var posStart = false;
|
||||
// 替换检验 utils.tagFilter()
|
||||
var filter = xss.utils.tagFilter(['script']);
|
||||
var html = xss('<b >script is <script t="d">alert("xss"); ooxx()</script>, wahaha!!</b>', {
|
||||
onIgnoreTag: function (tag, html, options) {
|
||||
if (tag === 'script') {
|
||||
var ret = '[removed]';
|
||||
if (posStart !== false && options.isClosing) {
|
||||
var end = options.position + ret.length;
|
||||
hidden.push([posStart, end]);
|
||||
posStart = false;
|
||||
} else {
|
||||
posStart = options.position;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
onIgnoreTag: filter.onIgnoreTag
|
||||
});
|
||||
var rethtml = '';
|
||||
var lastPos = 0;
|
||||
hidden.forEach(function (pos) {
|
||||
rethtml += html.slice(lastPos, pos[0]);
|
||||
lastPos = pos[1];
|
||||
assert.equal(filter.filter(html), '<b>script is , wahaha!!</b>');
|
||||
|
||||
var filter = xss.utils.tagFilter(['x2']);
|
||||
var html = xss('<x1></b><x2>dds</x2><x3>fd</x3>', {
|
||||
onIgnoreTag: filter.onIgnoreTag
|
||||
});
|
||||
rethtml += html.slice(lastPos);
|
||||
//console.log(hidden);
|
||||
//console.log(html);
|
||||
//console.log(rethtml);
|
||||
assert.equal(rethtml, '<b>script is , wahaha!!</b>');
|
||||
assert.equal(filter.filter(html), '<x1></b><x3>fd</x3>');
|
||||
|
||||
});
|
||||
|
||||
|
||||
49
utils.js
Normal file
49
utils.js
Normal file
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* 工具函数
|
||||
*
|
||||
* @author 老雷<leizongmin@gmail.com>
|
||||
*/
|
||||
|
||||
var utils = module.exports;
|
||||
|
||||
/**
|
||||
* 过滤代码块
|
||||
*
|
||||
* @param {array} tags 要隐藏的标签列表
|
||||
* @param {function} next 对不在列表中的标签的处理函数
|
||||
*/
|
||||
utils.tagFilter = function (tags, next) {
|
||||
if (typeof(next) !== 'function') {
|
||||
next = function () {};
|
||||
}
|
||||
var hidden = [];
|
||||
var posStart = false;
|
||||
return {
|
||||
onIgnoreTag: function (tag, html, options) {
|
||||
if (tags.indexOf(tag) !== -1) {
|
||||
var ret = '[removed]';
|
||||
if (posStart !== false && options.isClosing) {
|
||||
var end = options.position + ret.length;
|
||||
hidden.push([posStart, end]);
|
||||
posStart = false;
|
||||
} else {
|
||||
posStart = options.position;
|
||||
}
|
||||
return ret;
|
||||
} else {
|
||||
return next(tag, html, options);
|
||||
}
|
||||
},
|
||||
filter: function (html) {
|
||||
var rethtml = '';
|
||||
var lastPos = 0;
|
||||
hidden.forEach(function (pos) {
|
||||
rethtml += html.slice(lastPos, pos[0]);
|
||||
lastPos = pos[1];
|
||||
});
|
||||
rethtml += html.slice(lastPos);
|
||||
return rethtml;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user