Froze rails gems
[depot.git] / vendor / rails / actionpack / lib / action_view / helpers / sanitize_helper.rb
1 require 'action_view/helpers/tag_helper'
2
3 begin
4 require 'html/document'
5 rescue LoadError
6 html_scanner_path = "#{File.dirname(__FILE__)}/../../action_controller/vendor/html-scanner"
7 if File.directory?(html_scanner_path)
8 $:.unshift html_scanner_path
9 require 'html/document'
10 end
11 end
12
13 module ActionView
14 module Helpers #:nodoc:
15 # The SanitizeHelper module provides a set of methods for scrubbing text of undesired HTML elements.
16 # These helper methods extend ActionView making them callable within your template files.
17 module SanitizeHelper
18 # This +sanitize+ helper will html encode all tags and strip all attributes that aren't specifically allowed.
19 # It also strips href/src tags with invalid protocols, like javascript: especially. It does its best to counter any
20 # tricks that hackers may use, like throwing in unicode/ascii/hex values to get past the javascript: filters. Check out
21 # the extensive test suite.
22 #
23 # <%= sanitize @article.body %>
24 #
25 # You can add or remove tags/attributes if you want to customize it a bit. See ActionView::Base for full docs on the
26 # available options. You can add tags/attributes for single uses of +sanitize+ by passing either the <tt>:attributes</tt> or <tt>:tags</tt> options:
27 #
28 # Normal Use
29 #
30 # <%= sanitize @article.body %>
31 #
32 # Custom Use (only the mentioned tags and attributes are allowed, nothing else)
33 #
34 # <%= sanitize @article.body, :tags => %w(table tr td), :attributes => %w(id class style)
35 #
36 # Add table tags to the default allowed tags
37 #
38 # Rails::Initializer.run do |config|
39 # config.action_view.sanitized_allowed_tags = 'table', 'tr', 'td'
40 # end
41 #
42 # Remove tags to the default allowed tags
43 #
44 # Rails::Initializer.run do |config|
45 # config.after_initialize do
46 # ActionView::Base.sanitized_allowed_tags.delete 'div'
47 # end
48 # end
49 #
50 # Change allowed default attributes
51 #
52 # Rails::Initializer.run do |config|
53 # config.action_view.sanitized_allowed_attributes = 'id', 'class', 'style'
54 # end
55 #
56 # Please note that sanitizing user-provided text does not guarantee that the
57 # resulting markup is valid (conforming to a document type) or even well-formed.
58 # The output may still contain e.g. unescaped '<', '>', '&' characters and
59 # confuse browsers.
60 #
61 def sanitize(html, options = {})
62 self.class.white_list_sanitizer.sanitize(html, options)
63 end
64
65 # Sanitizes a block of CSS code. Used by +sanitize+ when it comes across a style attribute.
66 def sanitize_css(style)
67 self.class.white_list_sanitizer.sanitize_css(style)
68 end
69
70 # Strips all HTML tags from the +html+, including comments. This uses the
71 # html-scanner tokenizer and so its HTML parsing ability is limited by
72 # that of html-scanner.
73 #
74 # ==== Examples
75 #
76 # strip_tags("Strip <i>these</i> tags!")
77 # # => Strip these tags!
78 #
79 # strip_tags("<b>Bold</b> no more! <a href='more.html'>See more here</a>...")
80 # # => Bold no more! See more here...
81 #
82 # strip_tags("<div id='top-bar'>Welcome to my website!</div>")
83 # # => Welcome to my website!
84 def strip_tags(html)
85 self.class.full_sanitizer.sanitize(html)
86 end
87
88 # Strips all link tags from +text+ leaving just the link text.
89 #
90 # ==== Examples
91 # strip_links('<a href="http://www.rubyonrails.org">Ruby on Rails</a>')
92 # # => Ruby on Rails
93 #
94 # strip_links('Please e-mail me at <a href="mailto:me@email.com">me@email.com</a>.')
95 # # => Please e-mail me at me@email.com.
96 #
97 # strip_links('Blog: <a href="http://www.myblog.com/" class="nav" target=\"_blank\">Visit</a>.')
98 # # => Blog: Visit
99 def strip_links(html)
100 self.class.link_sanitizer.sanitize(html)
101 end
102
103 module ClassMethods #:nodoc:
104 attr_writer :full_sanitizer, :link_sanitizer, :white_list_sanitizer
105
106 def sanitized_protocol_separator
107 white_list_sanitizer.protocol_separator
108 end
109
110 def sanitized_uri_attributes
111 white_list_sanitizer.uri_attributes
112 end
113
114 def sanitized_bad_tags
115 white_list_sanitizer.bad_tags
116 end
117
118 def sanitized_allowed_tags
119 white_list_sanitizer.allowed_tags
120 end
121
122 def sanitized_allowed_attributes
123 white_list_sanitizer.allowed_attributes
124 end
125
126 def sanitized_allowed_css_properties
127 white_list_sanitizer.allowed_css_properties
128 end
129
130 def sanitized_allowed_css_keywords
131 white_list_sanitizer.allowed_css_keywords
132 end
133
134 def sanitized_shorthand_css_properties
135 white_list_sanitizer.shorthand_css_properties
136 end
137
138 def sanitized_allowed_protocols
139 white_list_sanitizer.allowed_protocols
140 end
141
142 def sanitized_protocol_separator=(value)
143 white_list_sanitizer.protocol_separator = value
144 end
145
146 # Gets the HTML::FullSanitizer instance used by +strip_tags+. Replace with
147 # any object that responds to +sanitize+.
148 #
149 # Rails::Initializer.run do |config|
150 # config.action_view.full_sanitizer = MySpecialSanitizer.new
151 # end
152 #
153 def full_sanitizer
154 @full_sanitizer ||= HTML::FullSanitizer.new
155 end
156
157 # Gets the HTML::LinkSanitizer instance used by +strip_links+. Replace with
158 # any object that responds to +sanitize+.
159 #
160 # Rails::Initializer.run do |config|
161 # config.action_view.link_sanitizer = MySpecialSanitizer.new
162 # end
163 #
164 def link_sanitizer
165 @link_sanitizer ||= HTML::LinkSanitizer.new
166 end
167
168 # Gets the HTML::WhiteListSanitizer instance used by sanitize and +sanitize_css+.
169 # Replace with any object that responds to +sanitize+.
170 #
171 # Rails::Initializer.run do |config|
172 # config.action_view.white_list_sanitizer = MySpecialSanitizer.new
173 # end
174 #
175 def white_list_sanitizer
176 @white_list_sanitizer ||= HTML::WhiteListSanitizer.new
177 end
178
179 # Adds valid HTML attributes that the +sanitize+ helper checks for URIs.
180 #
181 # Rails::Initializer.run do |config|
182 # config.action_view.sanitized_uri_attributes = 'lowsrc', 'target'
183 # end
184 #
185 def sanitized_uri_attributes=(attributes)
186 HTML::WhiteListSanitizer.uri_attributes.merge(attributes)
187 end
188
189 # Adds to the Set of 'bad' tags for the +sanitize+ helper.
190 #
191 # Rails::Initializer.run do |config|
192 # config.action_view.sanitized_bad_tags = 'embed', 'object'
193 # end
194 #
195 def sanitized_bad_tags=(attributes)
196 HTML::WhiteListSanitizer.bad_tags.merge(attributes)
197 end
198
199 # Adds to the Set of allowed tags for the +sanitize+ helper.
200 #
201 # Rails::Initializer.run do |config|
202 # config.action_view.sanitized_allowed_tags = 'table', 'tr', 'td'
203 # end
204 #
205 def sanitized_allowed_tags=(attributes)
206 HTML::WhiteListSanitizer.allowed_tags.merge(attributes)
207 end
208
209 # Adds to the Set of allowed HTML attributes for the +sanitize+ helper.
210 #
211 # Rails::Initializer.run do |config|
212 # config.action_view.sanitized_allowed_attributes = 'onclick', 'longdesc'
213 # end
214 #
215 def sanitized_allowed_attributes=(attributes)
216 HTML::WhiteListSanitizer.allowed_attributes.merge(attributes)
217 end
218
219 # Adds to the Set of allowed CSS properties for the #sanitize and +sanitize_css+ helpers.
220 #
221 # Rails::Initializer.run do |config|
222 # config.action_view.sanitized_allowed_css_properties = 'expression'
223 # end
224 #
225 def sanitized_allowed_css_properties=(attributes)
226 HTML::WhiteListSanitizer.allowed_css_properties.merge(attributes)
227 end
228
229 # Adds to the Set of allowed CSS keywords for the +sanitize+ and +sanitize_css+ helpers.
230 #
231 # Rails::Initializer.run do |config|
232 # config.action_view.sanitized_allowed_css_keywords = 'expression'
233 # end
234 #
235 def sanitized_allowed_css_keywords=(attributes)
236 HTML::WhiteListSanitizer.allowed_css_keywords.merge(attributes)
237 end
238
239 # Adds to the Set of allowed shorthand CSS properties for the +sanitize+ and +sanitize_css+ helpers.
240 #
241 # Rails::Initializer.run do |config|
242 # config.action_view.sanitized_shorthand_css_properties = 'expression'
243 # end
244 #
245 def sanitized_shorthand_css_properties=(attributes)
246 HTML::WhiteListSanitizer.shorthand_css_properties.merge(attributes)
247 end
248
249 # Adds to the Set of allowed protocols for the +sanitize+ helper.
250 #
251 # Rails::Initializer.run do |config|
252 # config.action_view.sanitized_allowed_protocols = 'ssh', 'feed'
253 # end
254 #
255 def sanitized_allowed_protocols=(attributes)
256 HTML::WhiteListSanitizer.allowed_protocols.merge(attributes)
257 end
258 end
259 end
260 end
261 end