You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
4.4 KiB

  1. // Copyright (c) 2014, David Kitchen <david@buro9.com>
  2. //
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice, this
  9. // list of conditions and the following disclaimer.
  10. //
  11. // * Redistributions in binary form must reproduce the above copyright notice,
  12. // this list of conditions and the following disclaimer in the documentation
  13. // and/or other materials provided with the distribution.
  14. //
  15. // * Neither the name of the organisation (Microcosm) nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  23. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  25. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. /*
  30. Package bluemonday provides a way of describing a whitelist of HTML elements
  31. and attributes as a policy, and for that policy to be applied to untrusted
  32. strings from users that may contain markup. All elements and attributes not on
  33. the whitelist will be stripped.
  34. The default bluemonday.UGCPolicy().Sanitize() turns this:
  35. Hello <STYLE>.XSS{background-image:url("javascript:alert('XSS')");}</STYLE><A CLASS=XSS></A>World
  36. Into the more harmless:
  37. Hello World
  38. And it turns this:
  39. <a href="javascript:alert('XSS1')" onmouseover="alert('XSS2')">XSS<a>
  40. Into this:
  41. XSS
  42. Whilst still allowing this:
  43. <a href="http://www.google.com/">
  44. <img src="https://ssl.gstatic.com/accounts/ui/logo_2x.png"/>
  45. </a>
  46. To pass through mostly unaltered (it gained a rel="nofollow"):
  47. <a href="http://www.google.com/" rel="nofollow">
  48. <img src="https://ssl.gstatic.com/accounts/ui/logo_2x.png"/>
  49. </a>
  50. The primary purpose of bluemonday is to take potentially unsafe user generated
  51. content (from things like Markdown, HTML WYSIWYG tools, etc) and make it safe
  52. for you to put on your website.
  53. It protects sites against XSS (http://en.wikipedia.org/wiki/Cross-site_scripting)
  54. and other malicious content that a user interface may deliver. There are many
  55. vectors for an XSS attack (https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet)
  56. and the safest thing to do is to sanitize user input against a known safe list
  57. of HTML elements and attributes.
  58. Note: You should always run bluemonday after any other processing.
  59. If you use blackfriday (https://github.com/russross/blackfriday) or
  60. Pandoc (http://johnmacfarlane.net/pandoc/) then bluemonday should be run after
  61. these steps. This ensures that no insecure HTML is introduced later in your
  62. process.
  63. bluemonday is heavily inspired by both the OWASP Java HTML Sanitizer
  64. (https://code.google.com/p/owasp-java-html-sanitizer/) and the HTML Purifier
  65. (http://htmlpurifier.org/).
  66. We ship two default policies, one is bluemonday.StrictPolicy() and can be
  67. thought of as equivalent to stripping all HTML elements and their attributes as
  68. it has nothing on it's whitelist.
  69. The other is bluemonday.UGCPolicy() and allows a broad selection of HTML
  70. elements and attributes that are safe for user generated content. Note that
  71. this policy does not whitelist iframes, object, embed, styles, script, etc.
  72. The essence of building a policy is to determine which HTML elements and
  73. attributes are considered safe for your scenario. OWASP provide an XSS
  74. prevention cheat sheet ( https://www.google.com/search?q=xss+prevention+cheat+sheet )
  75. to help explain the risks, but essentially:
  76. 1. Avoid whitelisting anything other than plain HTML elements
  77. 2. Avoid whitelisting `script`, `style`, `iframe`, `object`, `embed`, `base`
  78. elements
  79. 3. Avoid whitelisting anything other than plain HTML elements with simple
  80. values that you can match to a regexp
  81. */
  82. package bluemonday