doc.go

  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/*
 31Package bluemonday provides a way of describing an allowlist of HTML elements
 32and attributes as a policy, and for that policy to be applied to untrusted
 33strings from users that may contain markup. All elements and attributes not on
 34the allowlist will be stripped.
 35
 36The default bluemonday.UGCPolicy().Sanitize() turns this:
 37
 38	Hello <STYLE>.XSS{background-image:url("javascript:alert('XSS')");}</STYLE><A CLASS=XSS></A>World
 39
 40Into the more harmless:
 41
 42	Hello World
 43
 44And it turns this:
 45
 46	<a href="javascript:alert('XSS1')" onmouseover="alert('XSS2')">XSS<a>
 47
 48Into this:
 49
 50	XSS
 51
 52Whilst still allowing this:
 53
 54	<a href="http://www.google.com/">
 55	  <img src="https://ssl.gstatic.com/accounts/ui/logo_2x.png"/>
 56	</a>
 57
 58To pass through mostly unaltered (it gained a rel="nofollow"):
 59
 60	<a href="http://www.google.com/" rel="nofollow">
 61	  <img src="https://ssl.gstatic.com/accounts/ui/logo_2x.png"/>
 62	</a>
 63
 64The primary purpose of bluemonday is to take potentially unsafe user generated
 65content (from things like Markdown, HTML WYSIWYG tools, etc) and make it safe
 66for you to put on your website.
 67
 68It protects sites against XSS (http://en.wikipedia.org/wiki/Cross-site_scripting)
 69and other malicious content that a user interface may deliver. There are many
 70vectors for an XSS attack (https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet)
 71and the safest thing to do is to sanitize user input against a known safe list
 72of HTML elements and attributes.
 73
 74Note: You should always run bluemonday after any other processing.
 75
 76If you use blackfriday (https://github.com/russross/blackfriday) or
 77Pandoc (http://johnmacfarlane.net/pandoc/) then bluemonday should be run after
 78these steps. This ensures that no insecure HTML is introduced later in your
 79process.
 80
 81bluemonday is heavily inspired by both the OWASP Java HTML Sanitizer
 82(https://code.google.com/p/owasp-java-html-sanitizer/) and the HTML Purifier
 83(http://htmlpurifier.org/).
 84
 85We ship two default policies, one is bluemonday.StrictPolicy() and can be
 86thought of as equivalent to stripping all HTML elements and their attributes as
 87it has nothing on its allowlist.
 88
 89The other is bluemonday.UGCPolicy() and allows a broad selection of HTML
 90elements and attributes that are safe for user generated content. Note that
 91this policy does not allow iframes, object, embed, styles, script, etc.
 92
 93The essence of building a policy is to determine which HTML elements and
 94attributes are considered safe for your scenario. OWASP provide an XSS
 95prevention cheat sheet ( https://www.google.com/search?q=xss+prevention+cheat+sheet )
 96to help explain the risks, but essentially:
 97
 98 1. Avoid allowing anything other than plain HTML elements
 99 2. Avoid allowing `script`, `style`, `iframe`, `object`, `embed`, `base`
100    elements
101 3. Avoid allowing anything other than plain HTML elements with simple
102    values that you can match to a regexp
103*/
104package bluemonday