forked from flavorjones/loofah
/
scrubbers.rb
297 lines (280 loc) · 10.3 KB
/
scrubbers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
module Loofah
#
# Loofah provides some built-in scrubbers for sanitizing with
# HTML5lib's safelist and for accomplishing some common
# transformation tasks.
#
#
# === Loofah::Scrubbers::Strip / scrub!(:strip)
#
# +:strip+ removes unknown/unsafe tags, but leaves behind the pristine contents:
#
# unsafe_html = "ohai! <div>div is safe</div> <foo>but foo is <b>not</b></foo>"
# Loofah.fragment(unsafe_html).scrub!(:strip)
# => "ohai! <div>div is safe</div> but foo is <b>not</b>"
#
#
# === Loofah::Scrubbers::Prune / scrub!(:prune)
#
# +:prune+ removes unknown/unsafe tags and their contents (including their subtrees):
#
# unsafe_html = "ohai! <div>div is safe</div> <foo>but foo is <b>not</b></foo>"
# Loofah.fragment(unsafe_html).scrub!(:prune)
# => "ohai! <div>div is safe</div> "
#
#
# === Loofah::Scrubbers::Escape / scrub!(:escape)
#
# +:escape+ performs HTML entity escaping on the unknown/unsafe tags:
#
# unsafe_html = "ohai! <div>div is safe</div> <foo>but foo is <b>not</b></foo>"
# Loofah.fragment(unsafe_html).scrub!(:escape)
# => "ohai! <div>div is safe</div> <foo>but foo is <b>not</b></foo>"
#
#
# === Loofah::Scrubbers::Whitewash / scrub!(:whitewash)
#
# +:whitewash+ removes all comments, styling and attributes in
# addition to doing markup-fixer-uppery and pruning unsafe tags. I
# like to call this "whitewashing", since it's like putting a new
# layer of paint on top of the HTML input to make it look nice.
#
# messy_markup = "ohai! <div id='foo' class='bar' style='margin: 10px'>div with attributes</div>"
# Loofah.fragment(messy_markup).scrub!(:whitewash)
# => "ohai! <div>div with attributes</div>"
#
# One use case for this scrubber is to clean up HTML that was
# cut-and-pasted from Microsoft Word into a WYSIWYG editor or a
# rich text editor. Microsoft's software is famous for injecting
# all kinds of cruft into its HTML output. Who needs that crap?
# Certainly not me.
#
#
# === Loofah::Scrubbers::NoFollow / scrub!(:nofollow)
#
# +:nofollow+ adds a rel="nofollow" attribute to all links
#
# link_farmers_markup = "ohai! <a href='http://www.myswarmysite.com/'>I like your blog post</a>"
# Loofah.fragment(link_farmers_markup).scrub!(:nofollow)
# => "ohai! <a href='http://www.myswarmysite.com/' rel="nofollow">I like your blog post</a>"
#
#
# === Loofah::Scrubbers::NoOpener / scrub!(:noopener)
#
# +:noopener+ adds a rel="noopener" attribute to all links
#
# link_farmers_markup = "ohai! <a href='http://www.myswarmysite.com/'>I like your blog post</a>"
# Loofah.fragment(link_farmers_markup).scrub!(:noopener)
# => "ohai! <a href='http://www.myswarmysite.com/' rel="noopener">I like your blog post</a>"
#
#
# === Loofah::Scrubbers::Unprintable / scrub!(:unprintable)
#
# +:unprintable+ removes unprintable Unicode characters.
#
# markup = "<p>Some text with an unprintable character at the end\u2028</p>"
# Loofah.fragment(markup).scrub!(:unprintable)
# => "<p>Some text with an unprintable character at the end</p>"
#
# You may not be able to see the unprintable character in the above example, but there is a
# U+2028 character right before the closing </p> tag. These characters can cause issues if
# the content is ever parsed by JavaScript - more information here:
#
# http://timelessrepo.com/json-isnt-a-javascript-subset
#
module Scrubbers
#
# === scrub!(:strip)
#
# +:strip+ removes unknown/unsafe tags, but leaves behind the pristine contents:
#
# unsafe_html = "ohai! <div>div is safe</div> <foo>but foo is <b>not</b></foo>"
# Loofah.fragment(unsafe_html).scrub!(:strip)
# => "ohai! <div>div is safe</div> but foo is <b>not</b>"
#
class Strip < Scrubber
def initialize
@direction = :bottom_up
end
def scrub(node)
return CONTINUE if html5lib_sanitize(node) == CONTINUE
if node.children.length == 1 && node.children.first.cdata?
sanitized_text = Loofah.fragment(node.children.first.to_html).scrub!(:strip).to_html
node.before Nokogiri::XML::Text.new(sanitized_text, node.document)
else
node.before node.children
end
node.remove
end
end
#
# === scrub!(:prune)
#
# +:prune+ removes unknown/unsafe tags and their contents (including their subtrees):
#
# unsafe_html = "ohai! <div>div is safe</div> <foo>but foo is <b>not</b></foo>"
# Loofah.fragment(unsafe_html).scrub!(:prune)
# => "ohai! <div>div is safe</div> "
#
class Prune < Scrubber
def initialize
@direction = :top_down
end
def scrub(node)
return CONTINUE if html5lib_sanitize(node) == CONTINUE
node.remove
return STOP
end
end
#
# === scrub!(:escape)
#
# +:escape+ performs HTML entity escaping on the unknown/unsafe tags:
#
# unsafe_html = "ohai! <div>div is safe</div> <foo>but foo is <b>not</b></foo>"
# Loofah.fragment(unsafe_html).scrub!(:escape)
# => "ohai! <div>div is safe</div> <foo>but foo is <b>not</b></foo>"
#
class Escape < Scrubber
def initialize
@direction = :top_down
end
def scrub(node)
return CONTINUE if html5lib_sanitize(node) == CONTINUE
node.add_next_sibling Nokogiri::XML::Text.new(node.to_s, node.document)
node.remove
return STOP
end
end
#
# === scrub!(:whitewash)
#
# +:whitewash+ removes all comments, styling and attributes in
# addition to doing markup-fixer-uppery and pruning unsafe tags. I
# like to call this "whitewashing", since it's like putting a new
# layer of paint on top of the HTML input to make it look nice.
#
# messy_markup = "ohai! <div id='foo' class='bar' style='margin: 10px'>div with attributes</div>"
# Loofah.fragment(messy_markup).scrub!(:whitewash)
# => "ohai! <div>div with attributes</div>"
#
# One use case for this scrubber is to clean up HTML that was
# cut-and-pasted from Microsoft Word into a WYSIWYG editor or a
# rich text editor. Microsoft's software is famous for injecting
# all kinds of cruft into its HTML output. Who needs that crap?
# Certainly not me.
#
class Whitewash < Scrubber
def initialize
@direction = :top_down
end
def scrub(node)
case node.type
when Nokogiri::XML::Node::ELEMENT_NODE
if HTML5::Scrub.allowed_element? node.name
node.attributes.each { |attr| node.remove_attribute(attr.first) }
return CONTINUE if node.namespaces.empty?
end
when Nokogiri::XML::Node::TEXT_NODE, Nokogiri::XML::Node::CDATA_SECTION_NODE
return CONTINUE
end
node.remove
STOP
end
end
#
# === scrub!(:nofollow)
#
# +:nofollow+ adds a rel="nofollow" attribute to all links
#
# link_farmers_markup = "ohai! <a href='http://www.myswarmysite.com/'>I like your blog post</a>"
# Loofah.fragment(link_farmers_markup).scrub!(:nofollow)
# => "ohai! <a href='http://www.myswarmysite.com/' rel="nofollow">I like your blog post</a>"
#
class NoFollow < Scrubber
def initialize
@direction = :top_down
end
def scrub(node)
return CONTINUE unless (node.type == Nokogiri::XML::Node::ELEMENT_NODE) && (node.name == 'a')
append_attribute(node, 'rel', 'nofollow')
return STOP
end
end
#
# === scrub!(:noopener)
#
# +:noopener+ adds a rel="noopener" attribute to all links
#
# link_farmers_markup = "ohai! <a href='http://www.myswarmysite.com/'>I like your blog post</a>"
# Loofah.fragment(link_farmers_markup).scrub!(:noopener)
# => "ohai! <a href='http://www.myswarmysite.com/' rel="noopener">I like your blog post</a>"
#
class NoOpener < Scrubber
def initialize
@direction = :top_down
end
def scrub(node)
return CONTINUE unless (node.type == Nokogiri::XML::Node::ELEMENT_NODE) && (node.name == 'a')
append_attribute(node, 'rel', 'noopener')
return STOP
end
end
# This class probably isn't useful publicly, but is used for #to_text's current implemention
class NewlineBlockElements < Scrubber # :nodoc:
def initialize
@direction = :bottom_up
end
def scrub(node)
return CONTINUE unless Loofah::Elements::BLOCK_LEVEL.include?(node.name)
node.add_next_sibling Nokogiri::XML::Text.new("\n#{node.content}\n", node.document)
node.remove
end
end
#
# === scrub!(:unprintable)
#
# +:unprintable+ removes unprintable Unicode characters.
#
# markup = "<p>Some text with an unprintable character at the end\u2028</p>"
# Loofah.fragment(markup).scrub!(:unprintable)
# => "<p>Some text with an unprintable character at the end</p>"
#
# You may not be able to see the unprintable character in the above example, but there is a
# U+2028 character right before the closing </p> tag. These characters can cause issues if
# the content is ever parsed by JavaScript - more information here:
#
# http://timelessrepo.com/json-isnt-a-javascript-subset
#
class Unprintable < Scrubber
def initialize
@direction = :top_down
end
def scrub(node)
if node.type == Nokogiri::XML::Node::TEXT_NODE || node.type == Nokogiri::XML::Node::CDATA_SECTION_NODE
node.content = node.content.gsub(/\u2028|\u2029/, '')
end
CONTINUE
end
end
#
# A hash that maps a symbol (like +:prune+) to the appropriate Scrubber (Loofah::Scrubbers::Prune).
#
MAP = {
:escape => Escape,
:prune => Prune,
:whitewash => Whitewash,
:strip => Strip,
:nofollow => NoFollow,
:noopener => NoOpener,
:newline_block_elements => NewlineBlockElements,
:unprintable => Unprintable
}
#
# Returns an array of symbols representing the built-in scrubbers
#
def self.scrubber_symbols
MAP.keys
end
end
end