Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 170 lines (119 sloc) 5.714 kb
50cdcf2 @rgrove Initial commit.
authored
1 = Sanitize
2
3 Sanitize is a whitelist-based HTML sanitizer. Given a list of acceptable
4 elements and attributes, Sanitize will remove all unacceptable HTML from a
5 string.
6
7 Using a simple configuration syntax, you can tell Sanitize to allow certain
8 elements, certain attributes within those elements, and even certain URL
9 protocols within attributes that contain URLs. Any HTML elements or attributes
10 that you don't explicitly allow will be removed.
11
12 Because it's based on Hpricot, a full-fledged HTML parser, rather than a bunch
13 of fragile regular expressions, Sanitize has no trouble dealing with malformed
14 or maliciously-formed HTML. When in doubt, Sanitize always errs on the side of
15 caution.
16
17 *Author*:: Ryan Grove (mailto:ryan@wonko.com)
ff87e40 @rgrove Release 1.0.7
authored
18 *Version*:: 1.0.7 (2009-04-11)
77c428a @rgrove Happy New Year!
authored
19 *Copyright*:: Copyright (c) 2009 Ryan Grove. All rights reserved.
50cdcf2 @rgrove Initial commit.
authored
20 *License*:: MIT License (http://opensource.org/licenses/mit-license.php)
21 *Website*:: http://github.com/rgrove/sanitize
22
23 == Requires
24
25 * RubyGems
26 * Hpricot 0.6+
27
28 == Usage
29
30 If you don't specify any configuration options, Sanitize will use its strictest
31 settings by default, which means it will strip all HTML.
32
33 require 'rubygems'
34 require 'sanitize'
35
36 html = '<b><a href="http://foo.com/">foo</a></b><img src="http://foo.com/bar.jpg" />'
37
38 Sanitize.clean(html) # => 'foo'
39
40 == Configuration
41
42 In addition to the ultra-safe default settings, Sanitize comes with three other
43 built-in modes.
44
45 === Sanitize::Config::RESTRICTED
46
47 Allows only very simple inline formatting markup. No links, images, or block
48 elements.
49
50 Sanitize.clean(html, Sanitize::Config::RESTRICTED) # => '<b>foo</b>'
51
52 === Sanitize::Config::BASIC
53
54 Allows a variety of markup including formatting tags, links, and lists. Images
55 and tables are not allowed, links are limited to FTP, HTTP, HTTPS, and mailto
56 protocols, and a <code>rel="nofollow"</code> attribute is added to all links to
57 mitigate SEO spam.
58
59 Sanitize.clean(html, Sanitize::Config::BASIC)
60 # => '<b><a href="http://foo.com/" rel="nofollow">foo</a></b>'
61
62 === Sanitize::Config::RELAXED
63
64 Allows an even wider variety of markup than BASIC, including images and tables.
65 Links are still limited to FTP, HTTP, HTTPS, and mailto protocols, while images
66 are limited to HTTP and HTTPS. In this mode, <code>rel="nofollow"</code> is not
67 added to links.
68
69 Sanitize.clean(html, Sanitize::Config::RELAXED)
70 # => '<b><a href="http://foo.com/">foo</a></b><img src="http://foo.com/bar.jpg" />'
71
72 === Custom Configuration
73
74 If the built-in modes don't meet your needs, you can easily specify a custom
75 configuration:
76
77 Sanitize.clean(html, :elements => ['a', 'span'],
78 :attributes => {'a' => ['href', 'title'], 'span' => ['class']},
79 :protocols => {'a' => {'href' => ['http', 'https', 'mailto']}})
80
81 ==== :elements
82
83 Array of element names to allow. Specify all names in lowercase.
84
85 :elements => [
86 'a', 'b', 'blockquote', 'br', 'cite', 'code', 'dd', 'dl', 'dt', 'em',
87 'i', 'li', 'ol', 'p', 'pre', 'q', 'small', 'strike', 'strong', 'sub',
88 'sup', 'u', 'ul'
89 ]
90
91 ==== :attributes
92
93 Attributes to allow for specific elements. Specify all element names and
94 attributes in lowercase.
95
96 :attributes => {
97 'a' => ['href', 'title'],
98 'blockquote' => ['cite'],
99 'img' => ['alt', 'src', 'title']
100 }
101
c0495a8 @rgrove Use :all instead of '*' to specify attributes to be added to all elem…
authored
102 If you'd like to allow certain attributes on all elements, use the symbol
103 <code>:all</code> instead of an element name.
104
105 :attributes => {
106 :all => ['class'],
31fd7e3 @rgrove Fix stray comma.
authored
107 'a' => ['href', 'title']
c0495a8 @rgrove Use :all instead of '*' to specify attributes to be added to all elem…
authored
108 }
109
50cdcf2 @rgrove Initial commit.
authored
110 ==== :add_attributes
111
112 Attributes to add to specific elements. If the attribute already exists, it will
113 be replaced with the value specified here. Specify all element names and
114 attributes in lowercase.
115
116 :add_attributes => {
117 'a' => {'rel' => 'nofollow'}
118 }
119
120 ==== :protocols
121
122 URL protocols to allow in specific attributes. If an attribute is listed here
123 and contains a protocol other than those specified (or if it contains no
124 protocol at all), it will be removed.
125
126 :protocols => {
127 'a' => {'href' => ['ftp', 'http', 'https', 'mailto']},
128 'img' => {'src' => ['http', 'https']}
129 }
130
5682777 @rgrove You can now specify :relative in a protocol config array to allow att…
authored
131 If you'd like to allow the use of relative URLs which don't have a protocol,
c0495a8 @rgrove Use :all instead of '*' to specify attributes to be added to all elem…
authored
132 include the symbol <code>:relative</code> in the protocol array:
5682777 @rgrove You can now specify :relative in a protocol config array to allow att…
authored
133
134 :protocols => {
135 'a' => {'href' => ['http', 'https', :relative]}
136 }
137
91d22e4 @rgrove Add list of contributors.
authored
138
139 == Contributors
140
141 The following lovely people have contributed to Sanitize in the form of patches
142 or ideas that later became code:
143
144 * Ryan Grove <ryan@wonko.com>
11ae498 @rgrove Whoops, forgot one.
authored
145 * Adam Hooper <adam@adamhooper.com>
91d22e4 @rgrove Add list of contributors.
authored
146 * Mutwin Kraus <mutle@blogage.de>
147 * Dev Purkayastha <dev.purkayastha@gmail.com>
71238c1 @rgrove Work around an Hpricot bug that prevents attribute names from being d…
authored
148 * Ben Wanicur <bwanicur@verticalresponse.com>
91d22e4 @rgrove Add list of contributors.
authored
149
50cdcf2 @rgrove Initial commit.
authored
150 == License
151
ad7b483 @rgrove Release 1.0.1
authored
152 Copyright (c) 2009 Ryan Grove <ryan@wonko.com>
50cdcf2 @rgrove Initial commit.
authored
153
154 Permission is hereby granted, free of charge, to any person obtaining a copy of
155 this software and associated documentation files (the 'Software'), to deal in
156 the Software without restriction, including without limitation the rights to
157 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
158 the Software, and to permit persons to whom the Software is furnished to do so,
159 subject to the following conditions:
160
161 The above copyright notice and this permission notice shall be included in all
162 copies or substantial portions of the Software.
163
164 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
165 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
166 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
167 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
168 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
169 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Something went wrong with that request. Please try again.