Files
js-xss/README.md

500 lines
13 KiB
Markdown
Raw Normal View History

2015-11-18 10:44:05 +08:00
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![David deps][david-image]][david-url]
[![node version][node-image]][node-url]
[![npm download][download-image]][download-url]
2016-10-26 12:15:47 +08:00
[![npm license][license-image]][download-url]
2015-11-18 10:44:05 +08:00
[npm-image]: https://img.shields.io/npm/v/xss.svg?style=flat-square
[npm-url]: https://npmjs.org/package/xss
2015-11-18 10:48:47 +08:00
[travis-image]: https://img.shields.io/travis/leizongmin/js-xss.svg?style=flat-square
[travis-url]: https://travis-ci.org/leizongmin/js-xss
[coveralls-image]: https://img.shields.io/coveralls/leizongmin/js-xss.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/leizongmin/js-xss?branch=master
[david-image]: https://img.shields.io/david/leizongmin/js-xss.svg?style=flat-square
[david-url]: https://david-dm.org/leizongmin/js-xss
2015-11-18 10:58:17 +08:00
[node-image]: https://img.shields.io/badge/node.js-%3E=_0.10-green.svg?style=flat-square
2015-11-18 10:44:05 +08:00
[node-url]: http://nodejs.org/download/
[download-image]: https://img.shields.io/npm/dm/xss.svg?style=flat-square
[download-url]: https://npmjs.org/package/xss
2016-10-26 12:15:47 +08:00
[license-image]: https://img.shields.io/npm/l/xss.svg
2016-10-12 13:51:37 +08:00
2017-12-21 14:19:10 +08:00
# Sanitize untrusted HTML (to prevent XSS) with a configuration specified by a Whitelist.
2014-03-02 23:22:42 +08:00
2017-12-21 03:32:57 +00:00
[![Greenkeeper badge](https://badges.greenkeeper.io/leizongmin/js-xss.svg)](https://greenkeeper.io/)
2014-03-02 23:22:42 +08:00
![xss](https://nodei.co/npm/xss.png?downloads=true&stars=true)
2017-12-21 14:19:10 +08:00
---
2014-03-02 23:22:42 +08:00
`xss` is a module used to filter input from users to prevent XSS attacks.
([What is XSS attack?](http://en.wikipedia.org/wiki/Cross-site_scripting))
2015-08-18 18:47:03 +08:00
**Project Homepage:** http://jsxss.com
2014-03-02 23:22:42 +08:00
2015-08-18 18:47:03 +08:00
**Try Online:** http://jsxss.com/en/try.html
2014-07-25 16:42:10 +08:00
2016-05-04 12:46:13 +08:00
**[中文版文档](https://github.com/leizongmin/js-xss/blob/master/README.zh.md)**
2017-12-21 14:19:10 +08:00
---
2014-03-02 23:22:42 +08:00
## Features
2017-12-21 14:19:10 +08:00
* Specifies HTML tags and their attributes allowed with whitelist
* Handle any tags or attributes using custom function.
2014-03-02 23:22:42 +08:00
## Reference
2017-12-21 14:19:10 +08:00
* [XSS Filter Evasion Cheat Sheet](https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet)
* [Data URI scheme](http://en.wikipedia.org/wiki/Data_URI_scheme)
* [XSS with Data URI Scheme](http://hi.baidu.com/badzzzz/item/bdbafe83144619c199255f7b)
2014-03-02 23:22:42 +08:00
## Benchmark (for references only)
2020-07-27 10:21:57 +08:00
* the xss module: 22.53 MB/s
* `xss()` function from module `validator@0.3.7`: 6.9 MB/s
2014-03-02 23:22:42 +08:00
For test code please refer to `benchmark` directory.
2016-10-12 13:51:37 +08:00
## They are using xss module
2014-03-02 23:22:42 +08:00
2017-12-21 14:19:10 +08:00
* **nodeclub** - A Node.js bbs using MongoDB - https://github.com/cnodejs/nodeclub
* **cnpmjs.org** - Private npm registry and web for Enterprise - https://github.com/cnpm/cnpmjs.org
2014-03-02 23:22:42 +08:00
2015-01-16 20:27:23 +08:00
## Install
2014-03-02 23:22:42 +08:00
2015-01-16 20:27:23 +08:00
### NPM
2014-03-03 19:55:31 +08:00
```bash
2018-05-24 15:21:20 +08:00
npm install xss
2014-03-03 19:55:31 +08:00
```
2015-01-16 20:27:23 +08:00
### Bower
2014-03-03 19:55:31 +08:00
2014-03-03 20:01:34 +08:00
```bash
2018-05-24 15:21:20 +08:00
bower install xss
2014-03-03 20:01:34 +08:00
```
2014-03-03 19:55:31 +08:00
2015-01-16 20:27:23 +08:00
Or
2014-03-03 19:55:31 +08:00
```bash
2018-05-24 15:21:20 +08:00
bower install https://github.com/leizongmin/js-xss.git
2014-03-03 19:55:31 +08:00
```
2014-03-02 23:22:42 +08:00
## Usages
2015-01-16 20:27:23 +08:00
### On Node.js
2014-03-02 23:22:42 +08:00
2015-11-18 10:21:56 +08:00
```javascript
2017-12-21 14:19:10 +08:00
var xss = require("xss");
2014-03-02 23:22:42 +08:00
var html = xss('<script>alert("xss");</script>');
console.log(html);
```
2015-01-16 20:27:23 +08:00
### On Browser
Shim mode (reference file `test/test.html`):
2014-03-02 23:22:42 +08:00
2015-11-18 10:21:56 +08:00
```html
2018-05-24 15:20:02 +08:00
<script src="https://rawgit.com/leizongmin/js-xss/master/dist/xss.js"></script>
2014-03-02 23:22:42 +08:00
<script>
// apply function filterXSS in the same way
var html = filterXSS('<script>alert("xss");</scr' + 'ipt>');
alert(html);
</script>
```
AMD mode - shim:
2015-01-16 20:27:23 +08:00
2015-11-18 10:21:56 +08:00
```html
2015-01-16 20:27:23 +08:00
<script>
require.config({
baseUrl: './',
paths: {
2018-05-24 15:20:02 +08:00
xss: 'https://rawgit.com/leizongmin/js-xss/master/dist/xss.js'
},
shim: {
xss: {exports: 'filterXSS'}
}
2015-01-16 20:27:23 +08:00
})
require(['xss'], function (xss) {
var html = xss('<script>alert("xss");</scr' + 'ipt>');
alert(html);
});
</script>
```
2018-05-24 15:20:02 +08:00
**Notes: please don't use the URL https://rawgit.com/leizongmin/js-xss/master/dist/xss.js in production environment.**
2015-01-16 20:27:23 +08:00
## Command Line Tool
2015-01-16 20:29:41 +08:00
### Process File
2015-01-16 20:27:23 +08:00
You can use the xss command line tool to process a file. Usage:
```bash
2015-01-16 20:27:23 +08:00
xss -i <input_file> -o <output_file>
```
Example:
```bash
2018-05-24 15:21:20 +08:00
xss -i origin.html -o target.html
```
2015-01-16 20:27:23 +08:00
### Active Test
Run the following command, them you can type HTML
code in the command-line, and check the filtered output:
```bash
2018-05-24 15:21:20 +08:00
xss -t
2015-01-16 20:27:23 +08:00
```
For more details, please run `$ xss -h` to see it.
2014-03-02 23:22:42 +08:00
## Custom filter rules
When using the `xss()` function, the second parameter could be used to specify
custom rules:
2015-11-18 10:21:56 +08:00
```javascript
2017-12-21 14:19:10 +08:00
options = {}; // Custom rules
2014-03-02 23:22:42 +08:00
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:
2015-11-18 10:21:56 +08:00
```javascript
2017-12-21 14:19:10 +08:00
options = {}; // Custom rules
2014-03-02 23:22:42 +08:00
myxss = new xss.FilterXSS(options);
// then apply myxss.process()
html = myxss.process('<script>alert("xss");</script>');
```
Details of parameters in `options` would be described below.
### Whitelist
2014-03-07 10:27:35 +08:00
By specifying a `whiteList`, e.g. `{ 'tagName': [ 'attr-1', 'attr-2' ] }`. Tags
2014-03-02 23:22:42 +08:00
and attributes not in the whitelist would be filter out. For example:
2015-11-18 10:21:56 +08:00
```javascript
2014-03-02 23:22:42 +08:00
// only tag a and its attributes href, title, target are allowed
var options = {
whiteList: {
2017-12-21 14:19:10 +08:00
a: ["href", "title", "target"]
2014-03-02 23:22:42 +08:00
}
};
// With the configuration specified above, the following HTML:
// <a href="#" onclick="hello()"><i>Hello</i></a>
// would become:
2020-08-03 09:06:31 -04:00
// <a href="#">&lt;i&gt;Hello&lt;/i&gt;</a>
2014-03-02 23:22:42 +08:00
```
For the default whitelist, please refer `xss.whiteList`.
### Customize the handler function for matched tags
2014-03-07 10:27:35 +08:00
By specifying the handler function with `onTag`:
2014-03-02 23:22:42 +08:00
2015-11-18 10:21:56 +08:00
```javascript
2017-12-21 14:19:10 +08:00
function onTag(tag, html, options) {
2014-03-02 23:22:42 +08:00
// 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
}
```
### Customize the handler function for attributes of matched tags
2014-03-07 10:27:35 +08:00
By specifying the handler function with `onTagAttr`:
2014-03-02 23:22:42 +08:00
2015-11-18 10:21:56 +08:00
```javascript
2017-12-21 14:19:10 +08:00
function onTagAttr(tag, name, value, isWhiteAttr) {
2014-03-02 23:22:42 +08:00
// 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="#"
2017-02-20 21:27:44 +02:00
// isWhiteAttr whether the attribute is in whitelist
2014-03-02 23:22:42 +08:00
// 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
}
```
### Customize the handler function for tags not in the whitelist
2014-03-07 10:27:35 +08:00
By specifying the handler function with `onIgnoreTag`:
2014-03-02 23:22:42 +08:00
2015-11-18 10:21:56 +08:00
```javascript
2017-12-21 14:19:10 +08:00
function onIgnoreTag(tag, html, options) {
2014-03-02 23:22:42 +08:00
// 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)
}
```
### Customize the handler function for attributes not in the whitelist
2014-03-07 10:27:35 +08:00
By specifying the handler function with `onIgnoreTagAttr`:
2014-03-02 23:22:42 +08:00
2015-11-18 10:21:56 +08:00
```javascript
2017-12-21 14:19:10 +08:00
function onIgnoreTagAttr(tag, name, value, isWhiteAttr) {
2014-03-02 23:22:42 +08:00
// 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)
}
```
### Customize escaping function for HTML
2014-03-07 10:27:35 +08:00
By specifying the handler function with `escapeHtml`. Following is the default
2014-03-02 23:22:42 +08:00
function **(Modification is not recommended)**:
2015-11-18 10:21:56 +08:00
```javascript
2017-12-21 14:19:10 +08:00
function escapeHtml(html) {
return html.replace(/</g, "&lt;").replace(/>/g, "&gt;");
2014-03-02 23:22:42 +08:00
}
```
### Customize escaping function for value of attributes
2014-03-07 10:27:35 +08:00
By specifying the handler function with `safeAttrValue`:
2014-03-02 23:22:42 +08:00
2015-11-18 10:21:56 +08:00
```javascript
2017-12-21 14:19:10 +08:00
function safeAttrValue(tag, name, value) {
2014-03-02 23:22:42 +08:00
// Parameters are the same with onTagAttr (without options)
// Return the value as a string
}
```
2016-10-12 13:51:37 +08:00
### Customize CSS filter
If you allow the attribute `style`, the value will be processed by [cssfilter](https://github.com/leizongmin/js-css-filter) module. The cssfilter module includes a default css whitelist. You can specify the options for cssfilter module like this:
```javascript
myxss = new xss.FilterXSS({
css: {
whiteList: {
position: /^fixed|relative$/,
top: true,
2017-12-21 14:19:10 +08:00
left: true
2016-10-12 13:51:37 +08:00
}
}
});
html = myxss.process('<script>alert("xss");</script>');
```
If you don't want to filter out the `style` content, just specify `false` to the `css` option:
```javascript
myxss = new xss.FilterXSS({
2017-12-21 14:19:10 +08:00
css: false
});
```
2016-10-12 13:51:37 +08:00
For more help, please see https://github.com/leizongmin/js-css-filter
2014-03-02 23:22:42 +08:00
### Quick Start
#### Filter out tags not in the whitelist
By using `stripIgnoreTag` parameter:
2017-12-21 14:19:10 +08:00
* `true` filter out tags not in the whitelist
* `false`: by default: escape the tag using configured `escape` function
2014-03-02 23:22:42 +08:00
Example:
If `stripIgnoreTag = true` is set, the following code:
2015-11-18 10:21:56 +08:00
```html
2014-03-02 23:22:42 +08:00
code:<script>alert(/xss/);</script>
```
would output filtered:
2015-11-18 10:21:56 +08:00
```html
2014-03-02 23:22:42 +08:00
code:alert(/xss/);
```
#### Filter out tags and tag bodies not in the whitelist
By using `stripIgnoreTagBody` parameter:
2017-12-21 14:19:10 +08:00
* `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
2014-03-02 23:22:42 +08:00
Example:
If `stripIgnoreTagBody = ['script']` is set, the following code:
2015-11-18 10:21:56 +08:00
```html
2014-03-02 23:22:42 +08:00
code:<script>alert(/xss/);</script>
```
would output filtered:
2015-11-18 10:21:56 +08:00
```html
2014-03-02 23:22:42 +08:00
code:
```
#### Filter out HTML comments
By using `allowCommentTag` parameter:
2017-12-21 14:19:10 +08:00
* `true`: do nothing
* `false` by default: filter out HTML comments
Example:
If `allowCommentTag = false` is set, the following code:
2015-11-18 10:21:56 +08:00
```html
code:<!-- something --> END
```
would output filtered:
2015-11-18 10:21:56 +08:00
```html
code: END
```
2014-03-02 23:22:42 +08:00
## Examples
### Allow attributes of whitelist tags start with `data-`
2014-03-02 23:22:42 +08:00
2015-11-18 10:21:56 +08:00
```javascript
2014-03-02 23:22:42 +08:00
var source = '<div a="1" b="2" data-a="3" data-b="4">hello</div>';
var html = xss(source, {
2017-12-21 14:19:10 +08:00
onIgnoreTagAttr: function(tag, name, value, isWhiteAttr) {
if (name.substr(0, 5) === "data-") {
2014-03-02 23:22:42 +08:00
// escape its value using built-in escapeAttrValue function
return name + '="' + xss.escapeAttrValue(value) + '"';
}
}
});
2017-12-21 14:19:10 +08:00
console.log("%s\nconvert to:\n%s", source, html);
2014-03-02 23:22:42 +08:00
```
Result:
2016-05-05 21:45:38 +08:00
```html
2014-03-02 23:22:42 +08:00
<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-`
2015-11-18 10:21:56 +08:00
```javascript
2017-12-21 14:19:10 +08:00
var source = "<x><x-1>he<x-2 checked></x-2>wwww</x-1><a>";
2014-03-02 23:22:42 +08:00
var html = xss(source, {
2017-12-21 14:19:10 +08:00
onIgnoreTag: function(tag, html, options) {
if (tag.substr(0, 2) === "x-") {
2014-03-02 23:22:42 +08:00
// do not filter its attributes
return html;
}
}
});
2017-12-21 14:19:10 +08:00
console.log("%s\nconvert to:\n%s", source, html);
2014-03-02 23:22:42 +08:00
```
Result:
2016-05-05 21:45:38 +08:00
```html
2014-03-02 23:22:42 +08:00
<x><x-1>he<x-2 checked></x-2>wwww</x-1><a>
convert to:
&lt;x&gt;<x-1>he<x-2 checked></x-2>wwww</x-1><a>
```
### Parse images in HTML
2015-11-18 10:21:56 +08:00
```javascript
2017-12-21 14:19:10 +08:00
var source =
'<img src="img1">a<img src="img2">b<img src="img3">c<img src="img4">d';
2014-03-02 23:22:42 +08:00
var list = [];
var html = xss(source, {
2017-12-21 14:19:10 +08:00
onTagAttr: function(tag, name, value, isWhiteAttr) {
if (tag === "img" && name === "src") {
2014-03-02 23:22:42 +08:00
// Use the built-in friendlyAttrValue function to escape attribute
// values. It supports converting entity tags such as &lt; to printable
// characters such as <
list.push(xss.friendlyAttrValue(value));
}
// Return nothing, means keep the default handling measure
}
});
2017-12-21 14:19:10 +08:00
console.log("image list:\n%s", list.join(", "));
2014-03-02 23:22:42 +08:00
```
Result:
2016-05-05 21:45:38 +08:00
```html
2014-03-02 23:22:42 +08:00
image list:
img1, img2, img3, img4
```
### Filter out HTML tags (keeps only plain text)
2015-11-18 10:21:56 +08:00
```javascript
2017-12-21 14:19:10 +08:00
var source = "<strong>hello</strong><script>alert(/xss/);</script>end";
2014-03-02 23:22:42 +08:00
var html = xss(source, {
whiteList: {}, // empty, means filter out all tags
2020-01-29 20:46:43 -08:00
stripIgnoreTag: true, // filter out all HTML not in the whitelist
2017-12-21 14:19:10 +08:00
stripIgnoreTagBody: ["script"] // the script tag is a special case, we need
// to filter out its content
2014-03-02 23:22:42 +08:00
});
2017-12-21 14:19:10 +08:00
console.log("text: %s", html);
2014-03-02 23:22:42 +08:00
```
Result:
2016-05-05 21:45:38 +08:00
```html
2014-03-02 23:22:42 +08:00
text: helloend
```
## License
2017-12-21 14:19:10 +08:00
```text
2018-05-24 15:21:45 +08:00
Copyright (c) 2012-2018 Zongmin Lei(雷宗民) <leizongmin@gmail.com>
2015-12-01 22:07:53 +08:00
http://ucdok.com
2014-03-02 23:22:42 +08:00
The MIT License
2015-12-01 22:07:53 +08:00
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.
```