xss

NPM version Node.js CI Test coverage David deps node version npm download npm license

Sanitize untrusted HTML (to prevent XSS) with a configuration specified by a Whitelist.

Greenkeeper badge

xss

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

中文版文档


Features

  • Specifies HTML tags and their attributes allowed with whitelist

  • Handle any tags or attributes using custom function.

Reference

Benchmark (for references only)

For test code please refer to benchmark directory.

They are using xss module

  • 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

Install

NPM

Bower

Or

Usages

On Node.js

On Browser

Shim mode (reference file test/test.html):

AMD mode - shim:

Notes: please don't use the URL https://rawgit.com/leizongmin/js-xss/master/dist/xss.js in production environment.

Command Line Tool

Process File

You can use the xss command line tool to process a file. Usage:

Example:

Active Test

Run the following command, them you can type HTML code in the command-line, and check the filtered output:

For more details, please run $ xss -h to see it.

Custom filter rules

When using the xss() function, the second parameter could be used to specify custom rules:

To avoid passing options every time, you can also do it in a faster way by creating a FilterXSS instance:

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:

For the default whitelist, please refer xss.whiteList.

allowList is also supported, and has the same function as whiteList.

Customize the handler function for matched tags

By specifying the handler function with onTag:

Customize the handler function for attributes of matched tags

By specifying the handler function with onTagAttr:

Customize the handler function for tags not in the whitelist

By specifying the handler function with onIgnoreTag:

Customize the handler function for attributes not in the whitelist

By specifying the handler function with onIgnoreTagAttr:

Customize escaping function for HTML

By specifying the handler function with escapeHtml. Following is the default function (Modification is not recommended):

Customize escaping function for value of attributes

By specifying the handler function with safeAttrValue:

Customize CSS filter

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:

If you don't want to filter out the style content, just specify false to the css option:

For more help, please see https://github.com/leizongmin/js-css-filter

Quick Start

Filter out tags not in the whitelist

By using stripIgnoreTag parameter:

  • true filter out tags not in the whitelist

  • false: by default: escape the tag using configured escape function

Example:

If stripIgnoreTag = true is set, the following code:

would output filtered:

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:

would output filtered:

Filter out HTML comments

By using allowCommentTag parameter:

  • true: do nothing

  • false by default: filter out HTML comments

Example:

If allowCommentTag = false is set, the following code:

would output filtered:

Examples

Allow attributes of whitelist tags start with data-

Result:

Allow tags start with x-

Result:

Parse images in HTML

Result:

Filter out HTML tags (keeps only plain text)

Result:

License

Last updated