PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/README.md

https://gitlab.com/lileeyao/js-xss
Markdown | 439 lines | 309 code | 130 blank | 0 comment | 0 complexity | 368f2a13390920ca94ec357228fae253 MD5 | raw file
  1. [![NPM version](https://badge.fury.io/js/xss.png)](http://badge.fury.io/js/xss)
  2. [![Build Status](https://secure.travis-ci.org/leizongmin/js-xss.png?branch=master)](http://travis-ci.org/leizongmin/js-xss)
  3. [![Dependencies Status](https://david-dm.org/leizongmin/js-xss.png)](https://david-dm.org/leizongmin/js-xss)
  4. Sanitize untrusted HTML (to prevent XSS) with a configuration specified by a Whitelist.
  5. ======
  6. ![xss](https://nodei.co/npm/xss.png?downloads=true&stars=true)
  7. --------------
  8. **[中文版文档](https://github.com/leizongmin/js-xss/blob/master/README.zh.md)**
  9. `xss` is a module used to filter input from users to prevent XSS attacks.
  10. ([What is XSS attack?](http://en.wikipedia.org/wiki/Cross-site_scripting))
  11. This module is needed for situations that allows users to input HTML for
  12. typesetting or formatting, including fourms, blogs, e-shops, etc.
  13. The `xss` module controls the usage of tags and their attributes, according to
  14. the whitelist. It is also extendable with a series of APIs privided, which make
  15. it become more flexible, compares with other modules.
  16. **Project Homepage:** https://github.com/leizongmin/js-xss
  17. **Try Online:** http://ucdok.com/project/xss/
  18. ---------------
  19. ## Features
  20. + Specifies HTML tags and their attributes allowed with whitelist
  21. + Handle any tags or attributes using custom function.
  22. ## Reference
  23. + [XSS与字符编码的那些事儿 ---科普文](http://drops.wooyun.org/tips/689)
  24. + [腾讯实例教程那些年我们一起学XSS](http://www.wooyun.org/whitehats/%E5%BF%83%E4%BC%A4%E7%9A%84%E7%98%A6%E5%AD%90)
  25. + [mXSS攻击的成因及常见种类](http://drops.wooyun.org/tips/956)
  26. + [XSS Filter Evasion Cheat Sheet](https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet)
  27. + [Data URI scheme](http://en.wikipedia.org/wiki/Data_URI_scheme)
  28. + [XSS with Data URI Scheme](http://hi.baidu.com/badzzzz/item/bdbafe83144619c199255f7b)
  29. ## Benchmark (for references only)
  30. + the xss module: 8.2 MB/s
  31. + `xss()` function from module `validator@0.3.7`: 4.4 MB/s
  32. For test code please refer to `benchmark` directory.
  33. ## Unit Test
  34. Run `npm test` command in the source directary.
  35. ## Install
  36. ### NPM
  37. ```bash
  38. $ npm install xss
  39. ```
  40. ### Bower
  41. ```bash
  42. $ bower install xss
  43. ```
  44. Or
  45. ```bash
  46. $ bower install https://github.com/leizongmin/js-xss.git
  47. ```
  48. ## Usages
  49. ### On Node.js
  50. ```JavaScript
  51. var xss = require('xss');
  52. var html = xss('<script>alert("xss");</script>');
  53. console.log(html);
  54. ```
  55. ### On Browser
  56. Shim mode (reference file `test/test.html`):
  57. ```HTML
  58. <script src="https://raw.github.com/leizongmin/js-xss/master/dist/xss.js"></script>
  59. <script>
  60. // apply function filterXSS in the same way
  61. var html = filterXSS('<script>alert("xss");</scr' + 'ipt>');
  62. alert(html);
  63. </script>
  64. ```
  65. AMD mode (reference file `test/test_amd.html`):
  66. ```HTML
  67. <script>
  68. require.config({
  69. baseUrl: './'
  70. })
  71. require(['xss'], function (xss) {
  72. var html = xss('<script>alert("xss");</scr' + 'ipt>');
  73. alert(html);
  74. });
  75. </script>
  76. ```
  77. ## Command Line Tool
  78. ### Process File
  79. You can use the xss command line tool to process a file. Usage:
  80. ```bash
  81. xss -i <input_file> -o <output_file>
  82. ```
  83. Example:
  84. ```bash
  85. $ xss -i origin.html -o target.html
  86. ```
  87. ### Active Test
  88. Run the following command, them you can type HTML
  89. code in the command-line, and check the filtered output:
  90. ```bash
  91. $ xss -t
  92. ```
  93. For more details, please run `$ xss -h` to see it.
  94. ## Custom filter rules
  95. When using the `xss()` function, the second parameter could be used to specify
  96. custom rules:
  97. ```JavaScript
  98. options = {}; // Custom rules
  99. html = xss('<script>alert("xss");</script>', options);
  100. ```
  101. To avoid passing `options` every time, you can also do it in a faster way by
  102. creating a `FilterXSS` instance:
  103. ```JavaScript
  104. options = {}; // Custom rules
  105. myxss = new xss.FilterXSS(options);
  106. // then apply myxss.process()
  107. html = myxss.process('<script>alert("xss");</script>');
  108. ```
  109. Details of parameters in `options` would be described below.
  110. ### Whitelist
  111. By specifying a `whiteList`, e.g. `{ 'tagName': [ 'attr-1', 'attr-2' ] }`. Tags
  112. and attributes not in the whitelist would be filter out. For example:
  113. ```JavaScript
  114. // only tag a and its attributes href, title, target are allowed
  115. var options = {
  116. whiteList: {
  117. a: ['href', 'title', 'target']
  118. }
  119. };
  120. // With the configuration specified above, the following HTML:
  121. // <a href="#" onclick="hello()"><i>Hello</i></a>
  122. // would become:
  123. // <a href="#">Hello</a>
  124. ```
  125. For the default whitelist, please refer `xss.whiteList`.
  126. ### Customize the handler function for matched tags
  127. By specifying the handler function with `onTag`:
  128. ```JavaScript
  129. function onTag (tag, html, options) {
  130. // tag is the name of current tag, e.g. 'a' for tag <a>
  131. // html is the HTML of this tag, e.g. '<a>' for tag <a>
  132. // options is some addition informations:
  133. // isWhite boolean, whether the tag is in whitelist
  134. // isClosing boolean, whether the tag is a closing tag, e.g. true for </a>
  135. // position integer, the position of the tag in output result
  136. // sourcePosition integer, the position of the tag in input HTML source
  137. // If a string is returned, the current tag would be replaced with the string
  138. // If return nothing, the default measure would be taken:
  139. // If in whitelist: filter attributes using onTagAttr, as described below
  140. // If not in whitelist: handle by onIgnoreTag, as described below
  141. }
  142. ```
  143. ### Customize the handler function for attributes of matched tags
  144. By specifying the handler function with `onTagAttr`:
  145. ```JavaScript
  146. function onTagAttr (tag, name, value, isWhiteAttr) {
  147. // tag is the name of current tag, e.g. 'a' for tag <a>
  148. // name is the name of current attribute, e.g. 'href' for href="#"
  149. // isWhiteAttr whether the tag is in whitelist
  150. // If a string is returned, the attribute would be replaced with the string
  151. // If return nothing, the default measure would be taken:
  152. // If in whitelist: filter the value using safeAttrValue as described below
  153. // If not in whitelist: handle by onIgnoreTagAttr, as described below
  154. }
  155. ```
  156. ### Customize the handler function for tags not in the whitelist
  157. By specifying the handler function with `onIgnoreTag`:
  158. ```JavaScript
  159. function onIgnoreTag (tag, html, options) {
  160. // Parameters are the same with onTag
  161. // If a string is returned, the tag would be replaced with the string
  162. // If return nothing, the default measure would be taken (specifies using
  163. // escape, as described below)
  164. }
  165. ```
  166. ### Customize the handler function for attributes not in the whitelist
  167. By specifying the handler function with `onIgnoreTagAttr`:
  168. ```JavaScript
  169. function onIgnoreTagAttr (tag, name, value, isWhiteAttr) {
  170. // Parameters are the same with onTagAttr
  171. // If a string is returned, the value would be replaced with this string
  172. // If return nothing, then keep default (remove the attribute)
  173. }
  174. ```
  175. ### Customize escaping function for HTML
  176. By specifying the handler function with `escapeHtml`. Following is the default
  177. function **(Modification is not recommended)**:
  178. ```JavaScript
  179. function escapeHtml (html) {
  180. return html.replace(/</g, '&lt;').replace(/>/g, '&gt;');
  181. }
  182. ```
  183. ### Customize escaping function for value of attributes
  184. By specifying the handler function with `safeAttrValue`:
  185. ```JavaScript
  186. function safeAttrValue (tag, name, value) {
  187. // Parameters are the same with onTagAttr (without options)
  188. // Return the value as a string
  189. }
  190. ```
  191. ### Quick Start
  192. #### Filter out tags not in the whitelist
  193. By using `stripIgnoreTag` parameter:
  194. + `true` filter out tags not in the whitelist
  195. + `false`: by default: escape the tag using configured `escape` function
  196. Example:
  197. If `stripIgnoreTag = true` is set, the following code:
  198. ```HTML
  199. code:<script>alert(/xss/);</script>
  200. ```
  201. would output filtered:
  202. ```HTML
  203. code:alert(/xss/);
  204. ```
  205. #### Filter out tags and tag bodies not in the whitelist
  206. By using `stripIgnoreTagBody` parameter:
  207. + `false|null|undefined` by default: do nothing
  208. + `'*'|true`: filter out all tags not in the whitelist
  209. + `['tag1', 'tag2']`: filter out only specified tags not in the whitelist
  210. Example:
  211. If `stripIgnoreTagBody = ['script']` is set, the following code:
  212. ```HTML
  213. code:<script>alert(/xss/);</script>
  214. ```
  215. would output filtered:
  216. ```HTML
  217. code:
  218. ```
  219. #### Filter out HTML comments
  220. By using `allowCommentTag` parameter:
  221. + `true`: do nothing
  222. + `false` by default: filter out HTML comments
  223. Example:
  224. If `allowCommentTag = false` is set, the following code:
  225. ```HTML
  226. code:<!-- something --> END
  227. ```
  228. would output filtered:
  229. ```HTML
  230. code: END
  231. ```
  232. ## Examples
  233. ### Allow attributes of whitelist tags start with `data-`
  234. ```JavaScript
  235. var source = '<div a="1" b="2" data-a="3" data-b="4">hello</div>';
  236. var html = xss(source, {
  237. onIgnoreTagAttr: function (tag, name, value, isWhiteAttr) {
  238. if (name.substr(0, 5) === 'data-') {
  239. // escape its value using built-in escapeAttrValue function
  240. return name + '="' + xss.escapeAttrValue(value) + '"';
  241. }
  242. }
  243. });
  244. console.log('%s\nconvert to:\n%s', source, html);
  245. ```
  246. Result:
  247. ```
  248. <div a="1" b="2" data-a="3" data-b="4">hello</div>
  249. convert to:
  250. <div data-a="3" data-b="4">hello</div>
  251. ```
  252. ### Allow tags start with `x-`
  253. ```JavaScript
  254. var source = '<x><x-1>he<x-2 checked></x-2>wwww</x-1><a>';
  255. var html = xss(source, {
  256. onIgnoreTag: function (tag, html, options) {
  257. if (tag.substr(0, 2) === 'x-') {
  258. // do not filter its attributes
  259. return html;
  260. }
  261. }
  262. });
  263. console.log('%s\nconvert to:\n%s', source, html);
  264. ```
  265. Result:
  266. ```
  267. <x><x-1>he<x-2 checked></x-2>wwww</x-1><a>
  268. convert to:
  269. &lt;x&gt;<x-1>he<x-2 checked></x-2>wwww</x-1><a>
  270. ```
  271. ### Parse images in HTML
  272. ```JavaScript
  273. var source = '<img src="img1">a<img src="img2">b<img src="img3">c<img src="img4">d';
  274. var list = [];
  275. var html = xss(source, {
  276. onTagAttr: function (tag, name, value, isWhiteAttr) {
  277. if (tag === 'img' && name === 'src') {
  278. // Use the built-in friendlyAttrValue function to escape attribute
  279. // values. It supports converting entity tags such as &lt; to printable
  280. // characters such as <
  281. list.push(xss.friendlyAttrValue(value));
  282. }
  283. // Return nothing, means keep the default handling measure
  284. }
  285. });
  286. console.log('image list:\n%s', list.join(', '));
  287. ```
  288. Result:
  289. ```
  290. image list:
  291. img1, img2, img3, img4
  292. ```
  293. ### Filter out HTML tags (keeps only plain text)
  294. ```JavaScript
  295. var source = '<strong>hello</strong><script>alert(/xss/);</script>end';
  296. var html = xss(source, {
  297. whiteList: [], // empty, means filter out all tags
  298. stripIgnoreTag: true, // filter out all HTML not in the whilelist
  299. stripIgnoreTagBody: ['script'] // the script tag is a special case, we need
  300. // to filter out its content
  301. });
  302. console.log('text: %s', html);
  303. ```
  304. Result:
  305. ```
  306. text: helloend
  307. ```
  308. ## License
  309. The MIT License