Gathering detailed insights and metrics for xss
Gathering detailed insights and metrics for xss
Gathering detailed insights and metrics for xss
Gathering detailed insights and metrics for xss
Sanitize untrusted HTML (to prevent XSS) with a configuration specified by a Whitelist
npm install xss
98.3
Supply Chain
99.6
Quality
77.1
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
5,224 Stars
434 Commits
627 Forks
118 Watching
106 Branches
38 Contributors
Updated on 27 Nov 2024
Minified
Minified + Gzipped
HTML (62.48%)
JavaScript (36.54%)
TypeScript (0.72%)
Shell (0.23%)
Batchfile (0.03%)
Cumulative downloads
Total Downloads
Last day
-6.9%
484,517
Compared to previous day
Last week
2.6%
2,600,188
Compared to previous week
Last month
9.4%
10,816,563
Compared to previous month
Last year
-6.1%
118,097,916
Compared to previous year
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
: 6.9 MB/sFor test code please refer to benchmark
directory.
1npm install xss
1bower install xss
Or
1bower install https://github.com/leizongmin/js-xss.git
1var xss = require("xss"); 2var html = xss('<script>alert("xss");</script>'); 3console.log(html);
Shim mode (reference file test/test.html
):
1<script src="https://rawgit.com/leizongmin/js-xss/master/dist/xss.js"></script> 2<script> 3 // apply function filterXSS in the same way 4 var html = filterXSS('<script>alert("xss");</scr' + "ipt>"); 5 alert(html); 6</script>
AMD mode - shim:
1<script> 2 require.config({ 3 baseUrl: "./", 4 paths: { 5 xss: "https://rawgit.com/leizongmin/js-xss/master/dist/xss.js", 6 }, 7 shim: { 8 xss: { exports: "filterXSS" }, 9 }, 10 }); 11 require(["xss"], function (xss) { 12 var html = xss('<script>alert("xss");</scr' + "ipt>"); 13 alert(html); 14 }); 15</script>
Notes: please don't use the URL https://rawgit.com/leizongmin/js-xss/master/dist/xss.js in production environment.
You can use the xss command line tool to process a file. Usage:
1xss -i <input_file> -o <output_file>
Example:
1xss -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:
1xss -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:
1options = {}; // Custom rules 2html = 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:
1options = {}; // Custom rules 2myxss = new xss.FilterXSS(options); 3// then apply myxss.process() 4html = myxss.process('<script>alert("xss");</script>');
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:
1// only tag a and its attributes href, title, target are allowed 2var options = { 3 whiteList: { 4 a: ["href", "title", "target"], 5 }, 6}; 7// With the configuration specified above, the following HTML: 8// <a href="#" onclick="hello()"><i>Hello</i></a> 9// would become: 10// <a href="#"><i>Hello</i></a>
For the default whitelist, please refer xss.whiteList
.
allowList
is also supported, and has the same function as whiteList
.
By specifying the handler function with onTag
:
1function onTag(tag, html, options) { 2 // tag is the name of current tag, e.g. 'a' for tag <a> 3 // html is the HTML of this tag, e.g. '<a>' for tag <a> 4 // options is some addition informations: 5 // isWhite boolean, whether the tag is in whitelist 6 // isClosing boolean, whether the tag is a closing tag, e.g. true for </a> 7 // position integer, the position of the tag in output result 8 // sourcePosition integer, the position of the tag in input HTML source 9 // If a string is returned, the current tag would be replaced with the string 10 // If return nothing, the default measure would be taken: 11 // If in whitelist: filter attributes using onTagAttr, as described below 12 // If not in whitelist: handle by onIgnoreTag, as described below 13}
By specifying the handler function with onTagAttr
:
1function onTagAttr(tag, name, value, isWhiteAttr) { 2 // tag is the name of current tag, e.g. 'a' for tag <a> 3 // name is the name of current attribute, e.g. 'href' for href="#" 4 // isWhiteAttr whether the attribute is in whitelist 5 // If a string is returned, the attribute would be replaced with the string 6 // If return nothing, the default measure would be taken: 7 // If in whitelist: filter the value using safeAttrValue as described below 8 // If not in whitelist: handle by onIgnoreTagAttr, as described below 9}
By specifying the handler function with onIgnoreTag
:
1function onIgnoreTag(tag, html, options) { 2 // Parameters are the same with onTag 3 // If a string is returned, the tag would be replaced with the string 4 // If return nothing, the default measure would be taken (specifies using 5 // escape, as described below) 6}
By specifying the handler function with onIgnoreTagAttr
:
1function onIgnoreTagAttr(tag, name, value, isWhiteAttr) { 2 // Parameters are the same with onTagAttr 3 // If a string is returned, the value would be replaced with this string 4 // If return nothing, then keep default (remove the attribute) 5}
By specifying the handler function with escapeHtml
. Following is the default
function (Modification is not recommended):
1function escapeHtml(html) { 2 return html.replace(/</g, "<").replace(/>/g, ">"); 3}
By specifying the handler function with safeAttrValue
:
1function safeAttrValue(tag, name, value) { 2 // Parameters are the same with onTagAttr (without options) 3 // Return the value as a string 4}
By specifying a singleQuotedAttributeValue
. Use true
for '
. Otherwise default "
will be used
1var options = { 2 singleQuotedAttributeValue: true, 3}; 4// With the configuration specified above, the following HTML: 5// <a href="#">Hello</a> 6// would become: 7// <a href='#'>Hello</a>
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:
1myxss = new xss.FilterXSS({ 2 css: { 3 whiteList: { 4 position: /^fixed|relative$/, 5 top: true, 6 left: true, 7 }, 8 }, 9}); 10html = myxss.process('<script>alert("xss");</script>');
If you don't want to filter out the style
content, just specify false
to the css
option:
1myxss = new xss.FilterXSS({ 2 css: false, 3});
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:
1code: 2<script> 3 alert(/xss/); 4</script>
would output filtered:
1code: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:
1code: 2<script> 3 alert(/xss/); 4</script>
would output filtered:
1code:
By using allowCommentTag
parameter:
true
: do nothingfalse
by default: filter out HTML commentsExample:
If allowCommentTag = false
is set, the following code:
1code:<!-- something --> 2END
would output filtered:
1code: END
data-
1var source = '<div a="1" b="2" data-a="3" data-b="4">hello</div>'; 2var html = xss(source, { 3 onIgnoreTagAttr: function (tag, name, value, isWhiteAttr) { 4 if (name.substr(0, 5) === "data-") { 5 // escape its value using built-in escapeAttrValue function 6 return name + '="' + xss.escapeAttrValue(value) + '"'; 7 } 8 }, 9}); 10 11console.log("%s\nconvert to:\n%s", source, html);
Result:
1<div a="1" b="2" data-a="3" data-b="4">hello</div> 2convert to: 3<div data-a="3" data-b="4">hello</div>
x-
1var source = "<x><x-1>he<x-2 checked></x-2>wwww</x-1><a>"; 2var html = xss(source, { 3 onIgnoreTag: function (tag, html, options) { 4 if (tag.substr(0, 2) === "x-") { 5 // do not filter its attributes 6 return html; 7 } 8 }, 9}); 10 11console.log("%s\nconvert to:\n%s", source, html);
Result:
1<x 2 ><x-1>he<x-2 checked></x-2>wwww</x-1 3 ><a> 4 convert to: <x><x-1>he<x-2 checked></x-2>wwww</x-1><a></a></a 5></x>
1var source = 2 '<img src="img1">a<img src="img2">b<img src="img3">c<img src="img4">d'; 3var list = []; 4var html = xss(source, { 5 onTagAttr: function (tag, name, value, isWhiteAttr) { 6 if (tag === "img" && name === "src") { 7 // Use the built-in friendlyAttrValue function to escape attribute 8 // values. It supports converting entity tags such as < to printable 9 // characters such as < 10 list.push(xss.friendlyAttrValue(value)); 11 } 12 // Return nothing, means keep the default handling measure 13 }, 14}); 15 16console.log("image list:\n%s", list.join(", "));
Result:
1image list: img1, img2, img3, img4
1var source = "<strong>hello</strong><script>alert(/xss/);</script>end"; 2var html = xss(source, { 3 whiteList: {}, // empty, means filter out all tags 4 stripIgnoreTag: true, // filter out all HTML not in the whitelist 5 stripIgnoreTagBody: ["script"], // the script tag is a special case, we need 6 // to filter out its content 7}); 8 9console.log("text: %s", html);
Result:
1text: helloend
1Copyright (c) 2012-2018 Zongmin Lei(雷宗民) <leizongmin@gmail.com> 2http://ucdok.com 3 4The MIT License 5 6Permission is hereby granted, free of charge, to any person obtaining 7a copy of this software and associated documentation files (the 8"Software"), to deal in the Software without restriction, including 9without limitation the rights to use, copy, modify, merge, publish, 10distribute, sublicense, and/or sell copies of the Software, and to 11permit persons to whom the Software is furnished to do so, subject to 12the following conditions: 13 14The above copyright notice and this permission notice shall be 15included in all copies or substantial portions of the Software. 16 17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
security policy file detected
Details
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
SAST tool detected but not run on all commits
Details
Reason
Found 12/28 approved changesets -- score normalized to 4
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More