
Sanitize untrusted HTML (to prevent XSS) with a configuration specified by a Whitelist

xss是一个用于对用户输入的内容进行过滤,以避免遭受 XSS 攻击的模块(什么是 XSS 攻击?)。主要用于论坛、博客、网上商店等等一些可允许用户录入页面排版、格式控制相关的 HTML 的场景,xss模块通过白名单来控制允许的标签及相关的标签属性,另外还提供了一系列的接口以便用户扩展,比其他同类模块更为灵活。
项目主页: http://jsxss.com
在线测试: http://jsxss.com/zh/try.html
测试代码参考 benchmark 目录
npm install xss
bower install xss
或者
bower install https://github.com/leizongmin/js-xss.git
var xss = require("xss");
var html = xss('<script>alert("xss");</script>');
console.log(html);
可以使用 filterXSSWithResult 函数同时获取过滤后的 HTML 和被移除的标签/属性列表:
var xss = require("xss");
var result = xss.filterXSSWithResult('<script>alert("xss");</script><a href="#" onclick="evil()">click</a>');
console.log(result.html);
// 输出: "<script>alert(\"xss\");</script><a href=\"#\">click</a>"
console.log(result.removed);
// 输出: [
// { type: "tag", tag: "script", html: "<script>", isClosing: false },
// { type: "tag", tag: "script", html: "</script>", isClosing: true },
// { type: "attr", tag: "a", attr: "onclick", value: "evil()" }
// ]
Shim 模式(参考文件 test/test.html):
<script src="https://rawgit.com/leizongmin/js-xss/master/dist/xss.js"></script>
<script>
// 使用函数名 filterXSS,用法一样
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 -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']}。不在白名单上的标签将被过滤,不在白名单上的属性也会被过滤。以下是示例:
// 只允许a标签,该标签只允许href, title, target这三个属性
var options = {
whiteList: {
a: ["href", "title", "target"],
},
};
// 使用以上配置后,下面的HTML
// <a href="#" onclick="hello()"><i>大家好</i></a>
// 将被过滤为
// <a href="#">大家好</a>
默认白名单参考 xss.whiteList。
通过 onTag 来指定相应的处理函数。以下是详细说明:
function onTag(tag, html, options) {
// tag是当前的标签名称,比如<a>标签,则tag的值是'a'
// html是该标签的HTML,比如<a>标签,则html的值是'<a>'
// options是一些附加的信息,具体如下:
// isWhite boolean类型,表示该标签是否在白名单上
// isClosing boolean类型,表示该标签是否为闭合标签,比如</a>时为true
// position integer类型,表示当前标签在输出的结果中的起始位置
// sourcePosition integer类型,表示当前标签在原HTML中的起始位置
// 如果返回一个字符串,则当前标签将被替换为该字符串
// 如果不返回任何值,则使用默认的处理方法:
// 在白名单上: 通过onTagAttr来过滤属性,详见下文
// 不在白名单上:通过onIgnoreTag指定,详见下文
}
通过 onTagAttr 来指定相应的处理函数。以下是详细说明:
function onTagAttr(tag, name, value, isWhiteAttr) {
// tag是当前的标签名称,比如<a>标签,则tag的值是'a'
// name是当前属性的名称,比如href="#",则name的值是'href'
// value是当前属性的值,比如href="#",则value的值是'#'
// isWhiteAttr是否为白名单上的属性
// 如果返回一个字符串,则当前属性值将被替换为该字符串
// 如果不返回任何值,则使用默认的处理方法
// 在白名单上: 调用safeAttrValue来过滤属性值,并输出该属性,详见下文
// 不在白名单上:通过onIgnoreTagAttr指定,详见下文
}
通过 onIgnoreTag 来指定相应的处理函数。以下是详细说明:
function onIgnoreTag(tag, html, options) {
// 参数说明与onTag相同
// 如果返回一个字符串,则当前标签将被替换为该字符串
// 如果不返回任何值,则使用默认的处理方法(通过escape指定,详见下文)
}
通过 onIgnoreTagAttr 来指定相应的处理函数。以下是详细说明:
function onIgnoreTagAttr(tag, name, value, isWhiteAttr) {
// 参数说明与onTagAttr相同
// 如果返回一个字符串,则当前属性值将被替换为该字符串
// 如果不返回任何值,则使用默认的处理方法(删除该属性)
}
通过 escapeHtml 来指定相应的处理函数。以下是默认代码 (不建议修改) :
function escapeHtml(html) {
return html.replace(/</g, "<").replace(/>/g, ">");
}
当自定义白名单允许 action 或 formaction 属性时,默认 safeAttrValue
也会检查其提交地址。表单地址允许 HTTP(S) 或相对 URL(包括路径、查询参数和片段),
其他协议(包括 javascript: 和 data:)会被拒绝。
href 和 src 的 data:image/ 例外不适用于表单提交地址。
通过 safeAttrValue 来指定相应的处理函数。以下是详细说明:
function safeAttrValue(tag, name, value) {
// 参数说明与onTagAttr相同(没有options参数)
// 返回一个字符串表示该属性值
}
如果配置中允许了标签的 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:
通过 allowCommentTag 来设置:
true:不处理false:(默认),自动去掉 HTML 中的备注示例:
当设置 allowCommentTag = false 时,以下代码
code:<!-- something -->
END
过滤后将输出