test: stripIgnoreTagBody
This commit is contained in:
40
README.md
40
README.md
@@ -67,8 +67,8 @@ html = xss('<script>alert("xss");</script>', options);
|
|||||||
|
|
||||||
### 白名单
|
### 白名单
|
||||||
|
|
||||||
通过 `whiteList` 来指定,格式为:`{'标签名': ['属性1', '属性2']}`。不在白名单中
|
通过 `whiteList` 来指定,格式为:`{'标签名': ['属性1', '属性2']}`。不在白名单上
|
||||||
的标签将被过滤,不在白名单中的属性也会被过滤。以下是示例:
|
的标签将被过滤,不在白名单上的属性也会被过滤。以下是示例:
|
||||||
|
|
||||||
```JavaScript
|
```JavaScript
|
||||||
// 只允许a标签,该标签只允许href, title, target这三个属性
|
// 只允许a标签,该标签只允许href, title, target这三个属性
|
||||||
@@ -94,14 +94,14 @@ function onTag (tag, html, options) {
|
|||||||
// tag是当前的标签名称,比如<a>标签,则tag的值是'a'
|
// tag是当前的标签名称,比如<a>标签,则tag的值是'a'
|
||||||
// html是该标签的HTML,比如<a>标签,则html的值是'<a>'
|
// html是该标签的HTML,比如<a>标签,则html的值是'<a>'
|
||||||
// options是一些附加的信息,具体如下:
|
// options是一些附加的信息,具体如下:
|
||||||
// isWhite boolean类型,表示该标签是否在白名单中
|
// isWhite boolean类型,表示该标签是否在白名单上
|
||||||
// isClosing boolean类型,表示该标签是否为闭合标签,比如</a>时为true
|
// isClosing boolean类型,表示该标签是否为闭合标签,比如</a>时为true
|
||||||
// position integer类型,表示当前标签在输出的结果中的起始位置
|
// position integer类型,表示当前标签在输出的结果中的起始位置
|
||||||
// sourcePosition integer类型,表示当前标签在原HTML中的起始位置
|
// sourcePosition integer类型,表示当前标签在原HTML中的起始位置
|
||||||
// 如果返回一个字符串,则当前标签将被替换为该字符串
|
// 如果返回一个字符串,则当前标签将被替换为该字符串
|
||||||
// 如果不返回任何值,则使用默认的处理方法:
|
// 如果不返回任何值,则使用默认的处理方法:
|
||||||
// 在白名单中: 通过onTagAttr来过滤属性,详见下文
|
// 在白名单上: 通过onTagAttr来过滤属性,详见下文
|
||||||
// 不在白名单中:通过onIgnoreTag指定,详见下文
|
// 不在白名单上:通过onIgnoreTag指定,详见下文
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -114,15 +114,15 @@ function onTagAttr (tag, name, value, isWhiteAttr) {
|
|||||||
// tag是当前的标签名称,比如<a>标签,则tag的值是'a'
|
// tag是当前的标签名称,比如<a>标签,则tag的值是'a'
|
||||||
// name是当前属性的名称,比如href="#",则name的值是'href'
|
// name是当前属性的名称,比如href="#",则name的值是'href'
|
||||||
// value是当前属性的值,比如href="#",则value的值是'#'
|
// value是当前属性的值,比如href="#",则value的值是'#'
|
||||||
// isWhiteAttr是否为白名单中的属性
|
// isWhiteAttr是否为白名单上的属性
|
||||||
// 如果返回一个字符串,则当前属性值将被替换为该字符串
|
// 如果返回一个字符串,则当前属性值将被替换为该字符串
|
||||||
// 如果不返回任何值,则使用默认的处理方法
|
// 如果不返回任何值,则使用默认的处理方法
|
||||||
// 在白名单中: 输出该属性
|
// 在白名单上: 输出该属性
|
||||||
// 不在白名单中:通过onIgnoreTagAttr指定,详见下文
|
// 不在白名单上:通过onIgnoreTagAttr指定,详见下文
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### 自定义匹配到不在白名单中的标签时的处理方法
|
### 自定义匹配到不在白名单上的标签时的处理方法
|
||||||
|
|
||||||
通过 `onIgnoreTag` 来指定相应的处理函数。以下是详细说明:
|
通过 `onIgnoreTag` 来指定相应的处理函数。以下是详细说明:
|
||||||
|
|
||||||
@@ -134,7 +134,7 @@ function onIgnoreTag (tag, html, options) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### 自定义匹配到不在白名单中的属性时的处理方法
|
### 自定义匹配到不在白名单上的属性时的处理方法
|
||||||
|
|
||||||
通过 `onIgnoreTagAttr` 来指定相应的处理函数。以下是详细说明:
|
通过 `onIgnoreTagAttr` 来指定相应的处理函数。以下是详细说明:
|
||||||
|
|
||||||
@@ -169,22 +169,24 @@ function safeAttrValue (tag, name, value) {
|
|||||||
|
|
||||||
### 快捷配置
|
### 快捷配置
|
||||||
|
|
||||||
#### stripIgnoreTag
|
#### 去掉不在白名单只的标签
|
||||||
|
|
||||||
是否去掉不在白名单只的标签:
|
通过 `stripIgnoreTag` 来设置:
|
||||||
|
|
||||||
+ `true`:(默认),去掉不在白名单中的标签
|
+ `true`:(默认),去掉不在白名单上的标签
|
||||||
+ `false`:使用配置的`escape`函数对该标签进行转义
|
+ `false`:使用配置的`escape`函数对该标签进行转义
|
||||||
|
|
||||||
|
#### 去掉不在白名单上的标签及标签体
|
||||||
|
|
||||||
|
通过 `stripIgnoreTagBody` 来设置:
|
||||||
|
|
||||||
|
+ `false|null|undefined`:(默认),不特殊处理
|
||||||
|
+ `'*'|true`:去掉所有不在白名单上的标签
|
||||||
|
+ `['tag1', 'tag2']`:仅去掉指定的不在白名单上的标签
|
||||||
|
|
||||||
|
|
||||||
## 应用实例
|
## 应用实例
|
||||||
|
|
||||||
### 去掉<script>标签及标签体内的JS代码
|
|
||||||
|
|
||||||
```JavaScript
|
|
||||||
// 待续
|
|
||||||
```
|
|
||||||
|
|
||||||
### 允许标签以data-开头的属性
|
### 允许标签以data-开头的属性
|
||||||
|
|
||||||
```JavaScript
|
```JavaScript
|
||||||
|
|||||||
@@ -273,12 +273,18 @@ function StripTagBody (tags, next) {
|
|||||||
next = function () {};
|
next = function () {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var isRemoveAllTag = !Array.isArray(tags);
|
||||||
|
function isRemoveTag (tag) {
|
||||||
|
if (isRemoveAllTag) return true;
|
||||||
|
return tags.indexOf(tag) !== -1;
|
||||||
|
}
|
||||||
|
|
||||||
var removeList = []; // 要删除的位置范围列表
|
var removeList = []; // 要删除的位置范围列表
|
||||||
var posStart = false; // 当前标签开始位置
|
var posStart = false; // 当前标签开始位置
|
||||||
|
|
||||||
return {
|
return {
|
||||||
onIgnoreTag: function (tag, html, options) {
|
onIgnoreTag: function (tag, html, options) {
|
||||||
if (tags.indexOf(tag) !== -1) {
|
if (isRemoveTag(tag)) {
|
||||||
if (options.isClosing) {
|
if (options.isClosing) {
|
||||||
var ret = '[/removed]';
|
var ret = '[/removed]';
|
||||||
var end = options.position + ret.length;
|
var end = options.position + ret.length;
|
||||||
|
|||||||
@@ -1,49 +0,0 @@
|
|||||||
/**
|
|
||||||
* 工具函数
|
|
||||||
*
|
|
||||||
* @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;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
21
lib/xss.js
21
lib/xss.js
@@ -48,7 +48,9 @@ function getAttrs (html) {
|
|||||||
/**
|
/**
|
||||||
* XSS过滤对象
|
* XSS过滤对象
|
||||||
*
|
*
|
||||||
* @param {Object} options 选项:whiteList, onTag, onTagAttr, onIgnoreTag, onIgnoreTagAttr, safeAttrValue, escapeHtml
|
* @param {Object} options 选项:whiteList, onTag, onTagAttr, onIgnoreTag,
|
||||||
|
* onIgnoreTagAttr, safeAttrValue, escapeHtml
|
||||||
|
* stripIgnoreTagBody
|
||||||
*/
|
*/
|
||||||
function FilterXSS (options) {
|
function FilterXSS (options) {
|
||||||
options = options || {};
|
options = options || {};
|
||||||
@@ -87,7 +89,15 @@ FilterXSS.prototype.process = function (html) {
|
|||||||
var safeAttrValue = options.safeAttrValue;
|
var safeAttrValue = options.safeAttrValue;
|
||||||
var escapeHtml = options.escapeHtml
|
var escapeHtml = options.escapeHtml
|
||||||
|
|
||||||
return parseTag(html, function (sourcePosition, position, tag, html, isClosing) {
|
// 如果开启了stripIgnoreTagBody
|
||||||
|
if (options.stripIgnoreTagBody) {
|
||||||
|
var stripIgnoreTagBody = DEFAULT.StripTagBody(options.stripIgnoreTagBody, onIgnoreTag);
|
||||||
|
onIgnoreTag = stripIgnoreTagBody.onIgnoreTag;
|
||||||
|
} else {
|
||||||
|
stripIgnoreTagBody = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var retHtml = parseTag(html, function (sourcePosition, position, tag, html, isClosing) {
|
||||||
var info = {
|
var info = {
|
||||||
sourcePosition: sourcePosition,
|
sourcePosition: sourcePosition,
|
||||||
position: position,
|
position: position,
|
||||||
@@ -148,6 +158,13 @@ FilterXSS.prototype.process = function (html) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}, escapeHtml);
|
}, escapeHtml);
|
||||||
|
|
||||||
|
// 如果开启了stripIgnoreTagBody,需要对结果再进行处理
|
||||||
|
if (stripIgnoreTagBody) {
|
||||||
|
retHtml = stripIgnoreTagBody.remove(retHtml);
|
||||||
|
}
|
||||||
|
|
||||||
|
return retHtml;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -263,4 +263,44 @@ describe('test custom XSS method', function () {
|
|||||||
assert.equal(html, 'yy<a>bb</a>');
|
assert.equal(html, 'yy<a>bb</a>');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('#stripTagBody - true', function () {
|
||||||
|
var source = '<a>link</a><x>haha</x><y>a<y></y>b</y>k';
|
||||||
|
var html = xss(source, {
|
||||||
|
stripIgnoreTagBody: true
|
||||||
|
});
|
||||||
|
console.log(html);
|
||||||
|
assert.equal(html, '<a>link</a>bk');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('#stripIgnoreTagBody - *', function () {
|
||||||
|
var source = '<a>link</a><x>haha</x><y>a<y></y>b</y>k';
|
||||||
|
var html = xss(source, {
|
||||||
|
stripIgnoreTagBody: '*'
|
||||||
|
});
|
||||||
|
console.log(html);
|
||||||
|
assert.equal(html, '<a>link</a>bk');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('#stripIgnoreTagBody - [\'x\']', function () {
|
||||||
|
var source = '<a>link</a><x>haha</x><y>a<y></y>b</y>k';
|
||||||
|
var html = xss(source, {
|
||||||
|
stripIgnoreTagBody: ['x']
|
||||||
|
});
|
||||||
|
console.log(html);
|
||||||
|
assert.equal(html, '<a>link</a><y>a<y></y>b</y>k');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('#stripIgnoreTagBody - [\'x\'] & onIgnoreTag', function () {
|
||||||
|
var source = '<a>link</a><x>haha</x><y>a<y></y>b</y>k';
|
||||||
|
var html = xss(source, {
|
||||||
|
stripIgnoreTagBody: ['x'],
|
||||||
|
onIgnoreTag: function (tag, html, options) {
|
||||||
|
return '$' + tag + '$';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
console.log(html);
|
||||||
|
assert.equal(html, '<a>link</a>$y$a$y$$y$b$y$k');
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user