doc.go (4489B)
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 /* 31 Package bluemonday provides a way of describing an allowlist of HTML elements 32 and attributes as a policy, and for that policy to be applied to untrusted 33 strings from users that may contain markup. All elements and attributes not on 34 the allowlist will be stripped. 35 36 The default bluemonday.UGCPolicy().Sanitize() turns this: 37 38 Hello <STYLE>.XSS{background-image:url("javascript:alert('XSS')");}</STYLE><A CLASS=XSS></A>World 39 40 Into the more harmless: 41 42 Hello World 43 44 And it turns this: 45 46 <a href="javascript:alert('XSS1')" onmouseover="alert('XSS2')">XSS<a> 47 48 Into this: 49 50 XSS 51 52 Whilst 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 58 To 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 64 The primary purpose of bluemonday is to take potentially unsafe user generated 65 content (from things like Markdown, HTML WYSIWYG tools, etc) and make it safe 66 for you to put on your website. 67 68 It protects sites against XSS (http://en.wikipedia.org/wiki/Cross-site_scripting) 69 and other malicious content that a user interface may deliver. There are many 70 vectors for an XSS attack (https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet) 71 and the safest thing to do is to sanitize user input against a known safe list 72 of HTML elements and attributes. 73 74 Note: You should always run bluemonday after any other processing. 75 76 If you use blackfriday (https://github.com/russross/blackfriday) or 77 Pandoc (http://johnmacfarlane.net/pandoc/) then bluemonday should be run after 78 these steps. This ensures that no insecure HTML is introduced later in your 79 process. 80 81 bluemonday 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 85 We ship two default policies, one is bluemonday.StrictPolicy() and can be 86 thought of as equivalent to stripping all HTML elements and their attributes as 87 it has nothing on its allowlist. 88 89 The other is bluemonday.UGCPolicy() and allows a broad selection of HTML 90 elements and attributes that are safe for user generated content. Note that 91 this policy does not allow iframes, object, embed, styles, script, etc. 92 93 The essence of building a policy is to determine which HTML elements and 94 attributes are considered safe for your scenario. OWASP provide an XSS 95 prevention cheat sheet ( https://www.google.com/search?q=xss+prevention+cheat+sheet ) 96 to 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 */ 104 package bluemonday