xss-filter
xss-filter is a XSS (Cross-Site Script) Filter for Node.js & the browser, provides friendly, reliable XSS filter API for you.
Test HTML:
title desc just a div
Result in:
title desc just a div
Installation / Download
npm install xssfilter
or bower install xssFilter
or just download xssFilter.js from the git repo.
Usage
Node.js
var xssFilter = ;var xssfilter = ; var output = xssfilter;// output: <div class="like">something...</div>
Browser
<script src="./dist/xssFilter.js"></script><script> var xssfilter = ; var output = xssfilter; // output: <div class="like">something...</div></script>
require.js
Use with<script src="require.js"></script><script> ;</script>
sea.js
Use with<script src="sea.js"></script><script> seajs</script>
Manifest
matchStyleTag
whether match style
tag, default is true
. Set to false
to prevent remove the matched style
tags.
matchScriptTag
whether match script
tag, default is true
. Set to false
to prevent remove the matched script
tags.
removeMatchedTag
whether remove matched tag, default is true
. Set to false
to using escape instead of remove.
removeMatchedTag
should be used with matchStyleTag
and matchScriptTag
, for example:
var xssfilter = removeMatchedTag: false;
something...
Result in:
<style type="text"> .red{color: #f00} </style> something...<script>alert(88)</script>
blackListAttrs
attributes blacklist, attributes in this list will be cleared.
initial blacklist of attributes:
onclick: true ondblclick: true onchange: true onblur: true onfocus: true onkeydown: true onkeypress: true onkeyup: true onmousedown: true onmousemove: true onmouseover: true onmouseout: true onmouseup: true onselect: true onsubmit: true onreset: true onload: true onabort: true onerror: true
escape
escape tags of whole html string, "<" to "<", ">" to ">"
, default no.
Initialization
The configuration options can be specified by passing an options
parameter in the initialization. options
is optional, provided to override the default configuration.
var xssfilter = options;
Instance methods
filter
Filtering target string, accepts only one parameter.
options
Use this method to modify the configuration options after initialization.
var xssfilter = ; xssfilteroptions escape: true matchStyleTag: false; var output = xssfilter;
You can also configure single option:
var xssfilter = ;xssfilteroptions'escape' true;var output = xssfilter;
when set secondary attributes like blackListAttrs
, the second argument must be an object {}
:
var xssfilter = ; xssfilteroptions'blackListAttrs' onsubmit: false; var output = xssfilter;// output: <div class="like" onsubmit="dosomething()">something...</div>
Testing
npm test
Other xss filter view
License
MIT, see the LICENSE file for detail.