特性
- 白名单控制允许的 HTML 标签及各标签的属性
- 通过自定义处理函数,可对任意标签及其属性进行处理
参考资料
性能(仅作参考)
- xss 模块:22.53 MB/s
- validator@0.3.7 模块的 xss()函数:6.9 MB/s
测试代码参考 benchmark 目录
安装
NPM
npm install xss
Bower
bower install xss
或者
bower install https://github.com/leizongmin/js-xss.git
使用方法
在 Node.js 中使用
var xss = require("xss");
var html = xss('<script>alert("xss");</script>');
console.log(html);
在浏览器端使用
Shim 模式(参考文件 test/test.html):
<script src="https://rawgit.com/leizongmin/js-xss/master/dist/xss.js"></script>
<script>
var html = filterXSS('<script>alert("xss");</scr' + "ipt>");
alert(html);
</script>
AMD 模式(参考文件 test/test_amd.html):
<script>
require.config({
baseUrl: "./",
paths: {
xss: "https://rawgit.com/leizongmin/js-xss/master/dist/xss.js",
},
shim: {
xss: { exports: "filterXSS" },
},
});
require(["xss"], function (xss) {
var html = xss('<script>alert("xss");</scr' + "ipt>");
alert(html);
});
</script>
说明:请勿将 URL https://rawgit.com/leizongmin/js-xss/master/dist/xss.js 用于生产环境。
使用命令行工具来对文件进行 XSS 处理
处理文件
可通过内置的 xss 命令来对输入的文件进行 XSS 处理。使用方法:
xss -i <源文件> -o <目标文件>
例:
xss -i origin.html -o target.html
在线测试
执行以下命令,可在命令行中输入 HTML 代码,并看到过滤后的代码:
xss -t
详细命令行参数说明,请输入 $ xss -h 来查看。
自定义过滤规则
在调用 xss() 函数进行过滤时,可通过第二个参数来设置自定义规则:
options = {};
html = xss('<script>alert("xss");</script>', options);
如果不想每次都传入一个 options 参数,可以创建一个 FilterXSS 实例(使用这种方法速度更快):
options = {}; // 自定义规则
myxss = new xss.FilterXSS(options);
// 以后直接调用 myxss.process() 来处理即可
html = myxss.process('<script>alert("xss");</script>');
options 参数的详细说明见下文。
白名单
通过 whiteList 来指定,格式为:{'标签名': ['属性1', '属性2']}。不在白名单上的标签将被过滤,不在白名单上的属性也会被过滤。以下是示例:
var options = {
whiteList: {
a: ["href", "title", "target"],
},
};
默认白名单参考 xss.whiteList。
自定义匹配到标签时的处理方法
通过 onTag 来指定相应的处理函数。以下是详细说明:
function onTag(tag, html, options) {
}
自定义匹配到标签的属性时的处理方法
通过 onTagAttr 来指定相应的处理函数。以下是详细说明:
function onTagAttr(tag, name, value, isWhiteAttr) {
}
自定义匹配到不在白名单上的标签时的处理方法
通过 onIgnoreTag 来指定相应的处理函数。以下是详细说明:
function onIgnoreTag(tag, html, options) {
}
自定义匹配到不在白名单上的属性时的处理方法
通过 onIgnoreTagAttr 来指定相应的处理函数。以下是详细说明:
function onIgnoreTagAttr(tag, name, value, isWhiteAttr) {
}
自定义 HTML 转义函数
通过 escapeHtml 来指定相应的处理函数。以下是默认代码 (不建议修改) :
function escapeHtml(html) {
return html.replace(/</g, "<").replace(/>/g, ">");
}
自定义标签属性值的转义函数
通过 safeAttrValue 来指定相应的处理函数。以下是详细说明:
function safeAttrValue(tag, name, value) {
}
自定义 CSS 过滤器
如果配置中允许了标签的 style 属性,则它的值会通过cssfilter 模块处理。
cssfilter 模块包含了一个默认的 CSS 白名单,你可以通过以下的方式配置:
myxss = new xss.FilterXSS({
css: {
whiteList: {
position: /^fixed|relative$/,
top: true,
left: true,
},
},
});
html = myxss.process('<script>alert("xss");</script>');
如果不想使用 CSS 过滤器来处理 style 属性的内容,可指定 css 选项的值为 false:
myxss = new xss.FilterXSS({
css: false,
});
要获取更多的帮助信息可看这里:https://github.com/leizongmin/js-css-filter
快捷配置
去掉不在白名单上的标签
通过 stripIgnoreTag 来设置:
true:去掉不在白名单上的标签
false:(默认),使用配置的escape函数对该标签进行转义
示例:
当设置 stripIgnoreTag = true时,以下代码
code:
<script>
alert(/xss/);
</script>
过滤后将输出
code:alert(/xss/);
去掉不在白名单上的标签及标签体
通过 stripIgnoreTagBody 来设置:
false|null|undefined:(默认),不特殊处理
'*'|true:去掉所有不在白名单上的标签
['tag1', 'tag2']:仅去掉指定的不在白名单上的标签
示例:
当设置 stripIgnoreTagBody = ['script']时,以下代码
code:
<script>
alert(/xss/);
</script>
过滤后将输出
code:
去掉 HTML 备注
通过 allowCommentTag 来设置:
true:不处理
false:(默认),自动去掉 HTML 中的备注
示例:
当设置 allowCommentTag = false 时,以下代码
code:
END
过滤后将输出
code: END
应用实例
允许标签以 data-开头的属性
var source = '<div a="1" b="2" data-a="3" data-b="4">hello</div>';
var html = xss(source, {
onIgnoreTagAttr: function (tag, name, value, isWhiteAttr) {
if (name.substr(0, 5) === "data-") {
return name + '="' + xss.escapeAttrValue(value) + '"';
}
},
});
console.log("%s\nconvert to:\n%s", source, html);
运行结果:
<div a="1" b="2" data-a="3" data-b="4">hello</div>
convert to:
<div data-a="3" data-b="4">hello</div>
允许名称以 x-开头的标签
var source = "<x><x-1>he<x-2 checked></x-2>wwww</x-1><a>";
var html = xss(source, {
onIgnoreTag: function (tag, html, options) {
if (tag.substr(0, 2) === "x-") {
return html;
}
},
});
console.log("%s\nconvert to:\n%s", source, html);
运行结果:
<x
><x-1>he<x-2 checked></x-2>wwww</x-1
><a>
convert to: <x><x-1>he<x-2 checked></x-2>wwww</x-1><a></a></a
></x>
分析 HTML 代码中的图片列表
var source =
'<img src="img1">a<img src="img2">b<img src="img3">c<img src="img4">d';
var list = [];
var html = xss(source, {
onTagAttr: function (tag, name, value, isWhiteAttr) {
if (tag === "img" && name === "src") {
list.push(xss.friendlyAttrValue(value));
}
},
});
console.log("image list:\n%s", list.join(", "));
运行结果:
image list: img1, img2, img3, img4
去除 HTML 标签(只保留文本内容)
var source = "<strong>hello</strong><script>alert(/xss/);</script>end";
var html = xss(source, {
whiteList: {},
stripIgnoreTag: true,
stripIgnoreTagBody: ["script"],
});
console.log("text: %s", html);
运行结果:
text: helloend
授权协议
Copyright (c) 2012-2018 Zongmin Lei(雷宗民) <leizongmin@gmail.com>
http://ucdok.com
The MIT License
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.