Sanitize untrusted HTML (to prevent XSS) with a configuration specified by a Whitelist.
NOTE: The format of custom configuration (except Whitelist) from version
0.0.X was changed a lot since version 0.1.X. To use a newer version, it's
suggested to read the following guidelines carefully.
中文版文档
xss
is a module used to filter input from users to prevent XSS attacks.
(What is XSS attack?)
This module is needed for situations that allows users to input HTML for
typesetting or formatting, including fourms, blogs, e-shops, etc.
The xss
module controls the usage of tags and their attributes, according to
the whitelist. It is also extendable with a series of APIs privided, which make
it become more flexible, compares with other modules.
Project Homepage: https://github.com/leizongmin/js-xss
Features
- Specifies HTML tags and their attributes allowed with whitelist
- Handle any tags or attributes using custom function.
Reference
Benchmark (for references only)
- the xss module: 8.2 MB/s
xss()
function from module validator@0.3.7
: 4.4 MB/s
For test code please refer to benchmark
directory.
Unit Test
Run npm test
command in the source directary.
Active Test
Run the following command, them you can type HTML
code in the command-line, and check the filtered output:
$ xss -t
Command Line Tool
You can use the xss command line tool to process a file. Usage:
xss -i <input_file> -o <output_file>
Example:
$ xss -i origin.html -o target.html
For more details, please run $ xss -h
to see it.
Usages
In Node.js
To install:
$ npm install xss
Simple usage:
var xss = require('xss');
var html = xss('<script>alert("xss");</script>');
console.log(html);
In browsers
<script src="https://raw.github.com/leizongmin/js-xss/master/build/xss.js"></script>
<script>
var html = filterXSS('<script>alert("xss");</scr' + 'ipt>');
alert(html);
</script>
Custom filter rules
When using the xss()
function, the second parameter could be used to specify
custom rules:
options = {};
html = xss('<script>alert("xss");</script>', options);
To avoid passing options
every time, you can also do it in a faster way by
creating a FilterXSS
instance:
options = {};
myxss = new xss.FilterXSS(options);
html = myxss.process('<script>alert("xss");</script>');
Details of parameters in options
would be described below.
Whitelist
By specifying a whiteList
, e.g. { 'tagName': [ 'attr-1', 'attr-2' ] }
. Tags
and attributes not in the whitelist would be filter out. For example:
var options = {
whiteList: {
a: ['href', 'title', 'target']
}
};
For the default whitelist, please refer xss.whiteList
.
Customize the handler function for matched tags
By specifying the handler function with onTag
:
function onTag (tag, html, options) {
}
Customize the handler function for attributes of matched tags
By specifying the handler function with onTagAttr
:
function onTagAttr (tag, name, value, isWhiteAttr) {
}
Customize the handler function for tags not in the whitelist
By specifying the handler function with onIgnoreTag
:
function onIgnoreTag (tag, html, options) {
}
Customize the handler function for attributes not in the whitelist
By specifying the handler function with onIgnoreTagAttr
:
function onIgnoreTagAttr (tag, name, value, isWhiteAttr) {
}
Customize escaping function for HTML
By specifying the handler function with escapeHtml
. Following is the default
function (Modification is not recommended):
function escapeHtml (html) {
return html.replace(/</g, '<').replace(/>/g, '>');
}
Customize escaping function for value of attributes
By specifying the handler function with safeAttrValue
:
function safeAttrValue (tag, name, value) {
}
Quick Start
Filter out tags not in the whitelist
By using stripIgnoreTag
parameter:
true
filter out tags not in the whitelistfalse
: by default: escape the tag using configured escape
function
Example:
If stripIgnoreTag = true
is set, the following code:
code:<script>alert(/xss/);</script>
would output filtered:
code:alert(/xss/);
Filter out tags and tag bodies not in the whitelist
By using stripIgnoreTagBody
parameter:
false|null|undefined
by default: do nothing'*'|true
: filter out all tags not in the whitelist['tag1', 'tag2']
: filter out only specified tags not in the whitelist
Example:
If stripIgnoreTagBody = ['script']
is set, the following code:
code:<script>alert(/xss/);</script>
would output filtered:
code:
Examples
Allow attributes of whitelist tags start with 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);
Result:
<div a="1" b="2" data-a="3" data-b="4">hello</div>
convert to:
<div data-a="3" data-b="4">hello</div>
Allow tags start with 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);
Result:
<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>
Parse images in 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(', '));
Result:
image list:
img1, img2, img3, img4
Filter out HTML tags (keeps only plain text)
var source = '<strong>hello</strong><script>alert(/xss/);</script>end';
var html = xss(source, {
whiteList: [],
stripIgnoreTag: true,
stripIgnoreTagBody: ['script']
});
console.log('text: %s', html);
Result:
text: helloend
License
The MIT License
Donate
We made xss in our spare time because it's fun to build things. We hope you find it useful.
If you like this package and want to support future development—or just say thank you, please consider making a donation of $5, $10 or $20 (or whatever you can afford) to help us continue to improve it.
[]
(https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=leizongmin%40qq%2ecom&lc=US&no_note=0¤cy_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHostedGuest)