属性值过滤: HTML5新增实体编码 冒号: 换行

This commit is contained in:
18
lib/index.js
18
lib/index.js
@@ -54,6 +54,8 @@ var REGEXP_GT = />/g;
|
||||
var REGEXP_QUOTE = /"/g;
|
||||
var REGEXP_ATTR_NAME = /[^a-zA-Z0-9_:\.\-]/img;
|
||||
var REGEXP_ATTR_VALUE_1 = /&#([a-zA-Z0-9]*);?/img;
|
||||
var REGEXP_ATTR_VALUE_COLON = /:?/img;
|
||||
var REGEXP_ATTR_VALUE_NEWLINE = /&newline;?/img;
|
||||
var REGEXP_DEFAULT_ON_TAG_ATTR_1 = /\/\*|\*\//mg;
|
||||
var REGEXP_DEFAULT_ON_TAG_ATTR_2 = /^[\s"'`]*((j\s*a\s*v\s*a|v\s*b|l\s*i\s*v\s*e)\s*s\s*c\s*r\s*i\s*p\s*t\s*|m\s*o\s*c\s*h\s*a):/ig;
|
||||
var REGEXP_DEFAULT_ON_TAG_ATTR_3 = /\/\*|\*\//mg;
|
||||
@@ -154,6 +156,17 @@ function escapeHtmlEntities (str) {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 对html5新增的危险实体编码进行转义
|
||||
*
|
||||
* @param {String} str
|
||||
* @return {String}
|
||||
*/
|
||||
function escapeDangerHtml5Entities (str) {
|
||||
return str.replace(REGEXP_ATTR_VALUE_COLON, ':')
|
||||
.replace(REGEXP_ATTR_VALUE_NEWLINE, ' ');
|
||||
}
|
||||
|
||||
/**
|
||||
* 对属性值进行转义
|
||||
*
|
||||
@@ -167,9 +180,12 @@ function safeAttrValue (str) {
|
||||
// 过滤双引号
|
||||
str = escapeQuotes(str);
|
||||
|
||||
// 转换unicode字符 及过滤不可见字符
|
||||
// 转换HTML实体编码
|
||||
str = escapeHtmlEntities(str);
|
||||
|
||||
// 转换危险的HTML5新增实体编码
|
||||
str = escapeDangerHtml5Entities(str);
|
||||
|
||||
// 清除不可见字符
|
||||
str = clearNonPrintableCharacter(str);
|
||||
|
||||
|
||||
@@ -209,6 +209,11 @@ describe('test XSS', function () {
|
||||
assert.equal(xss('<!--[if gte IE 4]><SCRIPT>alert(\'XSS\');</SCRIPT><![endif]-->'),
|
||||
'<!--[if gte IE 4]><SCRIPT>alert(\'XSS\');</SCRIPT><![endif]-->');
|
||||
|
||||
// HTML5新增实体编码 冒号: 换行

|
||||
assert.equal(xss('<a href="javascript:alert(/xss/)">'), '<a href="#">');
|
||||
assert.equal(xss('<a href="javascript&colonalert(/xss/)">'), '<a href="#">');
|
||||
assert.equal(xss('<a href="a
b">'), '<a href="a b">');
|
||||
assert.equal(xss('<a href="a&NewLineb">'), '<a href="a b">');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user