What is xss?
The xss npm package is a library designed to sanitize input from users to prevent Cross-Site Scripting (XSS) attacks. It filters input from the user and escapes or removes any potentially malicious scripts, ensuring that the output is safe to display on web pages.
What are xss's main functionalities?
HTML Filtering
This feature allows you to filter out any HTML tags and content that could lead to XSS attacks, leaving only the safe content.
const xss = require('xss');
let html = '<script>alert("xss");</script><div>safe content</div>';
let safeHtml = xss(html);
console.log(safeHtml); // Output: '<div>safe content</div>'
Custom Rule Configuration
This feature allows you to define custom rules for what HTML tags and attributes are allowed, giving you fine-grained control over the sanitization process.
const xss = require('xss');
let options = {
whiteList: {
a: ['href', 'title', 'target'],
p: [],
div: []
},
stripIgnoreTag: true
};
let html = '<a href="http://example.com" onclick="stealCookies()">Link</a>';
let safeHtml = xss(html, options);
console.log(safeHtml); // Output: '<a href="http://example.com">Link</a>'
Escape HTML
This feature provides a method to escape HTML, converting HTML special characters to their corresponding entities, which is useful when you want to display the original HTML code on the web page without rendering it.
const xss = require('xss');
let html = '<div>hello</div>';
let escapedHtml = xss.escapeHtml(html);
console.log(escapedHtml); // Output: '<div>hello</div>'
Other packages similar to xss
sanitize-html
sanitize-html is another popular HTML sanitization library that allows you to specify allowed tags and attributes. It is similar to xss but offers a different API and additional options for sanitization.
dompurify
dompurify is a DOM-only XSS sanitizer for HTML, MathML, and SVG. It's different from xss in that it works in the browser and uses the DOM to sanitize input, which can be more effective in some cases.
过滤XSS攻击
安装
npm install xss
原理
通过标签白名单及属性白名单来过滤HTML标签,同时对包含特殊字符的属性值进行处理。默认配置可过滤大多数的XSS攻击代码,可根据实际应用场景来定制白名单及过滤方法。
使用方法
载入模块
var xss = require('xss');
使用默认的配置
var html = xss('<script>alert("xss");</script>');
console.log(html);
修改默认配置
xss.whiteList['p'] = ['class', 'style'];
xss.onTagAttr = function (tag, attr, vaule) {
};
xss.onIgnoreTag = function (tag, html) {
}
使用临时配置
var options = {
whiteList: {},
onTagAttr: function () {},
onIgnoreTag: function () {}
};
var html = xss('<script>alert("xss");</script>', options);
console.log(html);
测试
单元测试
在源码目录执行命令:npm test
在线测试
在源码目录执行命令:node cli.js,可在命令行中输入HTML代码,并看到过滤后的代码
性能
解析速度为5.81MB/s,而另外一个validator模块的xss()函数速度仅为2.48MB/s。
测试代码参考benchmark目录
授权协议
基于MIT协议发布:
Copyright (c) 2012 Lei Zongmin(雷宗民) <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.