xss
is a module used to filter input from users to prevent XSS attacks.
(What is XSS attack?)
Project Homepage: http://jsxss.com
Try Online: http://jsxss.com/en/try.html
xss()
function from module validator@0.3.7
: 4.4 MB/sFor test code please refer to benchmark
directory.
$ npm install xss
$ bower install xss
Or
$ bower install https://github.com/leizongmin/js-xss.git
var xss = ;var html = ;console;
Shim mode (reference file test/test.html
):
AMD mode - shim:
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
Run the following command, them you can type HTML code in the command-line, and check the filtered output:
$ xss -t
For more details, please run $ xss -h
to see it.
When using the xss()
function, the second parameter could be used to specify
custom rules:
options = {}; // Custom rules html = ;
To avoid passing options
every time, you can also do it in a faster way by
creating a FilterXSS
instance:
options = {}; // Custom rules myxss = options;// then apply myxss.process() html = myxss;
Details of parameters in options
would be described below.
By specifying a whiteList
, e.g. { 'tagName': [ 'attr-1', 'attr-2' ] }
. Tags
and attributes not in the whitelist would be filter out. For example:
// only tag a and its attributes href, title, target are allowed var options = whiteList: a: 'href' 'title' 'target' ;// With the configuration specified above, the following HTML: // <a href="#" onclick="hello()"><i>Hello</i></a> // would become: // <a href="#">Hello</a>
For the default whitelist, please refer xss.whiteList
.
By specifying the handler function with onTag
:
{ // tag is the name of current tag, e.g. 'a' for tag <a> // html is the HTML of this tag, e.g. '<a>' for tag <a> // options is some addition informations: // isWhite boolean, whether the tag is in whitelist // isClosing boolean, whether the tag is a closing tag, e.g. true for </a> // position integer, the position of the tag in output result // sourcePosition integer, the position of the tag in input HTML source // If a string is returned, the current tag would be replaced with the string // If return nothing, the default measure would be taken: // If in whitelist: filter attributes using onTagAttr, as described below // If not in whitelist: handle by onIgnoreTag, as described below }
By specifying the handler function with onTagAttr
:
{ // tag is the name of current tag, e.g. 'a' for tag <a> // name is the name of current attribute, e.g. 'href' for href="#" // isWhiteAttr whether the tag is in whitelist // If a string is returned, the attribute would be replaced with the string // If return nothing, the default measure would be taken: // If in whitelist: filter the value using safeAttrValue as described below // If not in whitelist: handle by onIgnoreTagAttr, as described below }
By specifying the handler function with onIgnoreTag
:
{ // Parameters are the same with onTag // If a string is returned, the tag would be replaced with the string // If return nothing, the default measure would be taken (specifies using // escape, as described below) }
By specifying the handler function with onIgnoreTagAttr
:
{ // Parameters are the same with onTagAttr // If a string is returned, the value would be replaced with this string // If return nothing, then keep default (remove the attribute) }
By specifying the handler function with escapeHtml
. Following is the default
function (Modification is not recommended):
{ return html;}
By specifying the handler function with safeAttrValue
:
{ // Parameters are the same with onTagAttr (without options) // Return the value as a string }
If you allow the attribute style
, the value will be processed by cssfilter module. The cssfilter module includes a default css whitelist. You can specify the options for cssfilter module like this:
myxss = css: whiteList: position: /^fixed|relative$/ top: true left: true ;html = myxss;
If you don't want to filter out the style
content, just specify false
to the css
option:
myxss = css: false;
For more help, please see https://github.com/leizongmin/js-css-filter
By using stripIgnoreTag
parameter:
true
filter out tags not in the whitelistfalse
: by default: escape the tag using configured escape
functionExample:
If stripIgnoreTag = true
is set, the following code:
code:
would output filtered:
code:alert(/xss/);
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 whitelistExample:
If stripIgnoreTagBody = ['script']
is set, the following code:
code:
would output filtered:
code:
By using allowCommentTag
parameter:
true
: do nothingfalse
by default: filter out HTML commentsExample:
If allowCommentTag = false
is set, the following code:
code:<!-- something --> END
would output filtered:
code: END
data-
var source = '<div a="1" b="2" data-a="3" data-b="4">hello</div>';var html = ; console;
Result:
helloconvert to:hello
x-
var source = '<x><x-1>he<x-2 checked></x-2>wwww</x-1><a>';var html = ; console;
Result:
hewwwwconvert to:<x>hewwww
var source = '<img src="img1">a<img src="img2">b<img src="img3">c<img src="img4">d';var list = ;var html = ; console;
Result:
image list:img1, img2, img3, img4
var source = '<strong>hello</strong><script>alert(/xss/);</script>end';var html = ; console;
Result:
text: helloend
Copyright (c) 2012-2016 Zongmin Lei(雷宗民) <leizongmin@gmail.com>http://ucdok.com The MIT License Permission is hereby granted, free of charge, to any person obtaininga copy of this software and associated documentation files (the"Software"), to deal in the Software without restriction, includingwithout limitation the rights to use, copy, modify, merge, publish,distribute, sublicense, and/or sell copies of the Software, and topermit persons to whom the Software is furnished to do so, subject tothe following conditions: The above copyright notice and this permission notice shall beincluded 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 OFMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE ANDNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BELIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTIONOF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTIONWITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.